pub struct MultiPatternEngine { /* private fields */ }Expand description
Multi-pattern matching engine
Implementations§
Source§impl MultiPatternEngine
impl MultiPatternEngine
Sourcepub fn new(algorithm: Option<MatchAlgorithm>, patterns: &[String]) -> Self
pub fn new(algorithm: Option<MatchAlgorithm>, patterns: &[String]) -> Self
Create a new engine and automatically select the algorithm based on the lexicon size
Pass None for algorithm to auto-select; pass Some to force a specific algorithm.
§Examples
use sensitive_rs::MultiPatternEngine;
let patterns = vec!["赌博".to_string(), "色情".to_string()];
let engine = MultiPatternEngine::new(None, &patterns);
assert_eq!(engine.find_first("含有赌博"), Some("赌博".to_string()));Sourcepub fn rebuild(&mut self, patterns: &[String])
pub fn rebuild(&mut self, patterns: &[String])
Rebuild the engine (called when the pattern is updated)
Sourcepub fn recommend_algorithm(word_count: usize) -> MatchAlgorithm
pub fn recommend_algorithm(word_count: usize) -> MatchAlgorithm
Recommended algorithm based on the lexicon size
- 0-100 patterns: WuManber (few patterns = small tables, quick scan)
- 101-10,000 patterns: AhoCorasick (automaton-based, O(n) scan)
- 10,000+ patterns: Regex (compilation overhead amortized)
Sourcepub fn rebuild_with_algorithm(
&mut self,
patterns: &[String],
algorithm: MatchAlgorithm,
)
pub fn rebuild_with_algorithm( &mut self, patterns: &[String], algorithm: MatchAlgorithm, )
Force rebuild using the specified algorithm
Sourcepub fn current_algorithm(&self) -> MatchAlgorithm
pub fn current_algorithm(&self) -> MatchAlgorithm
Get the currently used algorithm
Sourcepub fn get_patterns(&self) -> &[String]
pub fn get_patterns(&self) -> &[String]
Get all modes
Sourcepub fn find_first(&self, text: &str) -> Option<String>
pub fn find_first(&self, text: &str) -> Option<String>
Find the first match
§Examples
use sensitive_rs::MultiPatternEngine;
let patterns = vec!["赌博".to_string()];
let engine = MultiPatternEngine::new(None, &patterns);
assert_eq!(engine.find_first("含有赌博内容"), Some("赌博".to_string()));
assert_eq!(engine.find_first("正常文本"), None);Sourcepub fn replace_all(&self, text: &str, replacement: &str) -> String
pub fn replace_all(&self, text: &str, replacement: &str) -> String
Replace all matches with optimized performance
Sourcepub fn find_all(&self, text: &str) -> Vec<String>
pub fn find_all(&self, text: &str) -> Vec<String>
Find all matches
§Examples
use sensitive_rs::MultiPatternEngine;
let patterns = vec!["赌博".to_string(), "色情".to_string()];
let engine = MultiPatternEngine::new(None, &patterns);
let matches = engine.find_all("含有赌博和色情");
assert_eq!(matches.len(), 2);Sourcepub fn find_matches_with_positions(&self, text: &str) -> Vec<MatchInfo>
pub fn find_matches_with_positions(&self, text: &str) -> Vec<MatchInfo>
Get detailed match information
Sourcepub fn contains_any(&self, text: &str) -> bool
pub fn contains_any(&self, text: &str) -> bool
Check if text contains any patterns
Trait Implementations§
Source§impl Debug for MultiPatternEngine
impl Debug for MultiPatternEngine
Auto Trait Implementations§
impl Freeze for MultiPatternEngine
impl RefUnwindSafe for MultiPatternEngine
impl Send for MultiPatternEngine
impl Sync for MultiPatternEngine
impl Unpin for MultiPatternEngine
impl UnsafeUnpin for MultiPatternEngine
impl UnwindSafe for MultiPatternEngine
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more