Skip to main content

aidaemon_memory/
text.rs

1//! Small text helpers shared by the memory modules.
2
3/// `s` cut to at most `max_chars` characters, ending in "..." when cut.
4pub(crate) use aidaemon_tools::text::truncate_str as truncate_chars;
5
6/// Lowercased words of a fact value, punctuation and spacing ignored, for
7/// comparing values written by different extractors ("~/projects/" and
8/// "~/projects" are one value).
9/// The lowercase words of `text` (runs of letters and digits, any script).
10pub(crate) fn words(text: &str) -> impl Iterator<Item = String> + '_ {
11    text.split(|c: char| !c.is_alphanumeric())
12        .filter(|word| !word.is_empty())
13        .map(str::to_lowercase)
14}
15
16/// `value`'s lowercase words joined by single spaces: two values that
17/// differ only in case, spacing or punctuation compare equal.
18pub(crate) fn normalize_fact_value(value: &str) -> String {
19    words(value).collect::<Vec<_>>().join(" ")
20}
21
22pub(crate) fn canonicalize_fact_key(key: &str) -> String {
23    let raw = key.trim();
24    if raw.is_empty() {
25        return String::new();
26    }
27
28    let mut out = String::with_capacity(raw.len());
29    let mut last_was_sep = false;
30    for ch in raw.chars() {
31        if ch.is_ascii_alphanumeric() {
32            out.push(ch.to_ascii_lowercase());
33            last_was_sep = false;
34        } else if !last_was_sep {
35            out.push('_');
36            last_was_sep = true;
37        }
38    }
39
40    let trimmed = out.trim_matches('_');
41    if trimmed.is_empty() {
42        raw.to_string()
43    } else {
44        trimmed.to_string()
45    }
46}
47
48/// The one name for a personal fact several keys mean (`dob` and
49/// `Date of Birth` are both `birth_date`), after the key is canonicalized.
50pub fn canonical_personal_key(key: &str) -> String {
51    match canonicalize_fact_key(key).as_str() {
52        "birthday" | "date_of_birth" | "dob" => "birth_date".to_string(),
53        "current_residence" | "home" | "location" => "residence".to_string(),
54        "place_of_birth" => "birthplace".to_string(),
55        "favorite_name" | "preferred_first_name" => "preferred_name".to_string(),
56        "address_as" | "call_me" | "preferred_address" | "salutation" => "addressing".to_string(),
57        "avoid_names" | "rejected_name" | "rejected_names" | "do_not_call" => {
58            "rejected_alias".to_string()
59        }
60        other => other.to_string(),
61    }
62}
63
64/// Canonical family for matching projected owner records (e.g. `legal_name_variant`
65/// and `online_handle`) against flat input keys (`name`/`full_name`, `username`/`handle`).
66pub fn owner_projection_family(key: &str) -> String {
67    match canonical_personal_key(key).as_str() {
68        "name" | "full_name" | "legal_name_variant" => "name".to_string(),
69        "username" | "online_nickname" | "online_handle" | "handle" => "online_handle".to_string(),
70        other => other.to_string(),
71    }
72}
73
74/// Keys naming how the owner is named or addressed, regardless of whether a
75/// writer filed them under `user` or `preference`.
76pub fn is_owner_name_key(key: &str) -> bool {
77    matches!(
78        canonical_personal_key(key).as_str(),
79        "name"
80            | "full_name"
81            | "legal_name_variant"
82            | "first_name"
83            | "last_name"
84            | "preferred_name"
85            | "nickname"
86            | "addressing"
87            | "rejected_alias"
88    )
89}
90
91/// Keys belonging to the owner's structured canonical profile.
92pub fn is_owner_profile_key(key: &str) -> bool {
93    is_owner_name_key(key)
94        || matches!(
95            canonical_personal_key(key).as_str(),
96            "username"
97                | "online_nickname"
98                | "online_handle"
99                | "handle"
100                | "birth_date"
101                | "residence"
102                | "birthplace"
103        )
104}