forked from osmlab/name-suggestion-index
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_brands.js
382 lines (333 loc) · 16.5 KB
/
build_brands.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
const colors = require('colors/safe');
const fs = require('fs-extra');
const shell = require('shelljs');
const stringify = require('json-stringify-pretty-compact');
const fileTree = require('./lib/file_tree.js');
const matcher = require('./lib/matcher.js')();
const sort = require('./lib/sort.js');
const stemmer = require('./lib/stemmer.js');
const toParts = require('./lib/to_parts.js');
const validate = require('./lib/validate.js');
// Load cached wikidata
const _wikidata = require('./dist/wikidata.json').wikidata;
// Load and check filters.json
let filters = require('./config/filters.json');
const filtersSchema = require('./schema/filters.json');
validate('config/filters.json', filters, filtersSchema); // validate JSON-schema
// Lowercase and sort the filters for consistency
filters = {
keepTags: filters.keepTags.map(s => s.toLowerCase()).sort(),
discardKeys: filters.discardKeys.map(s => s.toLowerCase()).sort(),
discardNames: filters.discardNames.map(s => s.toLowerCase()).sort()
};
fs.writeFileSync('config/filters.json', stringify(filters));
// Load and check brand files
let brands = fileTree.read('brands');
// all names start out in _discard..
const allnames = require('./dist/names_all.json');
let _discard = Object.assign({}, allnames);
let _keep = {};
filterNames();
matcher.buildMatchIndex(brands);
checkBrands();
mergeBrands();
fileTree.write('brands', brands);
// `filterNames()` will process a `dist/names_all.json` file,
// splitting the data up into 2 files:
//
// `dist/names_keep.json` - candidates for suggestion presets
// `dist/names_discard.json` - everything else
//
// The file format is identical to the `names_all.json` file:
// "key/value|name": count
// "shop/coffee|Starbucks": 8284
//
function filterNames() {
console.log('\nfiltering names');
console.time(colors.green('names filtered'));
// Start clean
shell.rm('-f', ['dist/names_keep.json', 'dist/names_discard.json']);
// filter by keepTags (move from _discard -> _keep)
filters.keepTags.forEach(s => {
let re = new RegExp(s, 'i');
for (let kvnd in _discard) {
let tag = kvnd.split('|', 2)[0];
if (re.test(tag)) {
_keep[kvnd] = _discard[kvnd];
delete _discard[kvnd];
}
}
});
// filter by discardKeys (move from _keep -> _discard)
filters.discardKeys.forEach(s => {
let re = new RegExp(s, 'i');
for (let kvnd in _keep) {
if (re.test(kvnd)) {
_discard[kvnd] = _keep[kvnd];
delete _keep[kvnd];
}
}
});
// filter by discardNames (move from _keep -> _discard)
filters.discardNames.forEach(s => {
let re = new RegExp(s, 'i');
for (let kvnd in _keep) {
let name = kvnd.split('|', 2)[1];
if (re.test(name)) {
_discard[kvnd] = _keep[kvnd];
delete _keep[kvnd];
}
}
});
fs.writeFileSync('dist/names_discard.json', stringify(sort(_discard)));
fs.writeFileSync('dist/names_keep.json', stringify(sort(_keep)));
console.timeEnd(colors.green('names filtered'));
}
//
// mergeBrands() takes the brand names we are keeping
// and updates the files under `/brands/**/*.json`
//
function mergeBrands() {
console.log('\nmerging brands');
console.time(colors.green('brands merged'));
// Create/update entries
// First, entries in namesKeep (i.e. counted entries)
Object.keys(_keep).forEach(kvnd => {
let obj = brands[kvnd];
let parts = toParts(kvnd);
var m = matcher.matchParts(parts);
if (m) return; // already in the index
if (!obj) { // a new entry!
obj = { tags: {} };
brands[kvnd] = obj;
// assign default tags - new entries
obj.tags.brand = parts.n;
obj.tags.name = parts.n;
obj.tags[parts.k] = parts.v;
}
});
// now process all brands
Object.keys(brands).forEach(kvnd => {
let obj = brands[kvnd];
let parts = toParts(kvnd);
// assign default tags - new or existing entries
if (parts.kv === 'amenity/cafe') {
if (!obj.tags.takeaway) obj.tags.takeaway = 'yes';
if (!obj.tags.cuisine) obj.tags.cuisine = 'coffee_shop';
} else if (parts.kv === 'amenity/fast_food') {
if (!obj.tags.takeaway) obj.tags.takeaway = 'yes';
} else if (parts.kv === 'amenity/pharmacy') {
if (!obj.tags.healthcare) obj.tags.healthcare = 'pharmacy';
}
// Force `countryCode`, and duplicate `name:xx` and `brand:xx` tags
// if the name can only be reasonably read in one country.
// https://www.regular-expressions.info/unicode.html
if (/[\u0590-\u05FF]/.test(parts.n)) { // Hebrew
obj.countryCodes = ['il'];
// note: old ISO 639-1 lang code for Hebrew was `iw`, now `he`
if (obj.tags.name) { obj.tags['name:he'] = obj.tags.name; }
if (obj.tags.brand) { obj.tags['brand:he'] = obj.tags.brand; }
} else if (/[\u0E00-\u0E7F]/.test(parts.n)) { // Thai
obj.countryCodes = ['th'];
if (obj.tags.name) { obj.tags['name:th'] = obj.tags.name; }
if (obj.tags.brand) { obj.tags['brand:th'] = obj.tags.brand; }
} else if (/[\u1000-\u109F]/.test(parts.n)) { // Myanmar
obj.countryCodes = ['mm'];
if (obj.tags.name) { obj.tags['name:my'] = obj.tags.name; }
if (obj.tags.brand) { obj.tags['brand:my'] = obj.tags.brand; }
} else if (/[\u1100-\u11FF]/.test(parts.n)) { // Hangul
obj.countryCodes = ['kr'];
if (obj.tags.name) { obj.tags['name:ko'] = obj.tags.name; }
if (obj.tags.brand) { obj.tags['brand:ko'] = obj.tags.brand; }
} else if (/[\u1700-\u171F]/.test(parts.n)) { // Tagalog
obj.countryCodes = ['ph'];
if (obj.tags.name) { obj.tags['name:tl'] = obj.tags.name; }
if (obj.tags.brand) { obj.tags['brand:tl'] = obj.tags.brand; }
} else if (/[\u3040-\u30FF]/.test(parts.n)) { // Hirgana or Katakana
obj.countryCodes = ['jp'];
if (obj.tags.name) { obj.tags['name:ja'] = obj.tags.name; }
if (obj.tags.brand) { obj.tags['brand:ja'] = obj.tags.brand; }
} else if (/[\u3130-\u318F]/.test(parts.n)) { // Hangul
obj.countryCodes = ['kr'];
if (obj.tags.name) { obj.tags['name:ko'] = obj.tags.name; }
if (obj.tags.brand) { obj.tags['brand:ko'] = obj.tags.brand; }
} else if (/[\uA960-\uA97F]/.test(parts.n)) { // Hangul
obj.countryCodes = ['kr'];
if (obj.tags.name) { obj.tags['name:ko'] = obj.tags.name; }
if (obj.tags.brand) { obj.tags['brand:ko'] = obj.tags.brand; }
} else if (/[\uAC00-\uD7AF]/.test(parts.n)) { // Hangul
obj.countryCodes = ['kr'];
if (obj.tags.name) { obj.tags['name:ko'] = obj.tags.name; }
if (obj.tags.brand) { obj.tags['brand:ko'] = obj.tags.brand; }
}
brands[kvnd] = sort(brands[kvnd]);
});
console.timeEnd(colors.green('brands merged'));
}
//
// Checks all the brands for several kinds of issues
//
function checkBrands() {
let warnMatched = matcher.getWarnings();
let warnDuplicate = [];
let warnFormatWikidata = [];
let warnFormatWikipedia = [];
let warnMissingWikidata = [];
let warnMissingWikipedia = [];
let warnMissingLogos = [];
let warnMissingTag = [];
let seen = {};
Object.keys(brands).forEach(kvnd => {
let obj = brands[kvnd];
let parts = toParts(kvnd);
if (!parts.d) { // ignore ambiguous entries for these
// Warn if some other item matches this item
var m = matcher.matchParts(parts);
if (m && m.kvnd !== kvnd) {
warnMatched.push([m.kvnd, kvnd]);
}
// Warn if the name appears to be a duplicate
let stem = stemmer(parts.n);
let other = seen[stem];
if (other) {
// suppress warning?
let suppress = false;
if (brands[other].nomatch && brands[other].nomatch.indexOf(kvnd) !== -1) {
suppress = true;
} else if (obj.nomatch && obj.nomatch.indexOf(other) !== -1) {
suppress = true;
}
if (!suppress) {
warnDuplicate.push([kvnd, other]);
}
}
seen[stem] = kvnd;
}
// Warn if `brand:wikidata` or `brand:wikipedia` tags are missing or look wrong..
let wd = obj.tags['brand:wikidata'];
if (!wd) {
warnMissingWikidata.push(kvnd);
} else if (!/^Q\d+$/.test(wd)) {
warnFormatWikidata.push([kvnd, wd]);
}
let wp = obj.tags['brand:wikipedia'];
if (!wp) {
warnMissingWikipedia.push(kvnd);
} else if (!/^[a-z_]{2,}:[^_]*$/.test(wp)) {
warnFormatWikipedia.push([kvnd, wp]);
}
// Warn on missing logo
let logos = (wd && _wikidata[wd] && _wikidata[wd].logos) || {};
if (!Object.keys(logos).length) {
warnMissingLogos.push(kvnd);
}
// Warn on other missing tags
switch (parts.kv) {
case 'amenity/fast_food':
case 'amenity/restaurant':
if (!obj.tags.cuisine) { warnMissingTag.push([kvnd, 'cuisine']); }
break;
case 'amenity/vending_machine':
if (!obj.tags.vending) { warnMissingTag.push([kvnd, 'vending']); }
break;
case 'shop/beauty':
if (!obj.tags.beauty) { warnMissingTag.push([kvnd, 'beauty']); }
break;
}
});
if (warnMatched.length) {
console.warn(colors.yellow('\nWarning - Brands match other brands:'));
console.warn(colors.gray('--------------------------------------------------------------------------------'));
console.warn(colors.gray('If the brands are the different, add a disambiguator after the name, like `~(USA)` vs `~(Canada)`'));
console.warn(colors.gray('If the brands are the same, remove extra `matchTags` or `matchNames`. Remember:'));
console.warn(colors.gray('- Name matching ignores letter case, punctuation, spacing, and diacritical marks (é vs e). '));
console.warn(colors.gray(' No need to add `matchNames` for variations in these.'));
console.warn(colors.gray('- Tag matching automatically includes other similar tags in the same match group.'));
console.warn(colors.gray(' No need to add `matchTags` for similar tags. see `config/match_groups.json`'));
console.warn(colors.gray('--------------------------------------------------------------------------------'));
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 brands:'));
console.warn(colors.gray('--------------------------------------------------------------------------------'));
console.warn(colors.gray('To resolve these, add the missing tag.'));
console.warn(colors.gray('--------------------------------------------------------------------------------'));
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 brand names:'));
console.warn(colors.gray('--------------------------------------------------------------------------------'));
console.warn(colors.gray('To resolve these, remove the worse entry and add `matchNames`/`matchTags` properties on the better entry.'));
console.warn(colors.gray('To suppress this warning for entries that really are different, add a `nomatch` property on both entries.'));
console.warn(colors.gray('--------------------------------------------------------------------------------'));
warnDuplicate.forEach(w => console.warn(
colors.yellow(' "' + w[0] + '"') + ' -> duplicates? -> ' + colors.yellow('"' + w[1] + '"')
));
console.warn('total ' + warnDuplicate.length);
}
// if (warnMissingWikidata.length) {
// console.warn(colors.yellow('\nWarning - Brand missing `brand:wikidata`:'));
// console.warn(colors.gray('--------------------------------------------------------------------------------'));
// console.warn(colors.gray('To resolve these, make sure "brand:wikidata" tag looks like "Q191615".'));
// console.warn(colors.gray('--------------------------------------------------------------------------------'));
// warnMissingWikidata.forEach(w => console.warn(
// colors.yellow(' "' + w + '"') + ' -> missing -> "brand:wikidata"'
// ));
// console.warn('total ' + warnMissingWikidata.length);
// }
// if (warnMissingWikipedia.length) {
// console.warn(colors.yellow('\nWarning - Brand missing `brand:wikipedia`:'));
// console.warn(colors.gray('--------------------------------------------------------------------------------'));
// console.warn(colors.gray('To resolve these, make sure "brand:wikipedia" tag looks like "en:Pizza Hut".'));
// console.warn(colors.gray('--------------------------------------------------------------------------------'));
// warnMissingWikipedia.forEach(w => console.warn(
// colors.yellow(' "' + w + '"') + ' -> missing -> "brand:wikipedia"'
// ));
// console.warn('total ' + warnMissingWikipedia.length);
// }
// if (warnMissingLogos.length) {
// console.warn(colors.yellow('\nWarning - Brand missing `logos`:'));
// console.warn(colors.gray('--------------------------------------------------------------------------------'));
// console.warn(colors.gray('To resolve these, update the brands' entries on wikidata.org, then `npm run wikidata`.'));
// console.warn(colors.gray('--------------------------------------------------------------------------------'));
// warnMissingLogos.forEach(w => console.warn(
// colors.yellow(' "' + w + '"') + ' -> missing -> "logos"'
// ));
// console.warn('total ' + warnMissingLogos.length);
// }
if (warnFormatWikidata.length) {
console.warn(colors.yellow('\nWarning - Brand with incorrect `brand:wikidata` format:'));
console.warn(colors.gray('--------------------------------------------------------------------------------'));
console.warn(colors.gray('To resolve these, make sure "brand:wikidata" tag looks like "Q191615".'));
console.warn(colors.gray('--------------------------------------------------------------------------------'));
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 - Brand with incorrect `brand:wikipedia` format:'));
console.warn(colors.gray('--------------------------------------------------------------------------------'));
console.warn(colors.gray('To resolve these, make sure "brand:wikipedia" tag looks like "en:Pizza Hut".'));
console.warn(colors.gray('--------------------------------------------------------------------------------'));
warnFormatWikipedia.forEach(w => console.warn(
colors.yellow(' "' + w[0] + '"') + ' -> "brand:wikipedia": ' + '"' + w[1] + '"'
));
console.warn('total ' + warnFormatWikipedia.length);
}
let total = Object.keys(brands).length;
let hasWd = total - warnMissingWikidata.length;
let pctWd = (hasWd * 100 / total).toFixed(1);
let hasLogos = total - warnMissingLogos.length;
let pctLogos = (hasLogos * 100 / total).toFixed(1);
console.info(colors.blue.bold(`\nIndex completeness:`));
console.info(colors.blue.bold(` ${total} entries total.`));
console.info(colors.blue.bold(` ${hasWd} (${pctWd}%) with a 'brand:wikidata' tag.`));
console.info(colors.blue.bold(` ${hasLogos} (${pctLogos}%) with a logo.`));
}