use hashbrown::HashMap;
#[derive(Debug, Default)]
pub struct WordCounter {
pub word_counts: HashMap<String, usize>,
}
impl WordCounter {
pub fn new() -> Self {
Self::default()
}
pub fn inc(&mut self, word: &str) {
self.word_counts
.entry_ref(word)
.and_modify(|counter| *counter += 1)
.or_insert(1);
}
pub fn iter_top_n_words(&self, n: usize) -> impl Iterator<Item = &String> {
let mut counts: Vec<(&String, &usize)> = self.word_counts.iter().collect();
counts.sort_unstable_by(|a, b| b.1.cmp(a.1));
counts.into_iter().take(n).map(|(a, _b)| a)
}
}