Skip to content

Commit

Permalink
Merge pull request #465 from YuukiToriyama/feature/use-jisx0401/master
Browse files Browse the repository at this point in the history
jisx0401の導入をrelease/v0.1.19にマージ
  • Loading branch information
YuukiToriyama authored Oct 19, 2024
2 parents d8a430c + d1b22f0 commit 3338295
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 69 deletions.
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ regex = { version = "1.10.6", default-features = false, features = ["std", "unic
serde.workspace = true
reqwest = { version = "0.12.5", default-features = false, features = ["json", "rustls-tls"] }
js-sys = "0.3.67"
jisx0401 = "0.1.0-beta.3"

[dev-dependencies]
criterion = { version = "0.5.1", default-features = false, features = ["html_reports"] }
Expand Down
20 changes: 10 additions & 10 deletions core/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Parser {
pub async fn parse(api: Arc<AsyncApi>, input: &str) -> ParseResult {
let tokenizer = Tokenizer::new(input);
// 都道府県を特定
let (prefecture_name, tokenizer) = match tokenizer.read_prefecture() {
let (prefecture, tokenizer) = match tokenizer.read_prefecture() {
Ok(found) => found,
Err(tokenizer) => {
return ParseResult {
Expand All @@ -91,7 +91,7 @@ pub async fn parse(api: Arc<AsyncApi>, input: &str) -> ParseResult {
}
};
// その都道府県の市町村名リストを取得
let prefecture = match api.get_prefecture_master(&prefecture_name).await {
let prefecture_master = match api.get_prefecture_master(prefecture.name_ja()).await {
Err(error) => {
return ParseResult {
address: Address::from(tokenizer.finish()),
Expand All @@ -101,11 +101,11 @@ pub async fn parse(api: Arc<AsyncApi>, input: &str) -> ParseResult {
Ok(result) => result,
};
// 市町村名を特定
let (city_name, tokenizer) = match tokenizer.read_city(&prefecture.cities) {
let (city_name, tokenizer) = match tokenizer.read_city(&prefecture_master.cities) {
Ok(found) => found,
Err(not_found) => {
// 市区町村が特定できない場合かつフィーチャフラグが有効な場合、郡名が抜けている可能性を検討
match not_found.read_city_with_county_name_completion(&prefecture.cities) {
match not_found.read_city_with_county_name_completion(&prefecture_master.cities) {
Ok(found) if cfg!(feature = "city-name-correction") => found,
_ => {
// それでも見つからない場合は終了
Expand All @@ -118,7 +118,7 @@ pub async fn parse(api: Arc<AsyncApi>, input: &str) -> ParseResult {
}
};
// その市町村の町名リストを取得
let city = match api.get_city_master(&prefecture_name, &city_name).await {
let city = match api.get_city_master(prefecture.name_ja(), &city_name).await {
Err(error) => {
return ParseResult {
address: Address::from(tokenizer.finish()),
Expand Down Expand Up @@ -247,7 +247,7 @@ mod tests {
#[cfg(feature = "blocking")]
pub fn parse_blocking(api: Arc<BlockingApi>, input: &str) -> ParseResult {
let tokenizer = Tokenizer::new(input);
let (prefecture_name, tokenizer) = match tokenizer.read_prefecture() {
let (prefecture, tokenizer) = match tokenizer.read_prefecture() {
Ok(found) => found,
Err(tokenizer) => {
return ParseResult {
Expand All @@ -256,7 +256,7 @@ pub fn parse_blocking(api: Arc<BlockingApi>, input: &str) -> ParseResult {
}
}
};
let prefecture = match api.get_prefecture_master(&prefecture_name) {
let prefecture_master = match api.get_prefecture_master(prefecture.name_ja()) {
Err(error) => {
return ParseResult {
address: Address::from(tokenizer.finish()),
Expand All @@ -265,10 +265,10 @@ pub fn parse_blocking(api: Arc<BlockingApi>, input: &str) -> ParseResult {
}
Ok(result) => result,
};
let (city_name, tokenizer) = match tokenizer.read_city(&prefecture.cities) {
let (city_name, tokenizer) = match tokenizer.read_city(&prefecture_master.cities) {
Ok(found) => found,
Err(not_found) => {
match not_found.read_city_with_county_name_completion(&prefecture.cities) {
match not_found.read_city_with_county_name_completion(&prefecture_master.cities) {
Ok(found) if cfg!(feature = "city-name-correction") => found,
_ => {
return ParseResult {
Expand All @@ -279,7 +279,7 @@ pub fn parse_blocking(api: Arc<BlockingApi>, input: &str) -> ParseResult {
}
}
};
let city = match api.get_city_master(&prefecture_name, &city_name) {
let city = match api.get_city_master(prefecture.name_ja(), &city_name) {
Err(error) => {
return ParseResult {
address: Address::from(tokenizer.finish()),
Expand Down
74 changes: 15 additions & 59 deletions core/src/tokenizer/read_prefecture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,6 @@ use crate::tokenizer::{End, Init, PrefectureNameFound, Tokenizer};
use crate::util::extension::StrExt;
use std::marker::PhantomData;

const PREFECTURE_NAME_LIST: [&str; 47] = [
"北海道",
"青森県",
"岩手県",
"宮城県",
"秋田県",
"山形県",
"福島県",
"茨城県",
"栃木県",
"群馬県",
"埼玉県",
"千葉県",
"東京都",
"神奈川県",
"新潟県",
"富山県",
"石川県",
"福井県",
"山梨県",
"長野県",
"岐阜県",
"静岡県",
"愛知県",
"三重県",
"滋賀県",
"京都府",
"大阪府",
"兵庫県",
"奈良県",
"和歌山県",
"鳥取県",
"島根県",
"岡山県",
"広島県",
"山口県",
"徳島県",
"香川県",
"愛媛県",
"高知県",
"福岡県",
"佐賀県",
"長崎県",
"熊本県",
"大分県",
"宮崎県",
"鹿児島県",
"沖縄県",
];

impl Tokenizer<Init> {
pub(crate) fn new(input: &str) -> Self {
Self {
Expand All @@ -68,11 +18,12 @@ impl Tokenizer<Init> {

pub(crate) fn read_prefecture(
&self,
) -> Result<(String, Tokenizer<PrefectureNameFound>), Tokenizer<End>> {
for prefecture_name in PREFECTURE_NAME_LIST {
if self.rest.starts_with(prefecture_name) {
return Ok((
prefecture_name.to_string(),
) -> Result<(jisx0401::Prefecture, Tokenizer<PrefectureNameFound>), Tokenizer<End>> {
match find_prefecture(&self.rest) {
Some(prefecture) => {
let prefecture_name = prefecture.name_ja();
Ok((
prefecture.clone(),
Tokenizer {
tokens: vec![Token::Prefecture(Prefecture {
prefecture_name: prefecture_name.to_string(),
Expand All @@ -85,17 +36,22 @@ impl Tokenizer<Init> {
.collect::<String>(),
_state: PhantomData::<PrefectureNameFound>,
},
));
))
}
None => Err(self.finish()),
}
Err(self.finish())
}
}

fn find_prefecture(input: &str) -> Option<&jisx0401::Prefecture> {
jisx0401::Prefecture::values().find(|&prefecture| input.starts_with(prefecture.name_ja()))
}

#[cfg(test)]
mod tests {
use crate::domain::common::token::Token;
use crate::tokenizer::Tokenizer;
use jisx0401::Prefecture;

#[test]
fn new() {
Expand Down Expand Up @@ -124,8 +80,8 @@ mod tests {
let tokenizer = Tokenizer::new("東京都港区芝公園4丁目2-8");
let result = tokenizer.read_prefecture();
assert!(result.is_ok());
let (prefecture_name, tokenizer) = result.unwrap();
assert_eq!(prefecture_name, "東京都");
let (prefecture, tokenizer) = result.unwrap();
assert_eq!(prefecture, Prefecture::TOKYO);
assert_eq!(tokenizer.tokens.len(), 1);
assert_eq!(tokenizer.rest, "港区芝公園4丁目2-8");
}
Expand Down

0 comments on commit 3338295

Please sign in to comment.