Skip to content

Commit

Permalink
Merge pull request #511 from YuukiToriyama/release/v0.1.23
Browse files Browse the repository at this point in the history
release/v0.1.23をmainブランチにマージ
  • Loading branch information
YuukiToriyama authored Nov 5, 2024
2 parents ca13587 + 1b16a88 commit 8415984
Show file tree
Hide file tree
Showing 19 changed files with 530 additions and 361 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ members = [
resolver = "2"

[workspace.package]
version = "0.1.22"
version = "0.1.23"
edition = "2021"
description = "A Rust Library to parse japanese addresses."
repository = "https://github.com/YuukiToriyama/japanese-address-parser"
Expand Down
1 change: 1 addition & 0 deletions core/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(deprecated)]
pub use crate::repository::geolonia::city_master_api;
pub use crate::repository::geolonia::prefecture_master_api;

Expand Down
57 changes: 10 additions & 47 deletions core/src/domain/common/token.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::domain::common::latlng::LatLng;
use std::cmp::Ordering;
use std::cmp::Ordering::{Equal, Greater, Less};

#[derive(Clone, Debug, PartialEq)]
pub enum Token {
Prefecture(Prefecture),
City(City),
Town(Town),
Prefecture(String),
City(String),
Town(String),
Rest(String),
}

Expand Down Expand Up @@ -41,65 +40,29 @@ impl PartialOrd for Token {
}
}

#[derive(Debug, PartialEq, Clone)]
pub(crate) struct Prefecture {
pub(crate) prefecture_name: String,
pub(crate) representative_point: Option<LatLng>,
}

#[derive(Debug, PartialEq, Clone)]
pub(crate) struct City {
pub(crate) city_name: String,
pub(crate) representative_point: Option<LatLng>,
}

#[derive(Debug, PartialEq, Clone)]
pub(crate) struct Town {
pub(crate) town_name: String,
pub(crate) representative_point: Option<LatLng>,
}

pub(crate) fn append_token(tokens: &[Token], token: Token) -> Vec<Token> {
[tokens.to_owned(), vec![token]].concat()
}

#[cfg(test)]
mod tests {
use crate::domain::common::token::{City, Prefecture, Token, Town};
use crate::domain::common::token::Token;

#[test]
fn sort_token_vector() {
let mut tokens = vec![
Token::Rest("2-1".to_string()),
Token::City(City {
city_name: "小金井市".to_string(),
representative_point: None,
}),
Token::Prefecture(Prefecture {
prefecture_name: "東京都".to_string(),
representative_point: None,
}),
Token::Town(Town {
town_name: "貫井北町四丁目".to_string(),
representative_point: None,
}),
Token::City("小金井市".to_string()),
Token::Prefecture("東京都".to_string()),
Token::Town("貫井北町四丁目".to_string()),
];
tokens.sort_by(|a, b| a.partial_cmp(b).unwrap());
assert_eq!(
tokens,
vec![
Token::Prefecture(Prefecture {
prefecture_name: "東京都".to_string(),
representative_point: None,
}),
Token::City(City {
city_name: "小金井市".to_string(),
representative_point: None,
}),
Token::Town(Town {
town_name: "貫井北町四丁目".to_string(),
representative_point: None,
}),
Token::Prefecture("東京都".to_string()),
Token::City("小金井市".to_string()),
Token::Town("貫井北町四丁目".to_string()),
Token::Rest("2-1".to_string()),
]
);
Expand Down
32 changes: 7 additions & 25 deletions core/src/experimental/parse_with_chimeiruiju.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Parser {

#[cfg(test)]
mod tests {
use crate::domain::common::token::{City, Prefecture, Token, Town};
use crate::domain::common::token::Token;
use crate::experimental::parser::{DataSource, Parser, ParserOptions};

#[tokio::test]
Expand Down Expand Up @@ -136,10 +136,7 @@ mod tests {
assert_eq!(
tokens,
vec![
Token::Prefecture(Prefecture {
prefecture_name: "神奈川県".to_string(),
representative_point: None,
}),
Token::Prefecture("神奈川県".to_string()),
Token::Rest("横浜県磯子市洋光台3-10-3".to_string())
]
)
Expand All @@ -160,14 +157,8 @@ mod tests {
assert_eq!(
tokens,
vec![
Token::Prefecture(Prefecture {
prefecture_name: "神奈川県".to_string(),
representative_point: None,
}),
Token::City(City {
city_name: "横浜市磯子区".to_string(),
representative_point: None,
}),
Token::Prefecture("神奈川県".to_string()),
Token::City("横浜市磯子区".to_string()),
Token::Rest("陽光台3-10-3".to_string())
]
)
Expand All @@ -188,18 +179,9 @@ mod tests {
assert_eq!(
tokens,
vec![
Token::Prefecture(Prefecture {
prefecture_name: "神奈川県".to_string(),
representative_point: None,
}),
Token::City(City {
city_name: "横浜市磯子区".to_string(),
representative_point: None,
}),
Token::Town(Town {
town_name: "洋光台三丁目".to_string(),
representative_point: None,
}),
Token::Prefecture("神奈川県".to_string()),
Token::City("横浜市磯子区".to_string()),
Token::Town("洋光台三丁目".to_string()),
Token::Rest("10-3".to_string())
]
)
Expand Down
43 changes: 11 additions & 32 deletions core/src/experimental/parse_with_geolonia.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::api::AsyncApi;
use crate::domain::common::token::Token;
use crate::experimental::parser::Parser;
use crate::interactor::geolonia::{GeoloniaInteractor, GeoloniaInteractorImpl};
use crate::tokenizer::Tokenizer;

impl Parser {
#[inline]
pub(crate) async fn parse_with_geolonia(&self, address: &str) -> Vec<Token> {
let geolonia_api = AsyncApi::default();
let interactor = GeoloniaInteractorImpl::default();
let tokenizer = Tokenizer::new(address);

// 都道府県名の検出
Expand All @@ -21,10 +21,7 @@ impl Parser {
};

// 市区町村名の検出
let prefecture_master = match geolonia_api
.get_prefecture_master(prefecture.name_ja())
.await
{
let prefecture_master = match interactor.get_prefecture_master(prefecture.name_ja()).await {
Ok(result) => result,
Err(error) => {
if self.options.verbose {
Expand Down Expand Up @@ -57,7 +54,7 @@ impl Parser {
};

// 町名の検出
let city_master = match geolonia_api
let city_master = match interactor
.get_city_master(prefecture.name_ja(), &city_name)
.await
{
Expand Down Expand Up @@ -86,7 +83,7 @@ impl Parser {

#[cfg(test)]
mod tests {
use crate::domain::common::token::{City, Prefecture, Token, Town};
use crate::domain::common::token::Token;
use crate::experimental::parser::{DataSource, Parser, ParserOptions};

#[tokio::test]
Expand Down Expand Up @@ -122,10 +119,7 @@ mod tests {
assert_eq!(
result,
vec![
Token::Prefecture(Prefecture {
prefecture_name: "神奈川県".to_string(),
representative_point: None,
}),
Token::Prefecture("神奈川県".to_string()),
Token::Rest("横浜県磯子市洋光台3-10-3".to_string())
]
)
Expand All @@ -146,14 +140,8 @@ mod tests {
assert_eq!(
result,
vec![
Token::Prefecture(Prefecture {
prefecture_name: "神奈川県".to_string(),
representative_point: None,
}),
Token::City(City {
city_name: "横浜市磯子区".to_string(),
representative_point: None,
}),
Token::Prefecture("神奈川県".to_string()),
Token::City("横浜市磯子区".to_string()),
Token::Rest("陽光台3-10-3".to_string())
]
)
Expand All @@ -174,18 +162,9 @@ mod tests {
assert_eq!(
result,
vec![
Token::Prefecture(Prefecture {
prefecture_name: "神奈川県".to_string(),
representative_point: None,
}),
Token::City(City {
city_name: "横浜市磯子区".to_string(),
representative_point: None,
}),
Token::Town(Town {
town_name: "洋光台三丁目".to_string(),
representative_point: None,
}),
Token::Prefecture("神奈川県".to_string()),
Token::City("横浜市磯子区".to_string()),
Token::Town("洋光台三丁目".to_string()),
Token::Rest("10-3".to_string())
]
)
Expand Down
92 changes: 28 additions & 64 deletions core/src/experimental/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,29 +152,17 @@ impl From<Vec<Token>> for ParsedAddress {

for token in value {
match token {
Token::Prefecture(prefecture) => {
parsed_address.prefecture = prefecture.prefecture_name;
Token::Prefecture(prefecture_name) => {
parsed_address.prefecture = prefecture_name;
parsed_address.metadata.depth = 1;
if let Some(lat_lng) = prefecture.representative_point {
parsed_address.metadata.latitude = Some(lat_lng.latitude);
parsed_address.metadata.longitude = Some(lat_lng.longitude);
}
}
Token::City(city) => {
parsed_address.city = city.city_name;
Token::City(city_name) => {
parsed_address.city = city_name;
parsed_address.metadata.depth = 2;
if let Some(lat_lng) = city.representative_point {
parsed_address.metadata.latitude = Some(lat_lng.latitude);
parsed_address.metadata.longitude = Some(lat_lng.longitude);
}
}
Token::Town(town) => {
parsed_address.town = town.town_name;
Token::Town(town_name) => {
parsed_address.town = town_name;
parsed_address.metadata.depth = 3;
if let Some(lat_lng) = town.representative_point {
parsed_address.metadata.latitude = Some(lat_lng.latitude);
parsed_address.metadata.longitude = Some(lat_lng.longitude);
}
}
Token::Rest(rest) => {
parsed_address.rest = rest;
Expand All @@ -200,7 +188,7 @@ impl From<(Vec<Token>, Option<LatLng>)> for ParsedAddress {
#[cfg(test)]
mod tests {
use crate::domain::common::latlng::LatLng;
use crate::domain::common::token::{City, Prefecture, Token, Town};
use crate::domain::common::token::Token;
use crate::experimental::parser::{Metadata, ParsedAddress};

#[test]
Expand Down Expand Up @@ -228,16 +216,14 @@ mod tests {
#[test]
fn conversion_depthが1() {
let tokens = vec![
Token::Prefecture(Prefecture {
prefecture_name: "東京都".to_string(),
representative_point: Some(LatLng {
latitude: 139.748264,
longitude: 35.68532,
}),
}),
Token::Prefecture("東京都".to_string()),
Token::Rest("".to_string()),
];
let parsed_address = ParsedAddress::from(tokens);
let lat_lng = Some(LatLng {
latitude: 139.748264,
longitude: 35.68532,
});
let parsed_address = ParsedAddress::from((tokens, lat_lng));
assert_eq!(
parsed_address,
ParsedAddress {
Expand All @@ -257,23 +243,15 @@ mod tests {
#[test]
fn conversion_depthが2() {
let tokens = vec![
Token::Prefecture(Prefecture {
prefecture_name: "東京都".to_string(),
representative_point: Some(LatLng {
latitude: 139.748264,
longitude: 35.68532,
}),
}),
Token::City(City {
city_name: "台東区".to_string(),
representative_point: Some(LatLng {
latitude: 139.764379,
longitude: 35.711162,
}),
}),
Token::Prefecture("東京都".to_string()),
Token::City("台東区".to_string()),
Token::Rest("".to_string()),
];
let parsed_address = ParsedAddress::from(tokens);
let lat_lng = Some(LatLng {
latitude: 139.764379,
longitude: 35.711162,
});
let parsed_address = ParsedAddress::from((tokens, lat_lng));
assert_eq!(
parsed_address,
ParsedAddress {
Expand All @@ -293,30 +271,16 @@ mod tests {
#[test]
fn conversion_depthが3() {
let tokens = vec![
Token::Prefecture(Prefecture {
prefecture_name: "東京都".to_string(),
representative_point: Some(LatLng {
latitude: 139.748264,
longitude: 35.68532,
}),
}),
Token::City(City {
city_name: "文京区".to_string(),
representative_point: Some(LatLng {
latitude: 139.749542,
longitude: 35.708143,
}),
}),
Token::Town(Town {
town_name: "本駒込六丁目".to_string(),
representative_point: Some(LatLng {
latitude: 139.738043,
longitude: 35.72791,
}),
}),
Token::Prefecture("東京都".to_string()),
Token::City("文京区".to_string()),
Token::Town("本駒込六丁目".to_string()),
Token::Rest("16-3".to_string()),
];
let parsed_address = ParsedAddress::from(tokens);
let lat_lng = Some(LatLng {
latitude: 139.738043,
longitude: 35.72791,
});
let parsed_address = ParsedAddress::from((tokens, lat_lng));
assert_eq!(
parsed_address,
ParsedAddress {
Expand Down
Loading

0 comments on commit 8415984

Please sign in to comment.