This repository has been archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heteronyms-to-sqlite.mjs
292 lines (271 loc) · 6.54 KB
/
heteronyms-to-sqlite.mjs
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* Turn heteronyms.json into a database.
*
* Entries in heteronyms.json look like this if they were written in
* YAML:
*
* - title: word
* pns: [...]
* from: dict_revised
* props:
* - ...
* - title: word
* pns: [...]
* from: hakkadict
* props:
* - ...
* ...
*
* In the database, this becomes
*
* dicts:
* | id | name | lang |
* | hakkadict | ... | hak_TW |
* | dict_revised | ... | zh_TW |
*
* aliases ("false" and "true" are actually NULL and 1.0):
* | alias | het_id | exact |
* | abc | 1 | false |
* | def | 1 | false |
* | abc | 2 | false |
* | word | 1 | true |
* | word | 2 | true |
*
* heteronyms:
* | id | title | from | props |
* | 1 | word | hakkadict | {"def": "...", ...} |
* | 2 | word | dict_revised | {"def": "...", ...} |
*
* @name heteronyms-to-sqlite.js
*/
import fs from "node:fs";
import readline from "node:readline";
import Database from "better-sqlite3";
import { langs, dicts } from "./data.mjs";
import { pnCollect, pnToInputForm } from "./pn.mjs";
if (!fs.existsSync("heteronyms.json")) {
console.log("heteronyms.json should be generated first!");
process.exit(1);
}
if (fs.existsSync("entries.db")) {
fs.rmSync("entries.db");
}
const db = new Database("entries.db");
/**
* Parse the JSON in `path`, with the whole thing normalized to NFD form.
* @param {string} path
* @returns {any}
*/
function parse(path) {
return JSON.parse(fs.readFileSync(path).toString().normalize("NFD"))
}
function stringifyFields(thing) {
return {
title: thing.title,
from: thing.from,
lang: thing.lang,
props:
typeof thing.props === "string"
? thing.props
: JSON.stringify(thing.props),
};
}
// title: string
// from?: string
// null = this should not be shown in results
// props: object
db.exec(
`
PRAGMA user_version = 4;
CREATE TABLE langs (
"id" PRIMARY KEY,
"name" NOT NULL
);
CREATE TABLE dicts (
"id" PRIMARY KEY,
"name" NOT NULL,
"lang" REFERENCES langs("id")
);
CREATE TABLE heteronyms (
"id" INTEGER PRIMARY KEY,
"title" NOT NULL,
"from" REFERENCES dicts("id"),
"lang" REFERENCES langs("id"),
"props" NOT NULL
);
CREATE TABLE aliases (
"het_id" INTEGER REFERENCES heteronyms("id"),
"alias" NOT NULL,
"exact" INTEGER
);
CREATE TABLE links (
"from" NOT NULL,
"to" NOT NULL
);
`
);
/**
* Run `func` for each element of `array`, with a progress display, in
* a transaction.
* @param {array} array - The array to iterate over.
* @param {string} message - The message for the progress display.
* @param {function} func - Function called for each element.
* Receives two arguments, the element and the index.
*/
const EachPT = db.transaction((array, message = "", func) => {
// Whether we should print progress.
const verbose =
// Never verbose in CI; never verbose in Emacs except when in vterm
!process.env.CI &&
!(process.env.INSIDE_EMACS && !process.env.INSIDE_EMACS.includes("vterm"));
// Iterate through each entry, with optional verbose output.
let i = 0;
const length = array.length;
let last = { time: new Date().getTime(), i: i };
let diff = 0;
for (i = 0; i < length; i++) {
if (verbose) {
let now = new Date().getTime();
if (now - last.time > 1000) {
diff = i - last.i;
last.time = now;
last.i = i;
}
let progress = Math.floor(((i + 1) / length) * 100);
readline.cursorTo(process.stdout, 0);
process.stdout.write(
message + `${i + 1} / ${length} (${progress}%, ${diff}/s)`
);
}
func(array[i], i);
}
process.stdout.write("\n");
});
{
const langStmt = db.prepare(`
INSERT INTO
langs ("id", "name")
VALUES
(?, ?)`);
const dictStmt = db.prepare(`
INSERT INTO
dicts ("id", "name", "lang")
VALUES
(@id,@name,@lang)`);
EachPT(Object.entries(langs), "Preparing langs: ", ([id, name]) => {
langStmt.run(id, name);
});
EachPT(dicts, "Preparing dicts: ", (dict) => {
dictStmt.run(dict);
});
}
{
const heteronyms = parse("heteronyms.json").reverse();
const insertHet = db.prepare(`
INSERT INTO
heteronyms ("title","from","lang","props")
VALUES
(@title,@from,@lang,@props)`);
const insertAlias = db.prepare(`
INSERT INTO
aliases ("het_id","alias","exact")
VALUES
(@het_id,@alias,@exact)
`);
EachPT(heteronyms, "Inserting heteronyms into DB: ", (het, i) => {
insertHet.run(stringifyFields(het));
// SQLite integer primary key is 1-based
const het_id = i + 1;
insertAlias.run({
het_id,
alias: het.title,
exact: 1,
});
const pns = pnCollect(het).values();
for (const pn of pns) {
insertAlias.run({
het_id,
exact: 1,
alias: pn,
});
// Input versions.
// - don't duplicate if equal to original
// - don't bother for some dictionaries
if (
[
"moedict_twblg",
"chhoetaigi_itaigi",
"chhoetaigi_taioanpehoekichhoogiku",
"chhoetaigi_taijittoasutian",
].includes(het.from)
) {
const inputForm = pnToInputForm(pn);
if (inputForm !== pn) {
insertAlias.run({
het_id,
exact: null,
alias: inputForm,
});
}
}
}
});
}
{
const links = Object.values(parse("links.json"));
const insertLink = db.prepare(`
INSERT INTO
links ("from","to")
VALUES
(@from,@to)`);
EachPT(links, "Inserting links into DB: ", (entry) => {
insertLink.run(entry);
});
}
{
console.log("Creating table 'han'...");
db.exec(
`
CREATE TABLE a AS
SELECT DISTINCT
title,
json_tree.value AS radical
FROM heteronyms, json_tree(heteronyms.props)
WHERE json_tree.key = 'radical'
AND length(title) = 1;
CREATE TABLE b AS
SELECT DISTINCT
title,
cast(json_tree.value as integer) AS sc
FROM heteronyms, json_tree(heteronyms.props)
WHERE json_tree.key = 'sc'
AND length(title) = 1;
CREATE TABLE c AS
SELECT DISTINCT
title,
cast(json_tree.value as integer) AS nrsc
FROM heteronyms, json_tree(heteronyms.props)
WHERE json_tree.key = 'nrsc'
AND length(title) = 1;
CREATE TABLE han AS
SELECT DISTINCT
heteronyms.title,
a.radical,
b.sc,
c.nrsc
FROM heteronyms
LEFT JOIN a ON a.title = heteronyms.title
LEFT JOIN b ON b.title = heteronyms.title
LEFT JOIN c ON c.title = heteronyms.title
WHERE length(heteronyms.title) = 1
AND a.radical IS NOT NULL
AND b.sc IS NOT NULL
AND c.nrsc IS NOT NULL
ORDER BY b.sc;
DROP TABLE a;
DROP TABLE b;
DROP TABLE c;
VACUUM;
`
);
}