Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nomの除却をrelease/v0.1.8にマージ #361

Merged
merged 9 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ blocking = ["reqwest/blocking"]
[dependencies]
itertools = "0.13.0"
js-sys = "0.3.67"
nom = "7.1.3"
rapidfuzz = "0.5.0"
regex = "1.10.2"
serde.workspace = true
Expand Down
15 changes: 8 additions & 7 deletions core/src/parser/adapter/orthographical_variant_adapter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use itertools::Itertools;
use nom::bytes::complete::tag;
use nom::error::VerboseError;
use nom::Parser;

pub type Variant = &'static [&'static str];

Expand Down Expand Up @@ -86,11 +83,15 @@ impl OrthographicalVariantAdapter {
// マッチ候補の中でパターンに引っかかるものがあれば文字を置き換えてマッチを試す
if candidate.contains(permutation[0]) {
let edited_region_name = candidate.replace(permutation[0], permutation[1]);
if let Ok((rest, _)) =
tag::<&str, &str, VerboseError<&str>>(&edited_region_name).parse(input)
{
if input.starts_with(&edited_region_name) {
// マッチすれば早期リターン
return Some((rest.to_string(), region_name.to_string()));
return Some((
region_name.to_string(),
input
.chars()
.skip(edited_region_name.chars().count())
.collect(),
));
} else {
// マッチしなければsemi_candidatesに置き換え後の文字列をpush
semi_candidates.push(edited_region_name.clone());
Expand Down
33 changes: 12 additions & 21 deletions core/src/parser/adapter/vague_expression_adapter.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use crate::util::sequence_matcher::SequenceMatcher;
use nom::bytes::complete::{is_a, is_not};
use nom::combinator::rest;
use nom::error::Error;
use nom::sequence::tuple;

pub struct VagueExpressionAdapter;

Expand All @@ -11,13 +7,8 @@ impl VagueExpressionAdapter {
if let Ok(highest_match) =
SequenceMatcher::get_most_similar_match(input, region_name_list, None)
{
let mut parser = tuple((
is_not::<&str, &str, Error<&str>>("町村"),
is_a::<&str, &str, Error<&str>>("町村"),
rest,
));
if let Ok((_, (_, _, rest))) = parser(input) {
return Some((rest.to_string(), highest_match));
if let Some(position) = input.chars().position(|c| c == '町' || c == '村') {
return Some((highest_match, input.chars().skip(position + 1).collect()));
}
}
None
Expand All @@ -32,50 +23,50 @@ mod tests {
#[test]
fn 郡名が省略されている場合_吉田郡永平寺町() {
let fukui = Prefecture::fukui();
let (rest, city_name) = VagueExpressionAdapter {}
let (city_name, rest) = VagueExpressionAdapter {}
.apply("永平寺町志比5-5", &fukui.cities)
.unwrap();
assert_eq!(rest, "志比5-5");
assert_eq!(city_name, "吉田郡永平寺町");
assert_eq!(rest, "志比5-5");
}

#[test]
fn 郡名が省略されている場合_今立郡池田町() {
let fukui = Prefecture::fukui();
let (rest, city_name) = VagueExpressionAdapter {}
let (city_name, rest) = VagueExpressionAdapter {}
.apply("池田町稲荷28-7", &fukui.cities)
.unwrap();
assert_eq!(rest, "稲荷28-7");
assert_eq!(city_name, "今立郡池田町");
assert_eq!(rest, "稲荷28-7");
}

#[test]
fn 郡名が省略されている場合_南条郡南越前町() {
let fukui = Prefecture::fukui();
let (rest, city_name) = VagueExpressionAdapter {}
let (city_name, rest) = VagueExpressionAdapter {}
.apply("南越前町今庄74-7-1", &fukui.cities)
.unwrap();
assert_eq!(rest, "今庄74-7-1");
assert_eq!(city_name, "南条郡南越前町");
assert_eq!(rest, "今庄74-7-1");
}

#[test]
fn 郡名が省略されている場合_西村山郡河北町() {
let yamagata = Prefecture::yamagata();
let (rest, city_name) = VagueExpressionAdapter {}
let (city_name, rest) = VagueExpressionAdapter {}
.apply("河北町大字吉田字馬場261", &yamagata.cities)
.unwrap();
assert_eq!(rest, "大字吉田字馬場261");
assert_eq!(city_name, "西村山郡河北町");
assert_eq!(rest, "大字吉田字馬場261");
}

#[test]
fn 郡名と町名が一致している場合_最上郡最上町() {
let yamagata = Prefecture::yamagata();
let (rest, city_name) = VagueExpressionAdapter {}
let (city_name, rest) = VagueExpressionAdapter {}
.apply("最上町法田2672-2", &yamagata.cities)
.unwrap();
assert_eq!(rest, "法田2672-2");
assert_eq!(city_name, "最上郡最上町");
assert_eq!(rest, "法田2672-2");
}
}
8 changes: 4 additions & 4 deletions core/src/tokenizer/read_city.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ impl Tokenizer<PrefectureNameFound> {
return Ok(Tokenizer {
input: self.input.clone(),
prefecture_name: self.prefecture_name.clone(),
city_name: Some(result.1),
city_name: Some(result.0),
town_name: None,
rest: result.0,
rest: result.1,
_state: PhantomData::<CityNameFound>,
});
}
Expand All @@ -75,9 +75,9 @@ impl Tokenizer<PrefectureNameFound> {
return Ok(Tokenizer {
input: self.input.clone(),
prefecture_name: self.prefecture_name.clone(),
city_name: Some(result.1),
city_name: Some(result.0),
town_name: None,
rest: result.0,
rest: result.1,
_state: PhantomData::<CityNameFound>,
});
}
Expand Down
20 changes: 10 additions & 10 deletions core/src/tokenizer/read_town.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,36 @@ impl Tokenizer<CityNameFound> {
if rest.contains("丁目") {
rest = NonKanjiBlockNumberFilter {}.apply(rest);
}
if let Some(result) = find_town(&rest, &candidates) {
if let Some((town_name, rest)) = find_town(&rest, &candidates) {
return Ok(Tokenizer {
input: self.input.clone(),
prefecture_name: self.prefecture_name.clone(),
city_name: self.city_name.clone(),
town_name: Some(result.1),
rest: result.0,
town_name: Some(town_name),
rest,
_state: PhantomData::<TownNameFound>,
});
}
// 「〇〇町L丁目M番N」ではなく「〇〇町L-M-N」と表記されているような場合
rest = InvalidTownNameFormatFilter {}.apply(rest);
if let Some(result) = find_town(&rest, &candidates) {
if let Some((town_name, rest)) = find_town(&rest, &candidates) {
return Ok(Tokenizer {
input: self.input.clone(),
prefecture_name: self.prefecture_name.clone(),
city_name: self.city_name.clone(),
town_name: Some(result.1),
rest: result.0,
town_name: Some(town_name),
rest,
_state: PhantomData::<TownNameFound>,
});
}
// ここまでで町名の検出に成功しない場合は、「大字」の省略の可能性を検討する
if let Some(result) = find_town(&format!("大字{}", rest), &candidates) {
if let Some((town_name, rest)) = find_town(&format!("大字{}", rest), &candidates) {
return Ok(Tokenizer {
input: self.input.clone(),
prefecture_name: self.prefecture_name.clone(),
city_name: self.city_name.clone(),
town_name: Some(result.1),
rest: result.0,
town_name: Some(town_name),
rest,
_state: PhantomData::<TownNameFound>,
});
}
Expand All @@ -66,11 +66,11 @@ fn find_town(input: &str, candidates: &Vec<String>) -> Option<(String, String)>
for candidate in candidates {
if input.starts_with(candidate) {
return Some((
candidate.to_string(),
input
.chars()
.skip(candidate.chars().count())
.collect::<String>(),
candidate.to_string(),
));
}
let adapter = OrthographicalVariantAdapter {
Expand Down
Loading