本文整理汇总了Java中com.hankcs.hanlp.algoritm.ahocorasick.trie.Trie类的典型用法代码示例。如果您正苦于以下问题:Java Trie类的具体用法?Java Trie怎么用?Java Trie使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Trie类属于com.hankcs.hanlp.algoritm.ahocorasick.trie包,在下文中一共展示了Trie类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testAC
import com.hankcs.hanlp.algoritm.ahocorasick.trie.Trie; //导入依赖的package包/类
public void testAC() throws Exception
{
Trie trie = new Trie();
trie.addKeyword("hers");
trie.addKeyword("his");
trie.addKeyword("she");
trie.addKeyword("he");
Collection<Emit> emits = trie.parseText("ushers");
System.out.println(emits);
}
示例2: testTwoAC
import com.hankcs.hanlp.algoritm.ahocorasick.trie.Trie; //导入依赖的package包/类
public void testTwoAC() throws Exception
{
TreeMap<String, String> map = new TreeMap<String, String>();
IOUtil.LineIterator iterator = new IOUtil.LineIterator("data/dictionary/CoreNatureDictionary.mini.txt");
while (iterator.hasNext())
{
String line = iterator.next().split("\\s")[0];
map.put(line, line);
}
Trie trie = new Trie();
trie.addAllKeyword(map.keySet());
AhoCorasickDoubleArrayTrie<String> act = new AhoCorasickDoubleArrayTrie<String>();
act.build(map);
for (String key : map.keySet())
{
Collection<Emit> emits = trie.parseText(key);
Set<String> otherSet = new HashSet<String>();
for (Emit emit : emits)
{
otherSet.add(emit.getKeyword() + emit.getEnd());
}
List<AhoCorasickDoubleArrayTrie<String>.Hit<String>> entries = act.parseText(key);
Set<String> mySet = new HashSet<String>();
for (AhoCorasickDoubleArrayTrie<String>.Hit<String> entry : entries)
{
mySet.add(entry.value + (entry.end - 1));
}
assertEquals(otherSet, mySet);
}
}
示例3: testSegment
import com.hankcs.hanlp.algoritm.ahocorasick.trie.Trie; //导入依赖的package包/类
/**
* 测试构建和匹配,使用《我的团长我的团》.txt作为测试数据,并且判断匹配是否正确
* @throws Exception
*/
public void testSegment() throws Exception
{
TreeMap<String, String> map = new TreeMap<String, String>();
IOUtil.LineIterator iterator = new IOUtil.LineIterator("data/dictionary/CoreNatureDictionary.txt");
while (iterator.hasNext())
{
String line = iterator.next().split("\\s")[0];
map.put(line, line);
}
Trie trie = new Trie();
trie.addAllKeyword(map.keySet());
AhoCorasickDoubleArrayTrie<String> act = new AhoCorasickDoubleArrayTrie<String>();
long timeMillis = System.currentTimeMillis();
act.build(map);
System.out.println("构建耗时:" + (System.currentTimeMillis() - timeMillis) + " ms");
LinkedList<String> lineList = IOUtil.readLineList("D:\\Doc\\语料库\\《我的团长我的团》.txt");
timeMillis = System.currentTimeMillis();
for (String sentence : lineList)
{
// System.out.println(sentence);
List<AhoCorasickDoubleArrayTrie<String>.Hit<String>> entryList = act.parseText(sentence);
for (AhoCorasickDoubleArrayTrie<String>.Hit<String> entry : entryList)
{
int end = entry.end;
int start = entry.begin;
// System.out.printf("[%d:%d]=%s\n", start, end, entry.value);
assertEquals(sentence.substring(start, end), entry.value);
}
}
System.out.printf("%d ms\n", System.currentTimeMillis() - timeMillis);
}