1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::cmp::Ordering;
use std::collections::HashMap;
pub trait KeywordSort {
fn sort_by_score(&mut self);
}
#[derive(Debug, Clone)]
pub struct KeywordScore {
pub keyword: String,
pub score: f64,
}
impl KeywordScore {
pub fn from_map(mp: HashMap<String, f64>) -> Vec<Self> {
let mut keywords = Vec::new();
for (kw, score) in mp {
keywords.push(KeywordScore {
keyword: kw,
score: score,
});
}
keywords
}
}
impl Ord for KeywordScore {
fn cmp(&self, other: &Self) -> Ordering {
self.score
.partial_cmp(&other.score)
.unwrap_or(Ordering::Less)
}
}
impl PartialOrd for KeywordScore {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Eq for KeywordScore {}
impl PartialEq for KeywordScore {
fn eq(&self, other: &Self) -> bool {
self.score == other.score
}
}
impl KeywordSort for Vec<KeywordScore> {
fn sort_by_score(&mut self) {
self.sort_by(|a, b| b.cmp(a));
}
}