-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex-word-list.cc
426 lines (398 loc) · 12.8 KB
/
index-word-list.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#include <math.h>
#include <stdio.h>
#include "lufz-util.h"
using namespace std;
namespace lufz {
void AddKeyCounts(
const string& normalized,
int count,
LufzUtil* util,
map<string, int>* indexing_key_counts) {
const string key = util->Key(normalized);
const vector<string> key_parts = util->PartsOf(key, false);
int len = key_parts.size();
if (len > WILDIZE_ALL_BEYOND) {
len = WILDIZE_ALL_BEYOND;
}
for (int pattern = 0; pattern < (1 << len); pattern++) {
vector<string> key_variant_parts = key_parts;
for (int i = 0; i < len; i++) {
if (pattern & (1 << i)) {
key_variant_parts[i] = "?";
}
}
string key_variant = util->Join(key_variant_parts);
(*indexing_key_counts)[key_variant] += count;
}
}
void AddKeys(
const string& normalized,
const set<string>& indexing_keys,
const vector<int>& lex_indices,
LufzUtil* util,
map<string, set<int>>* index) {
const string key = util->Key(normalized);
const vector<string> key_parts = util->PartsOf(key, false);
int len = key_parts.size();
if (len > WILDIZE_ALL_BEYOND) {
len = WILDIZE_ALL_BEYOND;
}
for (int pattern = 0; pattern < (1 << len); pattern++) {
vector<string> key_variant_parts = key_parts;
for (int i = 0; i < len; i++) {
if (pattern & (1 << i)) {
key_variant_parts[i] = "?";
}
}
string key_variant = util->Join(key_variant_parts);
if (indexing_keys.count(key_variant) == 0) {
continue;
}
for (int li : lex_indices) {
if ((*index)[key_variant].count(li) > 0) {
fprintf(stderr, "Hmm. For normalized=[%s], key=[%s], key_variant=[%s], we already have index %d\n",
normalized.c_str(), key.c_str(), key_variant.c_str(), li);
}
(*index)[key_variant].insert(li);
}
}
}
void AddAgmKey(
const string& normalized,
const vector<int>& lex_indices,
LufzUtil* util,
vector<vector<int>>* agm_shards) {
string key = util->AgmKey(normalized);
int shard = util->IndexShard(key, AGM_INDEX_SHARDS);
for (int li : lex_indices) {
(*agm_shards)[shard].push_back(li);
}
}
/**
* Read a pronunciations file (such as the file derived from
* http://svn.code.sf.net/p/cmusphinx/code/trunk/cmudict/cmudict-0.7b) and add
* pronunciations (removing stress markers) to lexicon.
* Format:
* <entry>\t<phones>
* Example:
* BANANA\tB AH N AE N AH
* The phones can be in ARPAbet format (as in CMUdict), or in IPA format.
*/
bool AddPronunciations(
LufzUtil* util,
LufzUtil* phone_util,
const char* phones_file,
Lexicon* lexicon) {
if (!lexicon) {
fprintf(stderr, "Null lexicon passed");
return false;
}
FILE* fp = fopen(phones_file, "r");
if (!fp) {
fprintf(stderr, "Could not open %s\n", phones_file);
return false;
}
fprintf(stderr, "Adding proninciations from %s\n", phones_file);
unordered_map<string, int> lexicon_index;
for (int i = 0; i < lexicon->phrase_infos.size(); i++) {
const auto& phrase_info = lexicon->phrase_infos[i];
lexicon_index[phrase_info.normalized] = i;
}
char buf[MAX_LINE_LENGTH];
int num_pronunciations_used = 0;
int num_pronunciations_total = 0;
double total_phone_len = 0;
int max_phone_len = 0;
while (fgets(buf, sizeof(buf), fp)) {
++num_pronunciations_total;
string line(buf);
vector<string> line_parts = util->Split(line, "\t");
if (line_parts.size() != 2) {
fprintf(stderr, "Expect exactly one tab - ignoring line: %s\n", buf);
continue;
}
string normalized = util->StrLetterizedPrunedPartsOf(line_parts[0]);
if (normalized.empty()) {
// Skip comment line or weird-phrase line.
continue;
}
if (lexicon_index.find(normalized) == lexicon_index.end()) {
continue;
}
vector<string> phone_parts = phone_util->LettersOf(line_parts[1]);
if (phone_parts.empty()) {
fprintf(stderr, "Empty pronunciation in: %s\n", buf);
continue;
}
string phone = phone_util->Join(phone_parts);
int index = lexicon_index.at(normalized);
PhraseInfo& phrase_info = lexicon->phrase_infos[index];
if (num_pronunciations_used % 100 == 0) {
fprintf(stderr, "Added pronunciation [%s] for %s\n",
phone.c_str(),
normalized.c_str());
}
++num_pronunciations_used;
total_phone_len += phone_parts.size();
if (phone_parts.size() > max_phone_len) {
max_phone_len = phone_parts.size();
}
phrase_info.phones.insert(phone_parts);
}
fclose(fp);
fprintf(stderr, "Read pronunciations file, used %d out of %d\n",
num_pronunciations_used, num_pronunciations_total);
fprintf(stderr, "Max phone len = %d, avg = %3.2f\n",
max_phone_len, total_phone_len / num_pronunciations_used);
return true;
}
} // namespace lufz
int main(int argc, char* argv[]) {
using namespace lufz;
if (argc != 5) {
fprintf(stderr, "Usage: %s <Language> <lexicon_file> <cmu-pronunciations-file> <crossed-words>\n",
argv[0]);
return 2;
}
LufzUtil util(argv[1]);
LufzUtil phone_util("Phonetics");
Lexicon lexicon;
if (!util.ReadLexicon(argv[2], &lexicon, argv[4])) {
return 2;
}
fprintf(stderr, "Read lexicon, have %d entries\n", lexicon.phrase_infos.size());
if (!AddPronunciations(&util, &phone_util, argv[3], &lexicon)) {
return 2;
}
/**
* First, do a pass to decide which indexing keys to keep. This is
* much faster than building the full index and then pruning
* away keys that do not have many entries.
*/
map<string, int> indexing_key_counts;
fprintf(stderr, "Computing indexing_key_counts...\n");
for (int i = 0; i < lexicon.phrase_infos.size(); ++i) {
const PhraseInfo& phrase_info = lexicon.phrase_infos[i];
const string& normalized = phrase_info.normalized;
if (normalized.empty()) continue;
int count = phrase_info.forms.size();
AddKeyCounts(normalized, count, &util, &indexing_key_counts);
if (i > 0 && i % 1000 == 0) {
fprintf(stderr, "Indexing key counts at %d: %s\n", i, normalized.c_str());
}
}
fprintf(stderr, "Pre-filtering, index has size: %d\n", indexing_key_counts.size());
std::set<std::string> indexing_keys;
// Filter
const int MIN_COUNT = 1024;
int ki = 0;
for (const auto& [key, count] : indexing_key_counts) {
if (ki % 10000 == 0) {
fprintf(stderr, "Indexing key count #%d for [%s] = %d\n", ki, key.c_str(), count);
}
ki++;
if (count < MIN_COUNT && !util.AllWild(key)) {
continue;
}
indexing_keys.insert(key);
}
fprintf(stderr, "Post-filtering, index has size: %d\n", indexing_keys.size());
fprintf(stderr, "Building index and agm-index...\n");
map<string, set<int>> index;
vector<vector<int>> agm_shards(AGM_INDEX_SHARDS);
for (int i = 0; i < lexicon.phrase_infos.size(); ++i) {
const PhraseInfo& phrase_info = lexicon.phrase_infos[i];
const string& normalized = phrase_info.normalized;
if (normalized.empty()) continue;
vector<int> lex_indices;
for (int j = 0; j < phrase_info.forms.size(); ++j) {
lex_indices.push_back(phrase_info.base_index + j);
}
AddKeys(normalized, indexing_keys, lex_indices, &util, &index);
AddAgmKey(normalized, lex_indices, &util, &agm_shards);
if (i > 0 && i % 1000 == 0) {
fprintf(stderr, "Indexed at %d: %s\n", i, normalized.c_str());
}
}
fprintf(stderr, "Building phones-index...\n");
vector<set<int>> phone_shards(PHONE_INDEX_SHARDS);
for (int i = 0; i < lexicon.phrase_infos.size(); ++i) {
const PhraseInfo& phrase_info = lexicon.phrase_infos[i];
for (const vector<string>& phone : phrase_info.phones) {
string phone_str = phone_util.Join(phone);
int shard = phone_util.IndexShard(phone_str, PHONE_INDEX_SHARDS);
for (int j = 0; j < phrase_info.forms.size(); ++j) {
phone_shards[shard].insert(phrase_info.base_index + j);
}
}
}
struct KeyInfoByLen {
int num_keys;
int total_phrases;
int max_phrases_for_a_key;
int num_distinct_phrases;
KeyInfoByLen() :
num_keys(0),
total_phrases(0),
max_phrases_for_a_key(0),
num_distinct_phrases(0) {}
};
map<int, KeyInfoByLen> len_counts;
for (const auto& kv : index) {
int vsize = kv.second.size();
const vector<string> parts = util.PartsOf(kv.first, false);
KeyInfoByLen counts = len_counts[parts.size()];
counts.num_keys++;
counts.total_phrases += vsize;
if (util.AllWild(kv.first)) {
counts.num_distinct_phrases = vsize;
} else if (vsize > counts.max_phrases_for_a_key) {
counts.max_phrases_for_a_key = vsize;
}
len_counts[parts.size()] = counts;
}
int64_t total_keys = 0, total_vals = 0, total_distinct_phrases = 0;
for (const auto& lc : len_counts) {
fprintf(stderr, "len:%d #keys: %d #phrases: %d "
"max-phrases-for-akey: %d num-distinct-phrases: %d\n",
lc.first, lc.second.num_keys, lc.second.total_phrases,
lc.second.max_phrases_for_a_key, lc.second.num_distinct_phrases);
total_keys += lc.second.num_keys;
total_vals += lc.second.total_phrases;
total_distinct_phrases += lc.second.num_distinct_phrases;
}
fprintf(stderr, "Total #keys: %lld #phrases: %lld #distinct-phrases: %lld\n",
total_keys, total_vals, total_distinct_phrases);
map<int, int> agm_counts;
int biggest_key = -1;
int biggest_count = 0;
for (int i = 0; i < AGM_INDEX_SHARDS; i++) {
const auto& shard = agm_shards[i];
int vsize = shard.size();
agm_counts[vsize]++;
if (vsize > biggest_count) {
biggest_key = i;
biggest_count = vsize;
}
}
for (const auto& lc : agm_counts) {
fprintf(stderr, "agmvalslen:%5d #keys: %3d\n", lc.first, lc.second);
}
fprintf(stderr, "Total# agm keys: %d\n", agm_shards.size());
fprintf(stderr, "Bulkiest key: %d [%d]\n", biggest_key, biggest_count);
// Output the JSON object that looks this:
// exetLexicon = {
// id: 'Lufz-en-v0.08',
// language: 'en',
// script: 'Latin',
// letters: [ 'A','B',... ],
// lexicon: [ "a","the",...],
// index: {
// '???': [42,390,2234,...],
// 'A??': [234,678,...],
// ...
// },
// anagrams: [
// [43,1,...],
// [43,1,...],
// ...
// ],
// phones: [[],[],...,[["B","AH","N","AE","N","AH"]], ...],
// phindex: [
// [42,...],
// [142,3232, ...],
// ...
// ],
// };
printf("exetLexicon = {");
printf("\n id: \"Lufz-%s-%s\",",
util.Language().c_str(), VERSION.c_str());
printf("\n language: \"%s\",", util.Language().c_str());
printf("\n script: \"%s\",", util.Script().c_str());
printf("\n letters: [");
for (const string& letter : lexicon.letters) {
printf("\"%s\", ", letter.c_str());
}
printf("],");
printf("\n lexicon: [");
int row = 0;
for (int i = 0; i < lexicon.phrase_infos.size(); ++i) {
if (row % 100 == 0) printf("\n ");
const PhraseInfo& phrase_info = lexicon.phrase_infos[i];
for (const string& form : phrase_info.forms) {
printf("\"%s\",", form.c_str());
row++;
}
}
printf("\n ],");
printf("\n index: {\n");
for (const auto& kv : index) {
printf(" '%s': [", kv.first.c_str());
int counter = 0;
for (int lex_index : kv.second) {
if (counter % 100 == 0) printf("\n ");
counter++;
printf("%d,", lex_index);
}
printf("\n ],\n");
}
printf(" },");
printf("\n anagrams: [\n");
for (const auto& shard : agm_shards) {
printf(" [");
for (int i = 0; i < shard.size(); ++i) {
if (i % 100 == 0) printf("\n ");
printf("%d,", shard[i]);
}
printf("\n ],\n");
}
printf(" ],");
printf("\n phones: [");
row = 0;
for (int i = 0; i < lexicon.phrase_infos.size(); ++i) {
if (row % 100 == 0) printf("\n ");
const PhraseInfo& phrase_info = lexicon.phrase_infos[i];
for (const string& form : phrase_info.forms) {
row++;
printf("[");
int j = 0;
for (const vector<string>& phone : phrase_info.phones) {
if (j > 0) printf(",");
j++;
printf("[");
for (int k = 0; k < phone.size(); k++) {
if (k > 0) printf(",");
printf("\"%s\"", phone[k].c_str());
}
printf("]");
}
printf("],");
}
}
printf("\n ],");
printf("\n phindex: [\n");
for (const auto& shard : phone_shards) {
printf(" [");
int i = 0;
for (int idx : shard) {
if (i % 100 == 0) printf("\n ");
i++;
printf("%d,", idx);
}
printf("\n ],\n");
}
printf(" ],\n");
printf(" /**\n");
if (util.Language() == "en") {
printf(" * --- Paste contents of lufz-en-lexicon-stems-patch.js below. ---\n");
printf(" * --- Generate it using lufz-en-lexicon-get-stems-patch.html ---\n");
printf(" */\n");
}
printf("};\n");
return 0;
}