| 1 | //! Text entity extraction and overlap scoring for cross-source clustering. |
| 2 | //! |
| 3 | //! Mirrors `skills/last30days/scripts/lib/entity_extract.py`. |
| 4 | |
| 5 | use std::collections::BTreeSet; |
| 6 | |
| 7 | use crate::cjk; |
| 8 | |
| 9 | /// Stopwords ignored during text entity extraction. |
| 10 | pub const ENTITY_STOPWORDS: &[&str] = &[ |
| 11 | "the", "a", "an", "to", "for", "how", "is", "in", "of", "on", "and", "with", "from", "by", |
| 12 | "at", "this", "that", "it", "what", "are", "do", "can", "his", "her", "he", "she", "its", |
| 13 | "was", "has", "new", "just", "says", "said", "will", "about", "after", "now", "all", "been", |
| 14 | "here", "not", "out", "up", "more", "also", "but", "who", "year", "first", "make", "being", |
| 15 | "making", "over", "into", "than", "they", "their", "would", "could", "get", "got", "some", |
| 16 | "like", "back", "going", "breaking", "https", "http", "www", "com", |
| 17 | ]; |
| 18 | |
| 19 | /// Returns `true` when `word` carries an anchor signal: leading uppercase, |
| 20 | /// all cased characters uppercase (matching Python `str.isupper()`), or any digit. |
| 21 | pub fn has_anchor_signal(word: &str) -> bool { |
| 22 | let mut chars = word.chars(); |
| 23 | let Some(first) = chars.next() else { |
| 24 | return false; |
| 25 | }; |
| 26 | if first.is_uppercase() { |
| 27 | return true; |
| 28 | } |
| 29 | let mut has_cased = first.is_lowercase(); |
| 30 | let mut all_upper = false; |
| 31 | if first.is_numeric() { |
| 32 | return true; |
| 33 | } |
| 34 | if !has_cased { |
| 35 | all_upper = true; |
| 36 | } |
| 37 | for ch in chars { |
| 38 | if ch.is_numeric() { |
| 39 | return true; |
| 40 | } |
| 41 | if ch.is_uppercase() { |
| 42 | has_cased = true; |
| 43 | } else if ch.is_lowercase() { |
| 44 | has_cased = true; |
| 45 | all_upper = false; |
| 46 | } |
| 47 | } |
| 48 | has_cased && all_upper |
| 49 | } |
| 50 | |
| 51 | /// Extracts significant lowercased entity words used by clustering and evaluation. |
| 52 | pub fn extract_text_entities(text: &str) -> BTreeSet<String> { |
| 53 | let mut cleaned = String::with_capacity(text.len()); |
| 54 | for ch in text.chars() { |
| 55 | if cjk::is_word_char(ch) || ch.is_whitespace() { |
| 56 | cleaned.push(ch); |
| 57 | } else { |
| 58 | cleaned.push(' '); |
| 59 | } |
| 60 | } |
| 61 | let mut entities = BTreeSet::new(); |
| 62 | for word in cleaned.split_whitespace() { |
| 63 | let lower = word.to_lowercase(); |
| 64 | let char_len = word.chars().count(); |
| 65 | if char_len <= 2 || ENTITY_STOPWORDS.contains(&lower.as_str()) { |
| 66 | continue; |
| 67 | } |
| 68 | if has_anchor_signal(word) || char_len >= 4 { |
| 69 | entities.insert(lower); |
| 70 | } |
| 71 | } |
| 72 | entities |
| 73 | } |
| 74 | |
| 75 | #[inline] |
| 76 | #[expect( |
| 77 | clippy::as_conversions, |
| 78 | clippy::cast_precision_loss, |
| 79 | reason = "entity set sizes are small and convert cleanly to f64" |
| 80 | )] |
| 81 | fn ratio(num: usize, den: usize) -> f64 { |
| 82 | (num as f64) / (den as f64) |
| 83 | } |
| 84 | |
| 85 | /// Computes the Szymkiewicz-Simpson overlap coefficient for two entity sets. |
| 86 | pub fn entity_overlap(entities_a: &BTreeSet<String>, entities_b: &BTreeSet<String>) -> f64 { |
| 87 | if entities_a.is_empty() || entities_b.is_empty() { |
| 88 | return 0.0; |
| 89 | } |
| 90 | let shared = entities_a.intersection(entities_b).count(); |
| 91 | let min_len = entities_a.len().min(entities_b.len()); |
| 92 | ratio(shared, min_len) |
| 93 | } |
| 94 |