use crate::norm::normalize;
use crate::query::{hit_is_weak, query_cascade, Hit, Index};
use serde::Serialize;
#[derive(Serialize, Clone)]
pub struct Reading {
pub query: String,
pub words: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub postcode: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub house_number: Option<u32>,
pub answer: Option<Hit>,
}
pub fn read(addr: &Index, poi: Option<&Index>, query: &str) -> Reading {
let scope = crate::diagnosis::Scope::new();
let answer = query_cascade(addr, poi, query, 1).into_iter().next();
let trace = scope.finish(usize::from(answer.is_some()));
let first = &trace["parsed"]["examples"][0];
let words = first["street_or_rest"]
.as_str()
.map(normalize)
.unwrap_or_else(|| normalize(query));
Reading {
query: query.to_string(),
words,
postcode: first["postcode"]
.as_u64()
.and_then(|v| u32::try_from(v).ok()),
house_number: first["house_number"]
.as_u64()
.and_then(|v| u32::try_from(v).ok()),
answer,
}
}
#[derive(Serialize)]
pub struct Same {
pub level: &'static str,
pub reason: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub text: Option<&'static str>,
pub a: Reading,
pub b: Reading,
}
fn same_key(x: &str, y: &str) -> bool {
let x = normalize(x);
!x.is_empty() && x == normalize(y)
}
pub fn answer_level(a: &Hit, b: &Hit) -> &'static str {
let commune = same_key(&a.commune, &b.commune);
let street = commune && same_key(&a.street, &b.street);
match (&a.housenumber, &b.housenumber) {
(Some(x), Some(y)) if street && normalize(x) == normalize(y) => "house",
_ if street => "street",
_ if commune => "commune",
_ => "different",
}
}
fn text_level(a: &Reading, b: &Reading) -> &'static str {
let postcodes_agree = a.postcode.is_none() || b.postcode.is_none() || a.postcode == b.postcode;
if a.words.is_empty() || a.words != b.words || !postcodes_agree {
"different_text"
} else if a.house_number == b.house_number {
"same_text"
} else {
"different_number"
}
}
pub fn decide(a: Reading, b: Reading) -> Same {
let (level, reason, text) = match (&a.answer, &b.answer) {
(Some(x), Some(y)) => {
let weak = hit_is_weak(x) || hit_is_weak(y);
match answer_level(x, y) {
"house" if weak => (
"street",
"same street and number, but an answer is weak: not confirmed as one house".to_string(),
None,
),
"house" => ("house", "same street, number and commune; neither answer is weak".to_string(), None),
"different" if weak => (
"unknown",
"the answers differ and an answer is weak: the difference may be a wrong answer".to_string(),
None,
),
"street" => ("street", "same street and commune, different or missing house number".to_string(), None),
"commune" => ("commune", "same commune, different street".to_string(), None),
_ => ("different", "the answers are in different communes".to_string(), None),
}
}
_ => (
"unknown",
"a line has no answer in this sheet; `text` compares how the two lines read"
.to_string(),
Some(text_level(&a, &b)),
),
};
Same {
level,
reason,
text,
a,
b,
}
}
pub fn address_id(country: &str, commune: &str, street: &str, number: &str) -> String {
use blake2::{Blake2b512, Digest};
let mut h = Blake2b512::new();
for (i, part) in [
country.to_ascii_lowercase(),
normalize(commune),
normalize(street),
normalize(number),
]
.iter()
.enumerate()
{
if i > 0 {
h.update([0x1f]);
}
h.update(part.as_bytes());
}
h.finalize()[..8]
.iter()
.map(|b| format!("{b:02x}"))
.collect()
}
pub fn same_address(addr: &Index, poi: Option<&Index>, a: &str, b: &str) -> Same {
decide(read(addr, poi, a), read(addr, poi, b))
}
#[cfg(test)]
mod id_tests {
use super::address_id;
#[test]
fn the_id_reads_names_as_the_engine_does_and_holds_the_country() {
let a = address_id("rs", "Novi Sad", "Bulevar Oslobođenja", "100");
assert_eq!(a.len(), 16);
assert_eq!(
a,
address_id("RS", "novi sad", "bulevar oslobodenja", "100"),
"case and diacritics fold"
);
assert_ne!(
a,
address_id("ba", "Novi Sad", "Bulevar Oslobođenja", "100")
);
assert_ne!(
a,
address_id("rs", "Novi Sad", "Bulevar Oslobođenja", "102")
);
}
}
#[cfg(test)]
mod tests {
use super::*;
fn hit(
street: &str,
number: Option<&str>,
commune: &str,
precision: &'static str,
confidence: f32,
) -> Hit {
Hit {
lat: 0.0,
lon: 0.0,
precision,
score: 0.0,
confidence,
street: street.into(),
housenumber: number.map(Into::into),
commune: commune.into(),
postcode: String::new(),
flags: vec!["street_exact"],
region: None,
distance_m: None,
}
}
fn reading(
answer: Option<Hit>,
words: &str,
number: Option<u32>,
postcode: Option<u32>,
) -> Reading {
Reading {
query: words.into(),
words: words.into(),
postcode,
house_number: number,
answer,
}
}
fn level(a: Option<Hit>, b: Option<Hit>) -> &'static str {
decide(reading(a, "x", None, None), reading(b, "x", None, None)).level
}
#[test]
fn house_needs_the_same_street_number_and_commune_and_no_weak_answer() {
let strong = |street, number, commune| Some(hit(street, number, commune, "house", 0.9));
assert_eq!(
level(
strong("Main St", Some("12"), "Ames"),
strong("MAIN ST", Some("12"), "ames")
),
"house"
);
assert_eq!(
level(
strong("Main St", Some("12"), "Ames"),
strong("Main St", Some("14"), "Ames")
),
"street"
);
assert_eq!(
level(
strong("Main St", Some("12"), "Ames"),
strong("Main St", None, "Ames")
),
"street"
);
assert_eq!(
level(
strong("Main St", Some("12"), "Ames"),
strong("Oak St", Some("12"), "Ames")
),
"commune"
);
assert_eq!(
level(
strong("Main St", Some("12"), "Ames"),
strong("Main St", Some("12"), "Boone")
),
"different"
);
let mut fuzzy = hit("Main St", Some("12"), "Ames", "house", 0.2);
fuzzy.flags = vec!["street_fuzzy"];
let weak = Some(fuzzy);
assert_eq!(
level(strong("Main St", Some("12"), "Ames"), weak.clone()),
"street"
);
assert_eq!(level(weak, strong("Main St", Some("12"), "Ames")), "street");
let exact_low = Some(hit("Main St", Some("12"), "Ames", "house", 0.1));
assert_eq!(
level(exact_low, strong("Main St", Some("12"), "Ames")),
"house"
);
}
#[test]
fn a_difference_through_a_weak_answer_is_unknown_not_different() {
let city = Some(hit("", None, "Boone", "city", 0.05));
let strong = Some(hit("Main St", Some("12"), "Ames", "house", 0.9));
assert_eq!(level(strong.clone(), city), "unknown");
let other = Some(hit("Oak St", Some("1"), "Boone", "house", 0.9));
assert_eq!(level(strong, other), "different");
}
#[test]
fn without_an_answer_the_level_is_unknown_and_text_compares_the_readings() {
let a = reading(None, "main st ames", Some(12), Some(50010));
let same = decide(a.clone(), reading(None, "main st ames", Some(12), None));
assert_eq!((same.level, same.text), ("unknown", Some("same_text")));
let number = decide(
a.clone(),
reading(None, "main st ames", Some(14), Some(50010)),
);
assert_eq!(number.text, Some("different_number"));
let postcode = decide(
a.clone(),
reading(None, "main st ames", Some(12), Some(50011)),
);
assert_eq!(postcode.text, Some("different_text"));
let words = decide(a, reading(None, "oak st ames", Some(12), Some(50010)));
assert_eq!(words.text, Some("different_text"));
let answered = decide(
reading(
Some(hit("Main St", Some("12"), "Ames", "house", 0.9)),
"x",
None,
None,
),
reading(
Some(hit("Main St", Some("12"), "Ames", "house", 0.9)),
"x",
None,
None,
),
);
assert_eq!(answered.text, None);
}
}