forked from rime/librime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry_collector.cc
251 lines (237 loc) · 7.63 KB
/
entry_collector.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
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
//
// Copyright RIME Developers
// Distributed under the BSD License
//
// 2011-11-27 GONG Chen <chen.sst@gmail.com>
//
#include <fstream>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <rime/dict/dict_settings.h>
#include <rime/dict/entry_collector.h>
#include <rime/dict/preset_vocabulary.h>
namespace rime {
EntryCollector::EntryCollector() {}
EntryCollector::EntryCollector(Syllabary&& fixed_syllabary)
: syllabary(std::move(fixed_syllabary)),
build_syllabary(false) {}
EntryCollector::~EntryCollector() {}
void EntryCollector::Configure(DictSettings* settings) {
if (settings->use_preset_vocabulary()) {
LoadPresetVocabulary(settings);
}
if (settings->use_rule_based_encoder()) {
encoder.reset(new TableEncoder(this));
}
else {
encoder.reset(new ScriptEncoder(this));
}
encoder->LoadSettings(settings);
}
void EntryCollector::Collect(const vector<string>& dict_files) {
for (const string& dict_file : dict_files) {
Collect(dict_file);
}
Finish();
}
void EntryCollector::LoadPresetVocabulary(DictSettings* settings) {
auto vocabulary = settings->vocabulary();
LOG(INFO) << "loading preset vocabulary: " << vocabulary;
preset_vocabulary.reset(new PresetVocabulary(vocabulary));
if (preset_vocabulary) {
if (settings->max_phrase_length() > 0)
preset_vocabulary->set_max_phrase_length(settings->max_phrase_length());
if (settings->min_phrase_weight() > 0)
preset_vocabulary->set_min_phrase_weight(settings->min_phrase_weight());
}
}
void EntryCollector::Collect(const string& dict_file) {
LOG(INFO) << "collecting entries from " << dict_file;
// read table
std::ifstream fin(dict_file.c_str());
DictSettings settings;
if (!settings.LoadDictHeader(fin)) {
LOG(ERROR) << "missing dict settings.";
return;
}
// column definitions
int text_column = settings.GetColumnIndex("text");
int code_column = settings.GetColumnIndex("code");
int weight_column = settings.GetColumnIndex("weight");
int stem_column = settings.GetColumnIndex("stem");
if (text_column == -1) {
LOG(ERROR) << "missing text column definition.";
return;
}
bool enable_comment = true;
string line;
while (getline(fin, line)) {
boost::algorithm::trim_right(line);
// skip empty lines and comments
if (line.empty()) continue;
if (enable_comment && line[0] == '#') {
if (line == "# no comment") {
// a "# no comment" line disables further comments
enable_comment = false;
}
continue;
}
// read a dict entry
vector<string> row;
boost::algorithm::split(row, line,
boost::algorithm::is_any_of("\t"));
int num_columns = static_cast<int>(row.size());
if (num_columns <= text_column || row[text_column].empty()) {
LOG(WARNING) << "Missing entry text at #" << num_entries << ".";
continue;
}
const auto& word(row[text_column]);
string code_str;
string weight_str;
string stem_str;
if (code_column != -1 &&
num_columns > code_column && !row[code_column].empty())
code_str = row[code_column];
if (weight_column != -1 &&
num_columns > weight_column && !row[weight_column].empty())
weight_str = row[weight_column];
if (stem_column != -1 &&
num_columns > stem_column && !row[stem_column].empty())
stem_str = row[stem_column];
// collect entry
collection.insert(word);
if (!code_str.empty()) {
CreateEntry(word, code_str, weight_str);
}
else {
encode_queue.push({word, weight_str});
}
if (!stem_str.empty() && !code_str.empty()) {
DLOG(INFO) << "add stem '" << word << "': "
<< "[" << code_str << "] = [" << stem_str << "]";
stems[word].insert(stem_str);
}
}
fin.close();
LOG(INFO) << "Pass 1: total " << num_entries << " entries collected.";
LOG(INFO) << "num unique syllables: " << syllabary.size();
LOG(INFO) << "num of entries to encode: " << encode_queue.size();
}
void EntryCollector::Finish() {
while (!encode_queue.empty()) {
const auto& phrase(encode_queue.front().first);
const auto& weight_str(encode_queue.front().second);
if (!encoder->EncodePhrase(phrase, weight_str)) {
LOG(ERROR) << "Encode failure: '" << phrase << "'.";
}
encode_queue.pop();
}
LOG(INFO) << "Pass 2: total " << num_entries << " entries collected.";
if (preset_vocabulary) {
preset_vocabulary->Reset();
string phrase, weight_str;
while (preset_vocabulary->GetNextEntry(&phrase, &weight_str)) {
if (collection.find(phrase) != collection.end())
continue;
if (!encoder->EncodePhrase(phrase, weight_str)) {
LOG(WARNING) << "Encode failure: '" << phrase << "'.";
}
}
}
LOG(INFO) << "Pass 3: total " << num_entries << " entries collected.";
}
void EntryCollector::CreateEntry(const string &word,
const string &code_str,
const string &weight_str) {
RawDictEntry e;
e.raw_code.FromString(code_str);
e.text = word;
e.weight = 0.0;
bool scaled = boost::ends_with(weight_str, "%");
if ((weight_str.empty() || scaled) && preset_vocabulary) {
preset_vocabulary->GetWeightForEntry(e.text, &e.weight);
}
if (scaled) {
double percentage = 100.0;
try {
percentage = boost::lexical_cast<double>(
weight_str.substr(0, weight_str.length() - 1));
}
catch (...) {
LOG(WARNING) << "invalid entry definition at #" << num_entries << ".";
percentage = 100.0;
}
e.weight *= percentage / 100.0;
}
else if (!weight_str.empty()) { // absolute weight
try {
e.weight = boost::lexical_cast<double>(weight_str);
}
catch (...) {
LOG(WARNING) << "invalid entry definition at #" << num_entries << ".";
e.weight = 0.0;
}
}
// learn new syllables, or check if syllables are in the fixed syllabary.
for (const string& s : e.raw_code) {
if (syllabary.find(s) == syllabary.end()) {
if (build_syllabary) {
syllabary.insert(s);
} else {
LOG(ERROR) << "dropping entry '" << e.text
<< "' with invalid syllable: " << s;
return;
}
}
}
// learn new word
bool is_word = (e.raw_code.size() == 1);
if (is_word) {
if (words[e.text].find(code_str) != words[e.text].end()) {
LOG(WARNING) << "duplicate word definition '"
<< e.text << "': [" << code_str << "].";
return;
}
words[e.text][code_str] += e.weight;
total_weight[e.text] += e.weight;
}
entries.emplace_back(New<RawDictEntry>(e));
++num_entries;
}
bool EntryCollector::TranslateWord(const string& word,
vector<string>* result) {
ReverseLookupTable::const_iterator s = stems.find(word);
if (s != stems.end()) {
for (const string& stem : s->second) {
result->push_back(stem);
}
return true;
}
WordMap::const_iterator w = words.find(word);
if (w != words.end()) {
for (const auto& v : w->second) {
const double kMinimalWeight = 0.05; // 5%
double min_weight = total_weight[word] * kMinimalWeight;
if (v.second < min_weight)
continue;
result->push_back(v.first);
}
return true;
}
return false;
}
void EntryCollector::Dump(const string& file_name) const {
std::ofstream out(file_name.c_str());
out << "# syllabary:" << std::endl;
for (const string& syllable : syllabary) {
out << "# - " << syllable << std::endl;
}
out << std::endl;
for (const auto &e : entries) {
out << e->text << '\t'
<< e->raw_code.ToString() << '\t'
<< e->weight << std::endl;
}
out.close();
}
} // namespace rime