pub struct Tagger { /* private fields */ }Expand description
A Brill-style English part-of-speech tagger.
Each tagger owns its lexicon, so Tagger::extend_lexicon affects only that
instance. Construct one with Tagger::new.
use pos_tagger::Tagger;
let tagger = Tagger::new();
let tagged = tagger.tag(&["the", "dog"]);
assert_eq!(tagged, vec![
("the".to_string(), "DT".to_string()),
("dog".to_string(), "NN".to_string()),
]);Implementations§
Source§impl Tagger
impl Tagger
Sourcepub fn tag<S: AsRef<str>>(&self, words: &[S]) -> Vec<(String, String)>
pub fn tag<S: AsRef<str>>(&self, words: &[S]) -> Vec<(String, String)>
Tag each word in words.
Returns a vector the same length as the input. Each entry pairs the original word with its tag. The input is never modified.
words is any slice whose elements borrow as str, so &[&str],
&[String], and the lexer’s Vec<String> output all pass without an
intermediate Vec<&str>. Write tagger.tag(&lexer.lex(text)) directly.
Each result pair owns its word as a fresh String. The pair shape is the
public contract, so the word is copied out of the borrowed input. Callers
that only need tags can read the second field and drop the first.
An empty token tags as NN. It is a lexicon key with an empty tag list,
and the empty list falls back to NN instead of panicking.
Sourcepub fn word_in_lexicon(&self, word: &str) -> bool
pub fn word_in_lexicon(&self, word: &str) -> bool
Report whether word is in the lexicon.
Returns true when the exact word is a key or when its lowercase form is a
key. The empty string is a key with an empty tag list, so
word_in_lexicon("") returns true.
The lexicon is a HashMap, so object member names such as constructor,
valueOf, hasOwnProperty, toString, and __proto__ return false
unless they are real keys. None of them are. A lookup over a plain object
would inherit these names and report true. This crate keeps the HashMap
result on purpose.
Sourcepub fn extend_lexicon<I>(&mut self, entries: I)
pub fn extend_lexicon<I>(&mut self, entries: I)
Add entries from entries, keeping existing keys untouched.
Only keys absent from the current lexicon are inserted. Existing entries are never overwritten.
entries is any iterable of (word, tags) pairs. A HashMap, a
BTreeMap, a Vec, an array, or a map adaptor all work, so callers do
not build a HashMap first.