| 1 | pub struct Mood { |
| 2 | en_stemmer: rust_stemmers::Stemmer, |
| 3 | } |
| 4 | |
| 5 | impl Mood { |
| 6 | pub fn new() -> Self { |
| 7 | Self { |
| 8 | en_stemmer: rust_stemmers::Stemmer::create(rust_stemmers::Algorithm::English), |
| 9 | } |
| 10 | } |
| 11 | |
| 12 | /// **Note:** that the input is expected to be all lowercase (if that is applicable). |
| 13 | pub fn is_imperative(&self, word: &str) -> Option<bool> { |
| 14 | if crate::wordlist_codegen::BLACKLIST.contains(word) { |
| 15 | return Some(false); |
| 16 | } |
| 17 | |
| 18 | let stem = match word { |
| 19 | "added" => "add".into(), |
| 20 | _ => self.en_stemmer.stem(word), |
| 21 | }; |
| 22 | let imperative_forms = crate::wordlist_codegen::IMPERATIVES.get(stem.as_ref())?; |
| 23 | Some(imperative_forms.contains(word)) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | impl Clone for Mood { |
| 28 | fn clone(&self) -> Self { |
| 29 | Self::new() |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | impl Default for Mood { |
| 34 | fn default() -> Self { |
| 35 | Self::new() |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | #[cfg(test)] |
| 40 | mod test { |
| 41 | use super::*; |
| 42 | |
| 43 | #[test] |
| 44 | fn is_imperative() { |
| 45 | let mood = Mood::new(); |
| 46 | let cases = &[ |
| 47 | ("runs", Some(false)), |
| 48 | ("run", Some(true)), |
| 49 | ("returns", Some(false)), |
| 50 | ("return", Some(true)), |
| 51 | ("constructor", Some(false)), |
| 52 | ("adds", Some(false)), |
| 53 | ("add", Some(true)), |
| 54 | ("added", Some(false)), |
| 55 | ]; |
| 56 | for (word, expected) in cases.iter() { |
| 57 | println!("Checking {word}"); |
| 58 | assert_eq!(mood.is_imperative(word), expected.clone()); |
| 59 | } |
| 60 | } |
| 61 | } |