talking-pixel / src /lib /frontend.ts
cfahlgren1's picture
cfahlgren1 HF Staff
convert talking pixel to a sveltekit app that runs pivo-1.4m tts in the browser
3ec6b9e
Raw History Blame Contribute Delete
10.3 kB
// 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 = ['<pad>', '<bos>', '<eos>', '<space>', '.', ',', '?', '!', ';', ':'];
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<string, string> = { 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 = `(?<!${W})`;
const B1 = `(?!${W})`;
const D = '\\p{Nd}';
// Code points of every Unicode Nd "zero" (Unicode 16, from Python's unicodedata).
const ND_ZEROS = [0x30, 0x660, 0x6f0, 0x7c0, 0x966, 0x9e6, 0xa66, 0xae6, 0xb66, 0xbe6, 0xc66, 0xce6, 0xd66, 0xde6, 0xe50, 0xed0, 0xf20, 0x1040, 0x1090, 0x17e0, 0x1810, 0x1946, 0x19d0, 0x1a80, 0x1a90, 0x1b50, 0x1bb0, 0x1c40, 0x1c50, 0xa620, 0xa8d0, 0xa900, 0xa9d0, 0xa9f0, 0xaa50, 0xabf0, 0xff10, 0x104a0, 0x10d30, 0x10d40, 0x11066, 0x110f0, 0x11136, 0x111d0, 0x112f0, 0x11450, 0x114d0, 0x11650, 0x116c0, 0x116d0, 0x116da, 0x11730, 0x118e0, 0x11950, 0x11bf0, 0x11c50, 0x11d50, 0x11da0, 0x11f50, 0x16130, 0x16a60, 0x16ac0, 0x16b50, 0x16d70, 0x1ccf0, 0x1d7ce, 0x1d7d8, 0x1d7e2, 0x1d7ec, 0x1d7f6, 0x1e140, 0x1e2f0, 0x1e4f0, 0x1e5f1, 0x1e950, 0x1fbf0];
export class FrontendError extends Error {
characters?: string[];
}
export interface WordSpan {
text: string;
index: number;
tokenStart: number;
tokenEnd: number;
}
export interface FrontendResult {
normalized: string;
symbols: string[];
ids: number[];
unknown: string[];
words: WordSpan[];
}
export type Lexicon = Map<string, string[]>;
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<string, string>)[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<string, string> = { '’': "'", '‘': "'", '“': '"', '”': '"', '—': ',', '–': ',', '…': '...' };
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 = ['<bos>'];
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] !== '<bos>' && symbols[symbols.length - 1] !== '<space>') symbols.push('<space>');
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('<eos>');
return { normalized, symbols, ids: symbols.map((s) => SYMBOL_TO_ID.get(s) as number), unknown, words };
}