Words, Word, and Engine structs

This commit is contained in:
Lewis Wynne 2025-03-10 15:16:26 +00:00
parent a3b673db6e
commit 61b8786dce
2 changed files with 113 additions and 0 deletions

View file

@ -2024,6 +2024,7 @@ fn singular_pronoun_genders() -> Vec<String> {
fn pl_pron_nom() -> HashMap<String, String> { fn pl_pron_nom() -> HashMap<String, String> {
return vec![ return vec![
// Nominative: Reflexive
("i", "we"), ("i", "we"),
("myself", "ourselves"), ("myself", "ourselves"),
("you", "you"), ("you", "you"),
@ -2036,6 +2037,7 @@ fn pl_pron_nom() -> HashMap<String, String> {
("itself", "themselves"), ("itself", "themselves"),
("they", "they"), ("they", "they"),
("themself", "themselves"), ("themself", "themselves"),
// Possessive
("mine", "ours"), ("mine", "ours"),
("yours", "yours"), ("yours", "yours"),
("hers", "theirs"), ("hers", "theirs"),
@ -2490,3 +2492,84 @@ fn string_to_constant() -> HashMap<String, Option<bool>> {
.map(|&(k, v)| (k.to_string(), v)) .map(|&(k, v)| (k.to_string(), v))
.collect(); .collect();
} }
fn dollar_digits() -> Regex { Regex::new("\\$(\\d+)").expect("Failed to compile Regex") }
// TODO: Pre-compiled REGEX objects, ln1950 @ og inflect
pub struct Words {
pub lowered: String,
pub split_: Vec<String>,
pub first: String,
pub last: String
}
impl Words {
pub fn new(s: &str) -> Words {
let split: Vec<String> = s.split_whitespace().map(String::from).collect();
Words {
lowered: s.to_lowercase(),
split_: split.clone(),
first: split.get(0).cloned().unwrap_or_else(String::new),
last: split.last().cloned().unwrap_or_else(String::new)
}
}
}
struct Word(String);
impl Word {
fn new(word: String) -> Result<Self, &'static str> {
if word.len() >= 1 {
Ok(Word(word))
} else {
Err("Word must be >= 1 chars long.")
}
}
fn get(&self) -> &str {
&self.0
}
}
pub struct Engine {
classical_dict: HashMap<String, bool>,
persistent_count: Option<i32>,
mill_count: i32,
pl_sb_user_defined: Vec<Option<Word>>,
pl_v_user_defined: Vec<Option<Word>>,
pl_adj_user_defined: Vec<Option<Word>>,
si_sb_user_defined: Vec<Option<Word>>,
a_a_user_defined: Vec<Option<Word>>,
the_gender: String,
_number_args: Option<HashMap<String, String>>,
}
impl Engine {
pub fn new() -> Engine {
Engine {
classical_dict: def_classical(),
persistent_count: None,
mill_count: 0,
pl_sb_user_defined: Vec::new(),
pl_v_user_defined: Vec::new(),
pl_adj_user_defined: Vec::new(),
si_sb_user_defined: Vec::new(),
a_a_user_defined: Vec::new(),
the_gender: "neuter".to_string(),
_number_args: None,
}
}
fn checkpat(self, pattern: Option<Word>) {
return;
}
pub fn gender(&mut self, gender: &str) {
if singular_pronoun_genders().contains(&String::from(gender)) {
self.the_gender = gender.to_string();
}
}
pub fn check_gender(&self) -> &String { &self.the_gender }
}

View file

@ -53,3 +53,33 @@ fn test_si_pron() {
assert_ne!("him", get_si_pron("acc", "them", Some("feminine"))); assert_ne!("him", get_si_pron("acc", "them", Some("feminine")));
assert_ne!("her", get_si_pron("acc", "them", Some("masculine"))); assert_ne!("her", get_si_pron("acc", "them", Some("masculine")));
} }
#[test]
fn test_ordinal_suff() {
let pattern = "ty|one|two|three|five|eight|nine|twelve";
let re = ordinal_suff();
assert!(re.is_match(pattern));
assert!(re.find("one").is_some());
assert!(re.find("thre|e").is_none());
assert!(re.find("1").is_none());
}
#[test]
fn test_words() {
let words = Words::new("A quick brown fox.");
assert_eq!(words.lowered, "a quick brown fox.");
assert_eq!(words.split_, vec!["A", "quick", "brown", "fox."]);
assert_eq!(words.first, "A");
assert_eq!(words.last, "fox.");
}
#[test]
fn test_engine() {
let mut e = Engine::new();
assert_eq!(e.check_gender(), "neuter");
e.gender("masculine");
assert_eq!(e.check_gender(), "masculine");
e.gender("fff");
assert_ne!(e.check_gender(), "fff");
assert_eq!(e.check_gender(), "masculine");
}