pub struct Trie { /* private fields */ }
Expand description
Trie data structure. It is a tree data structure used for efficient retrieval of a key in a large dataset.
§Examples
use sensitive_rs::Trie;
use sensitive_rs::TrieNode;
let mut trie = Trie::new();
trie.add_word("bad");
trie.add_word("worse");
let result = trie.find_in("This is bad.");
assert_eq!(result, Some("bad".to_string()));
let result = trie.find_in("This is worse.");
assert_eq!(result, Some("worse".to_string()));
let result = trie.find_in("This is good.");
assert_eq!(result, None);
let result = trie.find_in("This is worse and bad.");
assert_eq!(result, Some("worse".to_string()));
let result = trie.find_in("This is bad and worse.");
assert_eq!(result, Some("bad".to_string()));
let result = trie.find_in("This is good and better.");
assert_eq!(result, None);
Represents a keyword filter based on the Trie data structure. The filter can be used to find, validate, filter, and replace sensitive words in a text. The filter is thread-safe.
§Example
use sensitive_rs::Trie;
let filter = Trie::new();
filter.add_word("bad");
filter.add_word("worse");
assert_eq!(filter.find_in("This is bad."), Some("bad".to_string()));
§Errors
Returns an error if the word is empty.
§Panics
Panics if the word is empty.
Implementations§
Source§impl Trie
impl Trie
Sourcepub fn del_word(&self, word: &str) -> bool
pub fn del_word(&self, word: &str) -> bool
Deletes a word from the filter.
§Arguments
word
: The word to be deleted.
§Returns
Returns a boolean indicating whether the word was successfully deleted.
§Example
use sensitive_rs::Trie;
let filter = Trie::new();
filter.add_word("bad");
filter.add_word("worse");
assert!(filter.del_word("bad"));
assert!(!filter.del_word("bad"));
§Errors
Returns an error if the word is not found.
§Panics
Panics if the word is not found.
Sourcepub fn find_word_at(&self, content: &str) -> Option<(String, usize)>
pub fn find_word_at(&self, content: &str) -> Option<(String, usize)>
Finds the first matching word and its position in the given content.
§Arguments
content
: The content to search in.
§Returns
Returns an Option<(String, usize)>
containing the matching word and its position.
If no match is found, returns None
.
§Example
use sensitive_rs::Trie;
let filter = Trie::new();
filter.add_word("bad");
filter.add_word("worse");
assert_eq!(filter.find_word_at("This is bad."), None);
assert_eq!(filter.find_word_at("This is worse."), None);
assert_eq!(filter.find_word_at("This is good."), None);
§Errors
Returns an error if the content is empty.
§Panics
Panics if the content is empty.
Sourcepub fn replace(&self, content: &str, replacement: char) -> String
pub fn replace(&self, content: &str, replacement: char) -> String
Replaces all matching words in the content with the specified character.
§Arguments
content
: The content to replace in.replacement
: The character to replace the matching words with.
§Returns
Returns the content with all matching words replaced.
§Example
use sensitive_rs::Trie;
let filter = Trie::new();
filter.add_word("bad");
filter.add_word("worse");
assert_eq!(filter.replace("This is bad and worse.", '*'), "This is *** and *****.");
§Errors
Returns an error if the content is empty.
§Panics
Panics if the content is empty.
Sourcepub fn filter(&self, content: &str) -> String
pub fn filter(&self, content: &str) -> String
Filters out all matching words in the content.
§Arguments
content
: The content to filter.
§Returns
Returns the content with all matching words removed.
§Example
use sensitive_rs::Trie;
let filter = Trie::new();
filter.add_word("bad");
filter.add_word("worse");
assert_eq!(filter.filter("This is bad and worse."), "This is and .");
§Errors
Returns an error if the content is empty.
§Panics
Panics if the content is empty.
Sourcepub fn find_in(&self, content: &str) -> Option<String>
pub fn find_in(&self, content: &str) -> Option<String>
Finds the first matching word in the content.
§Arguments
content
: The content to search in.
§Returns
Returns an Option<String>
containing the matching word.
If no match is found, returns None
.
§Example
use sensitive_rs::Trie;
let filter = Trie::new();
filter.add_word("bad");
filter.add_word("worse");
assert_eq!(filter.find_in("This is bad."), Some("bad".to_string()));
assert_eq!(filter.find_in("This is worse."), Some("worse".to_string()));
assert_eq!(filter.find_in("This is good."), None);
§Errors
Returns an error if the content is empty.
§Panics
Panics if the content is empty.
Sourcepub fn validate(&self, content: &str) -> Option<String>
pub fn validate(&self, content: &str) -> Option<String>
Validates whether the content contains any matching words.
§Arguments
content
: The content to validate.
§Returns
Returns an Option<String>
containing the matching word.
If no match is found, returns None
.
§Example
use sensitive_rs::Trie;
let filter = Trie::new();
filter.add_word("bad");
filter.add_word("worse");
assert_eq!(filter.validate("This is bad."), Some("bad".to_string()));
assert_eq!(filter.validate("This is good."), None);
§Errors
Returns an error if the content is empty.
§Panics
Panics if the content is empty.
Sourcepub fn find_all(&self, content: &str) -> Vec<String>
pub fn find_all(&self, content: &str) -> Vec<String>
Finds all matching words in the content.
§Arguments
content
: The content to search in.
§Returns
Returns a Vec<String>
containing all matching words.
§Example
use sensitive_rs::Trie;
let filter = Trie::new();
filter.add_word("bad");
filter.add_word("worse");
assert_eq!(filter.find_all("This is bad and worse."), vec!["bad", "worse"]);
§Errors
Returns an error if the content is empty.
§Panics
Panics if the content is empty.