File size: 4,003 Bytes
ee603ee | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | // the release text front end as one plain script for embedded JS engines (QuickJS from Python, JavaScriptCore on iOS, ...):
// globalThis.kaleFrontEnd.init(lexiconTsv, overrideTsv, termsTsv, g2pBase64); kaleFrontEnd.run(text) -> JSON [{text, ipa}]
// Same code path as js/kale-tts.mjs (dizione rules -> terms -> CMUdict lexicon + overrides + g2p model). Build: npm run build-frontend
import { G2p, G2pModel, normalizer, readTable, sentences } from "../frontend/words/kaleTts.js";
import { BritishG2p, britishNormalizer } from "../frontend/words/enGb.js";
import { EttinTokenizer, ettinInputs, wordIndex } from "../frontend/ettin.js";
function termsReplacer(table) { // = js/kale-tts.mjs termsReplacer
const terms = readTable(table);
if (terms.size === 0) return (t) => t;
const esc = (x) => x.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
// a written form starting with "." is a suffix too: ".js" also matches in "Node.js"
const forms = [...terms.keys()].sort((a, b) => b.length - a.length).map((f) => (f.startsWith(".") ? esc(f) : `(?<![\\p{L}\\p{N}_])${esc(f)}`));
const pattern = new RegExp(`(?:${forms.join("|")})(?![\\p{L}\\p{N}_])`, "gu");
const lookUp = (t) => t.replace(pattern, (f, at, whole) => {
const spoken = terms.get(f) ?? f;
return f.startsWith(".") && at > 0 && /[\p{L}\p{N}]/u.test(whole[at - 1]) ? ` ${spoken}` : spoken; // "Node.js" -> "Node dot J S"
});
const CAMEL = /(?<![\p{L}\p{N}_])[a-z]+(?:[A-Z][a-z0-9]*)+(?![\p{L}\p{N}_])/gu;
const split = (t) => t.replace(CAMEL, (w) => w.replace(/(?<=[a-z0-9])(?=[A-Z])/g, " "));
return (t) => lookUp(split(lookUp(t)));
}
function b64(s) {
const A = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", m = new Uint8Array(256); for (let i = 0; i < 64; i++) m[A.charCodeAt(i)] = i;
s = s.replace(/=+$/, ""); const out = new Uint8Array((s.length * 3) >> 2); let o = 0;
for (let i = 0; i < s.length; i += 4) { const n = (m[s.charCodeAt(i)] << 18) | (m[s.charCodeAt(i + 1)] << 12) | (m[s.charCodeAt(i + 2)] << 6) | m[s.charCodeAt(i + 3)];
out[o++] = n >> 16; if (o < out.length) out[o++] = (n >> 8) & 255; if (o < out.length) out[o++] = n & 255; }
return out.buffer;
}
const fes = {};
function make(lexicon, override, terms, g2pB64, gb) {
const lex = readTable(lexicon); readTable(override, lex);
const g2p = gb ? new BritishG2p(lex, new G2pModel(b64(g2pB64))) : new G2p(lex, new G2pModel(b64(g2pB64)));
const norm = gb ? britishNormalizer(lex) : normalizer(lex), applyTerms = termsReplacer(terms);
return { g2p, normalize: (t) => norm(applyTerms(t)) };
}
globalThis.kaleFrontEnd = {
init(lexicon, override, terms, g2pB64) { fes["en-us"] = make(lexicon, override, terms, g2pB64, false); return true; },
initGb(lexicon, override, terms, g2pB64) { fes["en-gb"] = make(lexicon, override, terms, g2pB64, true); return true; }, // = frontend/release-loader.mjs
initEttin(tokenizerJson) { fes.ettin = new EttinTokenizer(JSON.parse(tokenizerJson)); return true; },
// Ettin inputs for one sentence (kale-tts-1-tiny's Ettin text side): sub-tokens with the previous sentence as context, word averages,
// and the word index for the sentence's sound ids (sentIds, per word: the same front end's ids for that word)
ettin(text, prev, sentIdsJson, wordIdsJson) {
const e = ettinInputs(fes.ettin, text, prev), words = text.split(/\s+/).filter(Boolean);
return JSON.stringify({ ids: e.ettin_ids, W: e.W, S: e.S, wavg: Array.from(e.wavg), widx: wordIndex(JSON.parse(sentIdsJson), JSON.parse(wordIdsJson), words.length) });
},
ipaWords(text, accent = "en-us") { // the sentence's words through the same g2p, one IPA string per word
const fe = fes[accent] ?? fes["en-us"];
return JSON.stringify(text.split(/\s+/).filter(Boolean).map((w) => fe.g2p.phonemes(w)));
},
run(text, accent = "en-us") {
const fe = fes[accent] ?? fes["en-us"];
return JSON.stringify(sentences(text, fe.normalize).map((s) => ({ text: s, ipa: fe.g2p.phonemes(s) })));
},
};
|