// Exact JavaScript port of pivo/text.py (PiVo-1.4M English frontend). // Python's `re` is Unicode-aware for \d, \s and \b, so those classes are spelled // out explicitly here instead of relying on JavaScript's ASCII defaults. const CONSONANTS = 'B CH D DH F G HH JH K L M N NG P R S SH T TH V W Y Z ZH'.split(' '); const VOWELS = 'AA AE AH AO AW AY EH ER EY IH IY OW OY UH UW'.split(' '); export const SPECIAL = ['', '', '', '', '.', ',', '?', '!', ';', ':']; export const SYMBOLS = [...SPECIAL, ...CONSONANTS, ...VOWELS.flatMap((v) => ['0', '1', '2'].map((s) => v + s))]; const SYMBOL_TO_ID = new Map(SYMBOLS.map((s, i) => [s, i])); export const VOICE_IDS = { Pi: 0, Vo: 1 } as const; export type Voice = keyof typeof VOICE_IDS; const ONES = 'zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen'.split(' '); const TENS = 'zero ten twenty thirty forty fifty sixty seventy eighty ninety'.split(' '); const ORDINALS: Record = { one: 'first', two: 'second', three: 'third', five: 'fifth', eight: 'eighth', nine: 'ninth', twelve: 'twelfth' }; const MONTHS = 'January February March April May June July August September October November December'.split(' '); const SCALES: [bigint, string][] = [[10n ** 12n, 'trillion'], [10n ** 9n, 'billion'], [10n ** 6n, 'million'], [1000n, 'thousand'], [100n, 'hundred']]; // Python str.isspace() / re \s. const S = '\\t\\n\\v\\f\\r\\x1c-\\x1f \\x85\\xa0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000'; // Python re word characters (str.isalnum() or "_") for \b. const W = '[\\p{L}\\p{N}_]'; const B0 = `(?; function digitValue(ch: string): number { const cp = ch.codePointAt(0) as number; let zero = 0x30; for (const z of ND_ZEROS) { if (z > cp) break; zero = z; } return cp - zero; } // Python int() over a string of Unicode decimal digits. function toInt(digits: string): bigint { let ascii = ''; for (const ch of digits) ascii += String(digitValue(ch)); return BigInt(ascii); } export function cardinal(n: bigint): string { if (n < 0n) return 'minus ' + cardinal(-n); if (n < 20n) return ONES[Number(n)]; if (n < 100n) return TENS[Number(n / 10n)] + (n % 10n ? ' ' + cardinal(n % 10n) : ''); for (const [scale, label] of SCALES) { if (n < scale) continue; if (n >= 10n ** 15n) return [...String(n)].map((d) => ONES[Number(d)]).join(' '); return cardinal(n / scale) + ' ' + label + (n % scale ? ' ' + cardinal(n % scale) : ''); } throw new Error('unreachable'); } export function ordinal(n: bigint): string { const words = cardinal(n).split(' '); const last = words[words.length - 1]; words[words.length - 1] = ORDINALS[last] ?? (last.endsWith('y') ? last.slice(0, -1) + 'ieth' : last + 'th'); return words.join(' '); } function isoDate(_match: string, y: string, m: string, d: string): string { const year = Number(toInt(y)); const month = Number(toInt(m)); const day = Number(toInt(d)); if (year < 1 || year > 9999) throw new FrontendError(`year ${year} is out of range`); if (month < 1 || month > 12) throw new FrontendError('month must be in 1..12'); const leap = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0); const days = [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1]; if (day < 1 || day > days) throw new FrontendError('day is out of range for month'); return `${MONTHS[month - 1]} ${ordinal(BigInt(day))}, ${cardinal(BigInt(year))}`; } function money(_match: string, symbol: string, rawIn: string): string { const raw = rawIn.replaceAll(',', ''); const dot = raw.indexOf('.'); const whole = dot < 0 ? raw : raw.slice(0, dot); const fraction = dot < 0 ? '' : raw.slice(dot + 1); const unit = ({ $: 'dollar', '£': 'pound', '€': 'euro' } as Record)[symbol]; const value = toInt(whole); let result = cardinal(value) + ' ' + unit + (value !== 1n ? 's' : ''); const cents = fraction ? toInt(fraction.padEnd(2, '0')) : 0n; if (cents) { let name; if (cents === 1n && symbol === '£') name = ' penny'; else if (symbol === '£') name = ' pence'; else if (cents === 1n) name = ' cent'; else name = ' cents'; result += ' and ' + cardinal(cents) + name; } return result; } function spokenNumber(m: string): string { const negative = m.startsWith('-'); const body = m.replace(/^-+/, ''); let parts: string[]; if (m.includes('.')) { const fraction = m.split('.')[1]; parts = [cardinal(toInt(body.split('.')[0])), [...fraction].map((c) => ONES[digitValue(c)]).join(' ')]; } else { parts = [cardinal(toInt(body))]; } return (negative ? 'minus ' : '') + parts.join(' point '); } const ABBREVIATIONS = [['Mr', 'Mister'], ['Mrs', 'Missus'], ['Dr', 'Doctor'], ['Prof', 'Professor'], ['St', 'Saint'], ['vs', 'versus'], ['etc', 'et cetera']] .map(([a, e]) => [new RegExp(`${B0}${a}\\.(?=[${S}]|$)`, 'giu'), e] as const); const RE_ISO = new RegExp(`${B0}(${D}{4})-(${D}{2})-(${D}{2})${B1}`, 'gu'); const RE_SLASH = new RegExp(`${D}/${D}`, 'u'); const RE_MONEY_BAD = new RegExp(`[$£€]${D}[${D},]*\\.${D}{3,}`, 'u'); const RE_MONEY = new RegExp(`([$£€])(${D}[${D},]*(?:\\.${D}{1,2})?)(?!${D})`, 'gu'); const RE_ORDINAL = new RegExp(`${B0}(${D}+)(?:st|nd|rd|th)${B1}`, 'giu'); const RE_GROUPED = new RegExp(`${B0}${D}{1,3}(?:,${D}{3})+${B1}`, 'gu'); const RE_NUMBER = new RegExp(`-?(?:${D}+\\.${D}+|${D}+)`, 'gu'); const RE_NASA = new RegExp(`${B0}NASA${B1}`, 'gu'); const RE_INITIALISM = new RegExp(`${B0}[A-Z]{2,5}${B1}`, 'gu'); const RE_UNSUPPORTED = new RegExp(`[^a-zA-Z${S}'.,?!;:]`, 'gu'); const RE_SPACES = new RegExp(`[${S}]+`, 'gu'); const RE_TRIM = new RegExp(`^[${S}]+|[${S}]+$`, 'gu'); const TRANSLATE: Record = { '’': "'", '‘': "'", '“': '"', '”': '"', '—': ',', '–': ',', '…': '...' }; export function normalize(input: string): string { let text = input.normalize('NFKC').replace(/[’‘“”—–…]/g, (c) => TRANSLATE[c]); text = text.replace(RE_ISO, isoDate); if (RE_SLASH.test(text)) throw new FrontendError('write slash dates/fractions in words, or use an ISO date'); for (const [re, expansion] of ABBREVIATIONS) text = text.replace(re, () => expansion); if (RE_MONEY_BAD.test(text)) throw new FrontendError('currency amounts must have at most two decimal places'); text = text.replace(RE_MONEY, money); text = text.replace(RE_ORDINAL, (_m, n) => ordinal(toInt(n))); text = text.replace(RE_GROUPED, (m) => m.replaceAll(',', '')); text = text.replace(RE_NUMBER, spokenNumber); text = text.replaceAll('%', ' percent ').replaceAll('&', ' and ').replaceAll('+', ' plus '); text = text.replace(RE_NASA, 'nasa'); text = text.replace(RE_INITIALISM, (m) => m.split('').join(' ')); text = text.replaceAll('-', ' '); text = text.replace(/[()[\]{}"/]/g, ' '); const unsupported = [...new Set([...text.matchAll(RE_UNSUPPORTED)].map((m) => m[0]))] .sort((a, b) => (a.codePointAt(0) as number) - (b.codePointAt(0) as number)); if (unsupported.length) { const error = new FrontendError(`unsupported text characters after normalization: ${unsupported.join(' ')}`); error.characters = unsupported; throw error; } return text.replace(RE_SPACES, ' ').replace(RE_TRIM, ''); } // First pronunciation per word, in file order (pivo.text.lexicon()[word][0]). export function parseLexicon(raw: string): Lexicon { const dictionary: Lexicon = new Map(); for (const line of raw.split('\n')) { const parts = line.split('#', 1)[0].trim().split(/\s+/); if (parts.length < 2) continue; const word = parts[0].replace(/\(\d+\)$/, '').toLowerCase(); if (!dictionary.has(word)) dictionary.set(word, parts.slice(1)); } return dictionary; } const RE_ITEMS = /[A-Za-z]+(?:'[A-Za-z]+)*|[.,?!;:]/g; // Returns word spans (in normalized text) alongside tokens so the UI can align timing. export function phonemize(text: string, dictionary: Lexicon): FrontendResult { const normalized = normalize(text); const symbols = ['']; const unknown: string[] = []; const words: WordSpan[] = []; for (const match of normalized.matchAll(RE_ITEMS)) { const item = match[0]; if (SPECIAL.includes(item)) { symbols.push(item); continue; } const word = item.toLowerCase(); let phones = dictionary.get(word); if (phones === undefined) { unknown.push(word); phones = [...word].filter((c) => /[a-z]/.test(c)).flatMap((c) => dictionary.get(c) as string[]); } if (symbols[symbols.length - 1] !== '' && symbols[symbols.length - 1] !== '') symbols.push(''); const start = symbols.length; for (const phone of phones) { if (!SYMBOL_TO_ID.has(phone)) throw new FrontendError(`unknown ARPAbet phone ${phone} in ${word}`); symbols.push(phone); } words.push({ text: item, index: match.index as number, tokenStart: start, tokenEnd: symbols.length }); } symbols.push(''); return { normalized, symbols, ids: symbols.map((s) => SYMBOL_TO_ID.get(s) as number), unknown, words }; }