-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPinInTest.java
More file actions
331 lines (305 loc) · 12.5 KB
/
PinInTest.java
File metadata and controls
331 lines (305 loc) · 12.5 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package me.towdium.pinin;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
import me.towdium.pinin.elements.Char;
import me.towdium.pinin.elements.Pinyin;
import me.towdium.pinin.searchers.CachedSearcher;
import me.towdium.pinin.searchers.Searcher;
import me.towdium.pinin.searchers.Searcher.Logic;
import me.towdium.pinin.searchers.SimpleSearcher;
import me.towdium.pinin.searchers.TreeSearcher;
import me.towdium.pinin.utils.PinyinFormat;
import org.junit.jupiter.api.Test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import static me.towdium.pinin.Keyboard.*;
import static me.towdium.pinin.searchers.Searcher.Logic.CONTAIN;
import static me.towdium.pinin.searchers.Searcher.Logic.EQUAL;
public class PinInTest {
@Test
@SuppressWarnings({"unused", "MismatchedQueryAndUpdateOfCollection"})
public void performance() throws IOException {
List<String> search = new ArrayList<>();
search.add("boli");
search.add("yangmao");
search.add("hongse");
List<Function<Logic, Searcher<Integer>>> funcs = new ArrayList<>();
funcs.add(l -> new TreeSearcher<>(l, new PinIn()));
funcs.add(l -> new CachedSearcher<>(l, new PinIn()));
funcs.add(l -> new SimpleSearcher<>(l, new PinIn()));
String[] sources = new String[]{"small", "large"};
for (Logic j : Logic.values()) {
for (String k : sources) {
List<String> data = loadTestData(k);
boolean reduced = data.size() > 100000;
System.out.print("Logic: " + j.toString().toLowerCase() + ", source: " + k);
float contains = time(reduced ? 10 : 100, search, s -> {
IntSet result = new IntOpenHashSet();
for (int i = 0; i < data.size(); i++) {
String in = data.get(i);
if (j.raw(in, s)) result.add(i);
}
});
System.out.print(", contains search: " + String.format("%.2f", contains));
PinIn p = new PinIn();
float traverse = time(reduced ? 10 : 100, search, s -> {
IntSet result = new IntOpenHashSet();
for (int i = 0; i < data.size(); i++) {
String in = data.get(i);
if (j.test(p, in, s)) result.add(i);
}
});
System.out.println(", loop search: " + String.format("%.1f", traverse));
for (Function<Logic, Searcher<Integer>> i : funcs) {
performance(j, data, search, i);
}
}
}
}
private float time(int repeat, Runnable exec) {
long time = System.currentTimeMillis();
for (int i = 0; i < repeat; i++) {
exec.run();
}
return (System.currentTimeMillis() - time) / (float) repeat;
}
private <T> float time(int repeat, Collection<T> objs, Consumer<T> consumer) {
long time = System.currentTimeMillis();
for (int j = 0; j < repeat; j++) {
for (T i: objs) {
consumer.accept(i);
}
}
return (System.currentTimeMillis() - time) / (float) repeat / objs.size();
}
private List<String> loadTestData(String source) throws IOException {
String line;
List<String> data = new ArrayList<>();
InputStream is = PinInTest.class.getResourceAsStream(source + ".txt");
assert is != null;
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
if (line.isEmpty()) continue;
data.add(line);
}
return data;
}
private void performance(Logic logic, List<String> texts, List<String> tokens,
Function<Logic, Searcher<Integer>> func) {
long start = System.currentTimeMillis();
final Searcher<Integer> searcher = func.apply(logic);
for (int j = 0; j < texts.size(); j++) searcher.put(texts.get(j), j);
boolean reduced = texts.size() > 100000;
System.out.print(" " + searcher.getClass().getSimpleName());
int loop = reduced ? 10 : 100;
if (searcher instanceof TreeSearcher) loop /= 5;
float construct = time(loop, () -> {
Searcher<Integer> temp = func.apply(logic);
for (int j = 0; j < texts.size(); j++) temp.put(texts.get(j), j);
});
System.out.print(": construction: " + String.format("%.1f", construct));
if (searcher instanceof CachedSearcher) {
float warm = time(reduced ? 10 : 100, tokens, s -> {
List<Integer> is = searcher.search(s);
((CachedSearcher<Integer>) searcher).reset();
});
System.out.print(", warmup: " + String.format("%.1f", warm));
}
loop = reduced ? 1000 : 10000;
if (searcher instanceof SimpleSearcher) loop /= 100;
float search = time(loop, tokens, (s) -> {
List<Integer> is = searcher.search(s);
});
System.out.print(", accelerated: " + String.format("%.3f", search));
System.out.println(", total: " + (System.currentTimeMillis() - start));
}
@Test
public void quanpin() {
PinIn p = new PinIn();
assert p.contains("测试文本", "ceshiwenben");
assert p.contains("测试文本", "ceshiwenbe");
assert p.contains("测试文本", "ceshiwben");
assert p.contains("测试文本", "ce4shi4wb");
assert !p.contains("测试文本", "ce2shi4wb");
assert p.contains("合金炉", "hejinlu");
assert p.contains("洗矿场", "xikuangchang");
assert p.contains("流体", "liuti");
assert p.contains("轰20", "hong2");
assert p.contains("hong2", "hong2");
assert !p.begins("测", "ce4a");
assert !p.begins("", "a");
assert p.contains("石头", "stou");
assert p.contains("安全", "aquan");
assert p.contains("昂扬", "ayang");
assert !p.contains("昂扬", "anyang");
assert p.contains("昂扬", "angyang");
}
@Test
public void daqian() {
PinIn p = new PinIn().config().keyboard(DAQIAN).commit();
assert p.contains("测试文本", "hk4g4jp61p3");
assert p.contains("测试文本", "hkgjp1");
assert p.contains("錫", "vu6");
assert p.contains("鑽石", "yj0");
assert p.contains("物質", "j456");
assert p.contains("腳手架", "rul3g.3ru84");
assert p.contains("鵝", "k6");
assert p.contains("葉", "u,4");
assert p.contains("共同", "ej/wj/");
}
@Test
public void xiaohe() {
PinIn p = new PinIn().config().keyboard(XIAOHE).commit();
assert p.contains("测试文本", "ceuiwfbf");
assert p.contains("测试文本", "ceuiwf2");
assert !p.contains("测试文本", "ceuiw2");
assert p.contains("合金炉", "hej");
assert p.contains("洗矿场", "xikl4");
assert p.contains("月球", "ytqq");
}
@Test
public void ziranma() {
PinIn p = new PinIn().config().keyboard(ZIRANMA).commit();
assert p.contains("测试文本", "ceuiwfbf");
assert p.contains("测试文本", "ceuiwf2");
assert !p.contains("测试文本", "ceuiw2");
assert p.contains("合金炉", "hej");
assert p.contains("洗矿场", "xikd4");
assert p.contains("月球", "ytqq");
assert p.contains("安全", "anqr");
}
@Test
public void tree() {
TreeSearcher<Integer> tree = new TreeSearcher<>(CONTAIN, new PinIn());
tree.put("测试文本", 1);
tree.put("测试切分", 5);
tree.put("测试切分文本", 6);
tree.put("合金炉", 2);
tree.put("洗矿场", 3);
tree.put("流体", 4);
tree.put("轰20", 7);
tree.put("hong2", 8);
Collection<Integer> s;
s = tree.search("ceshiwenben");
assert s.size() == 1 && s.contains(1);
s = tree.search("ceshiwenbe");
assert s.size() == 1 && s.contains(1);
s = tree.search("ceshiwben");
assert s.size() == 1 && s.contains(1);
s = tree.search("ce4shi4wb");
assert s.size() == 1 && s.contains(1);
s = tree.search("ce2shi4wb");
assert s.size() == 0;
s = tree.search("hejinlu");
assert s.size() == 1 && s.contains(2);
s = tree.search("xikuangchang");
assert s.size() == 1 && s.contains(3);
s = tree.search("liuti");
assert s.size() == 1 && s.contains(4);
s = tree.search("ceshi");
assert s.size() == 3 && s.contains(1) && s.contains(5);
s = tree.search("ceshiqiefen");
assert s.size() == 2 && s.contains(5);
s = tree.search("ceshiqiefenw");
assert s.size() == 1 && s.contains(6);
s = tree.search("hong2");
assert s.contains(7) && s.contains(8);
}
@Test
public void context() {
PinIn p = new PinIn();
TreeSearcher<Integer> tree = new TreeSearcher<>(CONTAIN, p);
tree.put("测试文本", 0);
tree.put("测试文字", 3);
Collection<Integer> s;
s = tree.search("ce4shi4wb");
assert s.size() == 1 && s.contains(0);
s = tree.search("ce4shw");
assert s.size() == 2;
s = tree.search("ce4sw");
assert s.size() == 2;
s = tree.search("ce4siw");
assert s.isEmpty();
p.config().fSh2S(true).commit();
s = tree.search("ce4siw");
assert s.size() == 2;
p.config().fSh2S(false).keyboard(DAQIAN).commit();
s = tree.search("hk4g4jp61p3");
assert s.size() == 1;
s = tree.search("ce4shi4wb");
assert s.isEmpty();
}
@Test
public void full() {
List<Searcher<Integer>> ss = new ArrayList<>();
ss.add(new TreeSearcher<>(EQUAL, new PinIn()));
ss.add(new SimpleSearcher<>(EQUAL, new PinIn()));
for (Searcher<Integer> s : ss) {
s.put("测试文本", 1);
s.put("测试切分", 5);
s.put("测试切分文本", 6);
s.put("合金炉", 2);
s.put("洗矿场", 3);
s.put("流体", 4);
s.put("轰20", 7);
s.put("hong2", 8);
s.put("月球", 9);
s.put("汉化", 10);
s.put("喊话", 11);
List<Integer> is;
is = s.search("hong2");
assert is.size() == 1 && is.contains(8);
is = s.search("hong20");
assert is.size() == 1 && is.contains(7);
is = s.search("ceshqf");
assert is.size() == 1 && is.contains(5);
is = s.search("ceshqfw");
assert is.isEmpty();
is = s.search("hh");
assert is.size() == 2 && is.contains(10) && is.contains(11);
is = s.search("hhu");
assert is.isEmpty();
}
}
@Test
public void format() {
PinIn pi = new PinIn();
Char ch = pi.getChar('圆');
Pinyin py = ch.pinyins()[0];
assert PinyinFormat.NUMBER.format(py).equals("yuan2");
assert PinyinFormat.RAW.format(py).equals("yuan");
assert PinyinFormat.UNICODE.format(py).equals("yuán");
assert PinyinFormat.PHONETIC.format(py).equals("ㄩㄢˊ");
pi.config().format(PinyinFormat.PHONETIC).commit();
assert pi.format(pi.getPinyin("le0")).equals("˙ㄌㄜ");
}
@Test
public void dict() {
PinIn p = new PinIn();
TreeSearcher<Integer> searcher = new TreeSearcher<>(CONTAIN, p);
searcher.put("\uE900锭", 0);
assert !searcher.search("lu2d").contains(0);
assert !p.contains("\uE900", "lu2");
p = new PinIn(new DictLoader.Default(){
@Override
public void load(BiConsumer<Character, String[]> feed) {
super.load(feed);
feed.accept('\uE900', new String[]{"lu2"});
}
});
searcher = new TreeSearcher<>(CONTAIN, p);
searcher.put("\uE900锭", 0);
assert searcher.search("lu2d").contains(0);
assert p.contains("\uE900", "lu2");
}
}