sensitive_rs/lib.rs
1//! # Sensitive-rs
2//!
3//! `sensitive-rs` is a Rust library for finding, validating, filtering, and replacing sensitive words. It provides efficient algorithms to handle sensitive words, suitable for various application scenarios.
4//!
5//! ## Features
6//!
7//! - **Find**: Locate all sensitive words in a text.
8//! - **Validate**: Check if a text contains any sensitive words.
9//! - **Filter**: Remove sensitive words from a text.
10//! - **Replace**: Replace sensitive words in a text with specified characters.
11//!
12//! ## Installation
13//!
14//! Add the following dependency to your `Cargo.toml`:
15//!
16//! ```toml
17//! [dependencies]
18//! sensitive-rs = "0.1"
19//! ```
20//!
21//! ## Quick Start
22//!
23//! ```rust
24//! use sensitive_rs::Filter;
25//!
26//! // Create a new Filter
27//! let mut filter = Filter::new();
28//! filter.add_word("bad");
29//! filter.add_word("worse");
30//!
31//! // Find sensitive words
32//! let result = filter.find_in("This is bad.");
33//! assert_eq!(result, (true, "bad".to_string()));
34//!
35//! // Validate text
36//! let result = filter.validate("This is worse.");
37//! assert_eq!(result, (true, "worse".to_string()));
38//!
39//! // Filter sensitive words
40//! let filtered_text = filter.filter("This is bad and worse.");
41//! assert_eq!(filtered_text, "This is and .");
42//!
43//! // Replace sensitive words
44//! let replaced_text = filter.replace("This is bad and worse.", '*');
45//! assert_eq!(replaced_text, "This is *** and *****.");
46//! ```
47//!
48//! ## Documentation
49//!
50//! For detailed documentation, please refer to [Documentation](https://docs.rs/sensitive-rs).
51//!
52//! ## License
53//!
54//!
55//! Licensed under either of
56//!
57//! * Apache License, Version 2.0, [LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>
58//! * MIT license [LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>
59mod filter;
60mod trie;
61
62/// Sensitive word filter.
63/// It provides efficient algorithms to handle sensitive words, suitable for various application scenarios.
64/// # Examples
65/// ```
66/// use sensitive_rs::Filter;
67///
68/// let mut filter = Filter::new();
69/// filter.add_word("bad");
70/// filter.add_word("worse");
71///
72/// let result = filter.find_in("This is bad.");
73/// assert_eq!(result, (true, "bad".to_string()));
74/// ```
75///
76/// # Features
77/// - **Find**: Locate all sensitive words in a text.
78/// - **Validate**: Check if a text contains any sensitive words.
79/// - **Filter**: Remove sensitive words from a text.
80/// - **Replace**: Replace sensitive words in a text with specified characters.
81///
82/// # Installation
83/// Add the following dependency to your `Cargo.toml`:
84/// ```toml
85/// [dependencies]
86/// sensitive-rs = "0.1"
87/// ```
88///
89/// # Quick Start
90/// ```rust
91/// use sensitive_rs::Filter;
92///
93/// let mut filter = Filter::new();
94/// filter.add_word("bad");
95/// filter.add_word("worse");
96///
97/// let result = filter.find_in("This is bad.");
98/// assert_eq!(result, (true, "bad".to_string()));
99///
100/// let result = filter.validate("This is worse.");
101/// assert_eq!(result, (true, "worse".to_string()));
102///
103/// let filtered_text = filter.filter("This is bad and worse.");
104/// assert_eq!(filtered_text, "This is and .");
105///
106/// let replaced_text = filter.replace("This is bad and worse.", '*');
107/// assert_eq!(replaced_text, "This is *** and *****.");
108/// ```
109///
110/// # Documentation
111/// For detailed documentation, please refer to [Documentation](https://docs.rs/sensitive-rs).
112///
113/// # License
114/// Licensed under either of
115/// * Apache License, Version 2.0, [LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>
116/// * MIT license [LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>
117///
118/// # Authors
119/// - [houseme](https://github.com/houseme)
120///
121/// # Acknowledgments
122/// - [Aho-Corasick](https://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_algorithm)
123/// - [Trie](https://en.wikipedia.org/wiki/Trie)
124/// - [DFA](https://en.wikipedia.org/wiki/Deterministic_finite_automaton)
125/// - [NFA](https://en.wikipedia.org/wiki/Nondeterministic_finite_automaton)
126/// - [Regex](https://en.wikipedia.org/wiki/Regular_expression)
127/// - [KMP](https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm)
128/// - [Boyer-Moore](https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string-search_algorithm)
129/// - [Rabin-Karp](https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm)
130/// - [Sunday](https://en.wikipedia.org/wiki/String_searching_algorithm#Sunday's_algorithm)
131/// - [Horspool](https://en.wikipedia.org/wiki/String_searching_algorithm#Horspool's_algorithm)
132/// - [Comment](https://en.wikipedia.org/wiki/String_searching_algorithm#Comment's_algorithm)
133/// - [Shift-And](https://en.wikipedia.org/wiki/Bitap_algorithm)
134/// - [Bitap](https://en.wikipedia.org/wiki/Bitap_algorithm)
135/// - [Wu-Manber](https://en.wikipedia.org/wiki/Wu%E2%80%93Manber_algorithm)
136///
137/// # References
138/// - [Aho-Corasick](https://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_algorithm)
139/// - [Trie](https://en.wikipedia.org/wiki/Trie)
140///
141/// # See Also
142/// - [sensitive-rs](https://github.com/houseme/sensitive-rs)
143///
144/// # Tags
145/// - Sensitive
146/// - Filter
147/// - Find
148/// - Validate
149/// - Replace
150/// - Trie
151///
152/// # Categories
153/// - Sensitive
154/// - Filter
155/// - Find
156/// - Validate
157/// - Replace
158/// - Trie
159///
160/// # Dependencies
161/// - [reqwest](https://crates.io/crates/reqwest)
162/// - [regex](https://crates.io/crates/regex)
163///
164/// # Examples
165/// - [sensitive-rs](https://github.com/houseme/sensitive-rs)
166pub use filter::Filter;
167/// Trie data structure.
168/// It is a tree data structure used for efficient retrieval of a key in a large dataset.
169/// # Examples
170/// ```
171/// use sensitive_rs::Trie;
172/// use sensitive_rs::TrieNode;
173///
174/// let mut trie = Trie::new();
175/// trie.add_word("bad");
176/// trie.add_word("worse");
177///
178/// let result = trie.find_in("This is bad.");
179/// assert_eq!(result, Some("bad".to_string()));
180///
181/// let result = trie.find_in("This is worse.");
182/// assert_eq!(result, Some("worse".to_string()));
183///
184/// let result = trie.find_in("This is good.");
185/// assert_eq!(result, None);
186///
187/// let result = trie.find_in("This is worse and bad.");
188/// assert_eq!(result, Some("worse".to_string()));
189///
190/// let result = trie.find_in("This is bad and worse.");
191/// assert_eq!(result, Some("bad".to_string()));
192///
193/// let result = trie.find_in("This is good and better.");
194/// assert_eq!(result, None);
195/// ```
196pub use trie::Trie;
197/// Trie node.
198/// It is a tree data structure used for efficient retrieval of a key in a large dataset.
199/// # Examples
200/// ```
201/// use sensitive_rs::TrieNode;
202///
203/// let mut node = TrieNode::new('a', false);
204///
205/// assert!(!node.is_root_node());
206/// assert!(!node.is_end())
207/// ```
208pub use trie::TrieNode;