Skip to main content

harper_core/
irregular_verbs.rs

1use serde::Deserialize;
2use std::sync::{Arc, LazyLock};
3
4type Verb = (String, String, String);
5
6#[derive(Debug, Deserialize)]
7pub struct IrregularVerbs {
8    verbs: Vec<Verb>,
9}
10
11/// The uncached function that is used to produce the original copy of the
12/// irregular verb table.
13fn uncached_inner_new() -> Arc<IrregularVerbs> {
14    IrregularVerbs::from_json_file(include_str!("../irregular_verbs.json"))
15        .map(Arc::new)
16        .unwrap_or_else(|e| panic!("Failed to load irregular verb table: {}", e))
17}
18
19static VERBS: LazyLock<Arc<IrregularVerbs>> = LazyLock::new(uncached_inner_new);
20
21impl IrregularVerbs {
22    pub fn new() -> Self {
23        Self { verbs: vec![] }
24    }
25
26    pub fn from_json_file(json: &str) -> Result<Self, serde_json::Error> {
27        // Deserialize into Vec<serde_json::Value> to handle mixed types
28        let values: Vec<serde_json::Value> =
29            serde_json::from_str(json).expect("Failed to parse irregular verbs JSON");
30
31        let mut verbs = Vec::new();
32
33        for value in values {
34            match value {
35                serde_json::Value::Array(arr) if arr.len() == 3 => {
36                    // Handle array of 3 strings
37                    if let (Some(lemma), Some(preterite), Some(past_participle)) =
38                        (arr[0].as_str(), arr[1].as_str(), arr[2].as_str())
39                    {
40                        verbs.push((
41                            lemma.to_string(),
42                            preterite.to_string(),
43                            past_participle.to_string(),
44                        ));
45                    }
46                }
47                // Strings are used for comments to guide contributors editing the file
48                serde_json::Value::String(_) => {}
49                _ => {}
50            }
51        }
52
53        Ok(Self { verbs })
54    }
55
56    pub fn curated() -> Arc<Self> {
57        (*VERBS).clone()
58    }
59
60    pub fn get_past_participle_for_preterite(&self, preterite: &str) -> Option<&str> {
61        self.verbs
62            .iter()
63            .find(|(_, pt, _)| pt.eq_ignore_ascii_case(preterite))
64            .map(|(_, _, pp)| pp.as_str())
65    }
66
67    pub fn get_lemma_for_preterite(&self, preterite: &str) -> Option<&str> {
68        self.verbs
69            .iter()
70            .find(|(_, pt, _)| pt.eq_ignore_ascii_case(preterite))
71            .map(|(lemma, _, _)| lemma.as_str())
72    }
73}
74
75impl Default for IrregularVerbs {
76    fn default() -> Self {
77        Self::new()
78    }
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    #[test]
86    fn can_find_irregular_past_participle_for_preterite_lowercase() {
87        assert_eq!(
88            IrregularVerbs::curated().get_past_participle_for_preterite("arose"),
89            Some("arisen")
90        );
91    }
92
93    #[test]
94    fn can_find_irregular_past_participle_for_preterite_uppercase() {
95        assert_eq!(
96            IrregularVerbs::curated().get_past_participle_for_preterite("WENT"),
97            Some("gone")
98        );
99    }
100
101    #[test]
102    fn can_find_irregular_past_participle_same_as_past_tense() {
103        assert_eq!(
104            IrregularVerbs::curated().get_past_participle_for_preterite("taught"),
105            Some("taught")
106        );
107    }
108
109    #[test]
110    fn cant_find_regular_past_participle() {
111        assert_eq!(
112            IrregularVerbs::curated().get_past_participle_for_preterite("walked"),
113            None
114        );
115    }
116
117    #[test]
118    fn cant_find_non_verb() {
119        assert_eq!(
120            IrregularVerbs::curated().get_past_participle_for_preterite("the"),
121            None
122        );
123    }
124}