use nl3::Nl3;
use nl3::tagger::Tagger;
struct ExtendedTagger;
impl Tagger for ExtendedTagger {
fn tag(&self, text: &str) -> Vec<(String, String)> {
text.split_whitespace()
.map(|token| {
let tag = match token.to_lowercase().as_str() {
"betwixt" => "IN", "by" | "from" | "on" | "to" | "with" | "of" => "IN",
"who" | "which" | "that" => "WDT",
t if !t.is_empty() && t.bytes().all(|b| b.is_ascii_digit()) => "CD",
_ => "NN",
};
(token.to_string(), tag.to_string())
})
.collect()
}
}
fn main() {
let nl3 = Nl3::builder()
.grammar(["users follow users"])
.vocabulary([("follow", "follow")])
.tagger(ExtendedTagger)
.build();
let phrase = "users follow betwixt user 42";
match nl3.parse(phrase) {
Ok(t) => println!(
"{phrase:?} => {}({}) -{}-> {}({})",
t.subject.ty.as_deref().unwrap_or("?"),
t.subject.value.as_deref().unwrap_or("-"),
t.predicate.value.as_deref().unwrap_or("?"),
t.object.ty.as_deref().unwrap_or("?"),
t.object.value.as_deref().unwrap_or("-"),
),
Err(e) => println!("{phrase:?} => error: {e}"),
}
}