calc frequency of letters

This commit is contained in:
Christoph J. Scherr 2024-03-22 16:03:51 +01:00
parent a5e22349f6
commit 1fa8989f08
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
2 changed files with 32 additions and 3 deletions

View File

@ -44,7 +44,22 @@ pub trait WordList: Clone + std::fmt::Debug + Default {
for (k, v) in wpairs { for (k, v) in wpairs {
hm.insert(k.into(), *v); hm.insert(k.into(), *v);
} }
WordMap::new(hm) WordMap::from(hm)
} }
fn get_word(&self, word: &Word) -> Option<WordData>; fn get_word(&self, word: &Word) -> Option<WordData>;
fn letter_frequency(&self) -> WordMap {
// PERF: this function has complexity O(n²)!
let mut cmap: HashMap<char, usize> = 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<Word, usize> = cmap.into_iter().map(|p| (p.0.to_string(), p.1)).collect();
WordMap::from_absolute(cmap)
}
} }

View File

@ -21,8 +21,10 @@ pub struct WordMap {
} }
impl WordMap { impl WordMap {
pub fn new(inner: HashMap<Word, Frequency>) -> Self { pub fn new() -> Self {
Self { inner } Self {
inner: HashMap::new(),
}
} }
pub fn keys(&self) -> std::collections::hash_map::Keys<'_, String, Frequency> { pub fn keys(&self) -> std::collections::hash_map::Keys<'_, String, Frequency> {
self.inner.keys() self.inner.keys()
@ -70,6 +72,12 @@ impl WordMap {
None => None, None => None,
} }
} }
pub fn from_absolute(abs: HashMap<Word, usize>) -> Self {
let n: f64 = abs.keys().len() as f64;
let relative: HashMap<Word, Frequency> =
abs.into_iter().map(|p| (p.0, p.1 as f64 / n)).collect();
relative.into()
}
} }
impl std::fmt::Debug for WordMap { impl std::fmt::Debug for WordMap {
@ -89,3 +97,9 @@ impl std::fmt::Debug for WordMap {
) )
} }
} }
impl From<HashMap<Word, Frequency>> for WordMap {
fn from(value: HashMap<Word, Frequency>) -> Self {
Self { inner: value }
}
}