Struct Filter

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

Sensitive word filter. It provides efficient algorithms to handle sensitive words, suitable for various application scenarios.

§Examples

use sensitive_rs::Filter;

let mut filter = Filter::new();
filter.add_word("bad");
filter.add_word("worse");

let result = filter.find_in("This is bad.");
assert_eq!(result, (true, "bad".to_string()));

§Features

  • Find: Locate all sensitive words in a text.
  • Validate: Check if a text contains any sensitive words.
  • Filter: Remove sensitive words from a text.
  • Replace: Replace sensitive words in a text with specified characters.

§Installation

Add the following dependency to your Cargo.toml:

[dependencies]
sensitive-rs = "0.1"

§Quick Start

use sensitive_rs::Filter;

let mut filter = Filter::new();
filter.add_word("bad");
filter.add_word("worse");

let result = filter.find_in("This is bad.");
assert_eq!(result, (true, "bad".to_string()));

let result = filter.validate("This is worse.");
assert_eq!(result, (true, "worse".to_string()));

let filtered_text = filter.filter("This is bad and worse.");
assert_eq!(filtered_text, "This is  and .");

let replaced_text = filter.replace("This is bad and worse.", '*');
assert_eq!(replaced_text, "This is *** and *****.");

§Documentation

For detailed documentation, please refer to Documentation.

§License

Licensed under either of

§Authors

§Acknowledgments

§References

§See Also

§Tags

  • Sensitive
  • Filter
  • Find
  • Validate
  • Replace
  • Trie

§Categories

  • Sensitive
  • Filter
  • Find
  • Validate
  • Replace
  • Trie

§Dependencies

§Examples

  • sensitive-rs A filter that can be used to filter out sensitive words from the text. The filter is case-insensitive. The filter is fast and efficient.

§Example

use sensitive_rs::Filter;

let mut filter = Filter::new();
filter.add_word("bad");
filter.add_word("worse");

assert_eq!(filter.find_in("This is bad."), (true, "bad".to_string()));
assert_eq!(filter.find_in("This is worse."), (true, "worse".to_string()));
assert_eq!(filter.find_in("This is good."), (false, "".to_string()));

§Panics

Panics if the noise pattern is invalid.

§Errors

Returns an error if the noise pattern is invalid.

§Safety

The noise pattern must be a valid regular expression.

§Examples

use sensitive_rs::Filter;

let mut filter = Filter::new();
filter.update_noise_pattern(r"[\|\s&%$@*]+");

assert_eq!(filter.remove_noise("I |have& %an$ @apple*"), "Ihaveanapple");

§Panics

Panics if the pattern is invalid.

§Errors

Returns an error if the pattern is invalid.

§Arguments

  • pattern - A regular expression pattern.

§Returns

  • () - Returns nothing.

§Safety

The pattern must be a valid regular expression.

Implementations§

Source§

impl Filter

Source

pub fn new() -> Self

Create a new filter.

§Example
use sensitive_rs::Filter;

let mut filter = Filter::new();
§Returns

Returns a new filter object.

Source

pub fn with_default_dict() -> Result<Self, Error>

Create a new filter and load the default dictionary.

§Example
use sensitive_rs::Filter;

let filter = Filter::with_default_dict().unwrap();
§Returns
  • Result<Self, io::Error> - Returns the filter object or an error if the dictionary cannot be loaded.
§Errors

Returns an error if the dictionary cannot be loaded.

§Safety

The dictionary must be valid.

Source

pub fn update_noise_pattern(&mut self, pattern: &str)

Update the noise pattern used to remove unwanted characters from the text. The default pattern is r“[|\s&%$@*]+“.

§Example
use sensitive_rs::Filter;

let mut filter = Filter::new();
filter.update_noise_pattern(r"[\|\s&%$@*]+");
§Panics

Panics if the pattern is invalid.

§Errors

Returns an error if the pattern is invalid.

§Arguments
  • pattern - A regular expression pattern.
§Returns
  • () - Returns nothing.
§Safety

The pattern must be a valid regular expression.

Source

pub fn load_word_dict(&mut self, path: &str) -> Result<()>

Load a word dictionary from a file.

§Example
use sensitive_rs::Filter;

let mut filter = Filter::new();
filter.load_word_dict("dict/dict.txt").unwrap();
§Arguments
  • path - The path to the word dictionary file.
§Returns
  • () - Returns nothing.
§Errors

Returns an error if the file cannot be opened.

Source

pub fn load<R: Read>(&mut self, reader: R) -> Result<()>

Load a word dictionary from a reader.

§Example
use sensitive_rs::Filter;
use std::fs::File;

let mut filter = Filter::new();
filter.load(File::open("dict/dict.txt").unwrap()).unwrap();
§Arguments
  • reader - A reader that implements the Read trait.
§Returns
  • () - Returns nothing.
§Errors

Returns an error if the reader cannot be read.

Source

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

Add a word to the filter.

§Example
use sensitive_rs::Filter;

let mut filter = Filter::new();
filter.add_word("apple");
§Arguments
  • words - The word to add to the filter.
§Returns
  • () - Returns nothing.
Source

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

Add words to the filter.

§Example
use sensitive_rs::Filter;

let mut filter = Filter::new();
filter.add_words(&["apple", "app", "banana"]);
§Arguments
  • words - The words to add to the filter.
§Returns
  • () - Returns nothing.
Source

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

Delete a word from the filter.

§Example
use sensitive_rs::Filter;

let mut filter = Filter::new();
filter.add_word("apple");
filter.del_word("apple");
§Arguments
  • words - The word to delete from the filter.
§Returns
  • () - Returns nothing.
§Errors

Returns an error if the word is not found.

Source

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

Delete words from the filter.

§Example
use sensitive_rs::Filter;

let mut filter = Filter::new();
filter.add_words(&["apple", "app", "banana"]);
filter.del_words(&["app"]);
§Arguments
  • words - The words to delete from the filter.
§Returns
  • () - Returns nothing.
§Errors

Returns an error if the word is not found.

Source

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

Filter words from a string.

§Example
use sensitive_rs::Filter;

let mut filter = Filter::new();
filter.add_words(&["apple", "app", "banana"]);

assert_eq!(filter.filter("I have an apple and a banana"), "I have an  and a ");
§Arguments
  • text - The text to filter words from.
§Returns
  • String - The text with words filtered out.
Source

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

Replace words in a string.

§Example
use sensitive_rs::Filter;

let mut filter = Filter::new();
filter.add_words(&["apple", "app", "banana"]);

assert_eq!(filter.replace("I have an apple and a banana", '*'), "I have an ***** and a ******");
§Arguments
  • text - The text to replace words in.
  • repl - The character to replace words with.
§Returns
  • String - The text with words replaced.
Source

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

Find a word in a string.

§Example
use sensitive_rs::Filter;

let mut filter = Filter::new();
filter.add_words(&["apple", "app", "banana"]);

assert_eq!(filter.find_in("I have an apple and a banana"), (true, "apple".to_string()));
§Arguments
  • text - The text to find the word in.
§Returns
  • (bool, String) - A tuple containing a boolean and the word found.
§Errors

Returns an error if the word is not found.

§Panics

Panics if the word is not found.

Source

pub fn find_all(&self, text: &str) -> Vec<String>

Find all words in a string.

§Example
use sensitive_rs::Filter;

let mut filter = Filter::new();
filter.add_words(&["apple", "app", "banana"]);

assert_eq!(filter.find_all("I have an apple and a banana"), vec!["apple", "banana"]);
§Arguments
  • text - The text to find words in.
§Returns
  • Vec<String> - A vector containing the words found.
§Errors

Returns an error if the word is not found.

§Panics

Panics if the word is not found.

Source

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

Validate a string.

§Example
use sensitive_rs::Filter;

let mut filter = Filter::new();
filter.add_words(&["apple", "app", "banana"]);

assert_eq!(filter.validate("I have an apple and a banana"), (true, "apple".to_string()));
§Arguments
  • text - The text to validate.
§Returns
  • (bool, String) - A tuple containing a boolean and the word found.
§Errors

Returns an error if the word is not found.

§Panics

Panics if the word is not found.

Source

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

Remove unwanted characters from the text.

§Example
use sensitive_rs::Filter;

let mut filter = Filter::new();
filter.update_noise_pattern(r"[^\w]");

assert_eq!(filter.remove_noise("I |have& %an$ @apple*"), "Ihaveanapple");
§Arguments
  • text - The text to remove unwanted characters from.
§Returns
  • String - The text with unwanted characters removed.
§Safety

The noise pattern must be a valid regular expression.

§Panics

Panics if the noise pattern is invalid.

§Errors

Returns an error if the noise pattern is invalid.

Trait Implementations§

Source§

impl Default for Filter

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Filter

§

impl RefUnwindSafe for Filter

§

impl Send for Filter

§

impl Sync for Filter

§

impl Unpin for Filter

§

impl UnwindSafe for Filter

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.