-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_to_sql.js
65 lines (49 loc) · 1.43 KB
/
json_to_sql.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
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('table.db');
var fs = require('fs');
var frequencyTable = {};
try {
frequencyTable = JSON.parse(fs.readFileSync('table.json', 'utf8'));
} catch(err) {
console.error(err);
console.error('Failed to load table.json, to generate it run:\nnpm run-script table');
process.exit(1);
}
var bigramFrequency = frequencyTable.bigrams;
var monogramFrequency = frequencyTable.monograms;
db.serialize(function() {
db.run("CREATE TABLE monograms (word TEXT, log FLOAT)");
db.run("CREATE TABLE bigrams (word TEXT, log FLOAT)");
var len = Object.keys(monogramFrequency).length;
var i = 0;
db.run("BEGIN TRANSACTION");
for (var wordKey in monogramFrequency) {
if(wordKey.includes("\"")) {
continue;
}
var log = monogramFrequency[wordKey];
db.run("INSERT INTO monograms VALUES (\"" + wordKey + "\"," + log + ")");
if (i % 1000 == 0) {
console.log(i / len);
}
i++;
}
db.run("COMMIT TRANSACTION");
len = Object.keys(bigramFrequency).length;
i = 0;
db.run("BEGIN TRANSACTION");
for (var wordKey in bigramFrequency) {
if(wordKey.includes("\"")) {
continue;
}
var log = bigramFrequency[wordKey];
db.run("INSERT INTO bigrams VALUES (\"" + wordKey + "\"," + log + ")");
if (i % 1000 == 0) {
console.log(i / len);
}
i++;
}
db.run("COMMIT TRANSACTION");
console.log("CLOSING")
});
db.close();