harper_core/
irregular_verbs.rs1use 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
11fn 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 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 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 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
68impl Default for IrregularVerbs {
69 fn default() -> Self {
70 Self::new()
71 }
72}
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77
78 #[test]
79 fn can_find_irregular_past_participle_for_preterite_lowercase() {
80 assert_eq!(
81 IrregularVerbs::curated().get_past_participle_for_preterite("arose"),
82 Some("arisen")
83 );
84 }
85
86 #[test]
87 fn can_find_irregular_past_participle_for_preterite_uppercase() {
88 assert_eq!(
89 IrregularVerbs::curated().get_past_participle_for_preterite("WENT"),
90 Some("gone")
91 );
92 }
93
94 #[test]
95 fn can_find_irregular_past_participle_same_as_past_tense() {
96 assert_eq!(
97 IrregularVerbs::curated().get_past_participle_for_preterite("taught"),
98 Some("taught")
99 );
100 }
101
102 #[test]
103 fn cant_find_regular_past_participle() {
104 assert_eq!(
105 IrregularVerbs::curated().get_past_participle_for_preterite("walked"),
106 None
107 );
108 }
109
110 #[test]
111 fn cant_find_non_verb() {
112 assert_eq!(
113 IrregularVerbs::curated().get_past_participle_for_preterite("the"),
114 None
115 );
116 }
117}