Struct Trie

Source
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

Source

pub fn new() -> Self

Creates a new keyword filter.

§Returns

Returns a new Trie instance.

§Example
use sensitive_rs::Trie;

let filter = Trie::new();
§Errors

Returns an error if the word is empty.

§Panics

Panics if the word is empty.

Source

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

Adds a word to the filter.

§Arguments
  • word: The word to be added.
§Example
use sensitive_rs::Trie;

let filter = Trie::new();
filter.add_word("bad");
filter.add_word("worse");
§Panics

Panics if the word is empty.

§Errors

Returns an error if the word is empty.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Default for Trie

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl Freeze for Trie

§

impl RefUnwindSafe for Trie

§

impl Send for Trie

§

impl Sync for Trie

§

impl Unpin for Trie

§

impl UnwindSafe for Trie

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.