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
61
62
63
64
65
66
67
68
#[derive(Debug, Default, Clone)]
pub struct KWIndex<'a> {
word: Vec<&'a str>,
}
impl<'a> KWIndex<'a> {
pub fn new() -> Self {
let word = Vec::new();
Self { word }
}
pub fn extend_from_text(mut self, target: &'a str) -> Self {
for i in target.split_whitespace() {
let mut temp = i;
for j in i.chars() {
if !j.is_alphabetic() {
if j == i.chars().next().unwrap() || j == i.chars().last().unwrap() {
println!("[{}] is removed from [{}]", j, temp);
temp = temp.trim_matches(|c: char| c == j);
} else {
println!("[{}] is removed because {} is no alphabetic", temp, j);
temp = "";
break;
}
}
}
if !temp.is_empty() {
println!("[{}] is add to KWIndex index", temp);
self.word.push(temp);
}
}
self
}
pub fn count_matches(&self, keyword: &str) -> usize {
if self.is_empty() {
return 0;
}
let mut counter = 0;
for i in &self.word {
if i == &keyword {
counter += 1;
}
}
counter
}
pub fn len(&self) -> usize {
self.word.len()
}
pub fn is_empty(&self) -> bool {
self.word.len() == 0
}
}