-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzer.js
379 lines (329 loc) · 10.3 KB
/
analyzer.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
'use strict'
const startTime = Date.now();
const INPUT = process.env.INPUT || 'source_mojis.txt';
const OUTPUT = process.env.OUTPUT || 'results.json';
const PREVIOUS = process.env.PREVIOUS || OUTPUT;
const SPACING = process.env.SPACING || undefined;
const MINIMUM_TAG_SHARE = 0.6;
const NULL_ARRAY = [ null, null, null, null, null ];
const LEFT_SIDES = '([{༼|ʕ⁽ᶘˁ〳₍꒰⌈UU∪⎩╏ᘳ།Ꮚ('.split('');
const RIGHT_SIDES = ')]}༽|ʔ⁾ᶅˀ〵₎꒱⌉UU∪⎭╏ᘰ།Ꮚ)'.split('');
const SINGLE_CHAR_FACES = 'ツᐛᐖ∵Ö⌓̈'.split('');
const MIRRORED_EYES = {
'́': '̀',
'̀': '́',
'ˊ': 'ˋ',
'ˋ': 'ˊ',
'̀': '́',
'́': '̀',
'´': '`',
'`': '´',
'᷄': '᷅',
'᷅': '᷄',
'˃': '˂',
'˂': '˃',
'ó': 'ò',
'ò': 'ó',
'˂': '˃',
'˃': '˂',
'<': '>',
'>': '<',
'˒': '˓',
'˓': '˒',
'⊂': '⊃',
'⊃': '⊂',
'╰': '╯',
'╯': '╰'
};
const BAD_CHARS = [
811,
860,
865,
2636,
3642,
3665,
4349,
8408,
8409,
8807,
9678,
9770,
9774,
10047,
11193,
58164,
59132,
65417,
65507,
65533
];
const fs = require('fs');
const mojis = fs.readFileSync(INPUT)
.toString()
.replace(/ /g, '')
.split(/[\t\n]/);
const prevResults = (() => {
try { return require('./' + PREVIOUS); }
catch (e) { return { mouths: {}, eyes: {} }; }
})();
// Split moji into left, right, and middle using the innermost face sides
const parseSides = moji => {
const mojiArray = moji.split('');
const halfLength = Math.ceil(moji.length / 2);
const leftIndex = halfLength - 1 - mojiArray
.slice(0, halfLength)
.reverse()
.findIndex(char => LEFT_SIDES.includes(char))
const rightIndex = moji.length - halfLength + mojiArray
.slice(moji.length - halfLength)
.findIndex(char => RIGHT_SIDES.includes(char))
// Kaomoji without two sides to the face will have parsing errors later
if (leftIndex === halfLength - 1 - (-1)) {
return NULL_ARRAY;
}
if (rightIndex === moji.length - halfLength + (-1)) {
return NULL_ARRAY;
}
const leftSide = moji[leftIndex];
const rightSide = moji[rightIndex];
// Kaomoji with mismatching sides may have been parsed in error
if (LEFT_SIDES.indexOf(leftSide) !== RIGHT_SIDES.indexOf(rightSide)) {
return NULL_ARRAY;
}
return [
moji.slice(0, leftIndex),
leftSide,
moji.slice(leftIndex + 1, rightIndex),
rightSide,
moji.slice(rightIndex + 1)
];
}
// Detects whether arms are inside or outside the face, and splits them off
const parseArms = (left, mid, right) => {
const isLeftFacing = left.slice(-1) && left.slice(-1) === mid.slice(-1);
const isRightFacing = right[0] && right[0] === mid[0];
if (isLeftFacing && !isRightFacing) {
return [
left.slice(0, -1),
left.slice(-1) + left.slice(-1),
mid.slice(0, -1),
'',
right
]
}
if (isRightFacing && !isLeftFacing) {
return [
left,
'',
mid.slice(1),
right[0] + right[0],
right.slice(1)
]
}
return [
left.slice(0, -1),
left.slice(-1),
mid,
right[0] || '',
right.slice(1)
];
}
// Splits face characters into mouth, eyes, surrounding characters
const parseFace = face => {
if (face.length < 3) return NULL_ARRAY;
const midIndex = Math.floor(face.length / 2);
// If a previous mouth is in the middle, use as basis for split
const maybeMouths = face.length % 2 === 0
? [
[face.slice(midIndex - 1, midIndex + 1), midIndex - 1],
[face[midIndex - 1], midIndex - 1],
[face[midIndex], midIndex]
]
: [
[face.slice(midIndex - 1, midIndex + 1), midIndex - 1],
[face.slice(midIndex, midIndex + 2), midIndex],
[face[midIndex - 1], midIndex - 1],
[face[midIndex], midIndex],
[face[midIndex + 1], midIndex +1]
];
let matchedMouths = maybeMouths.filter(mouthTuple => {
const mouth = mouthTuple[0];
return prevResults.mouths[mouth] || results.mouths[mouth];
});
// If there is more than one match, try to narrow by matching eyes
if (matchedMouths.length > 1) {
matchedMouths = matchedMouths.filter(mouthTuple => {
const index = mouthTuple[1];
const leftEye = face[index - 1];
const rightEye = face[index + mouthTuple[0].length];
if (!leftEye || !rightEye) return false;
const eyes = leftEye + '%' + rightEye;
return prevResults.eyes[eyes] || results.eyes[eyes];
})
}
// If only one match, use mouth
if (matchedMouths.length === 1) {
const mouth = matchedMouths[0][0]
const mouthIndex = matchedMouths[0][1];
return [
face.slice(0, mouthIndex -1),
face[mouthIndex - 1] || '',
mouth,
face[mouthIndex + mouth.length] || '',
face.slice(mouthIndex + mouth.length + 1)
]
}
// Otherwise looks for symmetrical pairs, store as tuples with
// indexes of symmetrical pairs in order from inner to outer
const pairs = face.split('')
.reduce((charIndexes, char, index) => {
const prevIndex = charIndexes.findIndex(ci => {
return ci.chars[0] === char || MIRRORED_EYES[char] === ci.chars[0];
});
if (prevIndex === -1) {
charIndexes.push({ chars: [char], idxs: [index] });
} else {
charIndexes[prevIndex].chars.push(char);
charIndexes[prevIndex].idxs.push(index);
};
return charIndexes;
}, [])
.filter(indexes => indexes.idxs.length === 2)
.reverse()
.reduce((finished, { chars, idxs }, i, pairs) => {
const next = pairs[i + 1] || { idxs: [] };
// Interspersed pairs are a single unit
if (idxs[0] - next.idxs[0] === 1 && idxs[1] - next.idxs[1] === 1) {
next.chars[0] += chars[0];
next.chars[1] += chars[1];
} else {
finished.push({ chars, idxs })
}
return finished;
}, []);
// No symmetrical pairs, center char is mouth, reject if even number of char
if (pairs.length === 0) {
if (face.length % 2 === 0) return NULL_ARRAY;
return [
face.slice(0, midIndex - 1),
face[midIndex - 1],
face[midIndex],
face[midIndex + 1],
face.slice(midIndex + 2)
];
}
// If more than one pair, innermost pair is a mouth if adjacent
const eyes = pairs.length > 1 && pairs[0].idxs[1] - pairs[0].idxs[0] === 1
? pairs[1]
: pairs[0];
return [
face.slice(0, eyes.idxs[0]),
eyes.chars[0],
face.slice(eyes.idxs[0] + eyes.chars[0].length, eyes.idxs[1]),
eyes.chars[1],
face.slice(eyes.idxs[1] + eyes.chars.length)
];
};
// Updates results for a part
const tally = (category, part) => {
if (!category[part]) {
category[part] = {
count: 1,
tags: tagKeys.reduce((tags, t) => ({ [t]: {}, ...tags }), {})
}
} else {
category[part].count++;
}
tagKeys.forEach(type => {
const tag = tags[type];
if (!tag) return;
const tagType = category[part].tags[type];
tagType[tag] = tagType[tag] + 1 || 1;
})
};
// Build results object
const resultKeys = [ 'outsides', 'arms', 'sides', 'insides', 'eyes', 'mouths' ];
const results = resultKeys.reduce((results, s) => ({ [s]: {}, ...results }), {});
const tagKeys = [ 'emotion', 'animal', 'OTHER' ];
const tags = tagKeys.reduce((tags, t) => ({ [t]: '', ...tags }), {});
const individualKeys = [ 'count', 'tags' ];
mojis.forEach(moji => {
// `%` is an escape character, we must omit any kaomoji including it
if (moji.includes('%')) return;
if (BAD_CHARS.find(code => moji.includes(String.fromCharCode(code)))) return;
// Update tags
if (moji.slice(0, 3) === '###') {
const newTags = moji.slice(3).split(',')
tags.emotion = newTags[0];
tags.animal = newTags[1] || 'NOT_ANIMAL';
tags.OTHER = newTags[2];
return;
}
const [ lt, leftSide, mid, rightSide, rt ] = parseSides(moji);
if (lt === null) return;
if (!leftSide|| !rightSide) return;
const [leftOut, leftArm, face, rightArm, rightOut] = parseArms(lt, mid, rt);
if (leftOut === null) return;
if (RIGHT_SIDES.concat(LEFT_SIDES).find(side => face.includes(side))) return;
if (SINGLE_CHAR_FACES.includes(face)) {
tally(results.eyes, face);
} else {
const [ leftIn, leftEye, mouth, rightEye, rightIn ] = parseFace(face);
if (leftIn === null) return;
if (!leftEye || !rightEye || !mouth) return;
tally(results.mouths, mouth);
tally(results.eyes, leftEye + '%' + rightEye);
tally(results.insides, leftIn + '%' + rightIn);
}
tally(results.arms, leftArm + '%' + rightArm);
tally(results.sides, leftSide + '%' + rightSide);
tally(results.outsides, leftOut + '%' + rightOut);
})
// Transform tags objects into an array
resultKeys.forEach(resultKey => {
Object.keys(results[resultKey]).forEach(part => {
const counts = results[resultKey][part];
counts.tags = tagKeys.slice(0, -1)
.map(categoryName => {
// Only keep a tag if it has a large enough majority in its category
const category = counts.tags[categoryName];
const catKeys = Object.keys(category);
const total = catKeys.reduce((t, c) => t + category[c], 0);
const [ count, largest ] = catKeys.reduce((kept, tag) => {
if (category[tag] <= kept[0]) return kept;
return [category[tag], tag];
}, [0, null]);
if (count / total > MINIMUM_TAG_SHARE) {
return largest;
}
return null;
})
.filter(tag => tag !== null && tag !== 'NOT_ANIMAL')
.concat(Object.keys(counts.tags.OTHER).filter(otherTag => {
return counts.tags.OTHER[otherTag] / counts.count > MINIMUM_TAG_SHARE;
}));
});
});
// Build sorted keys array
const sortedKeys = resultKeys
.reduce((keys, categoryName) => {
return keys.concat(Object.keys(results[categoryName]))
}, resultKeys)
.concat(individualKeys)
.sort();
// Write output
const secs = (Date.now() - startTime) / 1000;
console.log(`Processed ${mojis.length} kaomoji in ${secs.toFixed(3)} seconds`);
console.log();
console.log('Tags used:')
const tagsUsed = Object.keys(results).reduce((tags, category) => {
Object.keys(results[category]).forEach(part => {
results[category][part].tags.forEach(tag => tags[tag] = true)
})
return tags;
}, {});
console.log(Object.keys(tagsUsed).sort().join(', '));
console.log();
console.log('Writing file...');
fs.writeFileSync(OUTPUT, JSON.stringify(results, sortedKeys, SPACING), 'utf8');
console.log('Done.');