-
Notifications
You must be signed in to change notification settings - Fork 3
/
utrie.cc
76 lines (74 loc) · 1.88 KB
/
utrie.cc
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
#include "utrie.h"
using namespace stakk;
string format(Entries entries) {
string result;
for (size_t i = 0; i < entries.size(); i++) {
Entry entry = entries.at(i);
for (size_t j = 0; j < entry.values.size(); j++) {
stringstream distance;
distance << entry.distance;
result += entry.key + " "
+ distance.str() + " "
+ entry.values.at(j) + "\n";
}
}
return result;
}
int main(int argc, char *argv[]) {
int result;
string filename = "data/dictionary.txt";
string mode = "all";
bool reverse = false;
while((result = getopt(argc, argv, "f:m:r")) != -1) {
switch(result) {
case 'f':
filename = optarg;
break;
case 'r':
reverse = true;
break;
case 'm':
mode = optarg;
break;
}
}
cout << "inserting.." << endl;
UTable utable;
UTrie trie(utable);
int field = reverse ? 4 : 0;
if (!trie.load(filename, field, '\t')) {
cout << filename << " is not found." << endl;
exit(0);
}
cout << "input kana:" << endl;
string input;
while (cin >> input) {
if (mode == "all" || mode == "search") {
vector<string> *result;
result = trie.search(input);
cout << "search:" << endl;
if (result) {
cout << join(*result, '\n') << endl;
}
}
if (mode == "all" || mode == "common") {
Entries results;
trie.common_prefix_search(input, results);
cout << "common:" << endl;
cout << format(results);
}
if (mode == "all" || mode == "predict") {
Entries results;
trie.predictive_search(input, results);
cout << "predict:" << endl;
cout << format(results);
}
if (mode == "all" || mode == "fuzzy") {
Entries results;
trie.fuzzy_search_ex(input, 1, results);
cout << "fuzzy:" << endl;
cout << format(results);
}
}
return 0;
}