diff --git a/src/inflect.rs b/src/inflect.rs index cbf65ea..22a7747 100644 --- a/src/inflect.rs +++ b/src/inflect.rs @@ -6,7 +6,7 @@ pub fn enclose(s: &str) -> String { } /// Joins the stem of each word in 'words' into a string for Regex. -pub fn joinstem(cutpoint: Option, words: Option>) -> String { +pub fn joinstem(cutpoint: Option, words: Option>) -> String { let words = words.unwrap_or_else(|| Vec::new()); let stem = words .iter() @@ -23,7 +23,7 @@ pub fn joinstem(cutpoint: Option, words: Option>) -> String { } /// From a list of words, returns a HashMap of HashSets of words, keyed by word length. -pub fn bysize(words: Vec<&str>) -> HashMap> { +pub fn bysize(words: Vec) -> HashMap> { let mut res: HashMap> = HashMap::new(); for word in words { let len = word.len(); @@ -34,7 +34,7 @@ pub fn bysize(words: Vec<&str>) -> HashMap> { } pub fn make_pl_si_lists( - list: Vec<&str>, + list: Vec, pl_ending: &str, si_ending_size: Option, do_joinstem: bool @@ -42,7 +42,7 @@ pub fn make_pl_si_lists( let si_ending_size = si_ending_size.map(|size| -size); let si_list: Vec = list .iter() - .map(|&w| { + .map(|w| { if let Some(size) = si_ending_size { format!("{}{}", &w[..w.len() - (size as usize)], pl_ending) } else { @@ -51,12 +51,7 @@ pub fn make_pl_si_lists( }) .collect(); let pl_bysize = bysize(list.clone()); - let si_bysize = bysize( - si_list - .iter() - .map(|s| s.as_str()) - .collect() - ); + let si_bysize = bysize(si_list.clone()); if do_joinstem { let stem = joinstem(si_ending_size, Some(list)); (si_list, si_bysize, pl_bysize, stem) @@ -179,18 +174,50 @@ fn si_sb_irregular_compound() -> HashMap<&'static str, &'static str> { si_sb_irregular_compound } -fn pl_sb_z_zes_list() -> Vec<&'static str> { - return vec!["quartz", "topaz"]; +fn pl_sb_z_zes_list() -> Vec { + return vec!["quartz", "topaz"] + .iter() + .map(|s| s.to_string()) + .collect(); } fn pl_sb_z_zes_bysize() -> HashMap> { return bysize(pl_sb_z_zes_list()); } -fn sb_ze_zes_list() -> Vec<&'static str> { - return vec!["snooze"]; +fn sb_ze_zes_list() -> Vec { + return vec!["snooze"] + .iter() + .map(|s| s.to_string()) + .collect(); } fn sb_ze_zes_bysize() -> HashMap> { return bysize(sb_ze_zes_list()); } + +fn pl_sb_c_is_ides_complete() -> Vec { + return vec!["ephemeris", "iris", "clitoris", "chrysalis", "epididymis"] + .iter() + .map(|s| s.to_string()) + .collect(); +} + +fn pl_sb_c_is_ides_endings() -> Vec { + return vec!["itis"] + .iter() + .map(|s| s.to_string()) + .collect(); +} + +fn pl_sb_c_is_ides() -> String { + let endings = pl_sb_c_is_ides_endings() + .into_iter() + .map(|w| format!(".*{}", w)); + let pl_sb_c_is_ides: Vec = pl_sb_c_is_ides_complete() + .iter() + .map(|s| s.to_string()) + .chain(endings) + .collect(); + return joinstem(Some(-2), Some(pl_sb_c_is_ides)); +} diff --git a/tests/inflect_tests.rs b/tests/inflect_tests.rs index 715a08c..ead569c 100644 --- a/tests/inflect_tests.rs +++ b/tests/inflect_tests.rs @@ -8,17 +8,23 @@ fn test_enclose() { #[test] fn test_joinstem() { assert_eq!( - joinstem(Some(-2), Some(vec!["ephemeris", "iris", ".*itis"])), + joinstem( + Some(-2), + Some(vec!["ephemeris".to_string(), "iris".to_string(), ".*itis".to_string()]) + ), "(?:ephemer|ir|.*it)" ); - assert_eq!(joinstem(None, Some(vec!["ephemeris"])), "(?:ephemeris)"); + assert_eq!(joinstem(None, Some(vec!["ephemeris".to_string()])), "(?:ephemeris)"); assert_eq!(joinstem(Some(5), None), "(?:)"); assert_eq!(joinstem(None, None), "(?:)"); } #[test] fn test_bysize() { - let words = vec!["ant", "cat", "dog", "pig", "frog", "goat", "horse", "elephant"]; + let words = vec!["ant", "cat", "dog", "pig", "frog", "goat", "horse", "elephant"] + .iter() + .map(|s| s.to_string()) + .collect(); let result = bysize(words); if let Some(set) = result.get(&3) { let mut sorted_words: Vec<&String> = set.iter().collect();