Cotton code
102 lines · 3.7 KB · RustRaw
//! Whose report it is: the name a lab printed against the people the owner//! keeps. A title (`Mr`, `Smt`), the order of the names, case and spacing//! never make two people; initials might be the same person or another of//! the family, so they are ASKED about, never merged or split silently.use std::collections::BTreeSet;/// Words a lab prints before a name.const TITLES: &[&str] = &["mr", "mrs", "ms", "miss", "mx", "dr", "master", "baby", "smt", "shri", "sri", "shree", "kumari", "kum", "km", "md", "late", "prof", "sir", "madam", "b o", "baby of"];/// A printed name as its words: lower case, accents and titles dropped.pub fn name_words(printed: &str) -> Vec<String> {    let folded = super::catalogue::fold_name(printed);    let mut words: Vec<String> = folded.split(' ').filter(|word| !word.is_empty()).map(str::to_string).collect();    while words.len() > 1 && TITLES.contains(&words[0].as_str()) {        words.remove(0);    }    words}/// A name's one comparable spelling: its words sorted, spaces gone.pub fn folded_name(printed: &str) -> String {    let mut words = name_words(printed);    words.sort();    words.concat()}/// How a printed name stands against one known name.#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]pub enum Likeness {    /// Nothing alike.    Other,    /// Could be: initials for names, or a name left out (`R Kumar`, `Rekha`).    Maybe,    /// The same name: title, order, case or spacing aside.    Same,}pub fn likeness(printed: &str, known: &str) -> Likeness {    let (a, b) = (name_words(printed), name_words(known));    if a.is_empty() || b.is_empty() {        return Likeness::Other;    }    if folded_name(printed) == folded_name(known) {        return Likeness::Same;    }    // Every word of the shorter is a word of the longer, or its initial; at    // least one whole word is shared.    let (short, long) = if a.len() <= b.len() { (&a, &b) } else { (&b, &a) };    let mut free: Vec<&String> = long.iter().collect();    let mut whole = false;    for word in short {        let found = free.iter().position(|other| *other == word).inspect(|_| whole = true).or_else(|| {            if word.chars().count() == 1 {                free.iter().position(|other| other.starts_with(word.as_str()))            } else {                free.iter().position(|other| other.chars().count() == 1 && word.starts_with(other.as_str()))            }        });        match found {            Some(at) => {                free.remove(at);            }            None => return Likeness::Other,        }    }    if whole { Likeness::Maybe } else { Likeness::Other }}/// Who a printed name is, among `known` (each person's id and every name/// they are known by: their own and the variants already confirmed).#[derive(Debug, Clone, PartialEq, Eq)]pub enum Whose {    /// This person, surely.    Person(String),    /// One of these, perhaps: the owner is asked.    Ask(Vec<String>),    /// Nobody the owner keeps: a new person is proposed.    New,}pub fn whose(printed: &str, known: &[(String, Vec<String>)]) -> Whose {    let mut same = BTreeSet::new();    let mut maybe = BTreeSet::new();    for (id, names) in known {        match names.iter().map(|name| likeness(printed, name)).max().unwrap_or(Likeness::Other) {            Likeness::Same => {                same.insert(id.clone());            }            Likeness::Maybe => {                maybe.insert(id.clone());            }            Likeness::Other => {}        }    }    match same.len() {        1 => Whose::Person(same.into_iter().next().expect("one")),        0 if maybe.is_empty() => Whose::New,        0 => Whose::Ask(maybe.into_iter().collect()),        _ => Whose::Ask(same.into_iter().collect()),    }}