forked from hankcs/HanLP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestBintrie.java
More file actions
135 lines (127 loc) · 3.76 KB
/
TestBintrie.java
File metadata and controls
135 lines (127 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.hankcs.test.seg;
import com.hankcs.hanlp.collection.trie.bintrie.BinTrie;
import com.hankcs.hanlp.seg.Other.LongestBinSegmentToy;
import junit.framework.TestCase;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class TestBintrie extends TestCase {
public void testPut() throws Exception {
{
BinTrie<String> binTrie = new BinTrie<String>();
// binTrie.put("你好", "hello");
// binTrie.put("我好", "fine");
// binTrie.put("你坏", "bad");
// System.out.println(binTrie.hasKey("我好"));
// System.out.println(binTrie.get("我好"));
// System.out.println(binTrie.hasKey("大家好"));
// System.out.println(binTrie.get("大家好"));
List<String> wordList = new ArrayList<String>();
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(
"D:\\JavaProjects\\TireSpeed\\data\\WordList.txt")));
String line;
while ((line = br.readLine()) != null) {
wordList.add(line);
binTrie.put(line, "值:" + line);
}
br.close();
// binTrie.remove("天才");
// for (String w : wordList)
// {
// String value = binTrie.get(w);
// if (!("值:" + w).equals(value))
// {
// throw new RuntimeException("错了:" + w + value);
// }
// // else
// // {
// // System.out.println(value);
// // }
// }
// System.out.println(binTrie.hasKey("好孩子"));
LongestBinSegmentToy<String> segmenter = new LongestBinSegmentToy<String>(
binTrie);
System.out.println(segmenter.seg("我们都是好孩子"));
// Map.Entry<String, String> entry;
// while ((entry = segmenter.next()) != null)
// {
// System.out.println(entry.getKey() + " - " +
// (segmenter.getOffset() - entry.getKey().length()));
// }
}
}
// public void testSmartVsNormal() throws Exception
// {
// BinTrie<String> trieNormal = new BinTrie<>();
// SmartBinTrie<String> trieSmart = new SmartBinTrie<>();
// BinTrie<CoreDictionary.Attribute> dictionary =
// CustomDictionary.getBinTrie();
// List<String> wordList = new LinkedList<>();
// for (Map.Entry<String, CoreDictionary.Attribute> entry :
// dictionary.entrySet())
// {
// wordList.add(entry.getKey());
// }
// // 似乎内存会影响速度
// {
// BinTrie<String> trie = new BinTrie<>();
// for (String word : wordList)
// {
// trie.put(word, word);
// }
// }
//
// long start;
//
// start = System.currentTimeMillis();
// for (String word : wordList)
// {
// trieNormal.put(word, word);
// }
// System.out.printf("trieNormal首次插入耗时:%dms%n", System.currentTimeMillis() -
// start);
//
// start = System.currentTimeMillis();
// for (String word : wordList)
// {
// trieSmart.put(word, word);
// }
// System.out.printf("trieSmart首次插入耗时:%dms%n", System.currentTimeMillis() -
// start);
//
// start = System.currentTimeMillis();
// for (String word : wordList)
// {
// trieNormal.put(word, word);
// }
// System.out.printf("trieNormal再次插入耗时:%dms%n", System.currentTimeMillis() -
// start);
//
// start = System.currentTimeMillis();
// for (String word : wordList)
// {
// trieSmart.put(word, word);
// }
// System.out.printf("trieSmart再次插入耗时:%dms%n", System.currentTimeMillis() -
// start);
//
// start = System.currentTimeMillis();
// for (String word : wordList)
// {
// assertEquals(word, trieNormal.get(word));
// }
// System.out.printf("trieNormal查询耗时:%dms%n", System.currentTimeMillis() -
// start);
//
// start = System.currentTimeMillis();
// for (String word : wordList)
// {
// assertEquals(word, trieSmart.get(word));
// }
// System.out.printf("trieSmart查询耗时:%dms%n", System.currentTimeMillis() -
// start);
// }
}