pub trait Tagger {
fn tag(&self, text: &str) -> Vec<(String, String)>;
}
#[derive(Debug, Clone, Copy, Default)]
pub struct LexiconTagger;
impl Tagger for LexiconTagger {
fn tag(&self, text: &str) -> Vec<(String, String)> {
text.split_whitespace()
.map(|token| (token.to_string(), tag_word(token).to_string()))
.collect()
}
}
fn tag_word(word: &str) -> &'static str {
if !word.is_empty() && word.bytes().all(|b| b.is_ascii_digit()) {
return "CD";
}
match word.to_lowercase().as_str() {
"by" | "from" | "on" | "for" | "to" | "with" | "of" | "in" | "at" | "into" | "onto"
| "about" | "as" | "per" | "via" => "IN",
"who" | "which" | "that" | "what" | "whom" | "whose" | "where" | "when" | "why" | "how" => {
"WDT"
}
_ => "NN",
}
}