Skip to main content

Tokenizer

Struct Tokenizer 

Source
pub struct Tokenizer { /* private fields */ }
Expand description

A regex tokenizer instance.

Each instance holds its own rule list, fingerprint codes, and the result of the last tokenize call. Fingerprint codes are per instance. A define_config call resets them. Create an instance with Tokenizer::new.

Implementations§

Source§

impl Tokenizer

Source

pub fn new() -> Self

Creates a tokenizer with every token type enabled except quoted_phrase.

§Example
use english_tokenizer::Tokenizer;
let mut tk = Tokenizer::new();
assert!(tk.tokenize("").is_empty());
Source

pub fn tokenize(&mut self, sentence: &str) -> Vec<Token>

Tokenizes sentence and returns the tokens in left to right order.

The result is also kept for the next get_tokens_fp call.

§Example
use english_tokenizer::Tokenizer;
let mut tk = Tokenizer::new();
let tokens = tk.tokenize("4pm");
assert_eq!(tokens[0].tag, "time");
Source

pub fn define_config(&mut self, config: &[(&str, bool)]) -> usize

Sets which token types are active and returns the count of unique active categories.

Each pair is a category name and a flag. A true flag enables the category. A false flag disables it. Categories not listed stay enabled. An empty list disables everything, so tokenize splits on spaces and tags every piece alien.

This also resets any rules added with add_regex and resets the fingerprint codes.

Categories that use more than one rule, like hashtag and number, count once. The full default returns 13 because quoted_phrase is off. Enabling quoted_phrase returns 14.

§Example
use english_tokenizer::Tokenizer;
let mut tk = Tokenizer::new();
assert_eq!(tk.define_config(&[("hashtag", false)]), 13);
assert_eq!(tk.define_config(&[]), 0);
assert_eq!(tk.define_config(&[("quoted_phrase", true)]), 14);
Source

pub fn get_tokens_fp(&self) -> String

Returns the fingerprint of the tokens from the last tokenize call.

For each token, if its tag has a fingerprint code, that single character is appended. Otherwise the token’s literal value is appended. Tokens tagged punctuation or symbol have no code, so their value passes through. Before any tokenize call this returns an empty string.

§Example
use english_tokenizer::Tokenizer;
let mut tk = Tokenizer::new();
tk.tokenize("feeling good #FunTime");
assert_eq!(tk.get_tokens_fp(), "wwh");
Source

pub fn add_regex( &mut self, regex: Regex, tag: &str, fingerprint_code: Option<&str>, ) -> Result<(), AddError>

Adds a regex rule that wins over the built-ins.

The rule is tried before every built-in rule. If tag is already known, fingerprint_code is ignored. If tag is new, fingerprint_code must be a non-empty string so the tag has a fingerprint. A later define_config call removes added rules.

Write the pattern with non-capturing groups (?:...). Matching takes the whole match and the text between matches. A capturing group does not inject its capture as an extra token.

§Errors

Returns AddError::MissingFingerprint when tag is new and the fingerprint code is missing or empty.

§Example
use english_tokenizer::Tokenizer;
use regex::Regex;
let mut tk = Tokenizer::new();
tk.add_regex(Regex::new(r"(?i)\(oo\)").unwrap(), "emoticon", None).unwrap();
let tokens = tk.tokenize("(oo) Hi!");
assert_eq!(tokens[0].tag, "emoticon");
Source

pub fn add_tag( &mut self, name: &str, fingerprint_code: &str, ) -> Result<(), AddError>

Registers a new tag with a fingerprint code.

§Errors

Returns AddError::MissingFingerprint when fingerprint_code is empty. Returns AddError::TagExists when name already has a code.

Trait Implementations§

Source§

impl Debug for Tokenizer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Tokenizer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.