sensitive-rs

Sensitive-rs

中文 English

Build crates.io docs.rs License Crates.io

Sensitive-rs 是一个用于敏感词查找、验证、过滤和替换的 Rust 库。它提供了高效的算法来处理敏感词,适用于多种应用场景。

功能

安装

Cargo.toml 中添加以下依赖:

Cargo.toml

[dependencies]
sensitive-rs = "0.1"

快速开始

use sensitive_rs::Trie;
use std::sync::Arc;
use std::thread;
use std::time::Duration;

fn main() {
    // 创建一个新的 Trie 过滤器
    let filter = Trie::new();
    filter.add_word("bad");
    filter.add_word("worse");

    // 查找敏感词
    let result = filter.find_all("This is bad and worse.");
    assert_eq!(result, vec!["bad", "worse"]);

    // 并发访问示例
    let filter = Arc::new(Trie::new());
    filter.add_word("concurrent");
    filter.add_word("test");

    let mut handles = vec![];

    for i in 0..10 {
        let filter_clone = filter.clone();
        handles.push(thread::spawn(move || {
            assert_eq!(filter_clone.find_in("This is a concurrent test."), Some("concurrent".to_string()));
            filter_clone.add_word(&format!("thread{}", i));

            // 给一些时间让其他线程也能添加它们的词
            thread::sleep(Duration::from_millis(10));

            let result = filter_clone.find_all("Thread test is concurrent.");
            assert!(result.contains(&"test".to_string()));
            assert!(result.contains(&"concurrent".to_string()));
        }));
    }

    for handle in handles {
        handle.join().unwrap();
    }

    // 删除敏感词示例
    let filter = Trie::new();
    filter.add_word("bad");
    filter.add_word("badge");
    filter.add_word("badger");

    assert_eq!(filter.find_in("This is bad."), Some("bad".to_string()));
    assert_eq!(filter.find_in("This is a badge."), Some("badge".to_string()));
    assert_eq!(filter.find_in("This is a badger."), Some("badger".to_string()));

    // 删除 "bad"
    assert!(filter.del_word("bad"));
    assert_eq!(filter.find_in("This is bad."), None);
    assert_eq!(filter.find_in("This is a badge."), Some("badge".to_string()));
    assert_eq!(filter.find_in("This is a badger."), Some("badger".to_string()));
}

以下是一些使用 Sensitive-rs Filter 的示例代码

use sensitive_rs::Filter;

fn main() {
    // 创建一个新的 Filter 过滤器
    let mut filter = Filter::new();
    filter.add_word("bad");
    filter.add_word("worse");

    // 查找敏感词
    let result = filter.find_in("This is bad.");
    assert_eq!(result, (true, "bad".to_string()));

    // 验证文本
    let result = filter.validate("This is worse.");
    assert_eq!(result, (true, "worse".to_string()));

    // 过滤敏感词
    let filtered_text = filter.filter("This is bad and worse.");
    assert_eq!(filtered_text, "This is  and .");

    // 替换敏感词
    let replaced_text = filter.replace("This is bad and worse.", '*');
    assert_eq!(replaced_text, "This is *** and *****.");
}

文档 详细文档请参阅 Documentation.

许可证

可以选择下列任意一种许可证:

贡献

除非您明确声明,否则您有意提交的任何贡献将根据 Apache-2.0 或 MIT 许可证的定义,按上述双重许可进行许可,不附加任何其他条款或条件。