|
| 1 | +//! Smart caps engine — detects capitalization regime from user input |
| 2 | +//! and applies it to lowercase predictions from the trie. |
| 3 | +
|
| 4 | +use std::collections::HashSet; |
| 5 | + |
| 6 | +/// Detected capitalization regime. |
| 7 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 8 | +pub enum CapsRegime { |
| 9 | + /// Normal lowercase or sentence-start capitalization. |
| 10 | + Normal, |
| 11 | + /// All characters uppercase: "HTTP", "NASA". |
| 12 | + AllCaps, |
| 13 | + /// camelCase: "getElementById". |
| 14 | + CamelCase, |
| 15 | + /// PascalCase: "MyClass". |
| 16 | + PascalCase, |
| 17 | +} |
| 18 | + |
| 19 | +/// Engine for capitalization detection and application. |
| 20 | +pub struct CapsEngine { |
| 21 | + proper_nouns: HashSet<String>, |
| 22 | +} |
| 23 | + |
| 24 | +impl CapsEngine { |
| 25 | + pub fn new() -> Self { |
| 26 | + Self { |
| 27 | + proper_nouns: HashSet::new(), |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + /// Detect caps regime from the typed prefix. |
| 32 | + /// |
| 33 | + /// Returns `Normal` for empty strings, all-lowercase, or single uppercase |
| 34 | + /// followed by lowercase (sentence-start pattern). Requires 2+ uppercase |
| 35 | + /// chars to detect `AllCaps`. |
| 36 | + pub fn detect_regime(prefix: &str) -> CapsRegime { |
| 37 | + if prefix.is_empty() { |
| 38 | + return CapsRegime::Normal; |
| 39 | + } |
| 40 | + let chars: Vec<char> = prefix.chars().collect(); |
| 41 | + |
| 42 | + // Count alphabetic chars and check their case. |
| 43 | + let alpha_chars: Vec<char> = chars |
| 44 | + .iter() |
| 45 | + .copied() |
| 46 | + .filter(|c| c.is_alphabetic()) |
| 47 | + .collect(); |
| 48 | + if alpha_chars.len() >= 2 && alpha_chars.iter().all(|c| c.is_uppercase()) { |
| 49 | + return CapsRegime::AllCaps; |
| 50 | + } |
| 51 | + |
| 52 | + let first = chars[0]; |
| 53 | + if first.is_uppercase() { |
| 54 | + // Check for PascalCase: first upper + internal upper. |
| 55 | + if chars[1..] |
| 56 | + .iter() |
| 57 | + .any(|c| c.is_alphabetic() && c.is_uppercase()) |
| 58 | + { |
| 59 | + return CapsRegime::PascalCase; |
| 60 | + } |
| 61 | + // Just first-char upper → Normal (sentence-start or proper noun). |
| 62 | + return CapsRegime::Normal; |
| 63 | + } |
| 64 | + |
| 65 | + // Check for camelCase: first lower + internal upper. |
| 66 | + if first.is_lowercase() |
| 67 | + && chars[1..] |
| 68 | + .iter() |
| 69 | + .any(|c| c.is_alphabetic() && c.is_uppercase()) |
| 70 | + { |
| 71 | + return CapsRegime::CamelCase; |
| 72 | + } |
| 73 | + |
| 74 | + CapsRegime::Normal |
| 75 | + } |
| 76 | + |
| 77 | + /// Apply caps regime to a lowercase prediction word. |
| 78 | + pub fn apply_caps(word: &str, regime: CapsRegime) -> String { |
| 79 | + match regime { |
| 80 | + CapsRegime::Normal => word.to_string(), |
| 81 | + CapsRegime::AllCaps => word.to_uppercase(), |
| 82 | + CapsRegime::PascalCase | CapsRegime::CamelCase => { |
| 83 | + // For camel/pascal, the trie stores lowercase. Since the |
| 84 | + // user's prefix already encodes the pattern, return as-is. |
| 85 | + word.to_string() |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /// Capitalize first letter only, preserving the rest. |
| 91 | + pub fn capitalize_first(word: &str) -> String { |
| 92 | + let mut chars = word.chars(); |
| 93 | + match chars.next() { |
| 94 | + None => String::new(), |
| 95 | + Some(first) => { |
| 96 | + let upper: String = first.to_uppercase().collect(); |
| 97 | + upper + chars.as_str() |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /// Check if a word is a known proper noun. |
| 103 | + pub fn is_proper_noun(&self, word: &str) -> bool { |
| 104 | + self.proper_nouns.contains(&word.to_lowercase()) |
| 105 | + } |
| 106 | + |
| 107 | + /// Register a proper noun (stored lowercase for case-insensitive lookup). |
| 108 | + pub fn register_proper_noun(&mut self, word: &str) { |
| 109 | + self.proper_nouns.insert(word.to_lowercase()); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +impl Default for CapsEngine { |
| 114 | + fn default() -> Self { |
| 115 | + Self::new() |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +#[cfg(test)] |
| 120 | +mod tests { |
| 121 | + use super::*; |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn detect_normal_lowercase() { |
| 125 | + assert_eq!(CapsEngine::detect_regime("hello"), CapsRegime::Normal); |
| 126 | + } |
| 127 | + |
| 128 | + #[test] |
| 129 | + fn detect_normal_capitalized() { |
| 130 | + assert_eq!(CapsEngine::detect_regime("Hello"), CapsRegime::Normal); |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn detect_all_caps() { |
| 135 | + assert_eq!(CapsEngine::detect_regime("HTTP"), CapsRegime::AllCaps); |
| 136 | + assert_eq!(CapsEngine::detect_regime("NASA"), CapsRegime::AllCaps); |
| 137 | + assert_eq!(CapsEngine::detect_regime("AB"), CapsRegime::AllCaps); |
| 138 | + } |
| 139 | + |
| 140 | + #[test] |
| 141 | + fn detect_single_upper_is_normal() { |
| 142 | + // Single uppercase can't be distinguished from sentence-start. |
| 143 | + assert_eq!(CapsEngine::detect_regime("H"), CapsRegime::Normal); |
| 144 | + } |
| 145 | + |
| 146 | + #[test] |
| 147 | + fn detect_camel_case() { |
| 148 | + assert_eq!( |
| 149 | + CapsEngine::detect_regime("getElement"), |
| 150 | + CapsRegime::CamelCase |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + #[test] |
| 155 | + fn detect_pascal_case() { |
| 156 | + assert_eq!(CapsEngine::detect_regime("MyClass"), CapsRegime::PascalCase); |
| 157 | + } |
| 158 | + |
| 159 | + #[test] |
| 160 | + fn apply_all_caps() { |
| 161 | + assert_eq!( |
| 162 | + CapsEngine::apply_caps("hello", CapsRegime::AllCaps), |
| 163 | + "HELLO" |
| 164 | + ); |
| 165 | + } |
| 166 | + |
| 167 | + #[test] |
| 168 | + fn apply_normal_unchanged() { |
| 169 | + assert_eq!(CapsEngine::apply_caps("hello", CapsRegime::Normal), "hello"); |
| 170 | + } |
| 171 | + |
| 172 | + #[test] |
| 173 | + fn capitalize_first_empty() { |
| 174 | + assert_eq!(CapsEngine::capitalize_first(""), ""); |
| 175 | + } |
| 176 | + |
| 177 | + #[test] |
| 178 | + fn capitalize_first_ascii() { |
| 179 | + assert_eq!(CapsEngine::capitalize_first("hello"), "Hello"); |
| 180 | + } |
| 181 | + |
| 182 | + #[test] |
| 183 | + fn capitalize_first_cyrillic() { |
| 184 | + assert_eq!(CapsEngine::capitalize_first("здравей"), "Здравей"); |
| 185 | + } |
| 186 | + |
| 187 | + #[test] |
| 188 | + fn proper_noun_check() { |
| 189 | + let mut engine = CapsEngine::new(); |
| 190 | + engine.register_proper_noun("London"); |
| 191 | + assert!(engine.is_proper_noun("london")); |
| 192 | + assert!(engine.is_proper_noun("London")); |
| 193 | + assert!(engine.is_proper_noun("LONDON")); |
| 194 | + assert!(!engine.is_proper_noun("hello")); |
| 195 | + } |
| 196 | +} |
0 commit comments