Sensitive-rs
English | 简体中文
A high-performance Rust crate for multi-pattern string matching, validation, filtering, and replacement.
Features
- Find all sensitive words:
find_all - First match with metadata (
Match):find_first_match - Validate text contains sensitive words:
validate - Remove sensitive words:
filter - Replace sensitive words with a character:
replace - Multi-algorithm engine: Aho-Corasick, Wu-Manber, Regex
- Noise removal via configurable regex
- Variant detection (拼音、形似字) — pinyin plus a 50+ group shape-confusable map
- Parallel search with optional
rayonsupport (parallelfeature, enabled by default) - LRU cache for hot queries
- Batch processing:
find_all_batch - Layered matching:
find_all_layered - Streaming processing:
find_all_streaming - Criterion benchmarks and runnable examples for release validation
Algorithm selection
The engine auto-selects based on vocabulary size:
| Patterns | Algorithm | Rationale |
|---|---|---|
| 0–100 | Wu-Manber | Small tables, quick scan |
| 101–10,000 | Aho-Corasick | O(n) automaton scan regardless of count |
| 10,000+ | Regex | Compilation overhead amortized |
Override with Filter::with_algorithm(...) or --algorithm on the CLI.
Platform Support
| Platform | Status | How |
|---|---|---|
| Linux / macOS / Windows | Full support | default features / --all-features |
| WASM (browser / Node.js) | Supported | wasm feature; no file/network I/O (use loadWords) |
Embedded (no_std) |
Experimental | --no-default-features; core exact matching only |
| Async (tokio) | Supported | async-io / net-async features |
WASM
[]
= { = "1.3", = false, = ["wasm"] }
import init from 'sensitive-rs';
await ;
const filter = ;
filter.;
filter.; // ['赌博']
filter.; // bulk-load from in-memory text
no_std (embedded)
[]
= { = "1.3", = false }
Core find_all / find_in / replace / filter work without std. Pinyin/shape variant
detection, the LRU cache, and the file/network loaders require the std feature (on by default).
Async
[]
= { = "1.3", = ["async-io"] }
async
Installation
Add to your Cargo.toml:
[]
= "1.3.0"
For environments that should avoid rayon (for example WASM or embedded targets), disable default features:
[]
= { = "1.3.0", = false }
Quick Start
use Filter;
Advanced Usage
Batch processing:
let texts = vec!;
let results = filter.find_all_batch;
Layered matching:
let layered = filter.find_all_layered;
Streaming large files:
use File;
use BufReader;
let reader = new;
let stream_results = filter.find_all_streaming?;
CLI Usage
Install with the cli feature:
[]
= { = "1.3.0", = ["cli"] }
Or install directly:
Both sensitive and sensitive-rs commands are available after installation.
Commands
# Find sensitive words
# Validate (exit 1 if sensitive words found)
# Replace sensitive words
# Remove sensitive words
# Read from file
# Pipe from stdin
|
Options
--dict <path>— custom dictionary file--dict-all— use extended dictionary (27k words)--algorithm <algo>— force algorithm:aho-corasick,wumanber,regex--variant— enable pinyin and shape variant detection--noise-pattern <regex>— custom noise removal regex--json— JSON output format--color— force colored output
Examples and Benchmarks
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
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 or MIT license, shall be dual licensed as above, without any additional terms or conditions.