forked from multimeric/wordnet-sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.js
executable file
·70 lines (60 loc) · 2.27 KB
/
setup.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
#!/usr/bin/env node
const _ = require('lodash');
const fs = require('fs');
const readline = require('readline');
const sqlite3 = require('sqlite3').verbose();
//Make the database and run it serially
var db = new sqlite3.Database('wordnet.dict');
db.serialize(function () {
//Create the main table
db.run("DROP TABLE IF EXISTS words");
db.run("CREATE TABLE words (id TEXT, lex TEXT, word TEXT, definition TEXT, type TEXT, fr TEXT)");
db.run("CREATE INDEX id_idx ON words (id ASC)");
db.run("BEGIN TRANSACTION");
//Prepare the insert statement
var stmt = db.prepare("INSERT INTO words VALUES (?, ?, ?, ?, ?, NULL)");
//For each input file
var types = ["adj", "adv", "noun", "verb"];
const typeCodes = {
adj: 'a',
adv: 'r',
noun: 'n',
verb: 'v',
}
var counter = 0;
types.forEach(function (type) {
//Read each line of the file
var rl = readline.createInterface({input: fs.createReadStream('dict/data.' + type)});
var rows = 0;
//Find the relevant variables and insert them
rl.on('line', function (line) {
//Skip the comment lines
if (line.substr(0, 2) === " ")
return;
//Split the line to find relevant variables
var sections = line.split(/\s+\|\s+/);
var cols = sections[0].split(/\s/);
var words = cols
.filter(col => col.match(/^[^\d!"#$%&'()\*\+\-\.,\/:;<=>?@\[\\\]^_`{|}~]/gm)) // doesn't start with number or special letter
.filter(col => col.length > 1); // has two or more charactors
var id = cols[0] + '-' + typeCodes[type];
//Preserve cols[4] which always has a vaild meaning
if(words.indexOf(cols[4]) === -1){
words.push(cols[4])
}
var definitions = sections[1].split(/;/);
stmt.run(id, cols[1], _.join(words, ' ; '), definitions[0], type);
rows++;
});
rl.on('close', function () {
counter++;
if (counter >= types.length) {
stmt.finalize(()=>{
db.run("END");
db.exec("VACUUM");
db.close();
});
}
});
});
});