forked from osmlab/name-suggestion-index
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_filterNames.js
410 lines (354 loc) · 13.9 KB
/
build_filterNames.js
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
const colors = require('colors/safe');
const diacritics = require('diacritics');
const fs = require('fs');
const shell = require('shelljs');
const stringify = require('json-stringify-pretty-compact');
const allNames = require('./dist/allNames.json');
const filters = require('./config/filters.json');
let canonical = require('./config/canonical.json');
// perform JSON-schema validation
const Validator = require('jsonschema').Validator;
const filtersSchema = require('./schema/filters.json');
const canonicalSchema = require('./schema/canonical.json');
validateSchema('config/filters.json', filters, filtersSchema);
validateSchema('config/canonical.json', canonical, canonicalSchema);
// all names start out in discard..
let discard = Object.assign({}, allNames);
let keep = {};
let rIndex = {};
let ambiguous = {};
filterNames();
mergeConfig();
// Perform JSON Schema validation
function validateSchema(fileName, object, schema) {
let v = new Validator();
let validationErrors = v.validate(object, schema).errors;
if (validationErrors.length) {
console.error(colors.red('\nError - Schema validation:'));
console.error(' ' + colors.yellow(fileName + ': '));
validationErrors.forEach(e => {
if (e.property) {
console.error(' ' + colors.yellow(e.property + ' ' + e.message));
} else {
console.error(' ' + colors.yellow(e));
}
});
console.error();
process.exit(1);
}
}
//
// `filterNames()` will process a `dist/allNames.json` file,
// splitting the data up into 2 files:
//
// `dist/keepNames.json` - candidates for suggestion presets
// `dist/discardNames.json` - everything else
//
// The file format is identical to the `allNames.json` file:
// "key/value|name": count
// "shop/coffee|Starbucks": 8284
//
function filterNames() {
console.log('filtering names');
console.time(colors.green('names filtered'));
// Start clean
shell.rm('-f', ['dist/keepNames.json', 'dist/discardNames.json']);
// filter by keepTags (move from discard -> keep)
filters.keepTags.forEach(s => {
let re = new RegExp(s, 'i');
for (let key in discard) {
let tag = key.split('|', 2)[0];
if (re.test(tag)) {
keep[key] = discard[key];
delete discard[key];
}
}
});
// filter by discardKeys (move from keep -> discard)
filters.discardKeys.forEach(s => {
let re = new RegExp(s, 'i');
for (let key in keep) {
if (re.test(key)) {
discard[key] = keep[key];
delete keep[key];
}
}
});
// filter by discardNames (move from keep -> discard)
filters.discardNames.forEach(s => {
let re = new RegExp(s, 'i');
for (let key in keep) {
let name = key.split('|', 2)[1];
if (re.test(name)) {
discard[key] = keep[key];
delete keep[key];
}
}
});
fs.writeFileSync('dist/discardNames.json', stringify(sort(discard)));
fs.writeFileSync('dist/keepNames.json', stringify(sort(keep)));
console.timeEnd(colors.green('names filtered'));
}
//
// mergeConfig() takes the names we are keeping and update
// `config/canonical.json`
//
function mergeConfig() {
buildReverseIndex();
checkCanonical();
console.log('\nmerging config/canonical.json');
console.time(colors.green('config updated'));
// Create/update entries in `config/canonical.json`
Object.keys(keep).forEach(k => {
if (rIndex[k] || ambiguous[k]) return;
let obj = canonical[k];
let parts = k.split('|', 2);
let tag = parts[0].split('/', 2);
let key = tag[0];
let value = tag[1];
let name = parts[1];
if (!obj) {
obj = { count: 0, tags: {} };
obj.tags.name = name;
obj.tags[key] = value;
canonical[k] = obj;
}
// https://www.regular-expressions.info/unicode.html
if (/[\u0590-\u05FF]/.test(name)) { // Hebrew
obj.countryCodes = ['il'];
} else if (/[\u0E00-\u0E7F]/.test(name)) { // Thai
obj.countryCodes = ['th'];
} else if (/[\u1000-\u109F]/.test(name)) { // Myanmar
obj.countryCodes = ['mm'];
} else if (/[\u1100-\u11FF]/.test(name)) { // Hangul
obj.countryCodes = ['kr'];
} else if (/[\u1700-\u171F]/.test(name)) { // Tagalog
obj.countryCodes = ['ph'];
} else if (/[\u3040-\u30FF]/.test(name)) { // Hirgana or Katakana
obj.countryCodes = ['jp'];
} else if (/[\u3130-\u318F]/.test(name)) { // Hangul
obj.countryCodes = ['kr'];
} else if (/[\uA960-\uA97F]/.test(name)) { // Hangul
obj.countryCodes = ['kr'];
} else if (/[\uAC00-\uD7AF]/.test(name)) { // Hangul
obj.countryCodes = ['kr'];
}
obj.count = keep[k];
obj.tags = sort(obj.tags);
});
Object.keys(canonical).forEach(k => { canonical[k] = sort(canonical[k]) });
fs.writeFileSync('config/canonical.json', stringify(sort(canonical), { maxLength: 50 }));
console.timeEnd(colors.green('config updated'));
}
//
// Returns an object with sorted keys and sorted values.
// (This is useful for file diffing)
//
function sort(obj) {
let sorted = {};
Object.keys(obj).sort().forEach(k => {
sorted[k] = Array.isArray(obj[k]) ? obj[k].sort() : obj[k];
});
return sorted;
}
// Some keys contain a disambiguation mark: "Eko~ca" vs "Eko~gr"
// If so, we save these in an `ambiguous` object for later use
function checkAmbiguous(k) {
let i = k.indexOf('~');
if (i !== -1) {
let stem = k.substring(0, i);
ambiguous[stem] = true;
return true;
}
return false;
}
//
// Returns a reverse index to map match keys back to their original keys
//
function buildReverseIndex() {
let warnCollisions = [];
for (let k in canonical) {
checkAmbiguous(k);
if (canonical[k].match) {
for (let i = canonical[k].match.length - 1; i >= 0; i--) {
let match = canonical[k].match[i];
checkAmbiguous(match);
if (rIndex[match]) {
warnCollisions.push([rIndex[match], match]);
warnCollisions.push([k, match]);
}
rIndex[match] = k;
}
}
}
if (warnCollisions.length) {
console.warn(colors.yellow('\nWarning - match name collisions in `canonical.json`:'));
console.warn('To resolve these, make sure multiple entries do not contain the same "match" property.');
warnCollisions.forEach(w => console.warn(
colors.yellow(' "' + w[0] + '"') + ' -> match? -> ' + colors.yellow('"' + w[1] + '"')
));
}
}
//
// Checks all the entries in `canonical.json` for several kinds of issues
//
function checkCanonical() {
let warnUncommon = [];
let warnMatched = [];
let warnDuplicate = [];
let warnFormatWikidata = [];
let warnFormatWikipedia = [];
let warnMissingWikidata = [];
let warnMissingWikipedia = [];
let warnMissingTag = [];
let seen = {};
Object.keys(canonical).forEach(k => {
let obj = canonical[k];
let parts = k.split('|', 2);
let tag = parts[0];
let name = parts[1];
// Warn if the item is uncommon (i.e. not found in keepNames)
if (!keep[k]) {
delete obj.count;
if (!obj.nocount) { // suppress warning?
warnUncommon.push(k);
}
} else {
delete obj.nocount;
}
// Warn if the item is found in rIndex (i.e. some other item matches it)
if (rIndex[k]) {
warnMatched.push([rIndex[k], k]);
}
// Warn if the name appears to be a duplicate
let stem = stemmer(name);
let other = seen[stem];
if (other) {
// suppress warning?
let suppress = false;
if (canonical[other].nomatch && canonical[other].nomatch.indexOf(k) !== -1) {
suppress = true;
} else if (obj.nomatch && obj.nomatch.indexOf(other) !== -1) {
suppress = true;
}
if (!suppress) {
warnDuplicate.push([k, other]);
}
}
seen[stem] = k;
// Warn if `brand:wikidata` or `brand:wikipedia` tags are missing or look wrong..
let wd = obj.tags['brand:wikidata'];
if (!wd) {
warnMissingWikidata.push(k);
} else if (!/^Q\d+$/.test(wd)) {
warnFormatWikidata.push([k, wd]);
}
let wp = obj.tags['brand:wikipedia'];
if (!wp) {
warnMissingWikipedia.push(k);
} else if (!/^[a-z_]{2,}:[^_]*$/.test(wp)) {
warnFormatWikipedia.push([k, wp]);
}
// Warn on other missing tags
switch (tag) {
case 'amenity/fast_food':
case 'amenity/restaurant':
if (!obj.tags.cuisine) {
warnMissingTag.push([k, 'cuisine']);
}
break;
case 'amenity/vending_machine':
if (!obj.tags.vending) {
warnMissingTag.push([k, 'vending']);
}
break;
}
});
if (warnMatched.length) {
console.warn(colors.yellow('\nWarning - Entries in `canonical.json` matched to other entries in `canonical.json`:'));
console.warn('To resolve these, remove the worse entry and add "match" property on the better entry.');
warnMatched.forEach(w => console.warn(
colors.yellow(' "' + w[0] + '"') + ' -> matches? -> ' + colors.yellow('"' + w[1] + '"')
));
console.warn('total ' + warnMatched.length);
}
if (warnMissingTag.length) {
console.warn(colors.yellow('\nWarning - Missing tags for entries in `canonical.json`:'));
console.warn('To resolve these, add the missing tag.');
warnMissingTag.forEach(w => console.warn(
colors.yellow(' "' + w[0] + '"') + ' -> missing tag? -> ' + colors.yellow('"' + w[1] + '"')
));
console.warn('total ' + warnMissingTag.length);
}
if (warnDuplicate.length) {
console.warn(colors.yellow('\nWarning - Potential duplicate names in `canonical.json`:'));
console.warn('To resolve these, remove the worse entry and add "match" property on the better entry.');
console.warn('To suppress this warning for entries that really are different, add a "nomatch" property on both entries.');
warnDuplicate.forEach(w => console.warn(
colors.yellow(' "' + w[0] + '"') + ' -> duplicates? -> ' + colors.yellow('"' + w[1] + '"')
));
console.warn('total ' + warnDuplicate.length);
}
if (warnUncommon.length) {
console.warn(colors.yellow('\nWarning - Uncommon entries in `canonical.json` not found in `keepNames.json`:'));
console.warn('These might be okay. It just means that the entry is not commonly found in OpenStreetMap.');
console.warn('To suppress this warning, add a "nocount" property to the entry.');
warnUncommon.forEach(w => console.warn(
colors.yellow(' "' + w + '"')
));
console.warn('total ' + warnUncommon.length);
}
// if (warnMissingWikidata.length) {
// console.warn(colors.yellow('\nWarning - Entries in `canonical.json` missing `brand:wikidata`:'));
// console.warn('To resolve these, make sure "brand:wikidata" tag looks like "Q191615".');
// warnMissingWikidata.forEach(w => console.warn(
// colors.yellow(' "' + w + '"') + ' -> missing -> "brand:wikidata"'
// ));
// console.warn('total ' + warnMissingWikidata.length);
// }
// if (warnMissingWikipedia.length) {
// console.warn(colors.yellow('\nWarning - Entries in `canonical.json` missing `brand:wikipedia`:'));
// console.warn('To resolve these, make sure "brand:wikipedia" tag looks like "en:Pizza Hut".');
// warnMissingWikipedia.forEach(w => console.warn(
// colors.yellow(' "' + w + '"') + ' -> missing -> "brand:wikipedia"'
// ));
// console.warn('total ' + warnMissingWikipedia.length);
// }
if (warnFormatWikidata.length) {
console.warn(colors.yellow('\nWarning - Entries in `canonical.json` with incorrect `brand:wikidata` format:'));
console.warn('To resolve these, make sure "brand:wikidata" tag looks like "Q191615".');
warnFormatWikidata.forEach(w => console.warn(
colors.yellow(' "' + w[0] + '"') + ' -> "brand:wikidata": ' + '"' + w[1] + '"'
));
console.warn('total ' + warnFormatWikidata.length);
}
if (warnFormatWikipedia.length) {
console.warn(colors.yellow('\nWarning - Entries in `canonical.json` with incorrect `brand:wikipedia` format:'));
console.warn('To resolve these, make sure "brand:wikipedia" tag looks like "en:Pizza Hut".');
warnFormatWikipedia.forEach(w => console.warn(
colors.yellow(' "' + w[0] + '"') + ' -> "brand:wikipedia": ' + '"' + w[1] + '"'
));
console.warn('total ' + warnFormatWikipedia.length);
}
let total = Object.keys(canonical).length;
let hasWd = total - warnMissingWikidata.length;
let pct = (hasWd * 100 / total).toFixed(1);
console.info(colors.blue(`\nIndex completeness: ${hasWd}/${total} (${pct}%) matched to Wikidata `));
}
// Removes noise from the name so that we can compare
// similar names for catching duplicates.
function stemmer(name) {
let noise = [
/ban(k|c)(a|o)?/ig,
/банк/ig,
/coop/ig,
/express/ig,
/(gas|fuel)/ig,
/wireless/ig,
/(shop|store)/ig,
/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,
/\s/g
];
name = noise.reduce((acc, regex) => acc.replace(regex, ''), name);
return diacritics.remove(name.toLowerCase());
}