diff --git a/src/wlist/mod.rs b/src/wlist/mod.rs index 67c4863..6f545e6 100644 --- a/src/wlist/mod.rs +++ b/src/wlist/mod.rs @@ -44,7 +44,22 @@ pub trait WordList: Clone + std::fmt::Debug + Default { for (k, v) in wpairs { hm.insert(k.into(), *v); } - WordMap::new(hm) + WordMap::from(hm) } fn get_word(&self, word: &Word) -> Option; + fn letter_frequency(&self) -> WordMap { + // PERF: this function has complexity O(n²)! + let mut cmap: HashMap = HashMap::new(); + // count the chars in each word + for word in self.wordmap().iter() { + for c in word.0.chars() { + if let Some(inner_value) = cmap.get_mut(&c) { + *inner_value += 1; + } + } + } + // make all chars to strings + let cmap: HashMap = cmap.into_iter().map(|p| (p.0.to_string(), p.1)).collect(); + WordMap::from_absolute(cmap) + } } diff --git a/src/wlist/word.rs b/src/wlist/word.rs index 72a8f82..785f4ea 100644 --- a/src/wlist/word.rs +++ b/src/wlist/word.rs @@ -21,8 +21,10 @@ pub struct WordMap { } impl WordMap { - pub fn new(inner: HashMap) -> Self { - Self { inner } + pub fn new() -> Self { + Self { + inner: HashMap::new(), + } } pub fn keys(&self) -> std::collections::hash_map::Keys<'_, String, Frequency> { self.inner.keys() @@ -70,6 +72,12 @@ impl WordMap { None => None, } } + pub fn from_absolute(abs: HashMap) -> Self { + let n: f64 = abs.keys().len() as f64; + let relative: HashMap = + abs.into_iter().map(|p| (p.0, p.1 as f64 / n)).collect(); + relative.into() + } } impl std::fmt::Debug for WordMap { @@ -89,3 +97,9 @@ impl std::fmt::Debug for WordMap { ) } } + +impl From> for WordMap { + fn from(value: HashMap) -> Self { + Self { inner: value } + } +}