const WORDS: [(&str, &str); 11] = [
("collectorate", "collector office"),
("collectorate's", "collector office"),
("govt", "government"),
("gov", "government"),
("gvt", "government"),
("talkies", "cinema"),
("multiplex", "cinema"),
("juma", "jama"),
("jumma", "jama"),
("jumah", "jama"),
("masjid", "mosque"),
];
const PHRASES: [(&str, &str); 10] = [
("civil hospital", "district hospital government hospital"),
("district hospital", "civil hospital government hospital"),
("sub divisional hospital", "government hospital"),
("sub district hospital", "government hospital"),
("community health centre", "government hospital"),
("rural hospital", "government hospital"),
("primary health centre", "government hospital"),
("general hospital", "government hospital"),
("bus station", "bus stand"),
("bus depot", "bus stand"),
];
#[must_use]
pub fn synonyms(name: &str) -> Vec<String> {
let words: Vec<String> = name
.split(|c: char| !(c.is_alphanumeric() || c == '\''))
.filter(|word| !word.is_empty())
.map(str::to_lowercase)
.collect();
let joined = format!(" {} ", words.join(" "));
let mut out: Vec<String> = Vec::new();
let mut add = |extra: &str| {
if !joined.contains(&format!(" {extra} ")) && !out.iter().any(|kept| kept == extra) {
out.push(extra.to_owned());
}
};
for word in &words {
if let Some((_, extra)) = WORDS.iter().find(|(from, _)| from == word) {
add(extra);
}
}
for (from, extra) in PHRASES {
if joined.contains(&format!(" {from} ")) {
add(extra);
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_name_gains_the_words_it_is_searched_by() {
assert_eq!(synonyms("Collectorate"), ["collector office"]);
assert_eq!(synonyms("Gov. Degree College. Khurai"), ["government"]);
assert_eq!(synonyms("Shree Talkies"), ["cinema"]);
assert_eq!(
synonyms("Civil Hospital Nadiad"),
["district hospital government hospital"]
);
assert_eq!(
synonyms("Sub-Divisional Hospital, Jhanjharpur"),
["government hospital"]
);
assert_eq!(synonyms("ST Bus Station Gondal"), ["bus stand"]);
assert_eq!(synonyms("juma masjid kottiyam"), ["jama", "mosque"]);
assert_eq!(synonyms("Jath Rural Hospital"), ["government hospital"]);
}
#[test]
fn words_already_held_or_unrelated_add_nothing() {
assert!(synonyms("Government College Cinema Hall").is_empty());
assert!(synonyms("Governor's House").is_empty());
assert!(synonyms("India Gate").is_empty());
assert!(synonyms("Natranjali Theatre Arts").is_empty());
assert!(synonyms("Collectorate Collector Office").is_empty());
}
}