2个版本
0.1.2 | 2021年6月11日 |
---|---|
0.1.0 | 2021年6月7日 |
#1681 in 算法
150KB
222 行
word_sensitive
这是一个用于敏感字符串匹配的库,实现语言为Rust,使用的算法是AC。
如何使用?
- 向字典树添加关键词
- 构建字典树
- 查询(从文本中匹配关键词)
手动添加关键词
use word_sensitive::trie;
let mut tree = trie::Trie::default();
tree.add_key_word("aaa".as_bytes().to_vec());
tree.add_key_word("aab".as_bytes().to_vec());
tree.add_key_word("aac".as_bytes().to_vec());
tree.add_key_word("aba".as_bytes().to_vec());
tree.add_key_word("abb".as_bytes().to_vec());
tree.add_key_word("abc".as_bytes().to_vec());
tree.add_key_word("aca".as_bytes().to_vec());
tree.add_key_word("acb".as_bytes().to_vec());
tree.add_key_word("acc".as_bytes().to_vec());
tree.add_key_word("baa".as_bytes().to_vec());
tree.add_key_word("bab".as_bytes().to_vec());
tree.add_key_word("bac".as_bytes().to_vec());
tree.add_key_word("bba".as_bytes().to_vec());
tree.add_key_word("bbb".as_bytes().to_vec());
tree.add_key_word("bbc".as_bytes().to_vec());
tree.add_key_word("bca".as_bytes().to_vec());
tree.add_key_word("bcb".as_bytes().to_vec());
tree.add_key_word("bcc".as_bytes().to_vec());
tree.add_key_word("caa".as_bytes().to_vec());
tree.add_key_word("cab".as_bytes().to_vec());
tree.add_key_word("cac".as_bytes().to_vec());
tree.add_key_word("cba".as_bytes().to_vec());
tree.add_key_word("cbb".as_bytes().to_vec());
tree.add_key_word("cbc".as_bytes().to_vec());
tree.add_key_word("cca".as_bytes().to_vec());
tree.add_key_word("ccb".as_bytes().to_vec());
tree.add_key_word("ccc".as_bytes().to_vec());
tree.build();
let matches = tree.query("abcabcbcca".as_bytes().as_ref());
assert_eq!(matches[0], "abc".as_bytes().as_ref());
assert_eq!(matches[1], "bca".as_bytes().as_ref());
assert_eq!(matches[2], "cab".as_bytes().as_ref());
assert_eq!(matches[3], "abc".as_bytes().as_ref());
assert_eq!(matches[4], "bcb".as_bytes().as_ref());
assert_eq!(matches[5], "cbc".as_bytes().as_ref());
assert_eq!(matches[6], "bcc".as_bytes().as_ref());
assert_eq!(matches[7], "cca".as_bytes().as_ref());
从文件中添加关键词
use word_sensitive::trie;
let mut tree = trie::Trie::default();
tree.add_key_word_from_file("key_words/keywords.txt").unwrap();
tree.build();
let matches = tree.query("回民吃猪肉".as_bytes().as_ref());
assert_eq!(matches[0], "回民".as_bytes().as_ref());
assert_eq!(matches[1], "回民吃猪肉".as_bytes().as_ref());