//! 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.pubfn name_words(printed: &str) -> Vec<String> {let folded = super::catalogue::fold_name(printed);letmut 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.pubfn folded_name(printed: &str) -> String {letmut 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)]pubenumLikeness {/// 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,}pubfn likeness(printed: &str, known: &str) -> Likeness {let (a, b) = (name_words(printed), name_words(known));if a.is_empty() || b.is_empty() {returnLikeness::Other; }if folded_name(printed) == folded_name(known) {returnLikeness::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) };letmut free: Vec<&String> = long.iter().collect();letmut 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 => returnLikeness::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)]pubenumWhose {/// 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,}pubfn whose(printed: &str, known: &[(String, Vec<String>)]) -> Whose {letmut same = BTreeSet::new();letmut 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")),0if maybe.is_empty() => Whose::New,0 => Whose::Ask(maybe.into_iter().collect()), _ => Whose::Ask(same.into_iter().collect()), }}