char_trie

Text segmentation based on trie tree, High performance, support for custom dictionary

1 unstable release

0.1.0 Dec 28, 2023

#1180 in Text processing

MIT/Apache

21KB
458 lines

一个trie树结构,并且实现了,正向最大匹配和全词匹配

use char_trie::Trie;

pub fn main() {
    let mut trie = Trie::default();

    trie.insert("中国人", String::from("ud"));
    trie.insert("中国", String::from("ud"));
    trie.insert("", String::from("ud"));
    trie.insert("", String::from("ud"));
    trie.insert("", String::from("ud"));
    trie.insert("北京", String::from("ud"));
    trie.insert("天安门", String::from("ud"));
    trie.insert("天安", String::from("ud"));
    trie.insert("安门", String::from("ud"));
    trie.insert("", String::from("ud"));
    trie.insert("太阳", String::from("ud"));
    trie.insert("", String::from("ud"));

    let text = "我爱北京天安门,天安门上太阳升。我是中国人,我爱中国。";

    let c: Vec<_> = trie.iter_all(text).map(|t| t.0).collect();
    //["我", "爱", "北京", "天安", "天安门", "安门", "天安", "天安门", "安门", "上", "太阳", "升", "我", "是", "中国", "中国人", "我", "爱", "中国"]
    println!("{:?}", c);

    let c: Vec<_> = trie.iter_max(text).map(|t| t.0).collect();

    //["我", "爱", "北京", "天安门", "天安门", "上", "太阳", "升", "我", "是", "中国人", "我", "爱", "中国"]
    println!("{:?}", c);
}

```


性能:加载30万辞典。2121857字耗时 89.604137ms 

No runtime deps