generated from PlexSheep/rs-base
calc frequency of letters
This commit is contained in:
parent
a5e22349f6
commit
1fa8989f08
|
@ -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<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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,8 +21,10 @@ pub struct WordMap {
|
|||
}
|
||||
|
||||
impl WordMap {
|
||||
pub fn new(inner: HashMap<Word, Frequency>) -> 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<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 {
|
||||
|
@ -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 }
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue