| 简体中文 | English |
一个高性能的 Rust 库,用于多模式字符串查找、验证、过滤和替换。
find_allMatch):find_first_matchvalidatefilterreplacerayon 的并行搜索(parallel feature,默认启用)find_all_batchfind_all_layeredfind_all_streaming引擎会根据词库规模自动选择算法:
| 词库规模 | 算法 | 说明 |
|---|---|---|
| 0–100 | Wu-Manber | 表小,扫描快 |
| 101–10,000 | Aho-Corasick | 自动机 O(n) 扫描,与词数无关 |
| 10,000+ | Regex | 编译开销在大量模式下均摊 |
可通过 Filter::with_algorithm(...) 或 CLI 的 --algorithm 强制指定。
| 平台 | 状态 | 方式 |
|---|---|---|
| Linux / macOS / Windows | 完整支持 | 默认 features / --all-features |
| WASM(浏览器 / Node.js) | 支持 | wasm feature;无文件/网络 I/O(用 loadWords) |
嵌入式(no_std) |
实验性 | --no-default-features;仅核心精确匹配 |
| 异步(tokio) | 支持 | async-io / net-async feature |
[dependencies]
sensitive-rs = { version = "1.3", default-features = false, features = ["wasm"] }
import init, { WasmFilter } from 'sensitive-rs';
await init();
const filter = new WasmFilter();
filter.addWord('赌博');
filter.findAll('含有赌博内容'); // ['赌博']
filter.loadWords('色情\n诈骗'); // 从内存文本批量加载
no_std(嵌入式)[dependencies]
sensitive-rs = { version = "1.3", default-features = false }
核心的 find_all / find_in / replace / filter 在无 std 时可用;拼音/形近字变体检测、LRU
缓存以及文件/网络加载器需要 std feature(默认开启)。
[dependencies]
sensitive-rs = { version = "1.3", features = ["async-io"] }
#[tokio::main]
async fn main() -> std::io::Result<()> {
use sensitive_rs::Filter;
let mut filter = Filter::new();
filter.load_word_dict_async("dict/dict.txt").await?;
Ok(())
}
在 Cargo.toml 中添加:
[dependencies]
sensitive-rs = "1.3.0"
如果目标环境不适合引入 rayon(例如 WASM 或嵌入式场景),可以关闭默认功能:
[dependencies]
sensitive-rs = { version = "1.3.0", default-features = false }
use sensitive_rs::Filter;
fn main() {
let mut filter = Filter::new();
filter.add_words(&["rust", "filter", "敏感词"]);
let text = "hello rust, this is a filter demo 包含敏感词";
let found = filter.find_all(text);
println!("匹配到:{:?}", found);
let cleaned = filter.replace(text, '*');
println!("过滤后:{}", cleaned);
}
批量处理:
let texts = vec!["文本 1", "文本 2"];
let results = filter.find_all_batch(&texts);
分层匹配:
let layered = filter.find_all_layered("一些长文本");
流式处理大文件:
use std::fs::File;
use std::io::BufReader;
let reader = BufReader::new(File::open("large.txt")?);
let stream_results = filter.find_all_streaming(reader)?;
启用 cli 功能安装:
[dependencies]
sensitive-rs = { version = "1.3.0", features = ["cli"] }
或直接安装:
cargo install sensitive-rs --features cli
安装后可使用 sensitive 和 sensitive-rs 两个命令。
# 查找敏感词
sensitive check "含有赌博和色情内容"
# 验证文本(发现敏感词时 exit 1)
sensitive validate "干净文本"
# 替换敏感词
sensitive replace '*' "含有赌博内容"
# 移除敏感词
sensitive filter "含有赌博内容"
# 从文件读取
sensitive check --file input.txt
# 从 stdin 管道读取
echo "文本" | sensitive check
--dict <path> — 自定义词典文件--dict-all — 使用扩展词典(2.7 万词)--algorithm <algo> — 强制指定算法:aho-corasick、wumanber、regex--variant — 启用拼音和形近字变体检测--noise-pattern <regex> — 自定义噪声去除正则--json — JSON 输出格式--color — 强制彩色输出cargo run --example basic
cargo run --example batch
cargo run --example custom_dict
cargo run --example variant
cargo bench
详细文档请参阅 Documentation.
可以选择下列任意一种许可证:
除非您明确声明,否则您有意提交的任何贡献将根据 Apache-2.0 或 MIT 许可证的定义,按上述双重许可进行许可,不附加任何其他条款或条件。