-
Notifications
You must be signed in to change notification settings - Fork 3
/
stakk.h
78 lines (73 loc) · 2.16 KB
/
stakk.h
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
#ifndef STAKK_H
#define STAKK_H
#include "common.h"
#include "util.h"
#include "trie.h"
#include "connection.h"
namespace stakk {
class Stakk {
private:
WideTrie ≜
Connection &connection;
public:
//result entry
struct Entry {
wstring yomi, word;
int lid, rid, cost, rank;
Entry(vector<wstring> splited, wstring yomi_) {
yomi = splited[0];
wstringstream(splited[1]) >> lid;
wstringstream(splited[2]) >> rid;
wstringstream(splited[3]) >> cost;
word = splited[4];
}
wstring format() {
return yomi + L"\t" + word;
}
};
//initalize
Stakk(WideTrie &trie_, Connection &connection_)
: trie(trie_), connection(connection_)
{
}
//spell correct
void correct(wstring input, int threshold, vector<Entry> &results) {
WideTrie::Entries entries;
trie.fuzzy_search_ex(input, threshold, entries);
for (size_t i = 0; i < entries.size(); i++) {
wstring yomi = entries[i].key;
int distance = entries[i].distance;
for (size_t j = 0; j < entries[i].values.size(); j++) {
vector<wstring> splited = split_w(entries[i].values[j], L'\t');
Entry entry(splited, yomi);
int total = entry.cost + connection.get(0, entry.lid);
entry.rank = total + (threshold-distance) * 5000;
results.push_back(entry);
}
}
sort(results.begin(), results.end());
}
//predictive input
void predict(wstring input, vector<Entry> &results) {
WideTrie::Entries entries;
trie.predictive_search(input, L"", entries);
for (size_t i = 0; i < entries.size(); i++) {
wstring yomi = entries[i].key;
int length = yomi.length()-input.length();
for (size_t j = 0; j < entries[i].values.size(); j++) {
vector<wstring> splited = split_w(entries[i].values[j], L'\t');
Entry entry(splited, yomi);
int total = entry.cost + connection.get(0, entry.lid);
entry.rank = total - int(1000.0 * log(1+length));
results.push_back(entry);
}
}
sort(results.begin(), results.end());
}
};
//for result sort
bool operator<(const Stakk::Entry& left, const Stakk::Entry& right) {
return left.rank < right.rank ;
}
}
#endif