Skip to content

Commit

Permalink
Merge pull request #371 from YuukiToriyama/release/v0.1.9
Browse files Browse the repository at this point in the history
release/v0.1.9をmainブランチにマージ
  • Loading branch information
YuukiToriyama authored Aug 17, 2024
2 parents 0cffa39 + f6b5426 commit 9c48ef5
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 4 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.8"
version = "0.1.9"
edition = "2021"
description = "A Rust Library to parse japanese addresses."
repository = "https://github.com/YuukiToriyama/japanese-address-parser"
Expand Down
29 changes: 29 additions & 0 deletions core/src/domain/geolonia/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,33 @@ impl Prefecture {
],
}
}

/// only for testing
pub fn saga() -> Self {
Prefecture {
name: "佐賀県".to_string(),
cities: vec![
"佐賀市".to_string(),
"唐津市".to_string(),
"鳥栖市".to_string(),
"多久市".to_string(),
"伊万里市".to_string(),
"武雄市".to_string(),
"鹿島市".to_string(),
"小城市".to_string(),
"嬉野市".to_string(),
"神埼市".to_string(),
"神埼郡吉野ヶ里町".to_string(),
"三養基郡基山町".to_string(),
"三養基郡上峰町".to_string(),
"三養基郡みやき町".to_string(),
"東松浦郡玄海町".to_string(),
"西松浦郡有田町".to_string(),
"杵島郡大町町".to_string(),
"杵島郡江北町".to_string(),
"杵島郡白石町".to_string(),
"藤津郡太良町".to_string(),
],
}
}
}
56 changes: 53 additions & 3 deletions core/src/parser/adapter/vague_expression_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,58 @@ impl VagueExpressionAdapter {
if let Ok(highest_match) =
SequenceMatcher::get_most_similar_match(input, region_name_list, None)
{
if let Some(position) = input.chars().position(|c| c == '町' || c == '村') {
return Some((highest_match, input.chars().skip(position + 1).collect()));
if let Ok(complemented_address) = complement_county_name(input, &highest_match) {
return Some((
highest_match.clone(),
complemented_address
.chars()
.skip(highest_match.chars().count())
.collect(),
));
}
}
None
}
}

/// 郡名が抜けている住所に郡名を補う関数
///
/// 欠けている郡名を補うだけで、それ以上のことはしない。
/// 市区町村名に表記揺れがあってもそれを上書きすることはしない。
fn complement_county_name(vague_address: &str, with: &str) -> Result<String, &'static str> {
match with.chars().position(|c| c == '郡') {
None => Err("郡名が見つかりませんでした"),
Some(position) => Ok(with.chars().take(position + 1).collect::<String>() + vague_address),
}
}

#[cfg(test)]
mod tests {
use crate::domain::geolonia::entity::Prefecture;
use crate::parser::adapter::vague_expression_adapter::VagueExpressionAdapter;
use crate::parser::adapter::vague_expression_adapter::{
complement_county_name, VagueExpressionAdapter,
};

#[test]
fn complement_county_name_郡名が省略されている場合() {
assert_eq!(
complement_county_name("大町町大字福母297", "杵島郡大町町").unwrap(),
"杵島郡大町町大字福母297"
);
assert_eq!(
complement_county_name("村田町大字村田字迫6", "柴田郡村田町").unwrap(),
"柴田郡村田町大字村田字迫6"
);
assert_eq!(
complement_county_name("玉村町上新田1116", "佐波郡玉村町").unwrap(),
"佐波郡玉村町上新田1116"
);
// 市区町村名に表記揺れも含む場合
assert_eq!(
complement_county_name("桧原村上元郷403", "西多摩郡檜原村").unwrap(),
"西多摩郡桧原村上元郷403"
)
}

#[test]
fn 郡名が省略されている場合_吉田郡永平寺町() {
Expand Down Expand Up @@ -60,6 +100,16 @@ mod tests {
assert_eq!(rest, "大字吉田字馬場261");
}

#[test]
fn 郡名が省略されている場合_杵島郡大町町() {
let saga = Prefecture::saga();
let (city_name, rest) = VagueExpressionAdapter {}
.apply("大町町大字大町5017番地", &saga.cities)
.unwrap();
assert_eq!(city_name, "杵島郡大町町");
assert_eq!(rest, "大字大町5017番地");
}

#[test]
fn 郡名と町名が一致している場合_最上郡最上町() {
let yamagata = Prefecture::yamagata();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ address,prefecture,city,town,rest
奈良県高市郡高取町大字丹生谷575,奈良県,高市郡高取町,大字丹生谷,575
奈良県明日香村大字橘86,奈良県,高市郡明日香村,大字橘,86
奈良県高市郡明日香村大字橘86,奈良県,高市郡明日香村,大字橘,86
# 市区町村名に「町」または「村」が含まれている場合
群馬県玉村町上新田1116,群馬県,佐波郡玉村町,大字上新田,1116
佐賀県大町町大字福母297,佐賀県,杵島郡大町町,大字福母,297
佐賀県大町町大字大町5017,佐賀県,杵島郡大町町,大字大町,5017
宮城県村田町大字村田字迫6,宮城県,柴田郡村田町,大字村田,字迫6

0 comments on commit 9c48ef5

Please sign in to comment.