geolith-search-builder 0.3.2

Builds geocoding indexes from Overture Maps and OpenStreetMap: Postgres COPY files, Photon and Pelias bulk files, and the search part of a .glx file
Documentation
//! Other words people search a place by: "collector office" for a Collectorate,
//! "government" for "Govt.", "cinema" for a talkies.
//!
//! Pelias adds such synonyms when it analyses names (`schema/synonyms`) and
//! Nominatim through its ICU variants; here they are written into a place's
//! searched text after the language mark, so they match without lengthening the
//! scored text.

/// Whole words and the words they also stand for.
// "theatre" is left out: in India it names drama halls and schools as
// often as cinemas ("Natranjali Theatre Arts").
const WORDS: [(&str, &str); 11] = [
    ("collectorate", "collector office"),
    ("collectorate's", "collector office"),
    ("govt", "government"),
    ("gov", "government"),
    ("gvt", "government"),
    ("talkies", "cinema"),
    ("multiplex", "cinema"),
    // Jama, Juma and Jumma Masjid are one name, written three ways.
    ("juma", "jama"),
    ("jumma", "jama"),
    ("jumah", "jama"),
    ("masjid", "mosque"),
];

/// Runs of words and the words they also stand for.
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"),
];

/// The extra words `name` is also searched by, each once; empty when none.
#[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());
    }
}