-
Notifications
You must be signed in to change notification settings - Fork 81
/
index.js
executable file
·254 lines (213 loc) · 6.39 KB
/
index.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
#!/usr/bin/env node
const fs = require("fs");
const log = require("npmlog");
const stringify = require("csv-stringify");
const mkdirp = require("mkdirp");
const ProgressBar = require('progress');
const download = module.exports.download = require("./lib/download");
const aggregate = require("./lib/aggregate");
const analysis = require("./lib/analysis");
const writeFlatFiles = require("./lib/writeFlatFiles");
let store = function(opts) {
if (!opts.format) {
log.error("Please provide a --format param. Options are json, csv, csvs, jsonp, mongodb");
return;
}
opts.format = opts.format.toLowerCase();
if (opts.maxima || opts.normalize || opts.peak) {
opts.peaks = true;
}
// aggregate the data
let data = aggregate(opts);
// add any analyses we'd like
Object.keys(analysis).forEach(function(key) {
if (opts[key]) {
console.log("Running analysis", key);
analysis[key](data, opts);
}
});
// fs.writeFileSync("test.json", JSON.stringify(data, null, 2));
// return;
if (opts.format == "mongo" || opts.format == "mongodb") {
mongo(data, opts);
return;
} else {
writeFlatFiles(data, opts);
}
}
function flatfiles(data, opts) {
mkdirp("flat/individuals").then(made => {
log.info("Writing to flat files.");
var bar = new ProgressBar(':bar :percent', { total: data.length, complete: "#", width: 100 });
var roster = [];
if (opts.format === "csv" || opts.format === "csvs") {
if (!opts.type) {
opts.type = "values";
}
var headers = ["name", "gender"];
if (opts.pronunciation) {
headers.push("pronunciation");
headers.push("stressed");
}
for (var year = opts.start; year <= opts.end; year += 1) {
headers.push(year);
}
if (opts.format === "csv") {
var ws = fs.createWriteStream("./flat/names.csv");
ws.write(headers.join(",") + "\n");
}
data.forEach(function(d) {
var row = [];
// densify
headers.forEach(function(header) {
if (typeof header == "number") {
if (typeof d[opts.type][header] == "undefined") {
d[opts.type][header] = 0;
}
row.push(d[opts.type][header]);
} else {
row.push(d[header]);
}
});
if (opts.format === "csv") {
ws.write(row.join(",") + "\n");
} else {
stringify([headers, row], function(err, output) {
fs.writeFileSync("./flat/individuals/" + d._id + ".csv", output);
});
}
bar.tick();
});
} else {
if (opts.format == "jsonp") {
opts.callback = opts.callback || "ticallback";
}
data.forEach(function(d) {
if (opts.format == "jsonp") {
fs.writeFileSync("./flat/individuals/" + d._id + ".json", opts.callback + "('" + JSON.stringify(d) + "');");
} else {
fs.writeFileSync("./flat/individuals/" + d._id + ".json", JSON.stringify(d, null, 2));
}
roster.push(d._id);
bar.tick();
});
fs.writeFileSync("./flat/roster.json", JSON.stringify(roster));
// make a roster with only gender specifications for names that show up for both genders
var roster_short = [];
roster.forEach(function(d) {
var name = d.split("-")[0],
gender = d.split("-")[1];
console.log(name, gender);
if (gender == "F") {
if (roster.indexOf(name + "-M") != -1) {
roster_short.push(name + " (F)");
} else {
roster_short.push(name);
}
} else {
if (roster.indexOf(name + "-F") != -1) {
roster_short.push(name + " (M)");
} else {
roster_short.push(name);
}
}
});
fs.writeFileSync("./flat/roster_short.json", JSON.stringify(roster_short));
}
});
}
function mongo(data, opts) {
const MongoClient = require('mongodb').MongoClient;
const dbName = opts.db_name || 'babynames';
const mongo_URI = opts.mongo_uri || 'mongodb://localhost:27017';
const client = new MongoClient(mongo_URI, { useNewUrlParser: true });
console.log("Connecting to Mongo...");
// Connect to the db
client.connect(function(err) {
if(err) {
log.error(err);
return;
}
let bar = new ProgressBar(':bar :percent', { total: data.length + 1, complete: "#", width: 100 });
bar.tick();
const db = client.db(dbName);
const collection = db.collection("names");
console.log(`Successfully connected to Mongo and created "${ dbName }" database with a collection called "names."`);
console.log("Now adding data");
Object.values(data).forEach(function(d) {
d._id = d.id;
});
collection.insertMany(Object.values(data), function(err, result) {
if (err) {
log.error(err);
}
console.log(`Added ${ result.result.n } names.` );
client.close();
});
});
}
// aggregate by Nth phoneme (negative N counts from back)
let phonemes = function(data, opts) {
let N = opts.N || 0;
let phonemes = {};
data.forEach(function(d) {
if (d.pronunciation) {
let phoneme = d.pronunciation.split(" ").slice(N)[0];
if (!phoneme) {
console.log("Couldn't location a pronunciation for", d.name);
return 0;
}
if (!phonemes[phoneme]) {
phonemes[phoneme] = {
percents: {},
names: []
}
for (let y = opts.start; y <= opts.end; y += 1) {
phonemes[phoneme].percents[y] = 0;
}
}
phonemes[phoneme].names.push({
name: d.name,
peak: d.peaks.percents.value
});
for (let y = opts.start; y <= opts.end; y += 1) {
phonemes[phoneme].percents[y] += d.percents[y] || 0;
}
}
});
phonemes = Object.entries(phonemes).map(function(d) {
return {
phoneme: d.key,
names: d.value.names.sort(function(a, b) { return b.peak - a.peak; }).map(function(d) { return d.name; }),
percents: Object.entries(d.value.percents).filter(function(d) { return d.value != 0; })
}
});
fs.writeFileSync("./flat_files/phonemes.json", JSON.stringify(phonemes));
}
const commands = {
download: download,
store: store
};
// if called directly
if (require.main === module) {
let argv = require('minimist')(process.argv.slice(2));
log.level = argv.log || argv.log_level || "info";
if (!commands[argv._[0]]) {
log.error("Command not found. Options are: ", Object.keys(commands));
}
if (argv._[0] == "download") {
if (argv.states) {
commands[argv._[0]]({ dataset: "states" });
} else {
commands[argv._[0]]({});
}
} else {
if (!commands.hasOwnProperty(argv._[0])) {
console.log("Please pass a function (`download` or `store`) as the first argument.")
return;
}
commands[argv._[0]](argv);
}
} else {
module.exports = commands;
}