use unicode_normalization::char::is_combining_mark;
use unicode_normalization::UnicodeNormalization;
pub fn ascii_signs(s: &str) -> std::borrow::Cow<'_, str> {
const SIGNS: [(char, char); 9] = [
('\u{2019}', '\''),
('\u{2018}', '\''),
('\u{02BC}', '\''),
('\u{00B4}', '\''),
('`', '\''),
('\u{2010}', '-'),
('\u{2011}', '-'),
('\u{2013}', '-'),
('\u{2014}', '-'),
];
if !s.chars().any(|c| SIGNS.iter().any(|(from, _)| *from == c)) {
return std::borrow::Cow::Borrowed(s);
}
let ch: Vec<char> = s.chars().collect();
let spaced = |i: usize| {
i > 0 && i + 1 < ch.len() && ch[i - 1].is_whitespace() && ch[i + 1].is_whitespace()
};
let mut out = String::with_capacity(s.len());
let mut skip_spaces = false;
for (i, &c) in ch.iter().enumerate() {
if skip_spaces && c.is_whitespace() {
continue;
}
skip_spaces = false;
match SIGNS.iter().find(|(from, _)| *from == c) {
Some((_, '-')) if !spaced(i) => out.push(c),
Some((_, '-')) => {
let first: String = out
.trim_end()
.chars()
.rev()
.take_while(char::is_ascii_digit)
.collect::<Vec<_>>()
.into_iter()
.rev()
.collect();
let second: String = ch[i + 1..]
.iter()
.skip_while(|d| d.is_whitespace())
.take_while(|d| d.is_ascii_digit())
.collect();
let range = match (first.parse::<u64>(), second.parse::<u64>()) {
(Ok(a), Ok(b)) => b > a && second.len() <= first.len() + 1,
_ => false,
};
if range {
out.truncate(out.trim_end().len());
skip_spaces = true;
}
out.push('-');
}
Some((_, to)) => out.push(*to),
None => out.push(c),
}
}
std::borrow::Cow::Owned(out)
}
pub fn without_line_break_tags(s: &str) -> std::borrow::Cow<'_, str> {
let lower = s.to_ascii_lowercase();
if !lower.contains("<br") {
return std::borrow::Cow::Borrowed(s);
}
let mut out = String::with_capacity(s.len());
let mut i = 0;
while i < s.len() {
if lower[i..].starts_with("<br") {
let rest = &lower[i + 3..];
let tail = rest.trim_start_matches(' ');
let tail = tail.strip_prefix('/').unwrap_or(tail);
if let Some(after) = tail.strip_prefix('>') {
out.push_str(", ");
i = s.len() - after.len();
continue;
}
}
let c = s[i..].chars().next().unwrap_or(' ');
out.push(c);
i += c.len_utf8();
}
std::borrow::Cow::Owned(out)
}
fn without_html_entities(q: &str) -> String {
let mut out = String::with_capacity(q.len());
let mut rest = q;
while let Some(at) = rest.find('&') {
out.push_str(&rest[..at]);
let tail = &rest[at + 1..];
let name_len = tail.find(';').filter(|&n| {
(1..=8).contains(&n)
&& tail[..n]
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '#')
});
match name_len {
Some(n) => {
out.push(' ');
rest = &tail[n + 1..];
}
None => {
out.push('&');
rest = tail;
}
}
}
out.push_str(rest);
out
}
pub fn listed_addresses(q: &str) -> Vec<String> {
let q = without_html_entities(q);
if !q.contains(';') {
return Vec::new();
}
let mut segs: Vec<String> = q.split(';').map(|s| s.trim().to_string()).collect();
let last = segs.pop().unwrap_or_default();
let (last_address, town) = match last.rsplit_once(',') {
Some((a, t)) if !t.trim().is_empty() => (a.trim().to_string(), t.trim().to_string()),
_ => (last, String::new()),
};
if town.is_empty() {
return Vec::new();
}
segs.push(last_address);
segs.into_iter()
.filter(|s| s.chars().any(char::is_alphanumeric))
.map(|s| {
if town.is_empty() || s.contains(&town) {
s
} else {
format!("{s}, {town}")
}
})
.collect()
}
pub fn without_place_notes(q: &str) -> Option<String> {
const NOTES: &[&str] = &[
"independent city",
"city",
"town",
"village",
"township",
"borough",
"rear",
];
let mut out = String::with_capacity(q.len());
let mut rest = q;
while let Some(open) = rest.find('(') {
let Some(close) = rest[open..].find(')').map(|c| open + c) else {
break; };
let inner = rest[open + 1..close].trim().to_lowercase();
out.push_str(&rest[..open]);
if !NOTES.contains(&inner.as_str()) {
out.push_str(&rest[open..=close]);
}
rest = &rest[close + 1..];
}
out.push_str(rest);
let segs: Vec<String> = out
.split(',')
.map(|seg| {
let t = seg.split_whitespace().collect::<Vec<_>>().join(" ");
let low = t.to_lowercase();
let t = if low.starts_with("borough of ") && t.len() > 11 {
t[11..].to_string()
} else if low.ends_with(" borough")
&& t.len() > 8
&& !low.chars().any(|c| c.is_ascii_digit())
{
t[..t.len() - 8].to_string()
} else {
t
};
t
})
.collect();
let joined = segs.join(", ");
let before = q
.split(',')
.map(|s| s.split_whitespace().collect::<Vec<_>>().join(" "))
.collect::<Vec<_>>()
.join(", ");
(joined != before && joined.chars().any(char::is_alphanumeric)).then_some(joined)
}
pub fn with_numbered_ordinals(q: &str) -> Option<String> {
const UNITS: &[(&str, u32)] = &[
("first", 1),
("second", 2),
("third", 3),
("fourth", 4),
("fifth", 5),
("sixth", 6),
("seventh", 7),
("eighth", 8),
("ninth", 9),
("tenth", 10),
("eleventh", 11),
("twelfth", 12),
("thirteenth", 13),
("fourteenth", 14),
("fifteenth", 15),
("sixteenth", 16),
("seventeenth", 17),
("eighteenth", 18),
("nineteenth", 19),
("twentieth", 20),
("thirtieth", 30),
("fortieth", 40),
("fiftieth", 50),
];
const TENS: &[(&str, u32)] = &[("twenty", 20), ("thirty", 30), ("forty", 40), ("fifty", 50)];
let suffix = |n: u32| match (n % 100, n % 10) {
(11..=13, _) => "th",
(_, 1) => "st",
(_, 2) => "nd",
(_, 3) => "rd",
_ => "th",
};
let unit = |w: &str| {
UNITS
.iter()
.find(|(u, _)| u.eq_ignore_ascii_case(w))
.map(|(_, n)| *n)
};
let mut changed = false;
let words: Vec<String> = q
.split(' ')
.map(|word| {
let core_end = word.trim_end_matches(|c: char| !c.is_alphabetic()).len();
let (core, rest) = word.split_at(core_end);
let n = match core.split_once('-') {
Some((tens, one)) => TENS
.iter()
.find(|(t, _)| t.eq_ignore_ascii_case(tens))
.zip(unit(one).filter(|n| *n < 10))
.map(|((_, t), u)| t + u),
None => unit(core),
};
match n {
Some(n) => {
changed = true;
format!("{n}{}{rest}", suffix(n))
}
None => word.to_string(),
}
})
.collect();
changed.then(|| words.join(" "))
}
const QUERY_BOX_WORDS: &str = include_str!("query_box_words.tsv");
pub fn without_postal_box(s: &str) -> std::borrow::Cow<'_, str> {
if !s.bytes().any(|b| b.is_ascii_digit()) && !s.to_lowercase().contains("cedex") {
return std::borrow::Cow::Borrowed(s);
}
let mut toks: Vec<(usize, usize)> = Vec::new();
let mut start = None;
for (i, c) in s.char_indices() {
if c.is_whitespace() || c == ',' {
if let Some(b) = start.take() {
toks.push((b, i));
}
} else if start.is_none() {
start = Some(i);
}
}
if let Some(b) = start {
toks.push((b, s.len()));
}
let key = |t: &str| normalize(t).replace(' ', "");
let words: Vec<&str> = QUERY_BOX_WORDS
.lines()
.map(str::trim)
.filter(|w| !w.is_empty() && !w.starts_with('#'))
.collect();
let number = |t: &str| !t.is_empty() && t.bytes().all(|b| b.is_ascii_digit());
let mut cut: Vec<(usize, usize)> = Vec::new();
let mut i = 0;
while i < toks.len() {
let text = |j: usize| &s[toks[j].0..toks[j].1];
let mut taken = false;
for n in (1..=3).rev() {
if i + n < toks.len() {
let glued: String = (i..i + n).map(|j| key(text(j))).collect();
if i > 0
&& words.contains(&glued.as_str())
&& number(text(i + n))
&& !s[toks[i].0..toks[i + n].0].contains(',')
{
cut.push((toks[i].0, toks[i + n].1));
i += n + 1;
taken = true;
break;
}
}
}
if taken {
continue;
}
let alone = |a: usize, b: usize| {
let before = s[..a].trim_end();
let after = s[b..].trim_start();
(before.is_empty() || before.ends_with(','))
&& (after.is_empty() || after.starts_with(','))
};
if i > 0 && words.contains(&key(text(i)).as_str()) && alone(toks[i].0, toks[i].1) {
cut.push((toks[i].0, toks[i].1));
i += 1;
continue;
}
if key(text(i)) == "cedex" {
let with_office = i + 1 < toks.len()
&& number(text(i + 1))
&& text(i + 1).len() <= 2
&& !s[toks[i].1..toks[i + 1].0].contains(',');
let end = if with_office {
toks[i + 1].1
} else {
toks[i].1
};
cut.push((toks[i].0, end));
i += if with_office { 2 } else { 1 };
continue;
}
i += 1;
}
if cut.is_empty() {
return std::borrow::Cow::Borrowed(s);
}
let mut out = String::with_capacity(s.len());
let mut at = 0;
for (a, b) in cut {
out.push_str(&s[at..a]);
at = b;
}
out.push_str(&s[at..]);
let mut tidy = out
.split(',')
.map(|seg| {
seg.trim()
.trim_end_matches(" -")
.trim_start_matches("- ")
.trim()
})
.filter(|seg| !seg.is_empty() && *seg != "-")
.collect::<Vec<_>>()
.join(", ");
while tidy.contains(" ") {
tidy = tidy.replace(" ", " ");
}
std::borrow::Cow::Owned(tidy)
}
pub fn normalize(s: &str) -> String {
let pre: String = {
let ch: Vec<char> = s.chars().collect();
let mut o = String::with_capacity(s.len());
let mut i = 0;
while i < ch.len() {
let c = ch[i];
if c == '-'
&& i > 0
&& i + 1 < ch.len()
&& ch[i - 1].is_ascii_digit()
&& ch[i + 1].is_ascii_digit()
{
i += 1;
while i < ch.len() && ch[i].is_ascii_digit() {
i += 1; }
continue;
}
if c == '-'
&& i > 0
&& i + 1 < ch.len()
&& ch[i - 1].is_alphabetic()
&& ch[i + 1].is_ascii_digit()
&& (i == 1 || !ch[i - 2].is_alphanumeric())
{
i += 1;
continue;
}
o.push(c);
i += 1;
}
o
};
let mut out = String::with_capacity(pre.len());
let mut prev_space = true;
for ch in pre.chars().flat_map(|c| c.to_lowercase()) {
for d in ch.nfd() {
if is_combining_mark(d) {
continue; }
let mapped: Option<char> = match d {
'-' | '\'' | '’' | '‘' | 'ʻ' | 'ʼ' | '`' | '.' | '/' | ',' | ';' | '(' | ')' => {
None
}
c if c.is_whitespace() => None,
'\u{0111}' => Some('d'),
'\u{200B}'..='\u{200F}'
| '\u{2028}'..='\u{202E}'
| '\u{FE00}'..='\u{FE0F}'
| '\u{2190}'..='\u{2BFF}'
| '\u{1F000}'..='\u{1FFFF}' => None,
c if c.is_control() => None,
c => Some(c),
};
match mapped {
Some(c) => {
out.push(c);
prev_space = false;
}
None => {
if !prev_space {
out.push(' ');
prev_space = true;
}
}
}
}
}
let mut out = split_glued_postcode(&out);
while out.ends_with(' ') {
out.pop();
}
out
}
fn is_glued_postcode_boundary(digit_run: usize, character: char, output_ends_space: bool) -> bool {
digit_run >= 4 && character.is_alphabetic() && !output_ends_space
}
fn split_glued_postcode(s: &str) -> String {
let mut out = String::with_capacity(s.len() + 2);
let mut digit_run = 0usize;
for c in s.chars() {
if c.is_ascii_digit() {
digit_run += 1;
} else {
if is_glued_postcode_boundary(digit_run, c, out.ends_with(' ')) {
out.push(' ');
}
digit_run = 0;
}
out.push(c);
}
out
}
#[cfg(test)]
pub(crate) fn de_postcode_seam_trace(raw: &str) -> Option<String> {
let mut digit_run = 0usize;
let mut ends_space = true;
for character in raw.chars() {
if character.is_ascii_digit() {
digit_run += 1;
} else {
if is_glued_postcode_boundary(digit_run, character, ends_space) {
return Some(format!("{raw} -> {}", normalize(raw)));
}
digit_run = 0;
}
ends_space = character.is_whitespace();
}
None
}
pub fn translit_cyr_lat(s: &str) -> String {
let mut out = String::with_capacity(s.len() * 2);
for c in s.chars() {
match c {
'а' => out.push('a'),
'б' => out.push('b'),
'в' => out.push('v'),
'г' => out.push('g'),
'д' => out.push('d'),
'ђ' => out.push('đ'),
'е' => out.push('e'),
'ж' => out.push('ž'),
'з' => out.push('z'),
'и' => out.push('i'),
'ј' => out.push('j'),
'к' => out.push('k'),
'л' => out.push('l'),
'љ' => out.push_str("lj"),
'м' => out.push('m'),
'н' => out.push('n'),
'њ' => out.push_str("nj"),
'о' => out.push('o'),
'п' => out.push('p'),
'р' => out.push('r'),
'с' => out.push('s'),
'т' => out.push('t'),
'ћ' => out.push('ć'),
'у' => out.push('u'),
'ф' => out.push('f'),
'х' => out.push('h'),
'ц' => out.push('c'),
'ч' => out.push('č'),
'џ' => out.push_str("dž"),
'ш' => out.push('š'),
'я' => out.push_str("ja"),
'ю' => out.push_str("ju"),
'щ' => out.push_str("šč"),
'ы' => out.push('i'),
'э' => out.push('e'),
'ь' | 'ъ' => {}
other => out.push(other),
}
}
out
}
pub fn translit_cyr_lat_en(s: &str) -> String {
let mut out = String::with_capacity(s.len() * 2);
for c in s.chars() {
match c {
'а' => out.push('a'),
'б' => out.push('b'),
'в' => out.push('v'),
'г' => out.push('g'),
'д' => out.push('d'),
'е' | 'э' => out.push('e'),
'ё' => out.push_str("yo"),
'ж' => out.push_str("zh"),
'з' => out.push('z'),
'и' | 'ы' => out.push('i'),
'й' => out.push('y'), 'к' => out.push('k'),
'л' => out.push('l'),
'м' => out.push('m'),
'н' => out.push('n'),
'о' => out.push('o'),
'п' => out.push('p'),
'р' => out.push('r'),
'с' => out.push('s'),
'т' => out.push('t'),
'у' => out.push('u'),
'ф' => out.push('f'),
'х' => out.push('h'),
'ц' => out.push_str("ts"),
'ч' => out.push_str("ch"),
'ш' => out.push_str("sh"),
'щ' => out.push_str("shch"),
'ю' => out.push_str("yu"),
'я' => out.push_str("ya"),
'ў' => out.push('o'),
'ғ' => out.push('g'),
'қ' => out.push('q'),
'ҳ' => out.push('h'),
'ҷ' => out.push('j'),
'ь' | 'ъ' => {}
other => out.push(other),
}
}
out
}
pub fn has_cyrillic(s: &str) -> bool {
s.chars().any(|c| ('\u{0400}'..='\u{04FF}').contains(&c))
}
pub fn has_latin(s: &str) -> bool {
s.chars().any(|c| c.is_ascii_alphabetic())
}
const PHON_GROUPS: &[(&str, char)] = &[
("aou", 'o'), ("eiy", 'i'), ("qk", 'k'),
("xh", 'h'),
("wv", 'v'), ];
pub fn phonetic_key(s: &str) -> String {
let lat = if has_cyrillic(s) {
translit_cyr_lat_en(s)
} else {
s.to_string()
};
let n = normalize(&lat);
let mut out = String::with_capacity(n.len());
let mut prev = '\0';
for ch in n.chars() {
if ch == ' ' {
out.push(' ');
prev = '\0';
continue;
}
let c = PHON_GROUPS
.iter()
.find(|(set, _)| set.contains(ch))
.map(|(_, k)| *k)
.unwrap_or(ch);
if c != prev {
out.push(c); prev = c;
}
}
out
}
pub fn fold_homoglyphs(s: &str) -> String {
fn homoglyph(c: char) -> Option<char> {
Some(match c {
'a' => 'а',
'b' => 'в',
'c' => 'с',
'e' => 'е',
'h' => 'н',
'k' => 'к',
'm' => 'м',
'o' => 'о',
'p' => 'р',
't' => 'т',
'x' => 'х',
'y' => 'у',
_ => return None,
})
}
if !s.chars().any(|c| c.is_ascii_alphabetic()) || !has_cyrillic(s) {
return s.to_string(); }
s.split(' ')
.map(|tok| {
let cyr = tok
.chars()
.filter(|c| ('\u{0400}'..='\u{04FF}').contains(c))
.count();
let lat = tok.chars().filter(|c| c.is_ascii_alphabetic()).count();
if cyr == 0 || lat == 0 || cyr <= lat {
return tok.to_string();
}
tok.chars().map(|c| homoglyph(c).unwrap_or(c)).collect()
})
.collect::<Vec<_>>()
.join(" ")
}
pub fn translit_lat_cyr(s: &str) -> String {
let ch: Vec<char> = s.chars().collect();
let n = ch.len();
let starts = |i: usize, p: &str| -> bool {
let pc: Vec<char> = p.chars().collect();
i + pc.len() <= n && ch[i..i + pc.len()] == pc[..]
};
let mut out = String::with_capacity(s.len() * 2);
let mut i = 0;
while i < n {
if starts(i, "shch") {
out.push('щ');
i += 4;
} else if starts(i, "sch") {
out.push('щ');
i += 3;
} else if starts(i, "zh") {
out.push('ж');
i += 2;
} else if starts(i, "kh") {
out.push('х');
i += 2;
} else if starts(i, "ch") {
out.push('ч');
i += 2;
} else if starts(i, "sh") {
out.push('ш');
i += 2;
} else if starts(i, "yo") {
out.push('ё');
i += 2;
} else if starts(i, "yu") {
out.push('ю');
i += 2;
} else if starts(i, "ya") {
out.push('я');
i += 2;
} else if ch[i] == 'y' {
let prev_vowel = matches!(
out.chars().last(),
Some('а' | 'е' | 'ё' | 'и' | 'о' | 'у' | 'ы' | 'э' | 'ю' | 'я')
);
out.push(if prev_vowel { 'й' } else { 'ы' });
i += 1;
} else {
out.push(match ch[i] {
'a' => 'а',
'b' => 'б',
'c' => 'к',
'd' => 'д',
'e' => 'е',
'f' => 'ф',
'g' => 'г',
'h' => 'х',
'i' => 'и',
'j' => 'й',
'k' => 'к',
'l' => 'л',
'm' => 'м',
'n' => 'н',
'o' => 'о',
'p' => 'п',
'q' => 'к',
'r' => 'р',
's' => 'с',
't' => 'т',
'u' => 'у',
'v' => 'в',
'w' => 'в',
'x' => 'х',
'z' => 'з',
other => other,
});
i += 1;
}
}
out
}
#[cfg(test)]
mod tests {
#[test]
fn a_line_loses_the_notes_about_its_place() {
use super::without_place_notes as w;
assert_eq!(
w("2 N. 5th St., Richmond (Independent City), VIRGINIA").as_deref(),
Some("2 N. 5th St., Richmond, VIRGINIA")
);
assert_eq!(
w("1511 (rear) Twenty-second St. NW, Washington").as_deref(),
Some("1511 Twenty-second St. NW, Washington")
);
assert_eq!(
w("416 S. Broadway, Borough of West Cape May, NEW JERSEY").as_deref(),
Some("416 S. Broadway, West Cape May, NEW JERSEY")
);
assert_eq!(
w("394 93rd St., Stone Harbor Borough, NEW JERSEY").as_deref(),
Some("394 93rd St., Stone Harbor, NEW JERSEY")
);
assert_eq!(w("2 Main St., Cape May"), None);
assert_eq!(w("2 Main St. (rear, Cape May"), None);
assert_eq!(w("LASALLE, QC (7400 ST-PATRICK)"), None);
assert_eq!(
w("Nummer 11A, Ypekolsgea (Súdwest-Fryslân), Friesland"),
None
);
assert_eq!(w("Smythe Road Perth (West-Australië)"), None);
assert_eq!(w("12 Borough High Street, London"), None);
assert_eq!(w("12 Old Borough, London"), None);
assert_eq!(w("12 Borough, London"), None);
assert_eq!(w("(Independent City)"), None);
}
#[test]
fn a_line_writes_its_spelled_ordinals_as_numbers() {
use super::with_numbered_ordinals as o;
assert_eq!(
o("620 S. Eighth St., Sheboygan").as_deref(),
Some("620 S. 8th St., Sheboygan")
);
assert_eq!(
o("461 Twenty-seventh St., Ogden").as_deref(),
Some("461 27th St., Ogden")
);
assert_eq!(o("645 W. Third Ave.").as_deref(), Some("645 W. 3rd Ave."));
assert_eq!(
o("601 West Second Street").as_deref(),
Some("601 West 2nd Street")
);
assert_eq!(
o("1 First, 2 Twelfth, 3 Thirteenth").as_deref(),
Some("1 1st, 2 12th, 3 13th")
);
assert_eq!(o("Twenty-first Ave").as_deref(), Some("21st Ave"));
assert_eq!(o("Twenty-second St").as_deref(), Some("22nd St"));
assert_eq!(o("Thirty-third St").as_deref(), Some("33rd St"));
assert_eq!(o("Twentieth St").as_deref(), Some("20th St"));
assert_eq!(o("2 Main St., Cape May"), None);
assert_eq!(o("Twenty Oaks Rd"), None);
assert_eq!(o("Twenty-twelfth St"), None);
assert_eq!(o("Firsthand Rd"), None);
}
#[test]
fn the_addresses_a_line_lists_each_take_the_town_of_its_tail() {
use super::listed_addresses as l;
assert_eq!(
l("1 quai Bourbon ; 3 rue des Deux-Ponts, Paris 4e Arrondissement"),
[
"1 quai Bourbon, Paris 4e Arrondissement",
"3 rue des Deux-Ponts, Paris 4e Arrondissement"
]
);
assert_eq!(
l("26 rue des Tanneries ; 21 quai des Cordeliers ; 47 rue Porte-aux-Saints, Mantes"),
[
"26 rue des Tanneries, Mantes",
"21 quai des Cordeliers, Mantes",
"47 rue Porte-aux-Saints, Mantes"
]
);
assert!(l("rue X ; rue Y").is_empty());
assert!(l("400 Rusk Street; Houston TX 77002").is_empty());
assert!(l("aan het einde van de Heirweg").is_empty());
assert_eq!(
l("1 rue A&B ; 2 rue C, Lyon"),
["1 rue A B, Lyon", "2 rue C, Lyon"]
);
assert!(l("12 rue du Bac, Paris").is_empty());
assert_eq!(l("5 rue X ; , Lyon"), ["5 rue X, Lyon"]);
assert_eq!(
l("AT&T Plaza 1 ; 2 rue C, Lyon"),
["AT&T Plaza 1, Lyon", "2 rue C, Lyon"]
);
}
#[test]
fn a_line_break_tag_is_a_separator() {
use super::without_line_break_tags as w;
assert_eq!(
w("Place de la Gare<br />67630 Lauterbourg"),
"Place de la Gare, 67630 Lauterbourg"
);
assert_eq!(w("Rue X<BR>Paris"), "Rue X, Paris");
assert_eq!(w("Rue X<br/>Paris"), "Rue X, Paris");
assert_eq!(w("Rue <b>X</b>, Paris"), "Rue <b>X</b>, Paris");
assert_eq!(w("Rue X <brique>"), "Rue X <brique>");
assert_eq!(w("Résidence <br />Les Pins"), "Résidence , Les Pins");
}
#[test]
fn a_post_office_box_and_the_cedex_mark_leave_the_query() {
use super::without_postal_box as w;
assert_eq!(
w("246 rue Saint Jean BP 70639 59500 Douai"),
"246 rue Saint Jean 59500 Douai"
);
assert_eq!(
w("10-12 avenue Foch - BP 70330,57016,Metz Cedex 1"),
"10-12 avenue Foch, 57016, Metz"
);
assert_eq!(
w("1517 PL DE L HOTEL DE VILLE, CS 40051, 76084 LE HAVRE CEDEX"),
"1517 PL DE L HOTEL DE VILLE, 76084 LE HAVRE"
);
assert_eq!(
w("Musterstraße 5, Postfach 1234, 10115 Berlin"),
"Musterstraße 5, 10115 Berlin"
);
assert_eq!(w("1 rue X, B.P. 12, 75001 Paris"), "1 rue X, 75001 Paris");
assert_eq!(
w("Boîte postale 7, 1 rue X, Lyon"),
"Boîte postale 7, 1 rue X, Lyon"
);
assert_eq!(
w("PO Box 12, Main Street 5, Ville"),
"PO Box 12, Main Street 5, Ville"
);
assert_eq!(w("Rue du BP, Paris"), "Rue du BP, Paris");
assert_eq!(
w("Station BP Total, 12 rue X, Lyon"),
"Station BP Total, 12 rue X, Lyon"
);
assert_eq!(
w("Calle Mayor 5, CP 28013 Madrid"),
"Calle Mayor 5, CP 28013 Madrid"
);
assert_eq!(
w("Box Hill Road 3, Melbourne"),
"Box Hill Road 3, Melbourne"
);
assert_eq!(w("Rue Cedexis 4, Lyon 3e"), "Rue Cedexis 4, Lyon 3e");
assert_eq!(
w("1 place X, 31040 TOULOUSE CEDEX 6"),
"1 place X, 31040 TOULOUSE"
);
assert_eq!(
w("1 place X, TOULOUSE CEDEX 31040"),
"1 place X, TOULOUSE 31040"
);
assert_eq!(w("12 rue du Bac, Paris"), "12 rue du Bac, Paris");
assert_eq!(
w("70014 VESOUL CEDEX, 58 RUE PAUL MOREL, BP 392"),
"70014 VESOUL, 58 RUE PAUL MOREL"
);
assert_eq!(
w("1517 PL DE L HOTEL DE VILLE, CS, 76084 LE HAVRE CEDEX"),
"1517 PL DE L HOTEL DE VILLE, 76084 LE HAVRE"
);
}
#[test]
fn typographic_signs_become_the_ascii_ones_and_nothing_else_changes() {
use super::ascii_signs;
assert_eq!(
ascii_signs("Unter den Linden 4 \u{2013} 10117 Berlin"),
"Unter den Linden 4 - 10117 Berlin"
);
assert_eq!(
ascii_signs("Rathausplatz 2\u{2013}4"),
"Rathausplatz 2\u{2013}4"
);
assert_eq!(
ascii_signs("Stari\u{2014}Grad 40 \u{2014} 44"),
"Stari\u{2014}Grad 40-44"
);
assert_eq!(
ascii_signs("Normannenweg 17 \u{2013} 21, 20357 Hamburg"),
"Normannenweg 17-21, 20357 Hamburg"
);
assert_eq!(
ascii_signs("Opferperspektive \u{2013} e.V."),
"Opferperspektive - e.V."
);
assert_eq!(
ascii_signs("Rua Garcia D\u{00B4}Avila 105"),
"Rua Garcia D'Avila 105"
);
assert_eq!(ascii_signs("O\u{2019}Brien Street"), "O'Brien Street");
assert_eq!(
ascii_signs("Aleja \u{201E}Solidarności\u{201D} 58"),
"Aleja \u{201E}Solidarności\u{201D} 58"
);
assert_eq!(
ascii_signs("\u{00AB}Le Cèdre\u{00BB}"),
"\u{00AB}Le Cèdre\u{00BB}"
);
assert_eq!(
ascii_signs("Calle \u{201C}Real\u{201D} 1"),
"Calle \u{201C}Real\u{201D} 1"
);
assert!(matches!(
ascii_signs("Rue de la Paix 10"),
std::borrow::Cow::Borrowed(_)
));
}
use super::normalize;
#[test]
fn mirrors_duckdb() {
assert_eq!(
normalize("Avenue du Général-Leclerc"),
"avenue du general leclerc"
);
assert_eq!(normalize("Rue de l'Église"), "rue de l eglise");
assert_eq!(normalize("Bulevar Oslobođenja"), "bulevar oslobodenja"); assert_eq!(normalize("Šafarikova ČĆŽ"), "safarikova ccz");
assert_eq!(normalize("Ёлки-Йод"), "елки иод"); assert_eq!(normalize("oʻzbekiston g‘afur"), "o zbekiston g afur");
}
#[test]
fn translit_serbian_pair() {
use super::translit_cyr_lat;
assert_eq!(
normalize(&translit_cyr_lat("милентија поповића")),
"milentija popovica"
);
assert_eq!(
normalize(&translit_cyr_lat("милентия поповича")),
"milentija popovica"
);
assert_eq!(
normalize(&translit_cyr_lat("кнеза михаила")),
"kneza mihaila"
);
}
#[test]
fn translit_latin_to_cyrillic() {
use super::translit_lat_cyr;
assert_eq!(normalize(&translit_lat_cyr("bratsk")), "братск");
assert_eq!(normalize(&translit_lat_cyr("tashkent")), "ташкент");
assert_eq!(
normalize(&translit_lat_cyr("улица возрождения 1 bratsk")),
"улица возрождения 1 братск"
);
assert_eq!(normalize(&translit_lat_cyr("leninskiy")), "ленинскии");
assert_eq!(
normalize(&translit_lat_cyr("novoyasenevskiy")),
"новоясеневскии"
);
assert_eq!(normalize(&translit_lat_cyr("tolstoy")), "толстои");
assert_eq!(normalize(&translit_lat_cyr("krym")), "крым"); }
#[test]
fn fold_homoglyphs_mixed_script() {
use super::fold_homoglyphs;
assert_eq!(
fold_homoglyphs("тв\u{0065}рск\u{0061}я 6 москва"),
"тверская 6 москва"
);
assert_eq!(fold_homoglyphs("tverskaya 6 moskva"), "tverskaya 6 moskva");
assert_eq!(fold_homoglyphs("тверская 6 москва"), "тверская 6 москва");
assert_eq!(fold_homoglyphs("ab\u{0432}"), "ab\u{0432}"); assert_eq!(
fold_homoglyphs("\u{0079}\u{0062}\u{043E}\u{0440}\u{043A}\u{0430}"),
"уворка"
);
}
}