-
Notifications
You must be signed in to change notification settings - Fork 1
/
arpabet.js
189 lines (177 loc) · 3.25 KB
/
arpabet.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
"use strict";
let ArpabetToIpaTable = {
"AA": "ɑ",
"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",
// arpasing doesn't use 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": "ʒ",
};
let ArpabetToIpaTableCaseInsensitive = {...ArpabetToIpaTable};
for (const key in ArpabetToIpaTable){
ArpabetToIpaTableCaseInsensitive[key.toLowerCase()] = ArpabetToIpaTable[key];
}
let arr = [
"aɪ",
"aʊ",
"b",
"d",
"dʒ",
"eɪ",
"f",
"h",
"i",
"j",
"k",
"l",
"l̩",
"m",
"m̩",
"n",
"n̩",
"oʊ",
"p",
"s",
"t",
"tʃ",
"u",
"v",
"w",
"z",
"æ",
"ð",
"ŋ",
"ɑ",
"ɔ",
"ɔɪ",
"ə",
"ɚ",
"ɛ",
"ɝ",
"ɡ",
"ɨ",
"ɪ",
"ɹ",
"ɾ",
"ɾ̃",
"ʃ",
"ʉ",
"ʊ",
"ʌ",
"ʍ",
"ʒ",
"ʔ",
"θ"
];
function compareKeys(a, b){
// longer text comes first
if (a.length != b.length){
return b.length - a.length;
}
// sort the rest alphabetically
if (a < b) {return -1;}
if (a > b) {return 1; }
return 0;
}
function* maximumMatch(str, dict){
if (!str || !str.length){return;}
let keys = Object.keys(dict);
keys.sort(compareKeys);
// iterate over the string to tokenize.
let i = 0 ;
while(i < str.length){
let found = false;
// try tokens from longest to shortest.
for (let j = 0; j < keys.length; j++){
let key = keys[j];
if (str.startsWith(key, i)){
found = true;
yield key;
i += key.length;
}
}
// if no token found, return a single character.
if (!found){
yield str.slice(i, i+1);
i++;
}
}
}
function dictToRegEx(dict){
let keys = Object.keys(dict);
keys.sort(compareKeys);
let pattern = keys.join("|");
let re = new RegExp(`$(pattern)`, "g");
return re;
}
function ArpabetToIpa(str){
let table = ArpabetToIpaTableCaseInsensitive;
function* strip_spaces (itor){
let last = [];
for (const token of itor){
last.push(token);
while (last.length >= 3){
if (last[0] in table && last[1] == " " && last[2] in table){
yield last.shift();
last.shift();
} else {
yield last.shift();
}
} // while
} // for
yield* last;
}
let tokens = [...strip_spaces(maximumMatch(str, table))];
return tokens.map(x=>(x in table? table[x]: x)).join("");
}
let srcPhon = ko.observable();
let dest = ko.computed(function _dest(){ return ArpabetToIpa(srcPhon()); } );
let viewmodel = {
srcPhon, dest, srcPhonType:"", destType: "International Phonetic Alphabet"
};
ko.applyBindings(viewmodel);