MCPcopy
hub / github.com/hexgrad/kokoro / phonemize

Function phonemize

kokoro.js/src/phonemize.js:174–204  ·  view source on GitHub ↗
(text, language = "a", norm = true)

Source from the content-addressed store, hash-verified

172 * @returns {Promise<string>} The phonemized text
173 */
174export async function phonemize(text, language = "a", norm = true) {
175 // 1. Normalize text
176 if (norm) {
177 text = normalize_text(text);
178 }
179
180 // 2. Split into chunks, to ensure we preserve punctuation
181 const sections = split(text, PUNCTUATION_PATTERN);
182
183 // 3. Convert each section to phonemes
184 const lang = language === "a" ? "en-us" : "en";
185 const ps = (await Promise.all(sections.map(async ({ match, text }) => (match ? text : (await espeakng(text, lang)).join(" "))))).join("");
186
187 // 4. Post-process phonemes
188 let processed = ps
189 // https://en.wiktionary.org/wiki/kokoro#English
190 .replace(/kəkˈoːɹoʊ/g, "kˈoʊkəɹoʊ")
191 .replace(/kəkˈɔːɹəʊ/g, "kˈəʊkəɹəʊ")
192 .replace(/ʲ/g, "j")
193 .replace(/r/g, "ɹ")
194 .replace(/x/g, "k")
195 .replace(/ɬ/g, "l")
196 .replace(/(?<=[a-zɹː])(?=hˈʌndɹɪd)/g, " ")
197 .replace(/ z(?=[;:,.!?¡¿—…"«»“” ]|$)/g, "z");
198
199 // 5. Additional post-processing for American English
200 if (language === "a") {
201 processed = processed.replace(/(?<=nˈaɪn)ti(?!ː)/g, "di");
202 }
203 return processed.trim();
204}

Callers 3

generateMethod · 0.90
streamMethod · 0.90
phonemize.test.jsFile · 0.90

Calls 2

normalize_textFunction · 0.85
splitFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…