@met4citizen/headtts 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,589 @@
1
+ import { LanguageBase } from "./language.mjs"
2
+ import * as utils from "./utils.mjs";
3
+
4
+ /**
5
+ * @class American English language module
6
+ * @author Mika Suominen
7
+ */
8
+
9
+ class Language extends LanguageBase {
10
+
11
+ /**
12
+ * @constructor
13
+ */
14
+ constructor( settings = null ) {
15
+ super(settings);
16
+
17
+ // OVERRIDE FROM BASE CLASS:
18
+ // Allowed letters in upper case
19
+ // NOTE: Diacritics will be removed unless added to this object.
20
+ this.normalizedLettersUpper = {
21
+ 'A': 'A', 'B': 'B', 'C': 'C', 'D': 'D', 'E': 'E', 'F': 'F', 'G': 'G',
22
+ 'H': 'H', 'I': 'I', 'J': 'J', 'K': 'K', 'L': 'L', 'M': 'M', 'N': 'N',
23
+ 'O': 'O', 'P': 'P', 'Q': 'Q', 'R': 'R', 'S': 'S', 'T': 'T', 'U': 'U',
24
+ 'V': 'V', 'W': 'W', 'X': 'X', 'Y': 'Y', 'Z': 'Z', 'ß': 'SS', 'Ø': 'O',
25
+ 'Æ': 'AE', 'Œ': 'OE', 'Ð': 'D', 'Þ': 'TH', 'Ł': 'L'
26
+ };
27
+
28
+ // English words to phonemes, algorithmic rules adapted from:
29
+ // NRL Report 7948, "Automatic Translation of English Text to Phonetics by Means of Letter-to-Sound Rules" (1976)
30
+ // by HONEY SUE ELOVITZ, RODNEY W. JOHNSON, ASTRID McHUGH, AND JOHN E. SHORE
31
+ // Available at: https://apps.dtic.mil/sti/pdfs/ADA021929.pdf
32
+ this.rules = {
33
+ 'A': [
34
+ "[A] =AX", " [ARE] =AA R", " [AR]O=AX R", "[AR]#=EH R",
35
+ " ^[AS]#=EY S", "[A]WA=AX", "[AW]=AO", " :[ANY]=EH N IY",
36
+ "[A]^+#=EY", "#:[ALLY]=AX L IY", " [AL]#=AX L", "[AGAIN]=AX G EH N",
37
+ "#:[AG]E=IH JH", "[A]^+:#=AE", ":[A]^+ =EY", "[A]^%=EY",
38
+ " [ARR]=AX R", "[ARR]=AE R", " :[AR] =AA R", "[AR] =ER",
39
+ "[AR]=AA R", "[AIR]=EH R", "[AI]=EY", "[AY]=EY", "[AU]=AO",
40
+ "#:[AL] =AX L", "#:[ALS] =AX L Z", "[ALK]=AO K", "[AL]^=AO L",
41
+ " :[ABLE]=EY B AX L", "[ABLE]=AX B AX L", "[ANG]+=EY N JH", "[A]=AE"
42
+ ],
43
+
44
+ 'B': [
45
+ " [BE]^#=B IH1", "[BEING]=B IH", " [BOTH] =B OW1 TH",
46
+ " [BUS]#=B IH1 Z", "[BUIL]=B IH L", "[B]=B"
47
+ ],
48
+
49
+ 'C': [
50
+ " [CH]^=K", "^E[CH]=K", "[CH]=CH", " S[CI]#=S AY1",
51
+ "[CI]A=SH", "[CI]O=SH", "[CI]EN=SH", "[C]+=S",
52
+ "[CK]=K", "[COM]%=K AH M", "[C]=K"
53
+ ],
54
+
55
+ 'D': [
56
+ "#:[DED] =D IH D", ".E[D] =D", "#^:E[D] =T", " [DE]^#=D IH1",
57
+ " [DO] =D UW1", " [DOES]=D AH Z", " [DOING]=D UW IH NX",
58
+ " [DOW]=D AW1", "[DU]A=JH UW1", "[D]=D"
59
+ ],
60
+
61
+ 'E': [
62
+ "#:[E] =", "'^:[E] =", " :[E] =IY", "#[ED] =D", "#:[E]D =",
63
+ "[EV]ER=EH V", "[E]^%=IY", "[ERI]#=IY R IY", "[ERI]=EH R IH",
64
+ "#:[ER]#=ER", "[ER]#=EH R", "[ER]=ER", " [EVEN]=IY V EH N",
65
+ "#:[E]W=", "@[EW]=UW", "[EW]=Y UW", "[E]O=IY", "#:&[ES] =IH Z",
66
+ "#:[E]S =", "#:[ELY] =L IY", "#:[EMENT]=M EH N T", "[EFUL]=F UH L",
67
+ "[EE]=IY", "[EARN]=ER N", " [EAR]^=ER", "[EAD]=EH D", "#:[EA] =IY AX",
68
+ "[EA]SU=EH", "[EA]=IY", "[EIGH]=EY", "[EI]=IY", " [EYE]=AY", "[EY]=IY",
69
+ "[EU]=Y UW", "[E]=EH"
70
+ ],
71
+
72
+ 'F': [
73
+ "[FUL]=F UH1 L", "[F]=F"
74
+ ],
75
+
76
+ 'G': [
77
+ "[GIV]=G IH V", " [G]I^=G", "[GE]T=G EH", "SU[GGES]=G JH EH S",
78
+ "[GG]=G", " B#[G]=G", "[G]+=JH", "[GREAT]=G R EY T",
79
+ "#[GH]=", "[G]=G"
80
+ ],
81
+
82
+ 'H': [
83
+ " [HAV]=HH AE V", " [HERE]=HH IY R", " [HOUR]=AW ER", "[HOW]=HH AW",
84
+ "[H]#=HH", "[H]="
85
+ ],
86
+
87
+ 'I': [
88
+ " [IN]=IH N", " [I] =AY", "[IN]D=AY N", "[IER]=IY ER",
89
+ "#:R[IED] =IY D", "[IED] =AY D", "[IEN]=IY EH N", "[IE]T=AY EH",
90
+ " :[I]%=AY", "[I]%=IY", "[IE]=IY", "[I]^+:#=IH", "[IR]#=AY R",
91
+ "[IZ]%=AY Z", "[IS]%=AY Z", "[I]D%=AY", "+^[I]^+=IH",
92
+ "[I]T%=AY", "#^:[I]^+=IH", "[I]^+=AY", "[IR]=ER", "[IGH]=AY",
93
+ "[ILD]=AY L D", "[IGN] =AY N", "[IGN]^=AY N", "[IGN]%=AY N",
94
+ "[IQUE]=IY K", "[I]=IH"
95
+ ],
96
+
97
+ 'J': [
98
+ "[J]=JH"
99
+ ],
100
+
101
+ 'K': [
102
+ " [K]N=", "[K]=K"
103
+ ],
104
+
105
+ 'L': [
106
+ "[LO]C#=L OW1", "L[L]=", "#^:[L]%=AX L", "[LEAD]=L IY D", "[L]=L"
107
+ ],
108
+
109
+ 'M': [
110
+ "[MOV]=M UW V", "[M]=M"
111
+ ],
112
+
113
+ 'N': [
114
+ "E[NG]+=N JH", "[NG]R=NX G", "[NG]#=NX G", "[NGL]%=NX G AX L",
115
+ "[NG]=NX", "[NK]=NX K", " [NOW] =N AW1", "[N]=N"
116
+ ],
117
+
118
+ 'O': [
119
+ "[OF] =AX V", "[OROUGH]=ER OW", "#:[OR] =ER", "#:[ORS] =ER Z",
120
+ "[OR]=AO R", " [ONE]=W AH N", "[OW]=OW", " [OVER]=OW V ER",
121
+ "[OV]=AH V", "[O]^%=OW", "[O]^EN=OW", "[O]^I#=OW", "[OL]D=OW L",
122
+ "[OUGHT]=AO T", "[OUGH]=AH F", " [OU]=AW", "H[OU]S#=AW",
123
+ "[OUS]=AX S", "[OUR]=AO R", "[OULD]=UH D", "^[OU]^L=AH",
124
+ "[OUP]=UW P", "[OU]=AW", "[OY]=OY", "[OING]=OW IH NX", "[OI]=OY",
125
+ "[OOR]=AO R", "[OOK]=UH K", "[OOD]=UH D", "[OO]=UW", "[O]E=OW",
126
+ "[O] =OW", "[OA]=OW", " [ONLY]=OW N L IY", " [ONCE]=W AH N S",
127
+ "[ON'T]=OW N T", "C[O]N=AA", "[O]NG=AO", " ^:[O]N=AH",
128
+ "I[ON]=AX N", "#:[ON] =AX N", "#^[ON]=AX N", "[O]ST =OW",
129
+ "[OF]^=AO F", "[OTHER]=AH DH ER", "[OSS] =AO S", "#^:[OM]=AH M",
130
+ "[O]=AA"
131
+ ],
132
+
133
+ 'P': [
134
+ "[PH]=F", "[PEOP]=P IY1 P", "[POW]=P AW1", "[PUT] =P UH1 T",
135
+ "[P]=P"
136
+ ],
137
+
138
+ 'Q': [
139
+ "[QUAR]=K W AO R", "[QU]=K W", "[Q]=K"
140
+ ],
141
+
142
+ 'R': [
143
+ " [RE]^#=R IY", "[R]=R"
144
+ ],
145
+
146
+ 'S': [
147
+ "[SH]=SH", "#[SION]=ZH AX N", "[SOME]=S AH M", "#[SUR]#=ZH ER",
148
+ "[SUR]#=SH ER", "#[SU]#=ZH UW", "#[SSU]#=SH UW", "#[SED] =Z D",
149
+ "#[S]#=Z", "[SAID]=S EH D", "^[SION]=SH AX N", "[S]S=",
150
+ ".[S] =Z", "#:.E[S] =Z", "#^:##[S] =Z", "#^:#[S] =S",
151
+ "U[S] =S", " :#[S] =Z", " [SCH]=S K", "[S]C+=",
152
+ "#[SM]=Z M", "#[SN]'=Z AX N", "[S]=S"
153
+ ],
154
+
155
+ 'T': [
156
+ " [THE] =DH AX", "[TO] =T UW", "[THAT] =DH AE T", " [THIS] =DH IH S",
157
+ " [THEY]=DH EY", " [THERE]=DH EH R", "[THER]=DH ER", "[THEIR]=DH EH R",
158
+ " [THAN] =DH AE N", " [THEM] =DH EH M", "[THESE] =DH IY Z",
159
+ " [THEN]=DH EH N", "[THROUGH]=TH R UW", "[THOSE]=DH OW Z",
160
+ "[THOUGH] =DH OW", " [THUS]=DH AH S", "[TH]=TH", "#:[TED] =T IH D",
161
+ "S[TI]#N=CH", "[TI]O=SH", "[TI]A=SH", "[TIEN]=SH AX N",
162
+ "[TUR]#=CH ER", "[TU]A=CH UW", " [TWO]=T UW", "[T]=T"
163
+ ],
164
+
165
+ 'U': [
166
+ " [UN]I=Y UW N", " [UN]=AH N", " [UPON]=AX P AO N",
167
+ "@[UR]#=UH R", "[UR]#=Y UH R", "[UR]=ER", "[U]^ =AH",
168
+ "[U]^^=AH", "[UY]=AY", " G[U]#=", "G[U]%=", "G[U]#=W",
169
+ "#N[U]=Y UW", "@[U]=UW", "[U]=Y UW"
170
+ ],
171
+
172
+ 'V': [
173
+ "[VIEW]=V Y UW", "[V]=V"
174
+ ],
175
+
176
+ 'W': [
177
+ " [WERE]=W ER", "[WA]S=W AA", "[WA]T=W AA", "[WHERE]=WH EH R",
178
+ "[WHAT]=WH AA T", "[WHOL]=HH OW L", "[WHO]=HH UW", "[WH]=WH",
179
+ "[WAR]=W AO R", "[WOR]^=W ER", "[WR]=R", "[W]=W"
180
+ ],
181
+
182
+ 'X': [
183
+ " [X]=S", "[X]=K S"
184
+ ],
185
+
186
+ 'Y': [
187
+ "[YOUNG]=Y AH NX", " [YOU]=Y UW", " [YES]=Y EH S", " [Y]=Y",
188
+ "#^:[Y] =IY", "#^:[Y]I=IY", " :[Y] =AY", " :[Y]#=AY",
189
+ " :[Y]^+:#=IH", " :[Y]^#=AY", "[Y]=IH"
190
+ ],
191
+
192
+ 'Z': [
193
+ "[Z]=Z"
194
+ ]
195
+ };
196
+
197
+ const ops = {
198
+ '#': '[AEIOUY]+', // One or more vowels AEIOUY
199
+ // This one is not used: "'": '[BCDFGHJKLMNPQRSTVWXZ]+', // One or more consonants BCDFGHJKLMNPQRSTVWXZ
200
+ '.': '[BDVGJLMNRWZ]', // One voiced consonant BDVGJLMNRWZ
201
+ // This one is not used: '$': '[BDVGJLMNRWZ][EI]', // One consonant followed by E or I
202
+ '%': '(?:ER|E|ES|ED|ING|ELY)', // One of ER, E, ES, ED, ING, ELY
203
+ '&': '(?:[SCGZXJ]|CH|SH)', // One of S, C, G, Z, X, J, CH, SH
204
+ '@': '(?:[TSRDLZNJ]|TH|CH|SH)', // One of T, S, R, D, L, Z, N, J, TH, CH, SH
205
+ '^': '[BCDFGHJKLMNPQRSTVWXZ]', // One consonant BCDFGHJKLMNPQRSTVWXZ
206
+ '+': '[EIY]', // One of E, I, Y
207
+ ':': '[BCDFGHJKLMNPQRSTVWXZ]*', // Zero or more consonants BCDFGHJKLMNPQRSTVWXZ
208
+ ' ': '\\b' // Start/end of the word
209
+ };
210
+
211
+ // Reference: https://en.wikipedia.org/wiki/Arpabet
212
+ const ArpabetToIPA = {
213
+ // Rules
214
+ 'AO': 'ɔ', 'AA': 'ɑ', 'IY': 'i', 'UW': 'u', 'EH': 'ɛ', 'IH': 'ɪ',
215
+ 'UH': 'ʊ', 'AH': 'ə', 'AE': 'æ', 'AX': 'ə', 'EY': 'eɪ', 'AY': 'aɪ',
216
+ 'OW': 'oʊ', 'AW': 'aʊ', 'OY': 'ɔɪ', 'P': 'p', 'B': 'b', 'T': 't',
217
+ 'D': 'd', 'K': 'k', 'G': 'ɡ', 'CH': 'tʃ', 'JH': 'dʒ', 'F': 'f',
218
+ 'V': 'v', 'TH': 'θ', 'DH': 'ð', 'S': 's', 'Z': 'z', 'SH': 'ʃ',
219
+ 'ZH': 'ʒ', 'HH': 'h', 'M': 'm', 'N': 'n', 'NX': 'ŋ', 'NG': 'ŋ',
220
+ 'L': 'l', 'R': 'ɹ', 'ER': 'ɚ', 'W': 'w', 'WH': 'w', 'Y': 'j',
221
+
222
+ // CMU dictionary stressed vowels
223
+ "AA0": "ɑ", "AA1": "ˈɑ", "AA2": "ˌɑ", "AE0": "æ", "AE1": "ˈæ",
224
+ "AE2": "ˌæ", "AH0": "ə", "AH1": "ˈʌ", "AH2": "ˌʌ", "AO0": "ɔ",
225
+ "AO1": "ˈɔ", "AO2": "ˌɔ", "AW0": "aʊ", "AW1": "ˈaʊ", "AW2": "ˌaʊ",
226
+ "AY0": "aɪ", "AY1": "ˈaɪ", "AY2": "ˌaɪ", "EH0": "ɛ", "EH1": "ˈɛ",
227
+ "EH2": "ˌɛ", "ER0": "ɚ", "ER1": "ˈɝ", "ER2": "ˌɝ", "EY0": "eɪ",
228
+ "EY1": "ˈeɪ", "EY2": "ˌeɪ", "IH0": "ɪ", "IH1": "ˈɪ", "IH2": "ˌɪ",
229
+ "IY0": "i", "IY1": "ˈi", "IY2": "ˌi", "OW0": "oʊ", "OW1": "ˈoʊ",
230
+ "OW2": "ˌoʊ", "OY0": "ɔɪ", "OY1": "ˈɔɪ", "OY2": "ˌɔɪ", "UH0": "ʊ",
231
+ "UH1": "ˈʊ", "UH2": "ˌʊ", "UW0": "u", "UW1": "ˈu", "UW2": "ˌu"
232
+ };
233
+
234
+ const IPAToMisaki = {
235
+ "ɚ": ["ɜ","ɹ"], "ˈɝ": ["ˈɜ","ɹ"], "ˌɝ": ["ˌɜ","ɹ"],
236
+ "tʃ": ["ʧ"], "dʒ": ["ʤ"],
237
+ "eɪ": ["A"], "ˈeɪ": ["ˈA"], "ˌeɪ": ["ˌA"],
238
+ "aɪ": ["I"], "ˈaɪ": ["ˈI"], "ˌaɪ": ["ˌI"],
239
+ "aʊ": ["W"], "ˈaʊ": ["ˈW"], "ˌaʊ": ["ˌW"],
240
+ "ɔɪ": ["Y"], "ˈɔɪ": ["ˈY"], "ˌɔɪ": ["ˌY"],
241
+ "oʊ": ["O"], "ˈoʊ": ["ˈO"], "ˌoʊ": ["ˌO"],
242
+ "əʊ": ["Q"], "əʊ": ["ˈQ"], "əʊ": ["ˌQ"]
243
+ };
244
+
245
+ // Convert rules to regex
246
+ Object.keys(this.rules).forEach( key => {
247
+ this.rules[key] = this.rules[key].map( rule => {
248
+ const posL = rule.indexOf('[');
249
+ const posR = rule.indexOf(']');
250
+ const posE = rule.indexOf('=');
251
+ const strLeft = rule.substring(0,posL);
252
+ const strLetters = rule.substring(posL+1,posR);
253
+ const strRight = rule.substring(posR+1,posE);
254
+ const strPhonemes = rule.substring(posE+1);
255
+
256
+ const o = { regex: '', move: 0, phonemes: [] };
257
+
258
+ let exp = '';
259
+ exp += [...strLeft].map( x => ops[x] || x ).join('');
260
+ const ctxLetters = [...strLetters];
261
+ ctxLetters[0] = ctxLetters[0].toLowerCase();
262
+ exp += ctxLetters.join('');
263
+ o.move = ctxLetters.length;
264
+ exp += [...strRight].map( x => ops[x] || x ).join('');
265
+ o.regex = new RegExp(exp);
266
+
267
+ if ( strPhonemes.length ) {
268
+ strPhonemes.split(' ').forEach( ph => {
269
+ const ipa = ArpabetToIPA[ph];
270
+ if ( IPAToMisaki.hasOwnProperty(ipa) ) {
271
+ o.phonemes.push( ...IPAToMisaki[ipa] );
272
+ } else {
273
+ o.phonemes.push( ipa );
274
+ }
275
+ });
276
+ }
277
+
278
+ return o;
279
+ });
280
+ });
281
+
282
+ // Characters to phonemes
283
+ this.charactersToPhonemes = {
284
+ '!': "ˌɛkskləmˈAʃənpˌYnt", '"': "kwˈOt", '#': "pˈWndsˌIn", '%': "pɜɹsˈɛnt",
285
+ '&': "ˈæmpɜɹsˌænd", "'": "əpˈɑstɹəfi", '(': "ˈOpənpɜɹˈɛnθəsˌiz",
286
+ ')': "klˈOzpɜɹˈɛnθəsˌiz", '+': "plˈʊs", '-': 'dˈæʃ', '—': 'dˈæʃ', ',': "kˈɑmə",
287
+ '.': "dˈɑt", '/': "slˈæʃ", ':': "kˈOlən", ';': "sˈɛmikˈOlən", '?': "kwˈɛsʧənmˈɑɹk",
288
+ 'A': "ˈə", 'B': "bˈi", 'C': "sˈi", 'D': "dˈi", 'E': "ˈi", 'F': "ˈɛf",
289
+ 'G': "ʤˈi", 'H': "ˈAʧ", 'I': "I", 'J': "ʤˈA", 'K': "kˈA", 'L': "ˈɛl",
290
+ 'M': "ˈɛm", 'N': "ˈɛn", 'O': "ˈO", 'P': "pˈi", 'Q': "kjˈu", 'R': "ˈɑɹ",
291
+ 'S': "ˈɛs", 'T': "tˈi", 'U': "jˈu", 'V': "vˈi", 'W': "dˈʌbəlju",
292
+ 'X': "ˈɛks", 'Y': "wˈI", 'Z': "zˈi", '1': "wˈʌn", '2': "tˈu", '3': "θɹˈi",
293
+ '4': "fˈɔɹ", '5': "fˈIv", '6': "sˈɪks", '7': "sˈɛvən", '8': "ˈAt",
294
+ '9': "nˈIn", '0': "zˈiɹO", '{': "ˈOpɛnbɹˈAs", '}': "klˈOzbɹˈAs",
295
+ '$': "dˈɑlɜɹ", '€': "jˈuɹO"
296
+ };
297
+
298
+ // English number words
299
+ this.digits = ['OH', 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE'];
300
+ this.ones = ['','ONE','TWO','THREE','FOUR','FIVE','SIX','SEVEN','EIGHT','NINE'];
301
+ this.tens = ['','','TWENTY','THIRTY','FORTY','FIFTY','SIXTY','SEVENTY','EIGHTY','NINETY'];
302
+ this.teens = ['TEN','ELEVEN','TWELVE','THIRTEEN','FOURTEEN','FIFTEEN','SIXTEEN','SEVENTEEN','EIGHTEEN','NINETEEN'];
303
+
304
+ // Date & Time
305
+ this.months = [
306
+ "", "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY",
307
+ "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"
308
+ ];
309
+ this.days = [
310
+ "", "FIRST", "SECOND", "THIRD", "FOURTH", "FIFTH", "SIXTH", "SEVENTH", "EIGHT", "NINTH", "TENTH",
311
+ "ELEVENTH", "TWELFTH", "THIRTEENTH", "FOURTEENTH", "FIFTEENTH", "SIXTEENTH", "SEVENTEETH",
312
+ "EIGHTEENTH", "NINETEENTH", "TWENTIETH", "TWENTY-FIRST", "TWENTY-SECOND", "TWENTY-THIRD",
313
+ "TWENTY-FOURTH", "TWENTY-FIFTH", "TWENTY-SIXTH", "TWENTY-SEVENTH", "TWENTY-EIGHT", "TWENTY-NINTH", "THIRTIETH",
314
+ "THIRTY-FIRST"
315
+ ];
316
+
317
+ // Symbols to English
318
+ // TODO: Implement
319
+ this.symbols = {
320
+ '%': 'PERCENT', '€': 'EUROS', '&': 'AND', '+': 'PLUS',
321
+ '$': 'DOLLARS'
322
+ };
323
+
324
+ this.symbolsReg = /[%€&\+\$]/g;
325
+
326
+ if ( this.settings.trace ) {
327
+ utils.trace( 'Language module "en-us" initiated.' );
328
+ }
329
+ }
330
+
331
+
332
+ /**
333
+ * Read number digit-by-digit.
334
+ *
335
+ * @param {number|string} num Number
336
+ * @return {string} String
337
+ */
338
+ convertDigitByDigit(num) {
339
+ num = String(num).split("");
340
+ let numWords = "";
341
+ for(let m=0; m<num.length; m++) {
342
+ numWords += this.digits[num[m]] + " ";
343
+ }
344
+ numWords = numWords.substring(0, numWords.length - 1); //kill final space
345
+ return numWords;
346
+ }
347
+
348
+ /**
349
+ * Read number in sets of two (year).
350
+ *
351
+ * @param {number|string} num Number
352
+ * @return {string} String
353
+ */
354
+ convertSetsOfTwo(num) {
355
+ let firstNumHalf = String(num).substring(0, 2);
356
+ let secondNumHalf = String(num).substring(2, 4);
357
+ let numWords = this.convertTens(firstNumHalf);
358
+ numWords += " " + this.convertTens(secondNumHalf);
359
+ return numWords;
360
+ }
361
+
362
+ /**
363
+ * Read millions.
364
+ *
365
+ * @param {number|string} num Number
366
+ * @return {string} String
367
+ */
368
+ convertMillions(num){
369
+ if (num>=1000000){
370
+ return this.convertMillions(Math.floor(num/1000000))+" MILLION "+this.convertThousands(num%1000000);
371
+ } else {
372
+ return this.convertThousands(num);
373
+ }
374
+ }
375
+
376
+ /**
377
+ * Read thousands.
378
+ *
379
+ * @param {number|string} num Number
380
+ * @return {string} String
381
+ */
382
+ convertThousands(num){
383
+ if (num>=1000){
384
+ return this.convertHundreds(Math.floor(num/1000))+" THOUSAND "+this.convertHundreds(num%1000);
385
+ } else {
386
+ return this.convertHundreds(num);
387
+ }
388
+ }
389
+
390
+ /**
391
+ * Read hundreds.
392
+ *
393
+ * @param {number|string} num Number
394
+ * @return {string} String
395
+ */
396
+ convertHundreds(num){
397
+ if (num>99){
398
+ return this.ones[Math.floor(num/100)]+" HUNDRED "+this.convertTens(num%100);
399
+ } else {
400
+ return this.convertTens(num);
401
+ }
402
+ }
403
+
404
+ /**
405
+ * Read tens.
406
+ *
407
+ * @param {number|string} num Number
408
+ * @return {string} String
409
+ */
410
+ convertTens(num){
411
+ if (num < 10){
412
+ return (Number(num) != 0 && num.toString().startsWith("0") ? "OH " : "") + this.ones[Number(num)];
413
+ } else if (num>=10 && num<20) {
414
+ return this.teens[num-10];
415
+ } else {
416
+ return (this.tens[Math.floor(num/10)]+" "+this.ones[num%10]).trim();
417
+ }
418
+ }
419
+
420
+ /**
421
+ * Convert number to words. Try to decide how to read it.
422
+ *
423
+ * @param {number|string} num Number
424
+ * @return {string} String
425
+ */
426
+ convertNumberToWords(num) {
427
+ const n = parseFloat(num);
428
+ if (num == "0") {
429
+ return "ZERO";
430
+ } else if(num.toString().startsWith('0')){
431
+ return this.convertDigitByDigit(num).trim();
432
+ } else if (num < 0 ) {
433
+ return "MINUS " + this.convertNumberToWords( Math.abs(num).toString() ).trim();
434
+ } else if ( n && !Number.isInteger(n) ) {
435
+ const parts = n.toString().split('.');
436
+ return this.convertNumberToWords(parts[0]).trim() + " POINT " + this.convertDigitByDigit(parts[1]).trim();
437
+ } else if ((num<1000&&num>99)||(num>10000&&num<1000000)) { //read area and zip codes digit by digit
438
+ return this.convertDigitByDigit(num).trim();
439
+ } else if ((num > 1000 && num < 2000)||(num>2009&&num<3000)) { //read years as two sets of two digits
440
+ return (num % 100 != 0 ? this.convertSetsOfTwo(num).trim() : this.convertTens(num.toString().substring(0, 2)).trim() + " HUNDRED");
441
+ } else {
442
+ return this.convertMillions(num).trim();
443
+ }
444
+ }
445
+
446
+ /**
447
+ * Set the `text` to be spoken by analysing the part content.
448
+ *
449
+ * @param {Object} part Current part
450
+ * @param {number} i Index
451
+ * @param {Object[]} arr All the parts.
452
+ */
453
+ partSetText(part,i,arr) {
454
+
455
+ // Call super to pre-populate
456
+ super.partSetText(part,i,arr);
457
+
458
+ // Language specific implementation
459
+ switch( part.type ) {
460
+
461
+ case "text":
462
+ // Check if this is actually a number
463
+ const s = part.text;
464
+ if ( s ) {
465
+ const num = s.replace(/,/g, '').trim();
466
+ if ( !isNaN(num) && !isNaN(parseFloat(num)) ) {
467
+ part.text = this.convertNumberToWords(num) + " ";
468
+ }
469
+ }
470
+ break;
471
+
472
+ case "characters":
473
+ const phonetic = [];
474
+ const chars = [...part.value.toUpperCase()];
475
+ const len = chars.length;
476
+ for( let i=0; i<len; i++ ) {
477
+ const c = chars[i];
478
+ if ( this.charactersToPhonemes.hasOwnProperty(c) ) {
479
+ phonetic.push( this.charactersToPhonemes[c] );
480
+ } else {
481
+ phonetic.push(""); // Generates a space for unknown characters
482
+ }
483
+ }
484
+ part.phonemes = phonetic.join(" ") + " ";
485
+ break;
486
+
487
+ case "number":
488
+ part.text = this.convertNumberToWords(part.value) + " ";
489
+ break;
490
+
491
+ case "date":
492
+ const date = new Date(part.value);
493
+ const month = this.months[date.getMonth()];
494
+ const day = this.days[date.getDate()];
495
+ const year = this.convertSetsOfTwo(date.getFullYear());
496
+ part.text = month + " " + day + ", " + year + " ";
497
+ break;
498
+
499
+ case "time":
500
+ const time = new Date(part.value);
501
+ let hours = time.getHours(); // 0–23
502
+ const minutes = time.getMinutes(); // 0–59
503
+ const ampm = hours >= 12 ? 'P M' : 'A M';
504
+ hours = hours % 12;
505
+ hours = hours ? hours : 12; // 0 becomes 12
506
+ part.text = this.convertNumberToWords(hours) + " ";
507
+ part.text += this.convertNumberToWords(minutes) + " ";
508
+ part.text += ampm;
509
+ break;
510
+ }
511
+
512
+ }
513
+
514
+ /**
515
+ * Convert graphemes to phonemes.
516
+ *
517
+ * @param {string} s Word, normalized and in upper case
518
+ * @return {string[]} Array of phonemes
519
+ */
520
+ phonemizeWord(s) {
521
+
522
+ const phonemes = [];
523
+
524
+ // Dictionary lookup
525
+ if ( this.dictionary ) {
526
+ if ( this.dictionary.hasOwnProperty(s) ) {
527
+ if ( this.settings.trace ) {
528
+ utils.trace( 'Dictionary match: "' + s + '" => "' + this.dictionary[s].join("") + '"' );
529
+ }
530
+ phonemes.push( ...this.dictionary[s] );
531
+ s = "";
532
+ } else if ( s.length >= 5 ) {
533
+ const len = s.length;
534
+ for(let i=1; i<3; i++) {
535
+ const part = s.substring(0,len-i);
536
+ if ( this.dictionary.hasOwnProperty(part) ) {
537
+ if ( this.settings.trace ) {
538
+ utils.trace( 'Dictionary partial: "' + part + '" => "' + this.dictionary[part].join("") + '"' );
539
+ }
540
+ phonemes.push( ...this.dictionary[part] );
541
+ s = s.substring(len-i);
542
+ break;
543
+ }
544
+ }
545
+ }
546
+ }
547
+
548
+ // If still not resolved, use rules
549
+ if ( s.length ) {
550
+ const firstPhoneme = phonemes.length;
551
+ const chars = [...s];
552
+ let len = chars.length;
553
+ let i = 0;
554
+ while( i < len ) {
555
+ const c = chars[i];
556
+ if ( this.punctuations.hasOwnProperty(c) ) {
557
+ phonemes.push( this.punctuations[c] );
558
+ i++;
559
+ } else {
560
+ const ruleset = this.rules[c];
561
+ if ( ruleset ) {
562
+ for(let j=0; j<ruleset.length; j++) {
563
+ const rule = ruleset[j];
564
+ const test = s.substring(0, i) + c.toLowerCase() + s.substring(i+1);
565
+ let matches = test.match(rule.regex);
566
+ if ( matches ) {
567
+ rule.phonemes.forEach( ph => {
568
+ phonemes.push( ph );
569
+ });
570
+ i += rule.move;
571
+ break;
572
+ }
573
+ }
574
+ } else {
575
+ i++;
576
+ }
577
+ }
578
+ }
579
+ if ( this.settings.trace ) {
580
+ utils.trace( 'Rules: "' + s + '" => "' + phonemes.slice(firstPhoneme).join("") + '"' );
581
+ }
582
+ }
583
+
584
+ return phonemes;
585
+ }
586
+
587
+ }
588
+
589
+ export { Language };