-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathsyllablize.ts
513 lines (451 loc) · 14.1 KB
/
syllablize.ts
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import { promises as fs } from "fs";
import {
createSonorityGraph,
getRandomSyllable,
loadSonorityGraph,
printGraph,
SonorityGraph,
SonorityGraphPart,
} from "./sonorityGraph";
import { oneSigFig, pad, progress } from "./util";
import * as util from "util";
async function getWordsByFrequency(): Promise<Array<string>> {
const content = await fs.readFile("./inputs/word_frequency.txt", {
encoding: "utf-8",
});
const lines = content.split("\n");
const words = lines.map((line) => line.split("\t")[0]);
// judging where we should stop counting things as worth including:
// for (const i of [
// 0, 1000, 10000, 30000, 60000, 80000, 100000, 200000, 300000,
// ]) {
// console.log(`${i}:\t${words.slice(i, i + 15).join(" ")}`);
// }
return words.slice(0, 60000);
}
// CMU dict gives input as ARPABET, similar to IPA
const APRABET_TO_IPA: { [key: string]: string } = {
AA: "ɑ", // ɑ or ɒ
AE: "æ",
AH: "ʌ",
AO: "ɔ",
AW: "aʊ",
AX: "əɹ", // ɚ
AXR: "ə",
AY: "aɪ",
EH: "ɛ",
ER: "ɛɹ", // ɝ
EY: "eɪ",
IH: "ɪ",
IX: "ɨ",
IY: "i",
OW: "oʊ",
OY: "ɔɪ",
UH: "ʊ",
UW: "u",
UX: "ʉ",
//
B: "b",
CH: "tʃ",
D: "d",
DH: "ð",
DX: "ɾ",
EL: "l̩",
EM: "m̩",
EN: "n̩",
F: "f",
G: "ɡ",
HH: "h",
H: "h",
JH: "dʒ",
K: "k",
L: "l",
M: "m",
N: "n",
NG: "ŋ",
NX: "ɾ̃",
P: "p",
Q: "ʔ",
R: "ɹ",
S: "s",
SH: "ʃ",
T: "t",
TH: "θ",
V: "v",
W: "w",
WH: "ʍ",
Y: "j",
Z: "z",
ZH: "ʒ",
};
/**
* Frequency ordered words -> syllable arrays
* very -> [ [v, ɛ], [ɹ, i] ]
* */
export type SyllablizedIPA = Array<[string, Array<Array<string>>]>;
const syllabilizedIpaFile = "outputs/syllablizedIPA.json";
/**
* Load previously written syllabized IPA from disk.
* If it doesn't exist, generate anew.
*/
export async function loadSyllabilizedIpa(): Promise<SyllablizedIPA> {
try {
const ipa = await fs.readFile(syllabilizedIpaFile, "utf8");
const result = JSON.parse(ipa) as SyllablizedIPA;
console.log("Loaded cached syllabilized IPA");
return result;
} catch (err) {
return generatedSyllabilizedIpa();
}
}
async function generatedSyllabilizedIpa(): Promise<SyllablizedIPA> {
const wordsByFrequency = await getWordsByFrequency();
const wordSet = new Set(wordsByFrequency);
console.log("loaded %d frequencies", wordSet.size);
const cmu_file = await fs.readFile("inputs/cmudict.0.6-syl.txt", "utf-8");
const lines = cmu_file.split("\n");
console.log(`converting ${lines.length} CMU words into IPA`);
let i = 0;
const ipaSyllables: { [key: string]: Array<Array<string>> } = {};
for (const line of lines) {
if (line.startsWith("#")) {
continue;
}
i += 1;
progress(i, lines.length, "");
if (line.trim() === "") continue;
const [wordUpper, sounds] = line.split(" ", 2);
if (/.*\(\d\)$/.test(wordUpper)) continue;
const syllables = sounds.split(".").map((syll) =>
syll
.trim()
.split(" ")
.map(
(phone) =>
APRABET_TO_IPA[/^([A-Z]+)\d*$/.exec(phone)![1]] ??
console.log("couldn't find phone ", phone)
)
);
const word = wordUpper.toLowerCase();
ipaSyllables[word] = syllables;
}
console.log();
console.log("sorting by frequency...");
const orderedResult: SyllablizedIPA = [];
// insert syllablized one by one
for (const word of wordsByFrequency) {
const found = ipaSyllables[word];
if (found) {
orderedResult.push([word, found]);
delete ipaSyllables[word];
}
}
// anything left in ipaSyllables we don't have a frequency for, but we still want to use
orderedResult.push(...Object.entries(ipaSyllables));
console.log("writing syllabized ipa result...");
await fs.writeFile(
syllabilizedIpaFile,
JSON.stringify(orderedResult, undefined, 2)
);
return orderedResult;
}
/**
* Construct ordered list of syllablized pronunciations
* map of word -> IPA split into syllables by |
* ordered by usage of the word (common words first)
* ['business', [ ["b", "ɪ", "z"], ["n", "ʌ", "s"] ]]
* words not in frequency list are appended to the end
*/
export async function loadSyllabalizedPronuncations(): Promise<
Array<[string, Array<Array<string>>]>
> {
const syllabilizedIpa = await loadSyllabilizedIpa();
const graph = await loadSonorityGraph(syllabilizedIpa);
await fs.writeFile(
"ui/public/syllableGraphDisplayData.json",
printGraph(graph)
);
console.log("wrote graphviz");
const existingOneSyllableWords: Array<string> = [];
for (const [_word, syllables] of syllabilizedIpa) {
if (syllables.length === 1) {
existingOneSyllableWords.push(
syllables.flatMap((s) => s.join("")).join("")
);
}
}
// Uncomment this to generate random syllables (takes a few minutes)
// await bulkGenerateSyllables(graph);
console.log("creating lots of random syllables");
await bulkGenerateSyllablesWithVariations(graph, existingOneSyllableWords);
console.log("-----------");
return syllabilizedIpa;
}
async function bulkGenerateSyllables(graph: SonorityGraph) {
const syllables = new Map<string, Array<string>>();
// many attempts with be repeats; 100 million typically generates ~190,000 syllables
// which is enough to cover our dictionary
let N = 100000000;
for (let j = 0; j < N; j++) {
const s = getRandomSyllable(graph);
const joined = s.join("");
if (syllables.has(joined)) {
continue;
}
syllables.set(joined, s);
if (j % 100 === 0) {
process.stdout.write("\u001b[2K");
progress(j, N, oneSigFig((100 * j) / N) + "% " + joined);
}
}
console.log();
console.log(`created ${syllables.size} unique syllables`);
console.log("writing random syllables");
await fs.writeFile(
"outputs/random_generated_syllables.json",
JSON.stringify([...syllables.entries()], undefined, 2)
);
}
// Tests / standalone
if (require.main === module) {
loadSyllabalizedPronuncations();
}
export type AlternativeCategory = "plural" | "gerund" | "past" | "actor";
export type AlternativesForSyllable = {
[key in AlternativeCategory]?: Array<string>;
};
export const alternants: { [key in AlternativeCategory]: string } = {
plural: "z", // bubbles
gerund: "ŋ", //bubbing
past: "d", // bubbled
actor: "ɹ", // bubbler
};
export type RandomSyllableInfo = {
syllable: Array<string>;
variations?: AlternativesForSyllable;
};
async function bulkGenerateSyllablesWithVariations(
graph: SonorityGraph,
existingOneSyllableWords: Array<string>
) {
const syllables = new Map<
string,
{ syllable: Array<string>; variations?: AlternativesForSyllable }
>();
const variations = new Set<string>();
for (const word of existingOneSyllableWords) {
// Make sure our random syllables aren't existing one-syllable words.
variations.add(word);
}
let numWithVariations = 0;
let numWithoutVariations = 0;
// many attempts with be repeats; 100 million typically generates ~150,000 syllables
// which is enough to cover our dictionary.
// we get slightly less using variations
let N = 100_000_000;
for (let j = 0; j < N; j++) {
const s = getRandomSyllable(graph);
const joined = s.join("");
if (syllables.has(joined) || variations.has(joined)) {
continue;
}
const result: RandomSyllableInfo = { syllable: s };
{
const foundVariations = generateSyllableAlternatives(
s,
graph,
syllables,
variations
);
if (foundVariations) {
result.variations = foundVariations;
for (const [alternant, variation] of Object.entries(foundVariations)) {
const joined = variation.join("");
variations.add(joined);
}
numWithVariations++;
} else {
numWithoutVariations++;
}
}
syllables.set(joined, result);
if (j % 100 === 0) {
process.stdout.write("\u001b[2K");
progress(j, N, oneSigFig((100 * j) / N) + "% " + joined);
}
}
console.log();
console.log(`created ${syllables.size} unique syllables`);
console.log(`${numWithVariations} with variations,`);
console.log(`${numWithoutVariations} without.`);
console.log("writing random syllables");
await fs.writeFile(
"outputs/random_generated_syllables_with_variations.json",
JSON.stringify([...syllables.entries()], undefined, 2)
);
}
const vowelRegex = /(ʌ|æ|u|ɔ|ɪ|ɑ|aɪ|i|oʊ|aʊ|eɪ|ɛɹ|ɛ|ʊ|ɔɪ)/;
/**
* Given a randomly generated syllable,
* Consider all alternants (plural: add z, past: add d, ...)
* Find where the alternant could be inserted to make a valid variation.
* Only tries to insert into the coda (so it's like a suffix)
* e.g. "blulb"
* extract coda "lb"
* for each alternant (z, d, ŋ, ɹ)
* find possible insertion points (*lb, l*b, lb*)
* compute probability of putting alternant in that place
* pick variant with highest
* if all 0, that variant is not allowed to exist
*
* This function also takes the set of existing variants/syllables so it won't
* duplicate existing already-generated syllables
*/
export function generateSyllableAlternatives(
syllable: Array<string>,
graph: SonorityGraph,
syllables: Map<string, unknown>,
variations: Set<string>
): AlternativesForSyllable | undefined {
let alternatives: AlternativesForSyllable | undefined = undefined;
// const log: typeof console.log = console.log;
const log: typeof console.log = () => undefined;
let codaStartIndex = 0;
let state = "onset";
for (const letter of syllable) {
if (state === "onset") {
if (vowelRegex.exec(letter)) {
state = "vowel";
}
} else if (state === "vowel") {
if (!vowelRegex.exec(letter)) {
state = "coda";
break;
}
}
codaStartIndex++;
}
const coda = syllable.slice(codaStartIndex);
const onsetAndVowel = syllable.slice(0, codaStartIndex);
log(syllable.join(""), "parts", onsetAndVowel.join(""), coda.join(""));
const codaGraph = graph.parts[2];
// probability counts should be at least this to consider it valid
// this helps avoid `zz` and other weird insertions
const MIN_SCORE = 2;
for (const [kind, alternant] of Object.entries(alternants) as Array<
[AlternativeCategory, string]
>) {
// one extra spot at the end
// blulb -> lb -> l b
// 0 1 2
// e.g.: try to insert 'z'
const scores: Array<[number, Array<string>]> = Array(coda.length + 1).fill([
0,
[],
]);
log(syllable.join(""), "+", alternant);
for (let spot = 0; spot < coda.length + 1; spot++) {
if (spot === 0) {
// beginning: zlb
const realization = [...onsetAndVowel, alternant, ...coda];
const joinedRealization = realization.join("");
log(" considering", joinedRealization);
if (
syllables.has(joinedRealization) ||
variations.has(joinedRealization)
) {
log(" already realized");
// this variant has been used before
continue;
}
// possible next steps after starting with the alternant: z->t, z->d, ...
const starting = codaGraph.get(alternant);
if (starting == null) {
log(" not a possible start");
continue;
}
// find which next step actually applies
const result = starting.find(([next, value]) => next === coda[0]);
if (result == null || result[1] <= MIN_SCORE) {
log(" not possible to insert continuation");
continue;
}
// if it's possible, take this score
scores[spot] = [result[1], realization];
} else {
// between letters: lzb
// or end: lbz
const realization = [
...onsetAndVowel,
...coda.slice(0, spot),
alternant,
...coda.slice(spot),
];
const joinedRealization = realization.join("");
log(" considering", joinedRealization);
if (
syllables.has(joinedRealization) ||
variations.has(joinedRealization)
) {
log(" already realized");
// this variant has been used before
continue;
}
const previous = coda[spot - 1];
const after = coda[spot]; // undefined at end
// possible next steps after starting with the previous: l->z, ...
const starting = codaGraph.get(previous);
if (starting == null) {
// should never happen, since we're here now...
log(" not a possible start (uh oh)", previous);
continue;
}
// find which next step actually applies the alternant
// l->z
const result = starting?.find(([next, value]) => next === alternant);
if (result == null || result[1] <= MIN_SCORE) {
log(
` alternant is not possible continuation (${previous} -> ${alternant})`
);
continue;
}
// additionally, after the alternant we must be able to resume the word
let continued = null;
if (after != null) {
// this is the end
const continuations = codaGraph.get(alternant);
const continued = continuations?.find(
([next, value]) => next === after
);
if (continued == null || continued[1] <= MIN_SCORE) {
log(
` alternant could not be continued by next (${alternant} -> ${after})`
);
continue;
}
}
// if it's possible, take the average score (or just the predecessor on the last letter)
scores[spot] = [
continued == null ? result[1] : (result[1] + continued[1]) / 2,
realization,
];
log(" score: ", scores[spot][0], scores[spot][1].join(""));
}
}
// pick the highest scoring alternant location
let max: [number, Array<string>] = [0, []];
for (const [score, realization] of scores) {
if (score > max[0]) {
max = [score, realization];
}
}
const [bestScore, realization] = max;
if (bestScore > 0) {
if (alternatives == null) {
alternatives = {};
}
alternatives[kind] = realization;
}
}
return alternatives;
}