-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #496 from YuukiToriyama/release/v0.1.22
release/v0.1.22をmainブランチにマージ
- Loading branch information
Showing
21 changed files
with
1,080 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
#[cfg(feature = "experimental")] | ||
pub mod chimei_ruiju; | ||
pub mod common; | ||
pub mod geolonia; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod entity; | ||
pub mod error; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use crate::domain::common::latlng::LatLng; | ||
use serde::Deserialize; | ||
|
||
#[derive(Deserialize, Debug)] | ||
pub struct PrefectureMaster { | ||
/// 都道府県名 | ||
pub(crate) name: String, | ||
/// 市区町村名リスト | ||
pub(crate) cities: Vec<String>, | ||
/// 代表点の緯度経度 | ||
pub(crate) coordinate: Coordinate, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
pub struct CityMaster { | ||
/// 市区町村名 | ||
pub(crate) name: String, | ||
/// 町名リスト | ||
pub(crate) towns: Vec<String>, | ||
/// 緯度経度 | ||
pub(crate) coordinate: Coordinate, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
pub struct TownMaster { | ||
/// 町名 | ||
pub(crate) name: String, | ||
/// 街区リスト | ||
blocks: Vec<Block>, | ||
/// 緯度経度 | ||
pub(crate) coordinate: Coordinate, | ||
} | ||
|
||
#[allow(dead_code)] | ||
#[derive(Deserialize, Debug)] | ||
pub struct Block { | ||
/// 小字・通称名 | ||
koaza: String, | ||
/// 街区符号・地番 | ||
block_number: String, | ||
/// 住居表示の有無 | ||
residential_address_indication: bool, | ||
/// 緯度経度 | ||
coordinate: Coordinate, | ||
} | ||
|
||
#[allow(dead_code)] | ||
#[derive(Deserialize, Debug)] | ||
pub struct Coordinate { | ||
/// 緯度 | ||
pub(crate) latitude: f64, | ||
/// 経度 | ||
pub(crate) longitude: f64, | ||
} | ||
|
||
impl Coordinate { | ||
pub(crate) fn to_lat_lng(&self) -> LatLng { | ||
LatLng { | ||
latitude: self.latitude, | ||
longitude: self.longitude, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use thiserror::Error; | ||
|
||
#[derive(Error, PartialEq, Debug)] | ||
pub enum ApiError { | ||
#[error("network error occurs: {url}")] | ||
Network { url: String }, | ||
#[error("resource not found: {url}")] | ||
NotFound { url: String }, | ||
#[error("deserialize error occurs: {url}")] | ||
Deserialize { url: String }, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
use crate::domain::common::latlng::LatLng; | ||
use crate::domain::common::token::Token; | ||
use crate::experimental::parser::Parser; | ||
use crate::interactor::chimei_ruiju::{ChimeiRuijuInteractor, ChimeiRuijuInteractorImpl}; | ||
use crate::tokenizer::Tokenizer; | ||
use std::option::Option; | ||
|
||
impl Parser { | ||
pub(crate) async fn parse_with_chimeiruiju( | ||
&self, | ||
address: &str, | ||
) -> (Vec<Token>, Option<LatLng>) { | ||
let interactor = ChimeiRuijuInteractorImpl::default(); | ||
let tokenizer = Tokenizer::new(address); | ||
let mut lat_lng: Option<LatLng> = None; | ||
|
||
// 都道府県名の検出 | ||
let (prefecture, tokenizer) = match tokenizer.read_prefecture() { | ||
Ok(found) => found, | ||
Err(not_found) => { | ||
if self.options.verbose { | ||
log::error!("都道府県名の検出に失敗しました") | ||
} | ||
return (not_found.tokens, lat_lng); | ||
} | ||
}; | ||
|
||
// 都道府県マスタの取得 | ||
let prefecture_master = match interactor.get_prefecture_master(&prefecture).await { | ||
Ok(result) => { | ||
lat_lng.replace(result.coordinate.to_lat_lng()); | ||
result | ||
} | ||
Err(error) => { | ||
if self.options.verbose { | ||
log::error!("{}", error) | ||
} | ||
return (tokenizer.finish().tokens, lat_lng); | ||
} | ||
}; | ||
// 市区町村名の検出 | ||
let (city_name, tokenizer) = match tokenizer.read_city(&prefecture_master.cities) { | ||
Ok(found) => found, | ||
Err(not_found) => { | ||
if self.options.correct_incomplete_city_names { | ||
match not_found.read_city_with_county_name_completion(&prefecture_master.cities) | ||
{ | ||
Ok(result) => result, | ||
Err(not_found) => { | ||
if self.options.verbose { | ||
log::error!("市区町村名の検出に失敗しました") | ||
} | ||
return (not_found.tokens, lat_lng); | ||
} | ||
} | ||
} else { | ||
if self.options.verbose { | ||
log::error!("市区町村名の検出に失敗しました") | ||
} | ||
return (not_found.finish().tokens, lat_lng); | ||
} | ||
} | ||
}; | ||
|
||
// 市区町村マスタの取得 | ||
let city_master = match interactor.get_city_master(&prefecture, &city_name).await { | ||
Ok(result) => { | ||
lat_lng.replace(result.coordinate.to_lat_lng()); | ||
result | ||
} | ||
Err(error) => { | ||
if self.options.verbose { | ||
log::error!("{}", error) | ||
} | ||
return (tokenizer.finish().tokens, lat_lng); | ||
} | ||
}; | ||
// 町名の検出 | ||
let (town_name, tokenizer) = match tokenizer.read_town(city_master.towns) { | ||
Ok(found) => found, | ||
Err(not_found) => { | ||
if self.options.verbose { | ||
log::error!("町名の検出に失敗しました") | ||
} | ||
return (not_found.tokens, lat_lng); | ||
} | ||
}; | ||
|
||
// 町村マスタの取得 | ||
if let Ok(town_master) = interactor | ||
.get_town_master(&prefecture, &city_name, &town_name) | ||
.await | ||
{ | ||
lat_lng.replace(town_master.coordinate.to_lat_lng()); | ||
}; | ||
|
||
(tokenizer.finish().tokens, lat_lng) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::domain::common::token::{City, Prefecture, Token, Town}; | ||
use crate::experimental::parser::{DataSource, Parser, ParserOptions}; | ||
|
||
#[tokio::test] | ||
async fn 都道府県名が誤っている場合() { | ||
let parser = Parser { | ||
options: ParserOptions { | ||
data_source: DataSource::ChimeiRuiju, | ||
correct_incomplete_city_names: false, | ||
verbose: false, | ||
}, | ||
}; | ||
let (tokens, _) = parser | ||
.parse_with_chimeiruiju("奈川県横浜市磯子区洋光台3-10-3") | ||
.await; | ||
assert_eq!( | ||
tokens, | ||
vec![Token::Rest("奈川県横浜市磯子区洋光台3-10-3".to_string())] | ||
) | ||
} | ||
|
||
#[tokio::test] | ||
async fn 市区町村名が誤っている場合() { | ||
let parser = Parser { | ||
options: ParserOptions { | ||
data_source: DataSource::ChimeiRuiju, | ||
correct_incomplete_city_names: false, | ||
verbose: false, | ||
}, | ||
}; | ||
let (tokens, _) = parser | ||
.parse_with_chimeiruiju("神奈川県横浜県磯子市洋光台3-10-3") | ||
.await; | ||
assert_eq!( | ||
tokens, | ||
vec![ | ||
Token::Prefecture(Prefecture { | ||
prefecture_name: "神奈川県".to_string(), | ||
representative_point: None, | ||
}), | ||
Token::Rest("横浜県磯子市洋光台3-10-3".to_string()) | ||
] | ||
) | ||
} | ||
|
||
#[tokio::test] | ||
async fn 町名が誤っている場合() { | ||
let parser = Parser { | ||
options: ParserOptions { | ||
data_source: DataSource::ChimeiRuiju, | ||
correct_incomplete_city_names: false, | ||
verbose: false, | ||
}, | ||
}; | ||
let (tokens, _) = parser | ||
.parse_with_chimeiruiju("神奈川県横浜市磯子区陽光台3-10-3") | ||
.await; | ||
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::Rest("陽光台3-10-3".to_string()) | ||
] | ||
) | ||
} | ||
|
||
#[tokio::test] | ||
async fn パースに成功した場合() { | ||
let parser = Parser { | ||
options: ParserOptions { | ||
data_source: DataSource::ChimeiRuiju, | ||
correct_incomplete_city_names: false, | ||
verbose: false, | ||
}, | ||
}; | ||
let (tokens, _) = parser | ||
.parse_with_chimeiruiju("神奈川県横浜市磯子区洋光台3-10-3") | ||
.await; | ||
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::Rest("10-3".to_string()) | ||
] | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#[cfg(feature = "experimental")] | ||
pub mod chimei_ruiju; |
Oops, something went wrong.