pub struct Filter { /* private fields */ }Expand description
Advanced sensitive word filter with variant detection
Implementations§
Source§impl Filter
impl Filter
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new filter with default settings.
§Examples
use sensitive_rs::Filter;
let mut filter = Filter::new();
filter.add_word("赌博");
assert_eq!(filter.find_in("含有赌博"), (true, "赌博".to_string()));Sourcepub fn clear_cache(&self)
pub fn clear_cache(&self)
Clear the cache
Sourcepub fn with_algorithm(algorithm: MatchAlgorithm) -> Self
pub fn with_algorithm(algorithm: MatchAlgorithm) -> Self
Create with specific algorithm
Sourcepub fn with_default_dict() -> Result<Self>
pub fn with_default_dict() -> Result<Self>
Load default dictionary
Sourcepub fn update_noise_pattern(&mut self, pattern: &str) -> Result<(), Error>
pub fn update_noise_pattern(&mut self, pattern: &str) -> Result<(), Error>
Update noise pattern
Sets the regex used to strip noise characters before matching. Characters
not matched by the regex are kept. Returns an error if pattern is invalid.
§Examples
use sensitive_rs::Filter;
let mut filter = Filter::new();
filter.add_word("赌博");
// Strip everything except CJK and ASCII word characters.
filter.update_noise_pattern(r"[^\w一-鿿]")?;
assert_eq!(filter.remove_noise("赌@#博"), "赌博");Sourcepub fn add_words(&mut self, words: &[&str])
pub fn add_words(&mut self, words: &[&str])
Add multiple words
§Examples
use sensitive_rs::Filter;
let mut filter = Filter::new();
filter.add_words(&["赌博", "色情"]);
assert!(filter.find_all("含有赌博和色情").contains(&"赌博".to_string()));Sourcepub fn current_algorithm(&self) -> MatchAlgorithm
pub fn current_algorithm(&self) -> MatchAlgorithm
Get the currently used algorithm
Sourcepub fn load_word_dict<P: AsRef<Path>>(&mut self, path: P) -> Result<()>
pub fn load_word_dict<P: AsRef<Path>>(&mut self, path: P) -> Result<()>
Load dictionary from file
Sourcepub fn load<R: BufRead>(&mut self, reader: R) -> Result<()>
pub fn load<R: BufRead>(&mut self, reader: R) -> Result<()>
Load dictionary from reader
Each line of the reader is one dictionary word.
§Examples
use sensitive_rs::Filter;
use std::io::Cursor;
let mut filter = Filter::new();
filter.load(Cursor::new("赌博\n色情"))?;
assert_eq!(filter.find_in("含有赌博"), (true, "赌博".to_string()));Sourcepub fn find_first_match(&self, text: &str) -> Option<Match>
pub fn find_first_match(&self, text: &str) -> Option<Match>
Find the first sensitive word, returning a Match with details, or None.
Exact matches are preferred; pinyin/shape variants are only consulted when no
exact hit is found. is_variant on the returned Match records which path hit.
§Examples
use sensitive_rs::{Filter, Match};
let mut filter = Filter::new();
filter.add_word("赌博");
// Exact hit:
assert_eq!(
filter.find_first_match("含有赌博"),
Some(Match { word: "赌博".to_string(), is_variant: false })
);
// Pinyin variant (no exact hit):
assert_eq!(
filter.find_first_match("dubo"),
Some(Match { word: "赌博".to_string(), is_variant: true })
);
// No match:
assert_eq!(filter.find_first_match("clean text"), None);Sourcepub fn find_in(&self, text: &str) -> (bool, String)
pub fn find_in(&self, text: &str) -> (bool, String)
Find first sensitive word.
Returns (found, word); word is empty when nothing matched. For richer detail
(including whether the hit was a variant), use Filter::find_first_match.
§Examples
use sensitive_rs::Filter;
let mut filter = Filter::new();
filter.add_word("赌博");
assert_eq!(filter.find_in("含有赌博"), (true, "赌博".to_string()));
assert_eq!(filter.find_in("clean text"), (false, String::new()));Sourcepub fn replace(&self, text: &str, replacement: char) -> String
pub fn replace(&self, text: &str, replacement: char) -> String
Replace sensitive words with replacement character.
Each matched character is replaced by one replacement char.
§Examples
use sensitive_rs::Filter;
let mut filter = Filter::new();
filter.add_word("赌博");
assert_eq!(filter.replace("含有赌博内容", '*'), "含有**内容");Sourcepub fn filter(&self, text: &str) -> String
pub fn filter(&self, text: &str) -> String
Filter out sensitive words (remove them completely).
§Examples
use sensitive_rs::Filter;
let mut filter = Filter::new();
filter.add_word("赌博");
assert_eq!(filter.filter("含有赌博内容"), "含有内容");Sourcepub fn validate(&self, text: &str) -> (bool, String)
pub fn validate(&self, text: &str) -> (bool, String)
Validate text
This is an alias for Filter::find_in: it returns (found, word) for the
first sensitive word encountered.
§Examples
use sensitive_rs::Filter;
let mut filter = Filter::new();
filter.add_word("赌博");
assert_eq!(filter.validate("含有赌博"), (true, "赌博".to_string()));
assert_eq!(filter.validate("clean text"), (false, String::new()));Sourcepub fn remove_noise(&self, text: &str) -> String
pub fn remove_noise(&self, text: &str) -> String
Remove only specific noise characters, preserve spaces
Sourcepub fn get_noise_pattern(&self) -> &Regex
pub fn get_noise_pattern(&self) -> &Regex
Get current noise pattern
Source§impl Filter
impl Filter
Sourcepub fn find_all(&self, text: &str) -> Vec<String>
pub fn find_all(&self, text: &str) -> Vec<String>
Optimized method of finding all sensitive words.
Returns the de-duplicated, sorted list of matched dictionary words (variants included). Results are cached, so repeated calls on the same text are cheap.
§Examples
use sensitive_rs::Filter;
let mut filter = Filter::new();
filter.add_words(&["赌博", "色情"]);
// Results are sorted: 色 (U+8272) sorts before 赌 (U+8D4C).
assert_eq!(filter.find_all("含有赌博和色情内容"), vec!["色情".to_string(), "赌博".to_string()]);Sourcepub fn find_all_batch(&self, texts: &[&str]) -> Vec<Vec<String>>
pub fn find_all_batch(&self, texts: &[&str]) -> Vec<Vec<String>>
Bulk search for optimized versions
Runs Filter::find_all over each text. With the parallel feature (default) the
texts are processed concurrently.
§Examples
use sensitive_rs::Filter;
let mut filter = Filter::new();
filter.add_words(&["赌博", "色情"]);
let results = filter.find_all_batch(&["含有赌博", "正常", "含有色情"]);
assert!(results[0].contains(&"赌博".to_string()));
assert!(results[1].is_empty());
assert!(results[2].contains(&"色情".to_string()));Sourcepub fn find_all_layered(&self, text: &str) -> Vec<String>
pub fn find_all_layered(&self, text: &str) -> Vec<String>
Hierarchical Matching - Preferential Matching by Sensitive Word Length
Matches the longest words first and consumes their span, so shorter overlapping entries are dropped. Useful when the dictionary contains both short and long forms.
§Examples
use sensitive_rs::Filter;
let mut filter = Filter::new();
filter.add_words(&["赌", "赌博", "赌博机"]);
let results = filter.find_all_layered("这里有赌博机");
assert!(results.contains(&"赌博机".to_string()));
assert!(!results.contains(&"赌博".to_string()));Sourcepub fn find_all_streaming<R: BufRead>(&self, reader: R) -> Result<Vec<String>>
pub fn find_all_streaming<R: BufRead>(&self, reader: R) -> Result<Vec<String>>
Streaming version - suitable for oversized text
Reads line-by-line from any BufRead and returns the de-duplicated matches
across all lines. Handy for files too large to hold in memory.
§Examples
use sensitive_rs::Filter;
use std::io::Cursor;
let mut filter = Filter::new();
filter.add_words(&["赌博", "色情"]);
let input = "第一行含有赌博\n第二行含有色情\n第三行正常";
let results = filter.find_all_streaming(Cursor::new(input))?;
assert_eq!(results.len(), 2);Trait Implementations§
Auto Trait Implementations§
impl Freeze for Filter
impl RefUnwindSafe for Filter
impl Send for Filter
impl Sync for Filter
impl Unpin for Filter
impl UnsafeUnpin for Filter
impl UnwindSafe for Filter
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
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>
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>
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