-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
182 lines (150 loc) · 4.7 KB
/
db.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
const Database = require('better-sqlite3');
const fs = require('fs');
const readline = require('readline');
class Backend {
constructor() {
this.db = new Database('etymology.db', { verbose: console.log });
this.createEngTables()
this.maybePopulateEnTables()
}
close() {//todo check with better-sqlite
// close the database connection
this.db.close((err) => {
if (err) {
return console.error(err.message);
}
console.log('Close the database connection.');
});
}
createEngTables() {
this.db.exec(`
CREATE TABLE IF NOT EXISTS en_root (
root TEXT UNIQUE,
def TEX
);
CREATE TABLE IF NOT EXISTS en_word (
word TEXT UNIQUE,
def TEXT
);
CREATE TABLE IF NOT EXISTS en_root_to_word (
root INTEGER,
word INTEGER
)
`);
}
getAllRoots() {
const stmt = this.db.prepare('SELECT * FROM en_root');
return stmt.all().map((row) => row.root)
}
findWord(word) {
let row = this.db.prepare('SELECT * FROM en_word WHERE word = ?').get(word)
return row
}
findRoot(root) {
let row = this.db.prepare('SELECT * FROM en_root WHERE root = ?').get(root)
return row
}
findWordsForRoot(root) {
const stmt = this.db.prepare('SELECT * FROM en_root_to_word WHERE root = ?');
return stmt.all(root).map((row) => row.word)
}
findRootsForWord(word) {
const stmt = this.db.prepare('SELECT * FROM en_root_to_word WHERE word = ?');
return stmt.all(word).map((row) => row.root)
}
findDefForEither(datum, isRoot) {
let row = isRoot ? this.findRoot(datum) : this.findWord(datum)
return row.def
}
//todo optimize
//FIXME recursion
findNDegreesOutFrom(datum, isRoot, N = 1) {
let output = {
'isRoot' :isRoot,
'def': this.findDefForEither(datum, isRoot),
'selected': datum,
};
let [nodes, links] = this.findChildren(datum, isRoot, N)
output.nodes = this.pruneNodes(nodes)
output.links = this.pruneLinks(links)
return output;
}
pruneNodes(nodes, selectedDatum) {
return nodes.reduce((acc, current) => {
const x = acc.find(item => {
return item.datum === current.datum
});
if (!x) {
return acc.concat([current]);
} else {
return acc;
}
}, []);
}
pruneLinks(links) {
return links.reduce((acc, current) => {
const x = acc.find(item => {
return (item.source === current.source && item.target === current.target)
|| (item.target === current.source && item.source === current.target)
});
if (!x) {
return acc.concat([current]);
} else {
return acc;
}
}, []);
}
findChildren(datum, isRoot, maxDepth, depth = 0) {
if (depth >= maxDepth) {
return [[],[]];
}
let children = isRoot ? this.findWordsForRoot(datum) : this.findRootsForWord(datum);
let mapped_words = [];
let mapped_links = [];
for (const child of children) {
mapped_words.push({
datum: child,
isRoot: !isRoot
})
mapped_links.push({
source: datum,
target: child,
})
let [grandchildren, child_links] = this.findChildren(child, !isRoot, maxDepth, depth+1)
mapped_words = mapped_words.concat(grandchildren)
mapped_links = mapped_links.concat(child_links)
}
return [mapped_words, mapped_links];
}
maybePopulateEnTables() {
let row = this.db.prepare('SELECT COUNT(ALL) FROM en_root').get(),
count = Object.entries(row)[0][1]; //there has got to be a better way
if (count > 0) return;
let readInterface = readline.createInterface({
input: fs.createReadStream('data/en_roots.txt'),
console: false
})
let insertRoot = this.db.prepare('INSERT INTO en_root (root, def) VALUES (@root, @def)');
let insertWord = this.db.prepare('INSERT OR IGNORE INTO en_word (word, def) VALUES (@word, @def)');
let insertPair = this.db.prepare('INSERT INTO en_root_to_word (root, word) VALUES (@root, @word)');
this.db.transaction(() => {
readInterface.on('line', function(line) {
line = JSON.parse(line)
console.log("reading: " + line.root)
let root = escape(line.root.trim())
let root_def = escape(line.def.trim())
try {
insertRoot.run({"root": root, "def": root_def});
for (var i = 0; i < line.wordList.length; i++) {
let word = escape(line.wordList[i].trim());
insertWord.run({"word": word, "def": "FIXME"});
insertPair.run({"root": root, "word": word})
}
} catch(err) {
console.log(err)
}
});
})();
}
}
module.exports = new Backend()