Text-to-Speech
ONNX
Vietnamese
vietnamese
flow-matching
webgpu
on-device
trada-tts / web /vi_fix.js
tsdocode's picture
Trà Đá TTS 10M: torch / ONNX / WebGPU inference, synthetic voices, web demo
531d4d5 verified
Raw History Blame Contribute Delete
2.91 kB
// Pre-normalisation fixes for two misaki ViCleaner bugs (also present in the Python original):
// "1,5" -> "mười lăm" (Vietnamese decimal comma ignored) => "một phẩy năm"
// "85.000.000" -> "... triệu không trăm nghìn không trăm" => "tám mươi lăm triệu"
// Numbers written with '.' thousand separators and decimal commas are spelled out here, before textNormalize().
const D = ['không', 'một', 'hai', 'ba', 'bốn', 'năm', 'sáu', 'bảy', 'tám', 'chín'];
function read3(n, full) { // 0..999; full = a higher group exists (so "không trăm", "linh" are spoken)
const h = Math.floor(n / 100), t = Math.floor(n / 10) % 10, u = n % 10, w = [];
if (h > 0 || full) w.push(D[h], 'trăm');
if (t > 1) { w.push(D[t], 'mươi'); if (u === 1) w.push('mốt'); else if (u === 4) w.push('tư'); else if (u === 5) w.push('lăm'); else if (u > 0) w.push(D[u]); }
else if (t === 1) { w.push('mười'); if (u === 5) w.push('lăm'); else if (u > 0) w.push(D[u]); }
else if (u > 0) { if (h > 0 || full) w.push('linh'); w.push(D[u]); }
return w;
}
export function readInt(s) { // digit string -> Vietnamese words (handles up to 10^18)
s = s.replace(/^0+(?=\d)/, '');
if (s === '0') return 'không';
const units = ['', 'nghìn', 'triệu', 'tỷ', 'nghìn tỷ', 'triệu tỷ'], groups = [];
for (let i = s.length; i > 0; i -= 3) groups.unshift(+s.slice(Math.max(0, i - 3), i));
const out = [];
groups.forEach((g, i) => {
const pos = groups.length - 1 - i;
if (g === 0) return; // skip all-zero groups ("85.000.000" -> "tám mươi lăm triệu")
out.push(...read3(g, i > 0)); if (units[pos]) out.push(units[pos]);
});
return out.join(' ');
}
const readDigits = s => [...s].map(c => D[+c]).join(' ');
export function fixNumbers(text) {
// money: 85.000.000đ / 20.000 VNĐ / 1,5 tỷ đồng (keep the unit word, spell the number)
text = text.replace(/(\d{1,3}(?:\.\d{3})+|\d+)(?:,(\d+))?\s*(đ|vnđ|vnd|đồng)(?![\p{L}])/giu,
(_, a, b, cur) => `${readInt(a.replace(/\./g, ''))}${b ? ' phẩy ' + readDigits(b) : ''} đồng`);
// grouped integers with optional decimal: 1.234.567 / 1.234,5
text = text.replace(/(?<![\d.,])(\d{1,3}(?:\.\d{3})+)(?:,(\d+))?(?![\d.,]\d)/gu,
(_, a, b) => `${readInt(a.replace(/\./g, ''))}${b ? ' phẩy ' + readDigits(b) : ''}`);
// decimal point written English-style with 1-2 digits (a '.' + 3 digits is a thousands group): 1.7% / 2.5 lần
text = text.replace(/(?<![\d.,])(\d+)\.(\d{1,2})(?![\d]|\.\d)/gu, (_, a, b) => `${readInt(a)} phẩy ${readInt(b)}`);
// plain decimals with a comma: 1,5 / 3,14 / 2,7kg / 3,5%
text = text.replace(/(?<![\d.,])(\d+),(\d+)(?![\d]|,\d)/gu, (_, a, b) => `${readInt(a)} phẩy ${b.length <= 2 ? readInt(b) : readDigits(b)}`);
return text;
}