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
impl Dictionary
Sourcepub fn builder() -> DictionaryBuilder
pub fn builder() -> DictionaryBuilder
Create a new DictionaryBuilder.
Sourcepub fn from_file(path: &str) -> Result<Self, String>
pub fn from_file(path: &str) -> Result<Self, String>
Load directly from a file path (convenience shorthand).
Sourcepub fn seek(&self, word: &str) -> Option<u8>
pub fn seek(&self, word: &str) -> Option<u8>
Return category key (0–254) for word, or None if not found.
Sourcepub fn mask(&self) -> u128
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.
Sourcepub fn scan(&self, text: &str, mode: Mode) -> Vec<Match>
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.
Sourcepub fn filter(&self, text: &str, mode: Mode) -> String
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.
Sourcepub fn scan_first(&self, text: &str, mode: Mode) -> Option<Match>
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.
Sourcepub fn contains(&self, text: &str, mode: Mode) -> bool
pub fn contains(&self, text: &str, mode: Mode) -> bool
Return true if text contains at least one keyword match.
Sourcepub fn severity(&self, text: &str, mode: Mode) -> Option<Match>
pub fn severity(&self, text: &str, mode: Mode) -> Option<Match>
Return the highest-severity (lowest key value) match in text, or None.
Sourcepub fn score(&self, text: &str, mode: Mode) -> HashMap<u8, f32>
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).
Sourcepub fn score_with_weights(
&self,
text: &str,
mode: Mode,
runtime_weights: &[(u8, f32)],
) -> HashMap<u8, f32>
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.
Sourcepub fn classify(&self, text: &str, mode: Mode) -> Option<ClassifyResult>
pub fn classify(&self, text: &str, mode: Mode) -> Option<ClassifyResult>
Classify text — returns the key with the highest weighted score, or None.
Sourcepub fn classify_with_weights(
&self,
text: &str,
mode: Mode,
runtime_weights: &[(u8, f32)],
) -> Option<ClassifyResult>
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).