use crate::records::Fact;
use std::collections::HashSet;
const RELATIONSHIP_ROOTS: &[&str] = &[
"mother",
"father",
"mom",
"dad",
"parent",
"partner",
"spouse",
"wife",
"husband",
"child",
"children",
"son",
"daughter",
"kid",
"sibling",
"brother",
"sister",
"grandmother",
"grandfather",
];
pub fn fact_namespace(key: &str) -> Option<&str> {
key.split_once(':')
.map(|(ns, _)| ns)
.filter(|ns| !ns.is_empty())
}
pub fn is_relationship_key(key: &str) -> bool {
if fact_namespace(key).is_some() {
return false; }
let lower = key.to_ascii_lowercase();
RELATIONSHIP_ROOTS.iter().any(|root| {
lower == *root
|| lower.starts_with(&format!("{root}_")) || lower.ends_with(&format!("_{root}")) })
}
fn word_tokens(s: &str) -> Vec<String> {
crate::text::words(s).collect()
}
pub fn entity_mentioned_as_words(haystack: &str, entity: &str) -> bool {
let haystack_tokens: HashSet<String> = word_tokens(haystack).into_iter().collect();
let entity_words: Vec<String> = word_tokens(entity);
let significant: Vec<&String> = entity_words.iter().filter(|w| w.len() >= 2).collect();
if significant.is_empty() {
return false;
}
significant.iter().all(|w| haystack_tokens.contains(*w))
}
pub fn normalize_namespace(ns: &str) -> String {
ns.chars()
.filter(|c| c.is_alphanumeric())
.flat_map(char::to_lowercase)
.collect()
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Seed {
Namespace(String),
Person(String),
}
const MAX_SEEDS: usize = 6;
pub fn seeds_from_hits(hits: &[Fact], known_people: &[String]) -> Vec<Seed> {
let mut seeds: Vec<Seed> = Vec::new();
let mut seen: HashSet<String> = HashSet::new();
let mut push = |seed: Seed, seeds: &mut Vec<Seed>| {
let identity = match &seed {
Seed::Namespace(ns) => format!("ns:{ns}"),
Seed::Person(name) => format!("person:{}", name.to_lowercase()),
};
if seen.insert(identity) {
seeds.push(seed);
}
};
for hit in hits {
if let Some(ns) = fact_namespace(&hit.key) {
let normalized = normalize_namespace(ns);
if !normalized.is_empty() {
push(Seed::Namespace(normalized), &mut seeds);
}
}
if is_relationship_key(&hit.key) {
let name = hit.value.trim();
if !name.is_empty() {
push(Seed::Person(name.to_string()), &mut seeds);
}
}
for person in known_people {
if entity_mentioned_as_words(&hit.key, person)
|| entity_mentioned_as_words(&hit.value, person)
{
push(Seed::Person(person.clone()), &mut seeds);
}
}
}
seeds.truncate(MAX_SEEDS);
seeds
}
pub fn eligible_neighbors<'a>(
all: &'a [Fact],
seeds: &[Seed],
relationship_cluster: bool,
exclude_ids: &HashSet<i64>,
) -> Vec<&'a Fact> {
let namespaces: HashSet<&str> = seeds
.iter()
.filter_map(|seed| match seed {
Seed::Namespace(ns) => Some(ns.as_str()),
Seed::Person(_) => None,
})
.collect();
let people: Vec<&str> = seeds
.iter()
.filter_map(|seed| match seed {
Seed::Person(name) => Some(name.as_str()),
Seed::Namespace(_) => None,
})
.collect();
let mut seen: HashSet<i64> = HashSet::new();
all.iter()
.filter(|f| !exclude_ids.contains(&f.id))
.filter(|f| {
let in_namespace = fact_namespace(&f.key)
.is_some_and(|ns| namespaces.contains(normalize_namespace(ns).as_str()));
let mentions_person = people.iter().any(|name| {
entity_mentioned_as_words(&f.key, name) || entity_mentioned_as_words(&f.value, name)
});
let in_cluster = relationship_cluster && is_relationship_key(&f.key);
in_namespace || mentions_person || in_cluster
})
.filter(|f| seen.insert(f.id))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::search_fixture::{self as fx, corpus};
fn fact(id: i64, category: &str, key: &str, value: &str) -> Fact {
Fact {
id,
category: category.into(),
key: key.into(),
value: value.into(),
source: "test".into(),
created_at: chrono::Utc::now(),
updated_at: chrono::Utc::now(),
superseded_at: None,
recall_count: 0,
last_recalled_at: None,
channel_id: None,
privacy: crate::records::FactPrivacy::Global,
first_seen_at: None,
source_excerpt: None,
}
}
#[test]
fn namespace_is_prefix_before_colon() {
assert_eq!(
fact_namespace("LearnEnglishSounds:path"),
Some("LearnEnglishSounds")
);
assert_eq!(fact_namespace("partner_name"), None);
}
#[test]
fn relationship_keys_detected() {
assert!(is_relationship_key("mother_name"));
assert!(is_relationship_key("father"));
assert!(is_relationship_key("partner_name"));
assert!(!is_relationship_key("LearnEnglishSounds:path"));
}
fn ids(facts: &[&Fact]) -> HashSet<i64> {
facts.iter().map(|f| f.id).collect()
}
fn by_id(all: &[Fact], id: i64) -> Fact {
all.iter().find(|f| f.id == id).unwrap().clone()
}
#[test]
fn generic_namespace_does_not_co_mention() {
let all = corpus();
let hit = by_id(&all, fx::WORKFLOW_RELEASE_CHECKLIST);
let seeds = seeds_from_hits(std::slice::from_ref(&hit), &[]);
assert_eq!(seeds, vec![Seed::Namespace("workflow".into())]);
let exclude: HashSet<i64> = [hit.id].into_iter().collect();
let out = ids(&eligible_neighbors(&all, &seeds, false, &exclude));
assert_eq!(out.len(), 29);
assert!(out.iter().all(|id| fx::WORKFLOW_NS.contains(id)));
assert!(!out.iter().any(|id| fx::GENERIC_MENTIONS.contains(id)));
}
#[test]
fn namespace_variants_are_one_namespace() {
let all = corpus();
let hit = by_id(&all, 4000); let seeds = seeds_from_hits(std::slice::from_ref(&hit), &[]);
let exclude: HashSet<i64> = [hit.id].into_iter().collect();
let out = ids(&eligible_neighbors(&all, &seeds, false, &exclude));
for id in (4001..4005).chain(4010..4015).chain(4020..4025) {
assert!(out.contains(&id), "{id} missing: {out:?}");
}
assert_eq!(out.len(), 14);
}
#[test]
fn seeds_only_from_returned_hits() {
let hits = vec![
fact(1, "user", "birth_date", "1990-04-12"),
fact(2, "preference", "reply_style", "short"),
];
assert!(seeds_from_hits(&hits, &[]).is_empty());
}
#[test]
fn family_cluster_still_travels() {
let all = corpus();
let hit = by_id(&all, fx::MOTHER_NAME);
let seeds = seeds_from_hits(std::slice::from_ref(&hit), &[]);
assert_eq!(seeds, vec![Seed::Person("Maria Rivera".into())]);
let exclude: HashSet<i64> = [hit.id].into_iter().collect();
let out = ids(&eligible_neighbors(&all, &seeds, true, &exclude));
assert!(out.contains(&fx::FATHER_NAME));
assert!(out.contains(&fx::PARTNER_NAME));
assert!(!out.iter().any(|id| fx::HOT_CLUSTER.contains(id)));
}
#[test]
fn known_person_in_a_hit_is_a_seed() {
let hits = vec![fact(1, "general", "gift_idea", "a kettle for Jordan Lee")];
let seeds = seeds_from_hits(&hits, &["Jordan Lee".into(), "Ana".into()]);
assert_eq!(seeds, vec![Seed::Person("Jordan Lee".into())]);
}
#[test]
fn seeds_are_capped() {
let hits: Vec<Fact> = (0..10)
.map(|i| fact(i, "project", &format!("ns{i}:attr"), "x"))
.collect();
assert_eq!(seeds_from_hits(&hits, &[]).len(), 6);
}
#[test]
fn co_mention_is_word_boundary_not_substring() {
let all = vec![
fact(1, "food", "preference", "I like banana bread"),
fact(2, "user", "father", "Frank Mendez"),
];
let none: HashSet<i64> = HashSet::new();
let ana = eligible_neighbors(&all, &[Seed::Person("Ana".into())], false, &none);
assert!(ana.is_empty(), "'Ana' must not match 'banana'");
let frank = eligible_neighbors(&all, &[Seed::Person("Frank".into())], false, &none);
assert_eq!(ids(&frank), [2].into_iter().collect());
}
#[test]
fn known_person_match_is_word_boundary() {
let hits = vec![fact(1, "user", "best_friend_note", "Banana Doe likes tea")];
assert!(seeds_from_hits(&hits, &["Ana".into()]).is_empty());
}
}