Skip to main content

Filter

Struct Filter 

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

Advanced sensitive word filter with variant detection

Implementations§

Source§

impl Filter

Source

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()));
Source

pub fn clear_cache(&self)

Clear the cache

Source

pub fn with_algorithm(algorithm: MatchAlgorithm) -> Self

Create with specific algorithm

Source

pub fn with_default_dict() -> Result<Self>

Load default dictionary

Source

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("赌@#博"), "赌博");
Source

pub fn add_word(&mut self, word: &str)

Add a sensitive word

Source

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()));
Source

pub fn current_algorithm(&self) -> MatchAlgorithm

Get the currently used algorithm

Source

pub fn del_word(&mut self, word: &str)

Remove a word

Source

pub fn del_words(&mut self, words: &[&str])

Remove multiple words

Source

pub fn load_word_dict<P: AsRef<Path>>(&mut self, path: P) -> Result<()>

Load dictionary from file

Source

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()));
Source

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);
Source

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()));
Source

pub fn replace(&self, text: &str, replacement: char) -> String

Replace sensitive words with replacement character.

Each matched character is replaced by one replacement char. Only exact dictionary matches are masked; variant forms are not (use Filter::find_all to detect them).

§Examples
use sensitive_rs::Filter;

let mut filter = Filter::new();
filter.add_word("赌博");
assert_eq!(filter.replace("含有赌博内容", '*'), "含有**内容");
Source

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

Filter out sensitive words (remove them completely).

Only exact dictionary matches are removed; variant forms are not (see Filter::replace). Use Filter::find_all to detect variants.

§Examples
use sensitive_rs::Filter;

let mut filter = Filter::new();
filter.add_word("赌博");
assert_eq!(filter.filter("含有赌博内容"), "含有内容");
Source

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()));
Source

pub fn remove_noise(&self, text: &str) -> String

Remove only specific noise characters, preserve spaces

Source

pub fn get_noise_pattern(&self) -> &Regex

Get current noise pattern

Source

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()]);
Source

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()));
Source

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()));
Source

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§

Source§

impl Debug for Filter

Source§

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

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

impl Default for Filter

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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.