#[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
}
}