wordcloud/rank/
mod.rs

1use rayon::iter::IntoParallelIterator;
2use rayon::iter::ParallelIterator;
3use std::collections::HashMap;
4
5#[derive(Clone)]
6pub(crate) struct Token {
7    content: String,
8    count: usize,
9}
10
11impl Token {
12    pub fn content(&self) -> &str {
13        &self.content
14    }
15    pub fn count(&self) -> usize {
16        self.count
17    }
18}
19
20/**
21    Used to precalculate the "common-ness" of the words.
22*/
23pub struct RankedWords(pub(crate) Vec<Token>);
24
25impl RankedWords {
26    /**
27        Rank the words by accuracy.
28    */
29    pub fn rank(words: Vec<String>) -> RankedWords {
30        let mut hs: HashMap<String, usize> = HashMap::new();
31        for s in words {
32            let f = hs.get(&s);
33            hs.insert(s, *f.unwrap_or(&0) + 1);
34        }
35
36        let mut n = hs
37            .into_par_iter()
38            .map(|c| (c.0, c.1))
39            .map(|k| Token {
40                content: k.0,
41                count: k.1,
42            })
43            .collect::<Vec<Token>>();
44
45        n.sort_by(|w, w2| w2.count.cmp(&w.count));
46
47        RankedWords(n)
48    }
49}