@suarify/kecil-tts 0.0.0-stage → 0.1.1
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 +202 -0
- package/NOTICE +21 -0
- package/README.md +224 -2
- package/docs/screenshot.jpg +0 -0
- package/examples/node-server.mjs +48 -0
- package/package.json +63 -4
- package/src/effects.js +250 -0
- package/src/g2p/data.js +6 -0
- package/src/g2p/index.js +16 -0
- package/src/g2p/ms_g2p.js +277 -0
- package/src/g2p/ms_norm.js +217 -0
- package/src/index.js +224 -0
- package/src/runtime.js +44 -0
- package/src/tts.js +149 -0
- package/src/wav.js +46 -0
package/src/g2p/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Malay normalization + grapheme-to-phoneme front end.
|
|
2
|
+
// Ported from the Kecil-TTS web Space; lexicons are embedded so the package
|
|
3
|
+
// works in bundlers and on servers without serving any extra static files.
|
|
4
|
+
import { G2P } from './ms_g2p.js';
|
|
5
|
+
import { vocab, lexiconE, lexiconX, lexiconEn } from './data.js';
|
|
6
|
+
|
|
7
|
+
// Must match how the training rows were made (see model config.json).
|
|
8
|
+
export const G2P_OPTIONS = Object.freeze({ stress: false, r: 'r', diph: 'glide' });
|
|
9
|
+
|
|
10
|
+
export function createG2P(options = G2P_OPTIONS) {
|
|
11
|
+
return new G2P({ vocab, lexE: lexiconE, lexX: lexiconX, lexEn: lexiconEn }, options);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { G2P } from './ms_g2p.js';
|
|
15
|
+
export { normalize } from './ms_norm.js';
|
|
16
|
+
export { chunk, loadG2P } from './ms_g2p.js';
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
// Malay G2P for Kokoro (kokoro-malay-bella). Line-for-line port of ms_g2p.py.
|
|
2
|
+
//
|
|
3
|
+
// import { G2P, loadG2P } from './ms_g2p.js';
|
|
4
|
+
// const g2p = await loadG2P('./g2p/'); // browser: fetches the JSON lexicons
|
|
5
|
+
// const ph = g2p.phonemize('Selamat pagi');
|
|
6
|
+
// const ids = g2p.tokens(ph); // [0, ..., 0]
|
|
7
|
+
|
|
8
|
+
const LETTERS = {
|
|
9
|
+
a: 'ˈA', b: 'bˈi', c: 'sˈi', d: 'dˈi', e: 'ˈi', f: 'ˈɛf', g: 'ʤˈi',
|
|
10
|
+
h: 'ˈAʧ', i: 'ˈI', j: 'ʤˈA', k: 'kˈA', l: 'ˈɛl', m: 'ˈɛm', n: 'ˈɛn',
|
|
11
|
+
o: 'ˈO', p: 'pˈi', q: 'kjˈu', r: 'ˈɑr', s: 'ˈɛs', t: 'tˈi', u: 'jˈu',
|
|
12
|
+
v: 'vˈi', w: 'dˈʌbəljˌu', x: 'ˈɛks', y: 'wˈI', z: 'zˈɛd',
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const FUNCTION_WORDS = new Set([
|
|
16
|
+
'di', 'ke', 'dari', 'daripada', 'yang', 'dan', 'atau', 'untuk', 'dengan', 'pada',
|
|
17
|
+
'kepada', 'akan', 'pun', 'lah', 'kah', 'tah', 'si', 'sang', 'para', 'oleh', 'bagi',
|
|
18
|
+
'ia', 'nya', 'ini', 'itu', 'tu', 'ni', 'je', 'la', 'kan', 'tak',
|
|
19
|
+
]);
|
|
20
|
+
|
|
21
|
+
const PREFIX_OUTER = [
|
|
22
|
+
['meng', ['', 'k']], ['meny', ['s']], ['mem', ['', 'p']], ['men', ['', 't']], ['me', ['']],
|
|
23
|
+
['peng', ['', 'k']], ['peny', ['s']], ['pem', ['', 'p']], ['pen', ['', 't']],
|
|
24
|
+
['per', ['']], ['pel', ['']], ['pe', ['']],
|
|
25
|
+
['ber', ['']], ['bel', ['']], ['be', ['']], ['ter', ['']], ['te', ['']],
|
|
26
|
+
['di', ['']], ['ke', ['']], ['se', ['']],
|
|
27
|
+
];
|
|
28
|
+
const PREFIX_INNER = ['per', 'ber', 'ter', 'ke', 'se', 'pe'];
|
|
29
|
+
const SUF_DERIV = ['kan', 'an', 'i'];
|
|
30
|
+
const SUF_ENCL = ['nya', 'lah', 'kah', 'tah', 'ku', 'mu'];
|
|
31
|
+
const SUFFIX_STRINGS = new Set(['']);
|
|
32
|
+
for (const d of ['', ...SUF_DERIV]) for (const e of ['', ...SUF_ENCL]) SUFFIX_STRINGS.add(d + e);
|
|
33
|
+
|
|
34
|
+
const VOWEL_LETTERS = 'aeéiou';
|
|
35
|
+
const NUCLEI = new Set('aeəiouAIOWY');
|
|
36
|
+
const SINGLE = {
|
|
37
|
+
a: 'a', 'é': 'e', e: 'ə', i: 'i', o: 'o', u: 'u',
|
|
38
|
+
b: 'b', c: 'ʧ', d: 'd', f: 'f', g: 'ɡ', h: 'h', j: 'ʤ', k: 'k',
|
|
39
|
+
l: 'l', m: 'm', n: 'n', p: 'p', q: 'k', s: 's', t: 't', v: 'v',
|
|
40
|
+
w: 'w', y: 'j', z: 'z',
|
|
41
|
+
};
|
|
42
|
+
const DIGRAPHS = {
|
|
43
|
+
ng: 'ŋ', ny: 'ɲ', sy: 'ʃ', sh: 'ʃ', kh: 'x', gh: 'ɣ', ch: 'ʧ',
|
|
44
|
+
th: 't', dh: 'd', ph: 'f', dz: 'z',
|
|
45
|
+
};
|
|
46
|
+
const DIPHTHONGS = { ai: ['aj', 'I'], au: ['aw', 'W'], oi: ['oj', 'Y'], 'éi': ['ej', 'A'] };
|
|
47
|
+
const PUNCT = ',.!?;:…—';
|
|
48
|
+
const TOKEN_RE = /[A-Za-zé']+|[,.!?;:…—]/g;
|
|
49
|
+
|
|
50
|
+
const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
|
|
51
|
+
const isVowelLetter = (c) => c !== '' && VOWEL_LETTERS.includes(c);
|
|
52
|
+
|
|
53
|
+
export function stripAccents(text) {
|
|
54
|
+
return text.normalize('NFD').replace(/\p{Mn}/gu, '').normalize('NFC');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export class G2P {
|
|
58
|
+
constructor({ vocab, lexE = {}, lexX = {}, lexEn = {} }, { stress = true, r = 'r', diph = 'glide' } = {}) {
|
|
59
|
+
this.vocab = vocab;
|
|
60
|
+
this.lexE = lexE;
|
|
61
|
+
this.lexX = lexX;
|
|
62
|
+
this.lexEn = lexEn;
|
|
63
|
+
this.stress = stress;
|
|
64
|
+
this.r = r;
|
|
65
|
+
this.diph = diph === 'glide' ? 0 : 1;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
*_prefixSplits(w) {
|
|
69
|
+
yield ['', w, 0];
|
|
70
|
+
for (const [p, restores] of PREFIX_OUTER) {
|
|
71
|
+
if (!w.startsWith(p) || w.length <= p.length) continue;
|
|
72
|
+
const rest = w.slice(p.length);
|
|
73
|
+
for (const rs of restores) {
|
|
74
|
+
yield [p, rs + rest, rs.length];
|
|
75
|
+
if (rs === '') {
|
|
76
|
+
for (const q of PREFIX_INNER) {
|
|
77
|
+
if (rest.startsWith(q) && rest.length > q.length) yield [p + q, rest.slice(q.length), 0];
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
_decompose(w) {
|
|
85
|
+
let best = null;
|
|
86
|
+
let bestFree = null;
|
|
87
|
+
const better = (c, b) => b === null || c[0] > b[0] || (c[0] === b[0] && c[1] > b[1]);
|
|
88
|
+
for (const encl of ['', ...SUF_ENCL]) {
|
|
89
|
+
if (encl && !w.endsWith(encl)) continue;
|
|
90
|
+
const w1 = w.slice(0, w.length - encl.length);
|
|
91
|
+
for (const der of ['', ...SUF_DERIV]) {
|
|
92
|
+
if (der && !w1.endsWith(der)) continue;
|
|
93
|
+
const w2 = w1.slice(0, w1.length - der.length);
|
|
94
|
+
const suffix = der + encl;
|
|
95
|
+
for (const [prefix, root, drop] of this._prefixSplits(w2)) {
|
|
96
|
+
if (root.length < 2) continue;
|
|
97
|
+
const nAff = prefix.length + suffix.length;
|
|
98
|
+
const cand = [root.length, -nAff, prefix, root, drop, suffix];
|
|
99
|
+
if (has(this.lexE, root)) {
|
|
100
|
+
if (better(cand, best)) best = cand;
|
|
101
|
+
} else if (!root.includes('e') && prefix &&
|
|
102
|
+
[...root].filter((c) => VOWEL_LETTERS.includes(c)).length >= 2) {
|
|
103
|
+
if (better(cand, bestFree)) bestFree = cand;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const pick = best || bestFree;
|
|
109
|
+
if (pick === null) return null;
|
|
110
|
+
const [, , prefix, root, drop, suffix] = pick;
|
|
111
|
+
const spelled = has(this.lexE, root) ? this.lexE[root] : root;
|
|
112
|
+
return prefix + spelled.slice(drop) + suffix;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
static _heuristic(w) {
|
|
116
|
+
const idx = [];
|
|
117
|
+
for (let i = 0; i < w.length; i++) if (VOWEL_LETTERS.includes(w[i])) idx.push(i);
|
|
118
|
+
if (!idx.length) return w;
|
|
119
|
+
const last = idx[idx.length - 1];
|
|
120
|
+
const chars = [...w];
|
|
121
|
+
if (w[last] === 'e') {
|
|
122
|
+
const coda = w.slice(last + 1);
|
|
123
|
+
if (coda === '' || (!'mnr'.includes(coda[coda.length - 1]) && coda !== 'ng')) chars[last] = 'é';
|
|
124
|
+
}
|
|
125
|
+
return chars.join('');
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
respell(w) {
|
|
129
|
+
if (!w.includes('e')) return w;
|
|
130
|
+
if (has(this.lexE, w)) return this.lexE[w];
|
|
131
|
+
const d = this._decompose(w);
|
|
132
|
+
if (d !== null) return d;
|
|
133
|
+
return G2P._heuristic(w);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
_letters(s) {
|
|
137
|
+
const out = [];
|
|
138
|
+
const n = s.length;
|
|
139
|
+
let i = 0;
|
|
140
|
+
while (i < n) {
|
|
141
|
+
const c = s[i];
|
|
142
|
+
const two = s.slice(i, i + 2);
|
|
143
|
+
if ('aoé'.includes(c) && has(DIPHTHONGS, two) && SUFFIX_STRINGS.has(s.slice(i + 2))) {
|
|
144
|
+
const ph = DIPHTHONGS[two][this.diph];
|
|
145
|
+
G2P._pushVowel(out, ph[0]);
|
|
146
|
+
out.push(...ph.slice(1));
|
|
147
|
+
i += 2;
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (has(DIGRAPHS, two)) {
|
|
151
|
+
out.push(DIGRAPHS[two]);
|
|
152
|
+
i += 2;
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if (c === "'") {
|
|
156
|
+
if (out.length && i + 1 < n) out.push('ʔ');
|
|
157
|
+
i += 1;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (c === 'k') {
|
|
161
|
+
const prevVowel = i > 0 && isVowelLetter(s[i - 1]);
|
|
162
|
+
const nxt = i + 1 < n ? s[i + 1] : '';
|
|
163
|
+
if (prevVowel && (nxt === '' || (!isVowelLetter(nxt) && !"srl'".includes(nxt)))) out.push('ʔ');
|
|
164
|
+
else out.push('k');
|
|
165
|
+
i += 1;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (c === 'x') {
|
|
169
|
+
if (i === 0) out.push('z');
|
|
170
|
+
else out.push('k', 's');
|
|
171
|
+
i += 1;
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
if (c === 'r') {
|
|
175
|
+
out.push(this.r);
|
|
176
|
+
i += 1;
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
if (!has(SINGLE, c)) {
|
|
180
|
+
i += 1;
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
const ph = SINGLE[c];
|
|
184
|
+
if (VOWEL_LETTERS.includes(c)) G2P._pushVowel(out, ph);
|
|
185
|
+
else out.push(ph);
|
|
186
|
+
i += 1;
|
|
187
|
+
}
|
|
188
|
+
return out;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
static _pushVowel(out, v) {
|
|
192
|
+
const last = out.length ? out[out.length - 1] : '';
|
|
193
|
+
if (last && NUCLEI.has(last) && (last === 'ə' || last === v)) out.push('ʔ');
|
|
194
|
+
out.push(v);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
_stress(ph, w) {
|
|
198
|
+
if (!this.stress || FUNCTION_WORDS.has(w)) return ph;
|
|
199
|
+
const nuc = [];
|
|
200
|
+
ph.forEach((p, i) => { if (NUCLEI.has(p)) nuc.push(i); });
|
|
201
|
+
if (!nuc.length) return ph;
|
|
202
|
+
let k = nuc.length >= 2 ? nuc[nuc.length - 2] : nuc[nuc.length - 1];
|
|
203
|
+
if (nuc.length >= 2 && ph[k] === 'ə') k = nuc[nuc.length - 1];
|
|
204
|
+
return [...ph.slice(0, k), 'ˈ', ...ph.slice(k)];
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
word(tok) {
|
|
208
|
+
if (tok.length >= 2 && isUpper(tok) && /^[A-Za-zé]+$/.test(tok) && !has(this.lexX, tok.toLowerCase())) {
|
|
209
|
+
return [...tok.toLowerCase()].filter((c) => has(LETTERS, c)).map((c) => LETTERS[c]).join(' ');
|
|
210
|
+
}
|
|
211
|
+
const w = tok.toLowerCase().replace(/^'+|'+$/g, '');
|
|
212
|
+
if (!w) return '';
|
|
213
|
+
if (has(this.lexX, w)) return this.lexX[w];
|
|
214
|
+
if (has(this.lexEn, w)) return this.lexEn[w];
|
|
215
|
+
return this._stress(this._letters(this.respell(w)), w).join('');
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
phonemize(text) {
|
|
219
|
+
text = stripAccents(text).split('-').join(' ');
|
|
220
|
+
let out = '';
|
|
221
|
+
for (const tok of text.match(TOKEN_RE) || []) {
|
|
222
|
+
if (PUNCT.includes(tok)) {
|
|
223
|
+
out = out.replace(/ +$/, '') + tok + ' ';
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
const ph = this.word(tok);
|
|
227
|
+
if (ph) out += ph + ' ';
|
|
228
|
+
}
|
|
229
|
+
return out.trim();
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
tokens(phonemes) {
|
|
233
|
+
const ids = [0];
|
|
234
|
+
for (const c of phonemes) if (has(this.vocab, c)) ids.push(this.vocab[c]);
|
|
235
|
+
ids.push(0);
|
|
236
|
+
return ids;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Python str.isupper(): at least one cased character and no lowercase ones.
|
|
241
|
+
function isUpper(s) {
|
|
242
|
+
return s !== s.toLowerCase() && s === s.toUpperCase();
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export function chunk(phonemes, limit = 510) {
|
|
246
|
+
const out = [];
|
|
247
|
+
let rest = phonemes.trim();
|
|
248
|
+
while (rest.length > limit) {
|
|
249
|
+
let cut = -1;
|
|
250
|
+
for (const marks of ['.!?…', ';:—', ',', ' ']) {
|
|
251
|
+
for (let j = limit - 1; j > Math.floor(limit / 3); j--) {
|
|
252
|
+
if (marks.includes(rest[j])) { cut = j + 1; break; }
|
|
253
|
+
}
|
|
254
|
+
if (cut > 0) break;
|
|
255
|
+
}
|
|
256
|
+
if (cut <= 0) cut = limit;
|
|
257
|
+
out.push(rest.slice(0, cut).trim());
|
|
258
|
+
rest = rest.slice(cut).trim();
|
|
259
|
+
}
|
|
260
|
+
if (rest) out.push(rest);
|
|
261
|
+
return out;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export async function loadG2P(base = './', opts = {}) {
|
|
265
|
+
const get = async (name, optional = false) => {
|
|
266
|
+
const r = await fetch(base + name);
|
|
267
|
+
if (!r.ok) {
|
|
268
|
+
if (optional) return {};
|
|
269
|
+
throw new Error(`failed to load ${name}: ${r.status}`);
|
|
270
|
+
}
|
|
271
|
+
return r.json();
|
|
272
|
+
};
|
|
273
|
+
const [vocab, lexE, lexX, lexEn] = await Promise.all([
|
|
274
|
+
get('vocab.json'), get('lexicon_e.json'), get('lexicon_exceptions.json', true), get('lexicon_en.json', true),
|
|
275
|
+
]);
|
|
276
|
+
return new G2P({ vocab, lexE, lexX, lexEn }, opts);
|
|
277
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
// Malay text normaliser for kokoro-malay-bella. Line-for-line port of ms_norm.py.
|
|
2
|
+
// Regexes avoid the 'u' flag so \d, \w and \b are ASCII, matching Python's re.ASCII.
|
|
3
|
+
|
|
4
|
+
const ONES = ['kosong', 'satu', 'dua', 'tiga', 'empat', 'lima', 'enam', 'tujuh', 'lapan', 'sembilan'];
|
|
5
|
+
const MONTHS = ['Januari', 'Februari', 'Mac', 'April', 'Mei', 'Jun', 'Julai', 'Ogos',
|
|
6
|
+
'September', 'Oktober', 'November', 'Disember'];
|
|
7
|
+
const MONTH_ABBR = {
|
|
8
|
+
jan: 1, feb: 2, mac: 3, mar: 3, apr: 4, mei: 5, may: 5, jun: 6,
|
|
9
|
+
jul: 7, ogo: 8, aug: 8, sep: 9, okt: 10, oct: 10, nov: 11, dis: 12, dec: 12,
|
|
10
|
+
};
|
|
11
|
+
const MONTH_RE = 'januari|january|jan|februari|february|feb|mac|march|mar|april|apr|mei|may|' +
|
|
12
|
+
'jun|june|julai|july|jul|ogos|august|aug|september|sept|sep|oktober|october|okt|oct|' +
|
|
13
|
+
'november|nov|disember|december|dis|dec';
|
|
14
|
+
|
|
15
|
+
const ABBR = [
|
|
16
|
+
['sdn. bhd.', 'sendirian berhad'], ['sdn bhd', 'sendirian berhad'],
|
|
17
|
+
['bhd.', 'berhad'], ['sdn.', 'sendirian'],
|
|
18
|
+
['dr.', 'doktor'], ['en.', 'encik'], ['pn.', 'puan'], ['cik.', 'cik'],
|
|
19
|
+
['tn.', 'tuan'], ['hj.', 'haji'], ['hjh.', 'hajah'], ['prof.', 'profesor'],
|
|
20
|
+
['jln.', 'jalan'], ['jln', 'jalan'], ['tmn.', 'taman'], ['tmn', 'taman'],
|
|
21
|
+
['kg.', 'kampung'], ['lrg.', 'lorong'],
|
|
22
|
+
['dll.', 'dan lain-lain'], ['dll', 'dan lain-lain'], ['dsb.', 'dan sebagainya'],
|
|
23
|
+
['spt.', 'seperti'], ['spt', 'seperti'], ['yg', 'yang'], ['dgn', 'dengan'],
|
|
24
|
+
['utk', 'untuk'], ['kpd', 'kepada'], ['drp', 'daripada'], ['sbb', 'sebab'],
|
|
25
|
+
['tlg', 'tolong'], ['tq', 'terima kasih'], ['tkasih', 'terima kasih'],
|
|
26
|
+
['lg', 'lagi'], ['jgn', 'jangan'], ['sy', 'saya'], ['x', 'tak'],
|
|
27
|
+
['no.', 'nombor'], ['tel.', 'telefon'], ['ptg', 'petang'], ['pg', 'pagi'],
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
const UNITS = [
|
|
31
|
+
['km/j', 'kilometer sejam'], ['km/h', 'kilometer sejam'],
|
|
32
|
+
['°c', 'darjah Celsius'], ['km', 'kilometer'], ['kg', 'kilogram'],
|
|
33
|
+
['cm', 'sentimeter'], ['mm', 'milimeter'], ['ml', 'mililiter'],
|
|
34
|
+
['mg', 'miligram'], ['g', 'gram'], ['m', 'meter'], ['l', 'liter'],
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
const DIGIT_CONTEXT = '(?:kod|otp|pin|nombor|no\\.?|akaun|telefon|tel\\.?|ic|tac)';
|
|
38
|
+
const NUM = '\\d{1,3}(?:,\\d{3})+(?:\\.\\d+)?|\\d+(?:\\.\\d+)?';
|
|
39
|
+
const TIME_SFX = '(am|pm|a\\.m\\.|p\\.m\\.|pagi|petang|tengah hari|malam)';
|
|
40
|
+
const DIGITS = '0123456789';
|
|
41
|
+
|
|
42
|
+
function sub(pattern, repl, text, ci = false) {
|
|
43
|
+
return text.replace(new RegExp(pattern, ci ? 'gi' : 'g'), repl);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Wrap a Python-style callback: m(i) returns group i or ''.
|
|
47
|
+
function cb(fn) {
|
|
48
|
+
return (...args) => {
|
|
49
|
+
let n = args.length - 2;
|
|
50
|
+
if (typeof args[n] !== 'number') n -= 1; // named groups object present
|
|
51
|
+
const groups = args.slice(0, n);
|
|
52
|
+
const m = (i) => groups[i] || '';
|
|
53
|
+
return fn(m);
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function cardinal(n) {
|
|
58
|
+
if (n < 10) return ONES[n];
|
|
59
|
+
if (n === 10) return 'sepuluh';
|
|
60
|
+
if (n === 11) return 'sebelas';
|
|
61
|
+
if (n < 20) return ONES[n - 10] + ' belas';
|
|
62
|
+
if (n < 100) {
|
|
63
|
+
const t = Math.floor(n / 10), r = n % 10;
|
|
64
|
+
return ONES[t] + ' puluh' + (r === 0 ? '' : ' ' + ONES[r]);
|
|
65
|
+
}
|
|
66
|
+
const scales = [[1e12, 'trilion', 'satu trilion'], [1e9, 'bilion', 'satu bilion'],
|
|
67
|
+
[1e6, 'juta', 'satu juta'], [1000, 'ribu', 'seribu'], [100, 'ratus', 'seratus']];
|
|
68
|
+
for (const [size, word, single] of scales) {
|
|
69
|
+
if (n >= size) {
|
|
70
|
+
const q = Math.floor(n / size), r = n % size;
|
|
71
|
+
const head = q === 1 ? single : cardinal(q) + ' ' + word;
|
|
72
|
+
return head + (r === 0 ? '' : ' ' + cardinal(r));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
throw new Error('cardinal ' + n);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function digits(s) {
|
|
79
|
+
const out = [];
|
|
80
|
+
for (const ch of s) {
|
|
81
|
+
if (DIGITS.includes(ch)) out.push(ONES[+ch]);
|
|
82
|
+
else if (ch === '+') out.push('tambah');
|
|
83
|
+
}
|
|
84
|
+
return out.join(' ');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function number(s) {
|
|
88
|
+
s = s.replace(/,/g, '');
|
|
89
|
+
const dot = s.indexOf('.');
|
|
90
|
+
if (dot >= 0) {
|
|
91
|
+
const whole = s.slice(0, dot), frac = s.slice(dot + 1);
|
|
92
|
+
return cardinal(parseInt(whole || '0', 10)) + ' perpuluhan ' + digits(frac);
|
|
93
|
+
}
|
|
94
|
+
return cardinal(parseInt(s, 10));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function ordinal(n) {
|
|
98
|
+
return n === 1 ? 'pertama' : 'ke' + cardinal(n);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function timeWords(h, m, suffix) {
|
|
102
|
+
h = parseInt(h, 10); m = parseInt(m, 10);
|
|
103
|
+
let part = '';
|
|
104
|
+
if (suffix) {
|
|
105
|
+
const sfx = suffix.toLowerCase().replace(/\./g, '');
|
|
106
|
+
if (sfx === 'am') part = 'pagi';
|
|
107
|
+
else if (sfx === 'pm') part = h === 12 ? 'tengah hari' : (h < 7 ? 'petang' : 'malam');
|
|
108
|
+
else part = suffix;
|
|
109
|
+
}
|
|
110
|
+
const words = cardinal(h) + (m === 0 ? '' : ' ' + cardinal(m));
|
|
111
|
+
return words + (part ? ' ' + part : '');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function stripAccents(text) {
|
|
115
|
+
return text.normalize('NFD').replace(/\p{Mn}/gu, '').normalize('NFC');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const money = cb((m) => {
|
|
119
|
+
const amount = m(2), scale = m(3);
|
|
120
|
+
const unit = m(1).toLowerCase() === 'rm' ? 'ringgit' : 'dolar';
|
|
121
|
+
if (scale) return number(amount) + ' ' + scale.toLowerCase() + ' ' + unit;
|
|
122
|
+
const a = amount.replace(/,/g, '');
|
|
123
|
+
const dot = a.indexOf('.');
|
|
124
|
+
const whole = dot < 0 ? a : a.slice(0, dot);
|
|
125
|
+
const frac = dot < 0 ? '' : a.slice(dot + 1);
|
|
126
|
+
const wholeN = parseInt(whole || '0', 10);
|
|
127
|
+
const sen = frac ? parseInt((frac + '00').slice(0, 2), 10) : 0;
|
|
128
|
+
const parts = [];
|
|
129
|
+
if (wholeN || !sen) parts.push(cardinal(wholeN) + ' ' + unit);
|
|
130
|
+
if (sen) parts.push(cardinal(sen) + ' sen');
|
|
131
|
+
return parts.join(' ');
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
const dateNum = cb((m) => {
|
|
135
|
+
const d = parseInt(m(1), 10), mo = parseInt(m(2), 10), y = m(3);
|
|
136
|
+
if (!(mo >= 1 && mo <= 12 && d >= 1 && d <= 31)) return m(0);
|
|
137
|
+
const yy = y.length === 4 ? parseInt(y, 10) : 2000 + parseInt(y, 10);
|
|
138
|
+
return cardinal(d) + ' ' + MONTHS[mo - 1] + ' ' + cardinal(yy);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
const dateWord = cb((m) => {
|
|
142
|
+
const mo = MONTH_ABBR[m(2).toLowerCase().slice(0, 3)];
|
|
143
|
+
let out = cardinal(parseInt(m(1), 10)) + ' ' + MONTHS[mo - 1];
|
|
144
|
+
if (m(3)) out += ' ' + cardinal(parseInt(m(3), 10));
|
|
145
|
+
return out;
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const timeM = cb((m) => {
|
|
149
|
+
const lead = m(1);
|
|
150
|
+
return (lead ? lead : 'pukul ') + timeWords(m(2), m(3), m(4));
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
const numM = cb((m) => {
|
|
154
|
+
const s = m(0);
|
|
155
|
+
const plain = s.replace(/,/g, '');
|
|
156
|
+
const isInt = [...plain].every((c) => DIGITS.includes(c));
|
|
157
|
+
if ((plain.length > 1 && plain[0] === '0' && !plain.includes('.')) || (isInt && plain.length >= 8)) {
|
|
158
|
+
return digits(plain);
|
|
159
|
+
}
|
|
160
|
+
return number(s);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
function esc(s) {
|
|
164
|
+
return s.replace(/([.*+?^${}()|[\]\\/])/g, '\\$1');
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function normalize(text) {
|
|
168
|
+
let t = stripAccents(text);
|
|
169
|
+
t = t.replace(/’/g, "'").replace(/‘/g, "'").replace(/“/g, '"').replace(/”/g, '"');
|
|
170
|
+
t = t.replace(/–/g, '-').replace(/—/g, ' — ');
|
|
171
|
+
t = t.replace(/[\u00a0\u1680\u2000-\u200b\u2028\u2029\u202f\u205f\u3000\ufeff\t\r\n\f\v]/g, ' ');
|
|
172
|
+
|
|
173
|
+
t = sub('\\+?\\b6?0\\d{1,2}[- ]?\\d{3,4}[- ]?\\d{3,4}\\b', cb((m) => ' ' + digits(m(0)) + ' '), t);
|
|
174
|
+
t = sub('\\b\\d{6}-\\d{2}-\\d{4}\\b', cb((m) => ' ' + digits(m(0)) + ' '), t);
|
|
175
|
+
t = sub('\\b(' + DIGIT_CONTEXT + ')((?:\\s+(?!RM|USD|US\\$)[A-Za-z]+){0,3}\\s*:?\\s*)(?!RM|USD|US\\$)(\\d[\\d\\- ]*\\d|\\d)\\b(?![.,]\\d)',
|
|
176
|
+
cb((m) => m(1) + m(2) + digits(m(3))), t, true);
|
|
177
|
+
|
|
178
|
+
t = sub('\\b(RM|USD|US\\$)\\s?(' + NUM + ')(?:\\s?(juta|bilion|ribu|trilion)\\b)?', money, t, true);
|
|
179
|
+
t = sub('(\\$)\\s?(' + NUM + ')(?:\\s?(juta|bilion|ribu|trilion)\\b)?', money, t, true);
|
|
180
|
+
|
|
181
|
+
t = sub('(' + NUM + ')\\s?%', cb((m) => number(m(1)) + ' peratus'), t);
|
|
182
|
+
|
|
183
|
+
t = sub('\\b(\\d{1,2})[/-](\\d{1,2})[/-](\\d{4}|\\d{2})\\b', dateNum, t);
|
|
184
|
+
t = sub('\\b(\\d{1,2})\\s+(' + MONTH_RE + ')\\.?(?:\\s+(\\d{4}))?\\b', dateWord, t, true);
|
|
185
|
+
|
|
186
|
+
t = sub('(\\b(?:pukul|jam)\\s+)?\\b(\\d{1,2}):(\\d{2})\\s?' + TIME_SFX + '?', timeM, t, true);
|
|
187
|
+
t = sub('(\\b(?:pukul|jam)\\s+)\\b(\\d{1,2})\\.(\\d{2})\\s?' + TIME_SFX + '?', timeM, t, true);
|
|
188
|
+
t = sub('()\\b(\\d{1,2})\\.(\\d{2})\\s?' + TIME_SFX, timeM, t, true);
|
|
189
|
+
t = sub('(\\b(?:pukul|jam)\\s+)?\\b(\\d{1,2})\\s?(am|pm|a\\.m\\.|p\\.m\\.)(?=\\W|$)',
|
|
190
|
+
cb((m) => (m(1) || 'pukul ') + timeWords(m(2), 0, m(3))), t, true);
|
|
191
|
+
|
|
192
|
+
t = sub('\\bke-?(\\d+)\\b', cb((m) => ordinal(parseInt(m(1), 10))), t, true);
|
|
193
|
+
t = sub('\\b(' + NUM + ')\\s?-\\s?(' + NUM + ')\\b',
|
|
194
|
+
cb((m) => number(m(1)) + ' hingga ' + number(m(2))), t);
|
|
195
|
+
|
|
196
|
+
for (const [u, w] of UNITS) {
|
|
197
|
+
t = sub('(\\d)\\s?' + esc(u) + '(?![a-zA-Z])', cb((m) => m(1) + ' ' + w), t, true);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
t = sub(NUM, numM, t);
|
|
201
|
+
|
|
202
|
+
const abbr = ABBR.slice().sort((a, b) => b[0].length - a[0].length);
|
|
203
|
+
for (const [a, w] of abbr) {
|
|
204
|
+
const pat = '(?<![\\w.])' + esc(a) + (a.endsWith('.') ? '(?!\\w)' : '(?![\\w.])');
|
|
205
|
+
t = sub(pat, () => w, t, true);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
t = t.split('&').join(' dan ').split('+').join(' tambah ').split('=').join(' sama dengan ')
|
|
209
|
+
.split('%').join(' peratus ');
|
|
210
|
+
|
|
211
|
+
t = sub('\\s+', ' ', t).trim();
|
|
212
|
+
t = sub('\\s+([,.!?;:…])', '$1', t);
|
|
213
|
+
if (/\.$/.test(text.replace(/\s+$/, '')) && t && !'.!?…'.includes(t[t.length - 1])) t += '.';
|
|
214
|
+
return t;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export { cardinal, digits, number };
|