@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.
- package/LICENSE +21 -0
- package/README.md +546 -0
- package/dictionaries/en-us.txt +125829 -0
- package/dictionaries/fi.txt +9939 -0
- package/modules/headtts-node.mjs +425 -0
- package/modules/headtts.mjs +975 -0
- package/modules/language-en-us.mjs +589 -0
- package/modules/language-fi.mjs +542 -0
- package/modules/language.mjs +469 -0
- package/modules/utils.mjs +206 -0
- package/modules/worker-tts.mjs +381 -0
- package/package.json +44 -0
|
@@ -0,0 +1,542 @@
|
|
|
1
|
+
import { LanguageBase } from "./language.mjs"
|
|
2
|
+
import * as utils from "./utils.mjs";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @class Finnish 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
|
+
|
|
18
|
+
// Add finnish letters with diaritics (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
|
+
|
|
29
|
+
// Finnish vowels
|
|
30
|
+
this.fiVowels = {
|
|
31
|
+
'A': 'A', 'E': 'E', 'I': 'I', 'O': 'O', 'U': 'U', 'Y': 'Y',
|
|
32
|
+
"Ä": "Ä", "Ö": "Ö", "Å": "Å"
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// Finnish consonants
|
|
36
|
+
this.fiConsonants = {
|
|
37
|
+
'B': 'B', 'C': 'C', 'D': 'D', 'F': 'F', 'G': 'G', 'H': 'H', 'J': 'J',
|
|
38
|
+
'K': 'K', 'L': 'L', 'M': 'M', 'N': 'N', 'P': 'P', 'Q': 'Q', 'R': 'R',
|
|
39
|
+
'S': 'S', 'T': 'T', 'V': 'V', 'W': 'W', 'X': 'X', 'Z': 'Z'
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// Diphthongs in the first syllable that we do not hyphenate
|
|
43
|
+
this.fiDiphthongFirst = {
|
|
44
|
+
"AI": "AI", "EI": "EI", "OI": "OI", "YI": "YI", "ÄI": "ÄI", "ÖI": "ÖI",
|
|
45
|
+
"EY": "EY", "IY": "IY", "ÄY": "ÄY", "ÖY": "ÖY", "AU": "AU", "EU": "EU",
|
|
46
|
+
"IU": "IU", "OU": "OU", "IE": "IE", "UO": "UO", "YÖ": "YÖ"
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// Diphthongs after the first syllable that we do not hyphenate
|
|
50
|
+
this.fiDiphthongEnd = {
|
|
51
|
+
"AI": "AI", "EI": "EI", "OI": "OI", "YI": "YI", "ÄI": "ÄI", "ÖI": "ÖI",
|
|
52
|
+
"EY": "EY", "IY": "IY", "ÄY": "ÄY", "ÖY": "ÖY", "AU": "AU", "EU": "EU",
|
|
53
|
+
"IU": "IU", "OU": "OU", "UO": "UO", "YÖ": "YÖ"
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// Pronounciation rules for Finnish
|
|
57
|
+
this.rules = {
|
|
58
|
+
'A': [ "AUSTR[A]=ɑ ː", "A[A]=ː", "[A]=ɑ" ],
|
|
59
|
+
'B': [ "B[B]=ː", "[B]=b" ],
|
|
60
|
+
'C': [
|
|
61
|
+
"[CITY]=s i t i", "C[C]=ː", "[C]I=s", "[C]E=s",
|
|
62
|
+
"[C]Y=s", "[C]=k"
|
|
63
|
+
],
|
|
64
|
+
'D': [ "D[D]=ː", "[D]=d" ],
|
|
65
|
+
'E': [ "E[E]=ː", "[E]=e" ],
|
|
66
|
+
'F': [ "[F]=f" ],
|
|
67
|
+
'G': [ "G[G]=ː", "[G]=ɡ" ],
|
|
68
|
+
'H': [ "[H]=h" ],
|
|
69
|
+
'I': [ "I[I]=ː", "[I]=i" ],
|
|
70
|
+
'J': [ "[J]=j" ],
|
|
71
|
+
'K': [ "#LLE[K]#=kː", "#NNE[K]#=kː", "K[K]=ː", "[K]=k" ],
|
|
72
|
+
'L': [ "[L]=l" ],
|
|
73
|
+
'M': [ "SYDÄ[M]=m ː", "[M]=m" ],
|
|
74
|
+
'N': [ "[NG]=ŋ ː", "[N]P=m", "[N]=n" ],
|
|
75
|
+
'O': [ "SYMB[O]LOI=o", "SYMB[O]L=o ː", "O[O]=ː", "[O]=o" ],
|
|
76
|
+
'P': [
|
|
77
|
+
" SHAM[P]O=p ː", " KAM[P]ANJ=p ː", " OTA[P]#=p ː",
|
|
78
|
+
" OLE[P]#=p ː", " TULE[P]#=p ː", "P[P]=ː", "[P]=p"
|
|
79
|
+
],
|
|
80
|
+
'Q': [ "Q[Q]=ː", "[Q]=q" ],
|
|
81
|
+
'R': [ "[RUO]AN=r u ː", "[R]=r" ],
|
|
82
|
+
'S': [ "[S]=s" ],
|
|
83
|
+
'T': [ "T[T]=ː", "[T]=t" ],
|
|
84
|
+
'U': [ "U[U]=ː", "[U]=u" ],
|
|
85
|
+
'V': [ "[V]=ʋ" ],
|
|
86
|
+
'W': [ "[W]=ʋ" ],
|
|
87
|
+
'X': [ "X[X]=ː s", "[X]=k s" ],
|
|
88
|
+
'Y': [ "Y[Y]=ː", "[Y]=y" ],
|
|
89
|
+
'Z': [ "[Z]=t s" ],
|
|
90
|
+
'Å': [ "Å[Å]=ː", "[Å]=o" ],
|
|
91
|
+
'Ä': [ "Ä[Ä]=ː", "[Ä]=æ" ],
|
|
92
|
+
'Ö': [ "Ö[Ö]=ː", "[Ö]=ø" ]
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const ops = {
|
|
96
|
+
'#': '[AEIOUYÄÖÅ]+', // One or more vowels AEIOUYÄÖÅ
|
|
97
|
+
' ': '\\b' // Start/end of the word
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const IPAToMisaki = {
|
|
101
|
+
"ɚ": ["ɜ","ɹ"], "ˈɝ": ["ˈɜ","ɹ"], "ˌɝ": ["ˌɜ","ɹ"],
|
|
102
|
+
"tʃ": ["ʧ"], "dʒ": ["ʤ"],
|
|
103
|
+
"eɪ": ["A"], "ˈeɪ": ["ˈA"], "ˌeɪ": ["ˌA"],
|
|
104
|
+
"aɪ": ["I"], "ˈaɪ": ["ˈI"], "ˌaɪ": ["ˌI"],
|
|
105
|
+
"aʊ": ["W"], "ˈaʊ": ["ˈW"], "ˌaʊ": ["ˌW"],
|
|
106
|
+
"ɔɪ": ["Y"], "ˈɔɪ": ["ˈY"], "ˌɔɪ": ["ˌY"],
|
|
107
|
+
"oʊ": ["O"], "ˈoʊ": ["ˈO"], "ˌoʊ": ["ˌO"],
|
|
108
|
+
"əʊ": ["Q"], "əʊ": ["ˈQ"], "əʊ": ["ˌQ"]
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
// Convert rules to regex
|
|
112
|
+
Object.keys(this.rules).forEach( key => {
|
|
113
|
+
this.rules[key] = this.rules[key].map( rule => {
|
|
114
|
+
const posL = rule.indexOf('[');
|
|
115
|
+
const posR = rule.indexOf(']');
|
|
116
|
+
const posE = rule.indexOf('=');
|
|
117
|
+
const strLeft = rule.substring(0,posL);
|
|
118
|
+
const strLetters = rule.substring(posL+1,posR);
|
|
119
|
+
const strRight = rule.substring(posR+1,posE);
|
|
120
|
+
const strPhonemes = rule.substring(posE+1);
|
|
121
|
+
|
|
122
|
+
const o = { regex: '', move: 0, phonemes: [] };
|
|
123
|
+
|
|
124
|
+
let exp = '';
|
|
125
|
+
exp += [...strLeft].map( x => ops[x] || x ).join('');
|
|
126
|
+
const ctxLetters = [...strLetters];
|
|
127
|
+
ctxLetters[0] = ctxLetters[0].toLowerCase();
|
|
128
|
+
exp += ctxLetters.join('');
|
|
129
|
+
o.move = ctxLetters.length;
|
|
130
|
+
exp += [...strRight].map( x => ops[x] || x ).join('');
|
|
131
|
+
o.regex = new RegExp(exp);
|
|
132
|
+
|
|
133
|
+
if ( strPhonemes.length ) {
|
|
134
|
+
strPhonemes.split(' ').forEach( ph => {
|
|
135
|
+
if ( IPAToMisaki.hasOwnProperty(ph) ) {
|
|
136
|
+
o.phonemes.push( ...IPAToMisaki[ph] );
|
|
137
|
+
} else {
|
|
138
|
+
o.phonemes.push( ph );
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return o;
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
// Characters to Finnish words
|
|
148
|
+
this.charactersToWords = {
|
|
149
|
+
'!': "HUUTOMERKKI", '"': "LAINAUSMERKKI", '#': "RISUAITA", '%': "PROSENTTI",
|
|
150
|
+
'&': "ET-MERKKI", "'": "HIPSUKKA", '(': "SULKU AUKI",
|
|
151
|
+
')': "SULKU KIINNI", '+': "PLUS", '-': 'VÄLIVIIVA', '—': 'VÄLIVIIVA', ',': "PILKKU",
|
|
152
|
+
'.': "PISTE", '/': "KAUTTAVIIVA", ':': "KAKSOISPISTE", ';': "PUOLIPISTE", '?': "KYSYMYSMERKKI",
|
|
153
|
+
'A': "AA", 'B': "BEE", 'C': "CEE", 'D': "DEE", 'E': "EE", 'F': "ÄF",
|
|
154
|
+
'G': "GEE", 'H': "HOO", 'I': "II", 'J': "JII", 'K': "KOO", 'L': "ÄL",
|
|
155
|
+
'M': "ÄM", 'N': "ÄN", 'O': "OO", 'P': "PEE", 'Q': "KUU", 'R': "ÄR",
|
|
156
|
+
'S': "ÄS", 'T': "TEE", 'U': "UU", 'V': "VEE", 'W': "KAKSOISVEE",
|
|
157
|
+
'X': "ÄKS", 'Y': "YY", 'Z': "TSET", '1': "YKSI", '2': "KAKSI", '3': "KOLME",
|
|
158
|
+
'4': "NELJÄ", '5': "VIISI", '6': "KUUSI", '7': "SEITSEMÄN", '8': "KAHDEKSAN",
|
|
159
|
+
'9': "YHDEKSÄN", '0': "NOLLAO", '{': "AALTOSULKU AUKI", '}': "AALTOSULKU KIINNI",
|
|
160
|
+
'$': "DOLLARI", '€': "EURO"
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
// Finnish number words
|
|
164
|
+
this.numbers = [
|
|
165
|
+
'NOLLA', 'YKSI', 'KAKSI', 'KOLME', 'NELJÄ', 'VIISI', 'KUUSI',
|
|
166
|
+
'SEITSEMÄN', 'KAHDEKSAN', 'YHDEKSÄN', "KYMMENEN", "YKSITOISTA",
|
|
167
|
+
"KAKSITOISTA", "KOLMETOISTA", "NELJÄTOISTA", "VIISITOISTA",
|
|
168
|
+
"KUUSITOISTA", 'SEITSEMÄNTOISTA', 'KAHDEKSANTOISTA', 'YHDEKSÄNTOISTA'
|
|
169
|
+
];
|
|
170
|
+
|
|
171
|
+
// Date & Time
|
|
172
|
+
this.days = [
|
|
173
|
+
"", "ENSIMMÄINEN", "TOINEN", "KOLMAS", "NELJÄS", "VIIDES", "KUUDES",
|
|
174
|
+
"SEITSEMÄS", "KAHDEKSAS", "YHDEKSÄS", "KYMMENES", "YHDESTOISTA",
|
|
175
|
+
"KAHDESTOISTA", "KOLMASTOISTA", "NELJÄSTOISTA", "VIIDESTOISTA",
|
|
176
|
+
"KUUDESTOISTA", "SEITSEMÄSTOISTA", "KAHDEKSASTOISTA", "YHDEKSÄSTOISTA",
|
|
177
|
+
"KAHDESKYMMENES", "KAHDESKYMMENES-ENSIMMÄINEN", "KAHDESKYMMENES-TOINEN",
|
|
178
|
+
"KAHDESKYMMENES-KOLMAS", "KAHDESKYMMENES-NELJÄS", "KAHDESKYMMENES-VIIDES",
|
|
179
|
+
"KAHDESKYMMENES-KUUDES", "KAHDESKYMMENES-SEITSEMÄS", "KAHDESKYMMENES-KAHDEKSAS",
|
|
180
|
+
"KAHDESKYMMENES-YHDEKSÄS", "KOLMASKYMMENES", "KOLMASKYMMENES-ENSIMMÄINEN"
|
|
181
|
+
];
|
|
182
|
+
this.months = [
|
|
183
|
+
"", "TAMMIKUUTA", "HELMIKUUTA", "MAALISKUUTA", "HUHTIKUUTA", "TOUKOKUUTA", "KESÄKUUTA", "HEINÄKUUTA",
|
|
184
|
+
"ELOKUUTA", "SYYSKUUTA", "LOKAKUUTA", "MARRASKUUTA", "JOULUKUUTA"
|
|
185
|
+
];
|
|
186
|
+
|
|
187
|
+
// Symbols to Finnish
|
|
188
|
+
// TODO: Implement these
|
|
189
|
+
this.symbols = {
|
|
190
|
+
'%': 'PROSENTTIA', '€': 'EUROA', '&': 'JA', '+': 'PLUS',
|
|
191
|
+
'$': 'DOLLARIA'
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
this.symbolsReg = /[%€&\+\$]/g;
|
|
195
|
+
|
|
196
|
+
if ( this.settings.trace ) {
|
|
197
|
+
utils.trace( 'Language module "fi" initiated.' );
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Add one dictionary line. For Finnish, it is a dictionary
|
|
204
|
+
* of compound words.
|
|
205
|
+
* NOTE: We only add three characters for the last part as
|
|
206
|
+
* we need only partial match.
|
|
207
|
+
*
|
|
208
|
+
* @param {string} s Line
|
|
209
|
+
*/
|
|
210
|
+
addToDictionary(s) {
|
|
211
|
+
if ( s.startsWith(";;;") ) return; // Ignore comment line
|
|
212
|
+
const parts = s.split("\t");
|
|
213
|
+
const len = parts.length;
|
|
214
|
+
if ( len >= 2 ) {
|
|
215
|
+
const first = parts[0];
|
|
216
|
+
if ( !this.dictionary.hasOwnProperty(parts[0]) ) {
|
|
217
|
+
this.dictionary[first] = {};
|
|
218
|
+
}
|
|
219
|
+
for( let i=1; i<len; i++ ) {
|
|
220
|
+
const second = parts[i];
|
|
221
|
+
this.dictionary[first][second] = null;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Convert number to words.
|
|
228
|
+
*
|
|
229
|
+
* @param {number|string} num Number
|
|
230
|
+
* @return {string} String
|
|
231
|
+
*/
|
|
232
|
+
convertNumberToWords(num) {
|
|
233
|
+
const w = [];
|
|
234
|
+
let n = parseFloat(num);
|
|
235
|
+
if ( n === undefined ) return num;
|
|
236
|
+
let p = (n,z,w0,w1,w2) => {
|
|
237
|
+
if ( n < z ) return n;
|
|
238
|
+
const d = Math.floor(n/z);
|
|
239
|
+
w.push( w0 + ((d === 1) ? w1 : this.convertNumberToWords(d.toString()) + w2) );
|
|
240
|
+
return n - d * z;
|
|
241
|
+
}
|
|
242
|
+
if ( n < 0 ) {
|
|
243
|
+
w.push('MIINUS ');
|
|
244
|
+
n = Math.abs(n);
|
|
245
|
+
}
|
|
246
|
+
n = p(n,1000000000,' ','MILJARDI',' MILJARDIA');
|
|
247
|
+
n = p(n,1000000,' ','MILJOONA',' MILJOONAA');
|
|
248
|
+
n = p(n,1000,'', 'TUHAT','TUHATTA');
|
|
249
|
+
n = p(n,100,'','SATA','SATAA');
|
|
250
|
+
if ( n > 20 ) n = p(n,10,'','','KYMMENTÄ');
|
|
251
|
+
if ( n >= 1) {
|
|
252
|
+
let d = Math.floor(n);
|
|
253
|
+
w.push( this.numbers[d] );
|
|
254
|
+
n -= d;
|
|
255
|
+
}
|
|
256
|
+
if ( n >= 0 && Math.abs(parseFloat(num)) < 1) w.push( 'NOLLA' );
|
|
257
|
+
if ( n > 0 ) {
|
|
258
|
+
let d = num.split('.');
|
|
259
|
+
if ( d.length > 1 ) {
|
|
260
|
+
w.push( ' PILKKU' );
|
|
261
|
+
let c = [...d[d.length-1]];
|
|
262
|
+
for( let i=0; i<c.length; i++ ) {
|
|
263
|
+
w.push( ' ' + this.numbers[c[i]] );
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return w.join('').trim();
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Set the `text` to be spoken by analysing the part content.
|
|
273
|
+
*
|
|
274
|
+
* @param {Object} part Current part
|
|
275
|
+
* @param {number} i Index
|
|
276
|
+
* @param {Object[]} arr All the parts.
|
|
277
|
+
*/
|
|
278
|
+
partSetText(part,i,arr) {
|
|
279
|
+
|
|
280
|
+
// Call super to pre-populate
|
|
281
|
+
super.partSetText(part,i,arr);
|
|
282
|
+
|
|
283
|
+
// Language specific implementation
|
|
284
|
+
switch( part.type ) {
|
|
285
|
+
|
|
286
|
+
case "text":
|
|
287
|
+
// Check if this is actually a number
|
|
288
|
+
const s = part.text;
|
|
289
|
+
if ( s ) {
|
|
290
|
+
const num = s.replace(/,/g, '').trim();
|
|
291
|
+
if ( !isNaN(num) && !isNaN(parseFloat(num)) ) {
|
|
292
|
+
part.text = this.convertNumberToWords(num) + " ";
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
break;
|
|
296
|
+
|
|
297
|
+
case "characters":
|
|
298
|
+
const t = [];
|
|
299
|
+
const chars = [...part.value.toUpperCase()];
|
|
300
|
+
const len = chars.length;
|
|
301
|
+
for( let i=0; i<len; i++ ) {
|
|
302
|
+
const c = chars[i];
|
|
303
|
+
if ( this.charactersToWords.hasOwnProperty(c) ) {
|
|
304
|
+
t.push( this.charactersToWords[c] );
|
|
305
|
+
} else {
|
|
306
|
+
t.push(""); // Generates a space for unknown characters
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
part.text = t.join(" ") + " ";
|
|
310
|
+
break;
|
|
311
|
+
|
|
312
|
+
case "number":
|
|
313
|
+
part.text = this.convertNumberToWords(part.value) + " ";
|
|
314
|
+
break;
|
|
315
|
+
|
|
316
|
+
case "date":
|
|
317
|
+
const date = new Date(part.value);
|
|
318
|
+
const month = this.months[date.getMonth()];
|
|
319
|
+
const day = this.days[date.getDate()];
|
|
320
|
+
const year = this.convertNumberToWords(date.getFullYear());
|
|
321
|
+
part.text = day + " " + month + " " + year + " ";
|
|
322
|
+
break;
|
|
323
|
+
|
|
324
|
+
case "time":
|
|
325
|
+
const time = new Date(part.value);
|
|
326
|
+
let hours = time.getHours(); // 0–23
|
|
327
|
+
const minutes = time.getMinutes(); // 0–59
|
|
328
|
+
part.text = this.convertNumberToWords(hours) + " ";
|
|
329
|
+
part.text += this.convertNumberToWords(minutes) + " ";
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Split compound word into its parts.
|
|
338
|
+
*
|
|
339
|
+
* @param {string} word Normalized word in upper case
|
|
340
|
+
* @return {string[]} Parts of the word
|
|
341
|
+
*/
|
|
342
|
+
splitOnCompoundWords(s) {
|
|
343
|
+
|
|
344
|
+
// Split for hyphens
|
|
345
|
+
const hyphens = s.split("-").filter( x => x.length );
|
|
346
|
+
const len = hyphens.length;
|
|
347
|
+
|
|
348
|
+
if ( len > 1 ) {
|
|
349
|
+
|
|
350
|
+
// Check if parts are compound words
|
|
351
|
+
return hyphens.map( (x,i) => {
|
|
352
|
+
const isLast = i === (len-1);
|
|
353
|
+
return this.splitOnCompoundWords( x + (isLast ? "" : "-") );
|
|
354
|
+
}).flat();
|
|
355
|
+
|
|
356
|
+
} else {
|
|
357
|
+
|
|
358
|
+
// Check compound word dictionary
|
|
359
|
+
for( let i=s.length; i>2; i-- ) {
|
|
360
|
+
const partFirst = s.substring(0,i);
|
|
361
|
+
if ( this.dictionary.hasOwnProperty(partFirst) ) {
|
|
362
|
+
const item = this.dictionary[partFirst];
|
|
363
|
+
const partNext4 = s.substring(i,i+4);
|
|
364
|
+
const partNext3 = s.substring(i,i+3);
|
|
365
|
+
if ( item.hasOwnProperty(partNext4) || item.hasOwnProperty(partNext3) ) {
|
|
366
|
+
return [ partFirst, ...this.splitOnCompoundWords(s.substring(i)) ];
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
return [s];
|
|
371
|
+
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Split single word into syllables.
|
|
379
|
+
* NOTE: Doesn't handle compound words, so you slip on
|
|
380
|
+
* compound words before calling this method.
|
|
381
|
+
*
|
|
382
|
+
* @param {string} s Normalized and upper case word
|
|
383
|
+
* @return {string[]} Syllables.
|
|
384
|
+
*/
|
|
385
|
+
splitOnSyllables(s) {
|
|
386
|
+
const syllables = [];
|
|
387
|
+
|
|
388
|
+
const chars = [...s];
|
|
389
|
+
let len = chars.length;
|
|
390
|
+
let i = 0;
|
|
391
|
+
let syllable = "";
|
|
392
|
+
let isVowelFound = false;
|
|
393
|
+
let isConsonantFound = false;
|
|
394
|
+
while( i < len ) {
|
|
395
|
+
|
|
396
|
+
const isLast = i === (len-1);
|
|
397
|
+
const c = chars[i];
|
|
398
|
+
const isVowel = this.fiVowels.hasOwnProperty(c);
|
|
399
|
+
const isConsonant = this.fiConsonants.hasOwnProperty(c);
|
|
400
|
+
isVowelFound ||= isVowel;
|
|
401
|
+
isConsonantFound ||= isConsonant;
|
|
402
|
+
const isNotLetter = !isVowel && !isConsonant;
|
|
403
|
+
const cSecond = isLast ? null : chars[i+1];
|
|
404
|
+
const isSecondVowel = this.fiVowels.hasOwnProperty(cSecond);
|
|
405
|
+
const cTwo = isLast ? null : (c + cSecond);
|
|
406
|
+
const isDiphthong = isVowel && isSecondVowel && (c !== cSecond) &&
|
|
407
|
+
(syllables.length === 0 ? !this.fiDiphthongFirst.hasOwnProperty(cTwo) : !this.fiDiphthongEnd.hasOwnProperty(cTwo));
|
|
408
|
+
const isBreakBefore = isVowelFound && isConsonant && isSecondVowel;
|
|
409
|
+
const isBreakAfter = isLast || isDiphthong || isNotLetter;
|
|
410
|
+
|
|
411
|
+
// Break before current letter
|
|
412
|
+
if ( isBreakBefore ) {
|
|
413
|
+
syllables.push( syllable );
|
|
414
|
+
syllable = "";
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
syllable += c;
|
|
418
|
+
|
|
419
|
+
// Break after the current letter
|
|
420
|
+
if ( isBreakAfter ) {
|
|
421
|
+
syllables.push( syllable );
|
|
422
|
+
syllable = "";
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
i++;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
return syllables;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Split word or compound word on stress.
|
|
434
|
+
*
|
|
435
|
+
* @param {string} s Normalized and upper case word or compound word
|
|
436
|
+
* @return {string[]} Stresses parts.
|
|
437
|
+
*/
|
|
438
|
+
splitOnStress(s) {
|
|
439
|
+
const stressed = [];
|
|
440
|
+
|
|
441
|
+
const words = this.splitOnCompoundWords(s);
|
|
442
|
+
const lenWords = words.length;
|
|
443
|
+
for( let i=0; i<lenWords; i++ ) {
|
|
444
|
+
const word = words[i];
|
|
445
|
+
const syllables = this.splitOnSyllables(word);
|
|
446
|
+
const lenSyllables = syllables.length;
|
|
447
|
+
let stress = "";
|
|
448
|
+
for( let j=0; j<lenSyllables; j++ ) {
|
|
449
|
+
const syllable = syllables[j];
|
|
450
|
+
const isLast = j === (lenSyllables-1);
|
|
451
|
+
if ( j > 0 && (j-1) % 2 && !isLast ) {
|
|
452
|
+
stressed.push( stress );
|
|
453
|
+
stress = "";
|
|
454
|
+
}
|
|
455
|
+
stress += syllable;
|
|
456
|
+
if ( isLast ) {
|
|
457
|
+
stressed.push( stress );
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
return stressed;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Convert graphemes to phonemes.
|
|
470
|
+
*
|
|
471
|
+
* @param {string} s Word, normalized and in upper case
|
|
472
|
+
* @return {string[]} Array of phonemes
|
|
473
|
+
*/
|
|
474
|
+
phonemizeWord(s) {
|
|
475
|
+
let phonemes = [];
|
|
476
|
+
|
|
477
|
+
// Split on stress
|
|
478
|
+
const parts = this.splitOnStress(s);
|
|
479
|
+
|
|
480
|
+
parts.forEach( (x,i) => {
|
|
481
|
+
|
|
482
|
+
// Stress
|
|
483
|
+
if ( x ) {
|
|
484
|
+
if ( i === 0 ) {
|
|
485
|
+
phonemes.push("ˈ");
|
|
486
|
+
} else {
|
|
487
|
+
phonemes.push("ˌ");
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
// Phonemize
|
|
492
|
+
const chars = [...x];
|
|
493
|
+
let len = chars.length;
|
|
494
|
+
let j = 0;
|
|
495
|
+
while( j < len ) {
|
|
496
|
+
const c = chars[j];
|
|
497
|
+
if ( this.punctuations.hasOwnProperty(c) ) {
|
|
498
|
+
phonemes.push( this.punctuations[c] );
|
|
499
|
+
j++;
|
|
500
|
+
} else {
|
|
501
|
+
const ruleset = this.rules[c];
|
|
502
|
+
if ( ruleset ) {
|
|
503
|
+
const lenRuleset = ruleset.length;
|
|
504
|
+
for(let k=0; k<lenRuleset; k++) {
|
|
505
|
+
const rule = ruleset[k];
|
|
506
|
+
const test = x.substring(0, j) + c.toLowerCase() + x.substring(j+1);
|
|
507
|
+
let matches = test.match(rule.regex);
|
|
508
|
+
if ( matches ) {
|
|
509
|
+
phonemes.push( ...rule.phonemes );
|
|
510
|
+
j += rule.move;
|
|
511
|
+
break;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
} else {
|
|
515
|
+
j++;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
if ( this.settings.trace ) {
|
|
522
|
+
utils.trace( 'Rules: "' + s + '" => "' + phonemes.join("") + '"' );
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
return phonemes;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Post process the parts to be spoken.
|
|
530
|
+
*
|
|
531
|
+
* @param {Object[]} parts Parts
|
|
532
|
+
*/
|
|
533
|
+
postProcessSpeak(parts) {
|
|
534
|
+
parts.forEach( x => {
|
|
535
|
+
|
|
536
|
+
});
|
|
537
|
+
return parts;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
export { Language };
|