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
|
import Database from "bun:sqlite";
import { Phoneme, Tone } from "../types/phonetics";
type Str = string | null;
type ItemType = "word" | "syllable" | "idiom";
class DatabaseHandler {
db: Database;
constructor() {
const dbPath = "/home/y/code/bun/ssr/waku/bulkdata/phon.db";
const db = new Database(dbPath, { create: true });
db.exec("PRAGMA journal_mode = WAL"); // Enable Write-Ahead Logging for better performance
db.exec("PRAGMA foreign_keys = ON");
this.db = db;
}
async init() {
const file = Bun.file("./prosodyschema.sql");
const sql = await file.text();
this.db.exec(sql);
}
// selects
fetchWords(words: string[]) {
const query = this.db.query(
`SELECT id FROM words where spelling IN (${words.map((w) => `'${w}'`).join(", ")})`,
);
return query.all() as Array<{ id: number }>;
}
// inserts
addLanguage(code: string, name: string) {
const query = this.db
.query(`INSERT OR IGNORE INTO languages(iso6392, english) VALUES(?, ?)`)
.run(code, name);
}
addPronunciation(
wordId: number | bigint,
ipa: string,
syllables: number,
tags: Str,
notes: Str,
) {
const query = this.db
.query(
`INSERT OR IGNORE INTO word_phonetics(word_id,ipa, syllables, tag, notes) VALUES(?, ?, ?, ?, ?)`,
)
.run(wordId, ipa, syllables, tags, notes);
}
addWordRhyme(wordId: number | bigint, ipa: string, lang: string, notes: Str) {
const query = this.db
.query(
`INSERT INTO word_rhymes(text, lang, notes) VALUES(?, ?, ?)
ON CONFLICT(text,lang) DO UPDATE SET
text = excluded.text
RETURNING rowid
`,
)
.get(ipa, lang, notes) as { id: number };
const query2 = this.db
.query(
`
INSERT INTO words_wrhymes(word_id, wrhyme_id) VALUES(?, ?)
`,
)
.run(wordId, query.id);
}
addIdiom(spelling: string, lang: string) {
const query = this.db.query(
`INSERT OR IGNORE INTO idioms(spelling, lang) VALUES(?, ?)`,
);
const res = query.run(spelling, lang);
return res;
}
findIdiomWords(spelling: string, idId: number | bigint) {
const split = spelling.split(" ");
const words = this.fetchWords(split);
console.log({ words });
const tx = this.db.transaction(() => {
for (const w of words) {
this.db
.query(
`
INSERT INTO words_idioms(word_id, idiom_id) VALUES(?, ?)
`,
)
.run(w.id, idId);
}
});
tx();
}
findIdiomsWords() {
const rows: any = this.db.query(`SELECT id, spelling FROM idioms`);
for (const row of rows) {
this.findIdiomWords(row.spelling, row.id);
}
}
addWord(
spelling: string,
lang: string,
frequency: number | null,
notes: Str,
) {
const query = this.db.query(
`INSERT OR IGNORE INTO words(spelling, lang, frequency, notes) VALUES(?, ?, ?, ?)`,
// `INSERT INTO words(spelling, lang) VALUES(?, ?)`,
);
const res = query.run(spelling, lang, frequency, notes);
const wordId = res.lastInsertRowid;
return wordId;
}
addSyllable(
wordId: number | bigint,
sylIdx: number,
lang: string,
ipa: string,
long: boolean,
text: string,
onset: Phoneme,
medial: Phoneme,
nucleus: Phoneme,
coda: Phoneme,
rhyme: Phoneme,
tone: Tone,
notes: Str,
) {
const tx = this.db.transaction(() => {
const onsetId = this.db
.query(
`INSERT INTO onsets(ipa, lang, text) VALUES(?, ?, ?)
ON CONFLICT(ipa, lang, text) DO UPDATE SET
text = excluded.text
RETURNING rowid
`,
)
.get(onset.ipa, lang, onset.spelling) as number;
const medialId = this.db
.query(
`INSERT INTO medials(ipa, lang, text) VALUES(?, ?, ?)
ON CONFLICT(ipa, lang, text) DO UPDATE SET
text = excluded.text
RETURNING rowid
`,
)
.get(medial.ipa, lang, medial.spelling) as number;
const nucleusId = this.db
.query(
`INSERT INTO nucleus(ipa, lang, text) VALUES(?, ?, ?)
ON CONFLICT(ipa, lang, text) DO UPDATE SET
text = excluded.text
RETURNING rowid
`,
)
.get(nucleus.ipa, lang, nucleus.spelling) as number;
const codaId = this.db
.query(
`INSERT INTO codas(ipa, lang, text) VALUES(?, ?, ?)
ON CONFLICT(ipa, lang, text) DO UPDATE SET
text = excluded.text
RETURNING rowid
`,
)
.get(coda.ipa, lang, coda.spelling) as number;
const rhymeId = this.db
.query(
`INSERT INTO rhymes(ipa, lang, text) VALUES(?, ?, ?)
ON CONFLICT(ipa, lang, text) DO UPDATE SET
text = excluded.text
RETURNING rowid
`,
)
.get(rhyme.ipa, lang, rhyme.spelling) as number;
const toneId = this.db
.query(
`INSERT INTO tones(ipa, lang, name, nums) VALUES(?, ?, ?, ?)
ON CONFLICT(ipa, lang) DO UPDATE SET
ipa = excluded.ipa
RETURNING rowid
`,
)
.get(tone.letters, lang, tone.name, tone.numbers) as number;
const query = this.db.query(
`INSERT INTO syllables(lang, ipa, long, text, onset, medial, nucleus, coda, rhyme, tone, notes) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
);
// TODO need a dual structure here for IPA and orto
const res = query.run(
lang,
ipa,
long,
text,
onsetId,
medialId,
nucleusId,
codaId,
rhymeId,
toneId,
notes,
);
const sylId = res.lastInsertRowid;
const ipaq = this.db.query(`
INSERT INTO syl_ipa(syl_id, ipa, onset, medial, nucleus, coda, rhyme, notes)
VALUES(?, ?, ?, ?, ?, ?, ?, ?)`);
ipaq.run(
sylId,
ipa,
onset.ipa,
medial.ipa,
nucleus.ipa,
coda.ipa,
rhyme.ipa,
null,
);
//
const res1 = this.db
.query(
`INSERT INTO syllables_words(syl_id, word_id, idx) VALUES(?, ?, ?)`,
)
.run(sylId, wordId, sylIdx);
//
return sylId;
});
const sylId = tx();
}
// reads
}
const db = new DatabaseHandler();
export default db;
|