Skip to main content

logicaffeine_language/
lexicon.rs

1//! Lexicon: Vocabulary lookup functions
2//!
3//! This module includes the compile-time generated lexicon lookup code
4//! from build.rs. It provides ~56 lookup functions for classifying words.
5
6// Include the generated lexicon lookup functions
7include!(concat!(env!("OUT_DIR"), "/lexicon_data.rs"));
8
9/// Every word table of `lexicon`, `ontology` and `semantics` written as the linear `match` it
10/// replaced, arm by arm, first arm winning; `differences(word)` and `pair_differences(first,
11/// second)` name the tables whose answer is not their reference's.
12#[cfg(feature = "lexicon-reference")]
13pub mod reference {
14    include!(concat!(env!("OUT_DIR"), "/word_table_reference.rs"));
15}
16
17// Re-export types from lexicon crate that aren't defined in generated code
18// Note: Polarity, CanonicalMapping are defined in lexicon_data.rs
19pub use logicaffeine_lexicon::{
20    AdjectiveMetadata, Aspect, Case, Definiteness, Feature, Gender, MorphologicalRule,
21    NounMetadata, Number, Sort, Time, VerbClass, VerbEntry, VerbMetadata,
22};
23
24/// Get canonical verb form and whether it's lexically negative.
25/// Used at parse time to transform "lacks" → ("Have", true).
26/// Returns (canonical_lemma, is_negative).
27pub fn get_canonical_verb(lemma: &str) -> Option<(&'static str, bool)> {
28    lookup_canonical(lemma).map(|m| (m.lemma, m.polarity == Polarity::Negative))
29}
30
31/// Lexicon trait for abstracting over static and dynamic lexicons
32pub trait LexiconTrait {
33    fn lookup_verb(&self, word: &str) -> Option<VerbMetadata>;
34    fn lookup_noun(&self, word: &str) -> Option<NounMetadata>;
35    fn lookup_adjective(&self, word: &str) -> Option<AdjectiveMetadata>;
36}
37
38/// Static lexicon implementation using compile-time generated data
39pub struct StaticLexicon;
40
41impl LexiconTrait for StaticLexicon {
42    fn lookup_verb(&self, word: &str) -> Option<VerbMetadata> {
43        lookup_verb_db(word)
44    }
45
46    fn lookup_noun(&self, word: &str) -> Option<NounMetadata> {
47        lookup_noun_db(word)
48    }
49
50    fn lookup_adjective(&self, word: &str) -> Option<AdjectiveMetadata> {
51        lookup_adjective_db(word)
52    }
53}
54
55/// Lexicon struct for verb lookup with inflection handling
56pub struct Lexicon {}
57
58impl Lexicon {
59    pub fn new() -> Self {
60        Lexicon {}
61    }
62
63    pub fn lookup_verb(&self, word: &str) -> Option<VerbEntry> {
64        let lower = word.to_lowercase();
65
66        if let Some(entry) = lookup_irregular_verb(&lower) {
67            return Some(entry);
68        }
69
70        if lower.ends_with("ing") {
71            let stem = self.strip_ing(&lower);
72            let lemma = Self::capitalize(&stem);
73            let class = self.lookup_verb_class(&lemma.to_lowercase());
74            return Some(VerbEntry {
75                lemma,
76                time: Time::None,
77                aspect: Aspect::Progressive,
78                class,
79            });
80        }
81
82        if lower.ends_with("ed") {
83            let stem = self.strip_ed(&lower);
84            // Only treat as verb if the stem is a known base verb
85            // This prevents "doomed" → "Doom" when "doom" isn't in lexicon
86            if !is_base_verb(&stem) {
87                return None;
88            }
89            let lemma = Self::capitalize(&stem);
90            let class = self.lookup_verb_class(&lemma.to_lowercase());
91            return Some(VerbEntry {
92                lemma,
93                time: Time::Past,
94                aspect: Aspect::Simple,
95                class,
96            });
97        }
98
99        let is_third_person = if lower.ends_with("es") && lower.len() > 2 {
100            true
101        } else if lower.ends_with("s") && !lower.ends_with("ss") && lower.len() > 2 {
102            true
103        } else {
104            false
105        };
106
107        if is_third_person {
108            if is_stemming_exception(&lower) {
109                return None;
110            }
111
112            let stem = Self::third_person_stem(&lower)?;
113            let lemma = Self::capitalize(&stem);
114            let class = self.lookup_verb_class(&lemma.to_lowercase());
115            return Some(VerbEntry {
116                lemma,
117                time: Time::Present,
118                aspect: Aspect::Simple,
119                class,
120            });
121        }
122
123        // Check if this is a base verb form
124        if is_base_verb(&lower) {
125            let lemma = Self::capitalize(&lower);
126            let class = self.lookup_verb_class(&lower);
127            return Some(VerbEntry {
128                lemma,
129                time: Time::Present,
130                aspect: Aspect::Simple,
131                class,
132            });
133        }
134
135        None
136    }
137
138    fn lookup_verb_class(&self, lemma: &str) -> VerbClass {
139        lookup_verb_class(lemma)
140    }
141
142    fn strip_ing(&self, word: &str) -> String {
143        let base = &word[..word.len() - 3];
144
145        if base.len() >= 2 {
146            let chars: Vec<char> = base.chars().collect();
147            let last = chars[chars.len() - 1];
148            let second_last = chars[chars.len() - 2];
149
150            if last == second_last && !"aeiou".contains(last) {
151                return base[..base.len() - 1].to_string();
152            }
153        }
154
155        if needs_e_ing(base) {
156            return format!("{}e", base);
157        }
158
159        base.to_string()
160    }
161
162    fn strip_ed(&self, word: &str) -> String {
163        let base = &word[..word.len() - 2];
164
165        if base.ends_with("i") {
166            // Silent-e verbs ending in -ie add only "d": "lied" → "lie",
167            // "died" → "die", "tied" → "tie". Only then the y-restoring
168            // rule: "studied" → "study".
169            let with_e = format!("{}e", base);
170            if is_base_verb(&with_e) {
171                return with_e;
172            }
173            return format!("{}y", &base[..base.len() - 1]);
174        }
175
176        if base.len() >= 2 {
177            let chars: Vec<char> = base.chars().collect();
178            let last = chars[chars.len() - 1];
179            let second_last = chars[chars.len() - 2];
180
181            // Doubled consonant handling for verbs like "stopped" → "stop"
182            // BUT: first check if the base WITH doubled consonant is already a verb
183            // This handles words like "passed" → "pass" (natural double 's')
184            if last == second_last && !"aeiou".contains(last) {
185                // First try the base as-is (handles "pass", "miss", "kiss", etc.)
186                if is_base_verb(base) {
187                    return base.to_string();
188                }
189                // Otherwise strip the doubled consonant (handles "stopped" → "stop")
190                return base[..base.len() - 1].to_string();
191            }
192
193            // Consonant clusters that typically come from silent-e verbs:
194            // "tabled" → "tabl" needs "e", "googled" → "googl" needs "e"
195            // Pattern: consonant + l/r at end, with vowel before the consonant
196            if (last == 'l' || last == 'r') && !"aeiou".contains(second_last) {
197                if chars.len() >= 3 && "aeiou".contains(chars[chars.len() - 3]) {
198                    return format!("{}e", base);
199                }
200            }
201        }
202
203        if needs_e_ed(base) {
204            return format!("{}e", base);
205        }
206
207        // Fallback: try adding 'e' and check if that's a valid verb
208        // This handles all silent-e verbs not explicitly in needs_e_ed
209        // e.g., "escaped" → "escap" → "escape" (valid verb)
210        let with_e = format!("{}e", base);
211        if is_base_verb(&with_e) {
212            return with_e;
213        }
214
215        base.to_string()
216    }
217
218    /// The generative third-person-singular rule: sibilant and -o stems take
219    /// "es" ("push" → "pushes", "go" → "goes"), consonant+y becomes "ies"
220    /// ("study" → "studies"), everything else takes "s" ("plan" → "plans").
221    fn third_person_of(stem: &str) -> String {
222        if stem.ends_with('s')
223            || stem.ends_with('x')
224            || stem.ends_with('z')
225            || stem.ends_with("ch")
226            || stem.ends_with("sh")
227            || stem.ends_with('o')
228        {
229            format!("{stem}es")
230        } else if stem.ends_with('y')
231            && !stem.ends_with("ay")
232            && !stem.ends_with("ey")
233            && !stem.ends_with("oy")
234            && !stem.ends_with("uy")
235        {
236            format!("{}ies", &stem[..stem.len() - 1])
237        } else {
238            format!("{stem}s")
239        }
240    }
241
242    /// Inverse of [`Self::third_person_of`]: the stem is whichever known base
243    /// verb regenerates the surface form under the forward rule. Deriving the
244    /// inverse from the generative rule means the two can never disagree —
245    /// "pushes" → "push" and "dies" → "die" resolve, while "planes" stays a
246    /// plural noun because "plan" forms "plans", never "planes".
247    fn third_person_stem(word: &str) -> Option<String> {
248        let mut candidates = vec![word[..word.len() - 1].to_string()];
249        if word.ends_with("es") {
250            candidates.push(word[..word.len() - 2].to_string());
251        }
252        if word.ends_with("ies") {
253            candidates.push(format!("{}y", &word[..word.len() - 3]));
254        }
255        candidates
256            .into_iter()
257            .find(|stem| is_base_verb(stem) && Self::third_person_of(stem) == word)
258    }
259
260    fn capitalize(s: &str) -> String {
261        let mut chars = s.chars();
262        match chars.next() {
263            None => String::new(),
264            Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
265        }
266    }
267}
268
269impl Default for Lexicon {
270    fn default() -> Self {
271        Self::new()
272    }
273}
274
275/// Result of smart word analysis for derivational morphology
276#[derive(Debug, Clone, PartialEq, Eq)]
277pub enum WordAnalysis {
278    /// A dictionary entry (exact match or derived plural)
279    Noun(NounMetadata),
280    /// A word derived via morphological rules (agentive nouns like "blogger")
281    DerivedNoun { lemma: String, number: Number },
282}
283
284/// Smart word analysis with derivational morphology support.
285///
286/// Three-step resolution:
287/// 1. **Exact Match** - Check if word exists in lexicon (handles irregulars like "mice")
288/// 2. **Plural Derivation** - Strip 's'/'es' and check if stem exists (farmers → farmer)
289/// 3. **Morphological Rules** - Apply suffix rules for unknown agentive nouns
290pub fn analyze_word(word: &str) -> Option<WordAnalysis> {
291    let lower = word.to_lowercase();
292
293    // 1. EXACT MATCH (Fast Path)
294    // Handles explicit entries like "farmer", "mice", "children"
295    if let Some(meta) = lookup_noun_db(&lower) {
296        return Some(WordAnalysis::Noun(meta));
297    }
298
299    // 2. PLURAL DERIVATION (Smart Path)
300    // "farmers" → stem "farmer" → lookup
301    if lower.ends_with('s') && lower.len() > 2 {
302        // Try simple 's' stripping: "farmers" -> "farmer"
303        let stem = &lower[..lower.len() - 1];
304        if let Some(meta) = lookup_noun_db(stem) {
305            // Found the singular base - return as plural
306            return Some(WordAnalysis::Noun(NounMetadata {
307                lemma: meta.lemma,
308                number: Number::Plural,
309                features: meta.features,
310            }));
311        }
312
313        // Try 'es' stripping: "boxes" -> "box", "churches" -> "church"
314        if lower.ends_with("es") && lower.len() > 3 {
315            let stem_es = &lower[..lower.len() - 2];
316            if let Some(meta) = lookup_noun_db(stem_es) {
317                return Some(WordAnalysis::Noun(NounMetadata {
318                    lemma: meta.lemma,
319                    number: Number::Plural,
320                    features: meta.features,
321                }));
322            }
323        }
324
325        // Try 'ies' -> 'y': "cities" -> "city"
326        if lower.ends_with("ies") && lower.len() > 4 {
327            let stem_ies = format!("{}y", &lower[..lower.len() - 3]);
328            if let Some(meta) = lookup_noun_db(&stem_ies) {
329                return Some(WordAnalysis::Noun(NounMetadata {
330                    lemma: meta.lemma,
331                    number: Number::Plural,
332                    features: meta.features,
333                }));
334            }
335        }
336    }
337
338    // 3. MORPHOLOGICAL RULES (Data-driven from lexicon.json)
339    // Handle agentive nouns like "blogger", "vlogger" even if not in lexicon
340    for rule in get_morphological_rules() {
341        // Check plural form first (e.g., "vloggers" -> "vlogger" -> rule match)
342        let (is_plural, check_word) = if lower.ends_with('s') && !rule.suffix.ends_with('s') {
343            (true, &lower[..lower.len() - 1])
344        } else {
345            (false, lower.as_str())
346        };
347
348        if check_word.ends_with(rule.suffix) {
349            return Some(WordAnalysis::DerivedNoun {
350                lemma: check_word.to_string(),
351                number: if is_plural {
352                    Number::Plural
353                } else {
354                    Number::Singular
355                },
356            });
357        }
358    }
359
360    None
361}
362
363/// Check if a word is a known common noun or derivable from one.
364/// This is used for sentence-initial capitalization disambiguation.
365pub fn is_derivable_noun(word: &str) -> bool {
366    analyze_word(word).is_some()
367}
368
369/// Check if a word is a proper name (has Feature::Proper in the lexicon).
370/// Used to distinguish "Socrates fears death" from "Birds fly" (bare plurals).
371/// Names like "Socrates", "James", "Chris" end in 's' but aren't plural nouns.
372pub fn is_proper_name(word: &str) -> bool {
373    let lower = word.to_lowercase();
374    if let Some(meta) = lookup_noun_db(&lower) {
375        return meta.features.contains(&Feature::Proper);
376    }
377    false
378}
379
380/// Get the canonical lemma for a noun.
381///
382/// Maps inflected forms to their dictionary headword:
383/// - "men" → "Man"
384/// - "children" → "Child"
385/// - "farmers" → "Farmer"
386///
387/// This is used for predicate canonicalization in the proof engine,
388/// ensuring "All men are mortal" and "Socrates is a man" produce
389/// matching predicates.
390pub fn get_canonical_noun(word: &str) -> Option<&'static str> {
391    match analyze_word(word) {
392        Some(WordAnalysis::Noun(meta)) => Some(meta.lemma),
393        Some(WordAnalysis::DerivedNoun { .. }) => {
394            // Derived nouns (e.g., "blogger") return owned Strings,
395            // so we can't return a static reference.
396            // Fall back to raw word handling in the caller.
397            None
398        }
399        _ => None,
400    }
401}