Skip to main content

Dictionary

Struct Dictionary 

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

Compiled keyword dictionary. Built via Dictionary::builder() or Dictionary::from_file().

Scans text in O(N) time (N = text bytes), independent of dictionary size.

Implementations§

Source§

impl Dictionary

Source

pub fn builder() -> DictionaryBuilder

Create a new DictionaryBuilder.

Source

pub fn from_file(path: &str) -> Result<Self, String>

Load directly from a file path (convenience shorthand).

Source

pub fn from_text(data: &str) -> Self

Parse directly from a string (convenience shorthand).

Source

pub fn seek(&self, word: &str) -> Option<u8>

Return category key (0–254) for word, or None if not found.

Source

pub fn mask(&self) -> u128

Return bitmask of category keys present in the dictionary. Bit i is set if at least one word with key == i exists. Covers keys 0–127.

Source

pub fn scan(&self, text: &str, mode: Mode) -> Vec<Match>

Scan text and return all keyword matches.

Pass Mode::FORBID to include words with key < 5 (BLOCK/ALERT/FLAG/THROTTLE/LOG). Pass Mode::IGNORE_CASE for case-insensitive matching (dictionary must be lowercase). Combine flags with |: Mode::HTML | Mode::FORBID | Mode::IGNORE_CASE.

Source

pub fn filter(&self, text: &str, mode: Mode) -> String

Replace all matched words in text with '*'.

With Mode::IGNORE_CASE, matches are case-insensitive but * masks the original casing.

Source

pub fn scan_first(&self, text: &str, mode: Mode) -> Option<Match>

Return the first keyword match in text, or None if no match.

Faster than scan() when you only need to know if a match exists.

Source

pub fn contains(&self, text: &str, mode: Mode) -> bool

Return true if text contains at least one keyword match.

Source

pub fn severity(&self, text: &str, mode: Mode) -> Option<Match>

Return the highest-severity (lowest key value) match in text, or None.

Source

pub fn score(&self, text: &str, mode: Mode) -> HashMap<u8, f32>

Return the total weighted score for each key present in text.

Score = per-word weight × per-key weight (both default 1.0).

Source

pub fn score_with_weights( &self, text: &str, mode: Mode, runtime_weights: &[(u8, f32)], ) -> HashMap<u8, f32>

Like [score], but applies additional runtime key weights on top of dictionary weights.

runtime_weights is a slice of (key, multiplier) pairs. The multiplier stacks on top of the dictionary-defined per-key weight.

Source

pub fn classify(&self, text: &str, mode: Mode) -> Option<ClassifyResult>

Classify text — returns the key with the highest weighted score, or None.

Source

pub fn classify_with_weights( &self, text: &str, mode: Mode, runtime_weights: &[(u8, f32)], ) -> Option<ClassifyResult>

Like [classify], but applies additional runtime key weights.

Useful when the same dictionary is reused in different contexts with different priority tuning (e.g. stricter BLOCK weight at night).

Source

pub fn scan_key(&self, text: &str, key: u8, mode: Mode) -> Vec<Match>

Extract only matches with a specific category key.

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.