-
Notifications
You must be signed in to change notification settings - Fork 1
/
qs-dict.js
397 lines (349 loc) · 12.8 KB
/
qs-dict.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
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
/*
Copyright 2018 akovaski
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const dictURL = "cmudict/cmudict.txt";
const transformURL = "transformation.json?v=7";
const JSONtestTransformURL = "test/test-transform.json";
let CMUdict = new Map();
let wordReplace = new Map();
let phenomeReplace = [];
let retrieveStat = {};
window.addEventListener("load",pageLoad);
function pageLoad() {
let hash = window.location.hash.substr(1);
if (hash) {
let inputElem = document.getElementById("word-input");
inputElem.value = hash;
}
// Request the CMU dictionary
let dictReq = new XMLHttpRequest();
let dictProgressElem = document.getElementById("dict-progress");
dictReq.addEventListener("progress", generateProgressHandler(dictProgressElem));
dictReq.addEventListener("readystatechange", function() {
if (dictReq.readyState === 4) {
let loaded = false;
if (dictReq.status === 200) {
try {
// Construct the CMUdict dictionary.
saveDictionary(dictReq.responseText);
retrieveDone("dict");
dictProgressElem.value = 1;
loaded = true;
} catch(e) {
console.log(e);
}
}
let dictStatusElem = document.getElementById("dict-status");
if (loaded) {
dictStatusElem.innerText = "Dictionary Loaded.";
} else {
dictStatusElem.innerText = "Dictionary failed to load";
}
}
});
dictReq.open("GET",dictURL, true);
dictReq.responseType = "text";
dictReq.send();
// Request the transformation json file
let transReq = new XMLHttpRequest();
let transProgressElem = document.getElementById("transform-progress");
transReq.addEventListener("progress", generateProgressHandler(transProgressElem));
transReq.addEventListener("readystatechange", function() {
if (transReq.readyState === 4) {
let loaded = false;
if (transReq.status === 200) {
// Construct the CMUdict dictionary.
try {
saveTransform(transReq.response);
retrieveDone("trans");
transProgressElem.value = 1;
loaded = true;
} catch(e) {
console.log(e);
}
}
let transStatusElem = document.getElementById("transform-status");
if (loaded) {
transStatusElem.innerText = "Transform Loaded.";
} else {
transStatusElem.innerText = "Transform failed to load";
}
}
});
transReq.open("GET",transformURL, true);
transReq.responseType = "json";
transReq.send();
let titleElem = document.getElementById("title");
let alternativeText = " "; // Quickscript Dictionary
titleElem.addEventListener("click", function() {
let curText = titleElem.innerText;
titleElem.innerText = alternativeText;
alternativeText = curText;
});
}
function retrieveDone(reqID) {
retrieveStat[reqID] = true;
if (retrieveStat["dict"] && retrieveStat["trans"]) {
let inputElem = document.getElementById("word-input");
let buttonElem = document.getElementById("word-button");
// Setup the HTML elements to respond to input.
function submitWordAndAddHistory() {
history.pushState("", "", "#" + inputElem.value);
submitWord();
}
inputElem.addEventListener("keydown",function(event){
let e = event || window.event;
if(e.keyCode == 13){
submitWordAndAddHistory();
}
});
buttonElem.addEventListener("click", function(){
submitWordAndAddHistory();
});
inputElem.disabled=false;
buttonElem.disabled=false;
if (inputElem.value) {
submitWord(); // The input value may be specified by navigating to the page via a
}
// execute whenever the url hash is updated (going back/forward through history)
// or when pushstate is called
window.addEventListener("popstate", function(evt) {
let hash = document.location.hash.substr(1);
if (hash) {
inputElem.value = hash;
submitWord();
}
})
}
}
function generateProgressHandler(progressElem) {
return function(evt) {
if (evt.lengthComputable) {
let percentComplete = (evt.loaded / evt.total);
progressElem.value = percentComplete;
} else {
let remaining = 1 - progressElem.value;
progressElem.value += remaining/4;
}
};
}
function saveDictionary(dictText) {
let lines = dictText.trim().split("\n");
for (let line of lines) {
let re = /^(.*?)(?:\([0-9]+\))?\s(.*)$/;
let matches = re.exec(line);
if (matches) {
let word = matches[1];
let phonemes = matches[2];
let wordEntry = CMUdict.get(word) || [];
try {
wordEntry.push(phonemes);
}catch (e) {
console.log(e);
}
CMUdict.set(word, wordEntry);
} else {
console.log("line does not match: " + line);
}
}
}
let qsm = { // Quickscript map
"pea": "\uE650",
"bay": "\uE651",
"tea": "\uE652",
"day": "\uE653",
"key": "\uE654",
"gay": "\uE655",
"thaw": "\uE656",
"they": "\uE657",
"fee": "\uE658",
"vie": "\uE659",
"see": "\uE65A",
"zoo": "\uE65B",
"she": "\uE65C",
"j'ai": "\uE65D",
"cheer": "\uE65E",
"jay": "\uE65F",
"ye": "\uE660",
"way": "\uE661",
"he": "\uE662",
"why": "\uE663",
"ing": "\uE664",
"may": "\uE665",
"no": "\uE666",
"low": "\uE667",
"roe": "\uE668",
"it": "\uE670",
"eat": "\uE671",
"et": "\uE672",
"eight": "\uE673",
"at": "\uE674",
"i": "\uE675",
"ah": "\uE676",
"awe": "\uE677",
"ox": "\uE678",
"oy": "\uE679",
"utter": "\uE67A",
"out": "\uE67B",
"owe": "\uE67C",
"foot": "\uE67D",
"ooze": "\uE67E",
};
function convertNamesToQS(str) {
return str.split(" ").map(name => qsm[name] || name).join("");
}
function convertQSToNames(str) {
let reverseLookup = {};
for (let name in qsm) {
reverseLookup[qsm[name]] = name;
}
return str.split("").map(qsChar => reverseLookup[qsChar] || qsChar).join(" ");
}
function saveTransform(transJSON) {
// Load the direct English -> Quickscript translations.
for (let replace of transJSON["wordReplace"]) {
let qsWords = replace[1];
wordReplace.set(replace[0], qsWords);
}
// Load the regex transformations.
for (let replace of transJSON["regexReplace"]) {
let reStr = replace[0];
let substituteStr = replace[1];
let flag = replace[2];
let wordContitionStr = replace[3];
if (flag === "plain") {
reStr = "\\b" + reStr + "\\b";
flag = "g";
} else if (flag === "prefix") {
reStr = "^([ a-z]*)" + reStr + "(?= )";
flag = "";
substituteStr = "$1 " + substituteStr;
} else if (flag === "suffix") {
reStr = "(?!^)\\b" + reStr + "(?=[ a-z]*$)";
flag = "";
}
let re = new RegExp(reStr, flag);
let wordConditionRe;
if (wordContitionStr) {
wordConditionRe = new RegExp(wordContitionStr);
}
phenomeReplace.push([re, substituteStr, wordConditionRe]);
}
}
function submitWord() {
let inputElem = document.getElementById("word-input");
let phenomElem = document.getElementById("phenomes-output");
let QSOutElem = document.getElementById("quickscript-output");
let notFoundElem = document.getElementById("word-not-found-display");
let word = inputElem.value;
let results = getQSTranscripts(word);
let phenomes = results[0];
let manualTranscripts = results[1];
let qsTranscripts = results[2];
if (manualTranscripts.length === 0 && qsTranscripts.length === 0) {
notFoundElem.innerText = "<Word not found: \"" + word + "\">";
} else {
notFoundElem.innerText = "";
}
phenomElem.innerText = phenomes.join("\n");
QSOutElem.innerHTML = "";
addQSOut(QSOutElem, manualTranscripts, "resultPreferred");
addQSOut(QSOutElem, qsTranscripts, "resultNormal");
}
function getQSTranscripts(wordInput) {
let word = wordInput.toLowerCase();
let manualTranscripts = (wordReplace.get(word) || []).map(convertNamesToQS);
let phenomes = CMUdict.get(word) || [];
let qsTranscripts = [];
for (let phenomeStr of phenomes) {
let qsStr = transformPhenome(phenomeStr, word);
if (!manualTranscripts.includes(qsStr) && !qsTranscripts.includes(qsStr)) {
qsTranscripts.push(qsStr);
}
}
return [phenomes, manualTranscripts, qsTranscripts];
}
function transformPhenome(phenomeStr, word, usedTransforms) {
// Transform the phenomes in order.
for (let transform of phenomeReplace) {
let wordConditionRe = transform[2];
if (wordConditionRe && !word.match(wordConditionRe)) {
continue;
}
let updatedStr = phenomeStr.replace(transform[0], transform[1]);
if (wordConditionRe && phenomeStr !== updatedStr) {
word = word.replace(wordConditionRe, "");
}
if (usedTransforms && phenomeStr !== updatedStr) {
usedTransforms.push(transform);
}
phenomeStr = updatedStr;
}
phenomeStr = convertNamesToQS(phenomeStr);
return phenomeStr;
}
function addQSOut(elem, qsStrings, styleClass) {
for (let str of qsStrings) {
let div = document.createElement("div");
div.innerText = str;
div.className = styleClass;
elem.appendChild(div);
}
}
// not called anywhere, just used for testing
function testTransform() {
// Request the transformation json file
let testReq = new XMLHttpRequest();
testReq.addEventListener("readystatechange", function() {
if (testReq.readyState === 4) {
let loaded = false;
if (testReq.status === 200) {
// Construct the CMUdict dictionary.
try {
let tests = testReq.response;
let numFails = 0;
for (let test of tests) {
// the test file should have words and verified transformations in the format of:
// [
// ["word", ["way utter roe day", ...]] ...
// ]
let word = test[0];
let spellings = test[1].map(convertNamesToQS);
let results = getQSTranscripts(word);
let transcripts = results[1].concat(results[2]);
let same = spellings.length === transcripts.length &&
spellings.every((val)=>transcripts.includes(val));
if (!same) {
console.log("Mismatch: " + word);
console.log("Expected: " + JSON.stringify(spellings.map(convertQSToNames)));
console.log("Actual: " + JSON.stringify(transcripts.map(convertQSToNames)));
numFails += 1;
}
}
if (numFails > 0) {
console.log(numFails + " tests failed.");
} else {
console.log("All " + tests.length + " tests ran successfully.");
}
} catch(e) {
console.log(e);
}
} else {
console.log("Could not load test file");
}
}
});
// request tests, bypass the cache
testReq.open("GET",JSONtestTransformURL + "?" + Date.now(), true);
testReq.responseType = "json";
testReq.send();
}