-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
process.js
93 lines (79 loc) · 3.05 KB
/
process.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
const fs = require('fs');
const wordcount = require('./data/wordcount.json');
const characters = require('./data/characters.json');
const characterGenders = require('./data/characters-gender-all.json');
const data = new Map();
// const houseNames = new Set(characters.characters.map((c) => c.houseName).flat().filter(Boolean));
// console.log(houseNames);
// const sets = ['isRoyal', 'parfOfKingsguard', 'wasKilled', 'hasKilled', 'hasSiblings', 'male', 'female', 'isMarriedOrEngaged', ...houseNames];
characters.characters.forEach((c) => {
if (!c.houseName || c.characterName.startsWith('Young')) {
return;
}
data.set(c.characterName, {
name: c.characterName,
episodes: 0,
words: 0,
sets: [
c.royal && 'royal',
// c.kingsguard && 'kingsguard',
c.killedBy && 'was killed',
c.killed && 'has killed',
// (c.sibling || c.siblings) && 'has siblings',
//c.marriedEngaged && 'has partner',
c.houseName,
].flat().filter(Boolean)
});
});
Object.entries(characterGenders).forEach(([gender, characters]) => {
characters.forEach((c) => {
if (data.has(c)) {
const d = data.get(c);
d.sets.push(gender);
}
});
});
wordcount.count.forEach((episode) => {
const map = new Map();
episode.text.forEach((c) => {
map.set(c.name, c.count + (map.get(c.name) || 0));
});
map.forEach((value, key) => {
const c = data.get(key);
if (!c) {
return;
}
c.episodes++;
c.words += value;
});
});
// assume characters with no words are direwolf
data.forEach((v) => {
if (v.words === 0 && v.sets.includes('Stark') && !v.name.startsWith('Brandon')) {
v.sets.push('direwolf');
}
})
const values = Array.from(data.values()).sort((a, b) => a.name.localeCompare(b.name));
console.log(values.slice(0, 10));
fs.writeFileSync('./data.json', JSON.stringify(values, null, 2));
// create a small version
{
const subset = values.filter((d) => (d.sets.includes('Lannister') || d.sets.includes('Stark')) && !(d.sets.includes('direwolf')))
.map((s) => ({ name: s.name, sets: s.sets.filter((si) => si !== 'Baratheon' && si !== 'Targaryen' && si !== 'Tully' && si !== 'has killed') }));
fs.writeFileSync('./data.small.json', JSON.stringify(subset, null, 2));
const sets = Array.from(new Set(subset.map((s) => s.sets).flat())).sort();
const rows = [
['Name', ...sets],
...subset.map((v) => [v.name, ...sets.map((s) => v.sets.includes(s) ? 1 : 0)])
].map((row) => row.map(String).join(',')).join('\n');
fs.writeFileSync('./data.small.csv', rows);
}
// create a csv version
{
const sets = Array.from(new Set(values.map((s) => s.sets).flat())).sort();
const rows = [
['Name', ...sets, 'Words', 'Episodes'],
...values.map((v) => [v.name, ...sets.map((s) => v.sets.includes(s) ? 1 : 0), v.words, v.episodes])
].map((row) => row.map(String).join(',')).join('\n');
fs.writeFileSync('./data.csv', rows);
}