From 1fa8989f08f263ce2a594680d40c73301b53a0d0 Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Fri, 22 Mar 2024 16:03:51 +0100 Subject: [PATCH] calc frequency of letters --- src/wlist/mod.rs | 17 ++++++++++++++++- src/wlist/word.rs | 18 ++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) 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 } + } +}