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
- Apache License, Version 2.0, LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0
- MIT license LICENSE-MIT or http://opensource.org/licenses/MIT
§Authors
§Acknowledgments
- Aho-Corasick
- Trie
- DFA
- NFA
- Regex
- KMP
- Boyer-Moore
- Rabin-Karp
- Sunday
- Horspool
- Comment
- Shift-And
- Bitap
- Wu-Manber
§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
impl Filter
Sourcepub fn with_default_dict() -> Result<Self, Error>
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.
Sourcepub fn update_noise_pattern(&mut self, pattern: &str)
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.
Sourcepub fn load_word_dict(&mut self, path: &str) -> Result<()>
pub fn load_word_dict(&mut self, path: &str) -> Result<()>
Sourcepub fn load<R: Read>(&mut self, reader: R) -> Result<()>
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.
Sourcepub fn del_words(&mut self, words: &[&str])
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.
Sourcepub fn filter(&self, text: &str) -> String
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.
Sourcepub fn replace(&self, text: &str, repl: char) -> String
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.
Sourcepub fn find_in(&self, text: &str) -> (bool, String)
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.
Sourcepub fn find_all(&self, text: &str) -> Vec<String>
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.
Sourcepub fn validate(&self, text: &str) -> (bool, String)
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.
Sourcepub fn remove_noise(&self, text: &str) -> String
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.