forked from osmlab/name-suggestion-index
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
80 lines (71 loc) · 2.36 KB
/
build.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
var fs = require('fs'),
filter = require('./filter.json'),
raw = require('./topNames.json'),
canon = require('./canonical.json'),
correctNames = buildReverseIndex(canon);
var out = {},
defined = {};
for (var fullName in raw) {
filterValues(fullName);
}
function buildReverseIndex(canon) {
var rIndex = {};
for (var can in canon) {
if (canon[can].matches) {
for (var i = canon[can].matches.length - 1; i >= 0; i--) {
var match = canon[can].matches[i];
rIndex[match] = can;
}
}
}
return rIndex;
}
function filterValues(fullName) {
var theName = fullName.split('|', 2),
tag = theName[0].split('/', 2),
key = tag[0],
value = tag[1];
theName = theName[1];
if (filter.wanted[key] &&
filter.wanted[key].indexOf(value) !== -1 &&
filter.discardedNames.indexOf(theName) == -1) {
if (correctNames[theName]) theName = correctNames[theName];
set(key, value, theName, raw[fullName]);
}
}
function set(k, v, name, count) {
if (!out[k]) out[k] = {};
if (!out[k][v]) out[k][v] = {};
if (!out[k][v][name]) {
if (canon[name] && canon[name].nix_value) {
for (var i = 0; i < canon[name].nix_value.length; i++) {
if (canon[name].nix_value[i] == v) return;
}
}
if (defined[name]) {
var string = name;
for (var i = 0; i < defined[name].length; i++) {
string += '\n\t in ' + defined[name][i] + ' - ';
var kv = defined[name][i].split('/');
string += out[kv[0]][kv[1]][name].count + ' times';
}
console.log(string + '\n\t and ' + k + '/' + v + ' - ' + count + ' times');
}
out[k][v][name] = {count: count};
if (defined[name]) {
defined[name].push(k + '/' + v);
} else {
defined[name] = [k + '/' + v];
}
} else {
out[k][v][name].count += count;
}
if (canon[name]) {
for (var tag in canon[name].tags) {
if (!out[k][v][name].tags) out[k][v][name].tags = {};
out[k][v][name].tags[tag] = canon[name].tags[tag];
}
}
}
fs.writeFileSync('name-suggestions.json', JSON.stringify(out, null, 4));
fs.writeFileSync('name-suggestions.min.json', JSON.stringify(out));