forked from phelrine/stakk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uconverter.h
138 lines (132 loc) · 4.07 KB
/
uconverter.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
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
#ifndef CONVERTER_H
#define CONVERTER_H
#include "common.h"
#include "util.h"
#include "utrie.h"
#include "connection.h"
namespace stakk {
class Converter {
private:
UTrie ≜
Connection &connection;
Definition &definition;
public:
//lattice node
struct Node {
string yomi, word, key;
unsigned short lid, rid, cost;
unsigned int index, total, back;
Node(vector<string> splited, string key_) {
yomi = splited[0];
stringstream(splited[1]) >> lid;
stringstream(splited[2]) >> rid;
stringstream(splited[3]) >> cost;
word = splited[4];
key = key_;
index = back = 0;
total = INT_MAX / 2 - 1;
}
Node() {
word = yomi = key = "S";
lid = rid = cost = index = back = total = 0;
}
string format() {
stringstream s;
s << index << " " << cost << " " << total << " " << back
<< " " << lid << " " << rid;
return yomi + " " + word + " " + s.str();
}
};
const Node bos;
//initialize references
Converter(UTrie &trie_, Connection &connection_, Definition &definition_)
: trie(trie_), connection(connection_), definition(definition_){}
//convert from kana to kanji
vector<Node> convert(string input, bool debug=false) {
//create lattice
vector<vector<Node> > lattice;
lattice.clear();
lattice.resize(input.length() + 2);
lattice[0].push_back(bos);
lattice[lattice.size()-1].push_back(bos);
//common prefix search
for (size_t i = 0; i < input.length(); i++) {
Entries entries;
string query = input.substr(i);
trie.common_prefix_search(query, entries);
for (size_t j = 0; j < entries.size(); j++) {
for (size_t k = 0; k < entries[j].values.size(); k++) {
Node node(split(entries[j].values[k], '\t'), entries[j].key);
int index = i + entries[j].key.length();
node.index = lattice[index].size();
lattice[index].push_back(node);
}
}
}
//forward search
for (size_t i = 0; i < lattice.size(); i++) {
for (size_t j = 0; j < lattice[i].size(); j++) {
int index = i - lattice[i][j].key.length();
if (index < 0 || lattice[index].size() == 0)
continue;
int best_score = -1, best_index = -1;
for (size_t k = 0; k < lattice[index].size(); k++) {
unsigned short lid = lattice[index][k].rid;
unsigned short rid = lattice[i][j].lid;
int transition = connection.get(lid, rid);
int score = lattice[index][k].total + transition;
if (best_score == -1 || score < best_score) {
best_index = lattice[index][k].index;
best_score = score;
}
}
lattice[i][j].total = lattice[i][j].cost + best_score;
lattice[i][j].back = best_index;
}
}
//debug
if (debug) {
for (size_t i = 0; i < lattice.size(); i++) {
for (size_t j = 0; j < lattice[i].size(); j++) {
cout << lattice[i][j].format() << endl;
}
}
}
//back trace
vector<Node> nodes;
int current = lattice.size()-2;
int position = lattice[lattice.size()-1][0].back;
while (current > 0 && lattice[current].size() > 0) {
Node &node = lattice[current][position];
nodes.insert(nodes.begin(), node);
position = node.back;
current -= node.key.length();
}
return nodes;
}
string format(vector<Node> nodes, string output) {
string result = "";
if (output == "mecab") {
for (size_t i = 0; i < nodes.size(); i++) {
Node &node = nodes[i];
string lid = definition.get(node.lid);
string rid = definition.get(node.rid);
result += node.word + "\t" + node.yomi + "\t" + lid + "\t" + rid + "\n";
}
result += "EOS\n";
} else if (output == "wakati") {
for (size_t i = 0; i < nodes.size(); i++) {
result += nodes[i].word + " ";
}
result += "\n";
} else if (output == "yomi") {
for (size_t i = 0; i < nodes.size(); i++) {
result += nodes[i].yomi;
}
result += "\n";
}
return result;
}
};
}
#endif