UNPKG

phonemize

Version:

Fast phonemizer with rule-based G2P prediction. Pure JavaScript implementation.

2 lines (1 loc) 5.62 kB
import{addDict,pinyin}from"pinyin-pro";import*as dict from"./zh/dict.json";import{r as resolveJson,p as pinyinToZhuyin}from"./utils-Bo_ZN3X7.mjs";var ZH_DIGITS=[`零`,`一`,`二`,`三`,`四`,`五`,`六`,`七`,`八`,`九`],ZH_UNITS_4=[``,`十`,`百`,`千`],ZH_BIG_SCALES=[``,`万`,`亿`,`万亿`,`亿亿`];function readSection(e){if(e===0)return``;for(var l=``,u=!1,d=1e3,f=3;f>=0;f--){var h=Math.floor(e/d)%10;h===0?l.length>0&&(u=!0):(u&&=(l+=`零`,!1),l+=ZH_DIGITS[h]+ZH_UNITS_4[f]),d=Math.floor(d/10)}return l}function numberToWords(e){if(e===0)return`零`;if(e<0)return`负`+numberToWords(-e);for(var l=[],u=e;u>0;)l.unshift(u%1e4),u=Math.floor(u/1e4);for(var d=``,f=0;f<l.length;f++){var p=l[f],m=l.length-1-f;if(p===0){d.length>0&&!d.endsWith(`零`)&&l.slice(f+1).some(function(e){return e!==0})&&(d+=`零`);continue}d.length>0&&p<1e3&&!d.endsWith(`零`)&&(d+=`零`),d+=readSection(p),d+=ZH_BIG_SCALES[m]}return e>=10&&e<20&&(d=d.replace(/^一十/,`十`)),d}function digitByDigit(e){return e.split(``).map(function(e){return e>=`0`&&e<=`9`?ZH_DIGITS[e.charCodeAt(0)-48]:e}).join(``)}function readDecimal(e,l){var u=parseInt(e,10);return numberToWords(u)+`点`+digitByDigit(l)}function readCurrency(e,l){var u=e.indexOf(`.`);if(u===-1)return numberToWords(parseInt(e,10))+l;var d=parseInt(e.slice(0,u),10),f=e.slice(u+1).padEnd(2,`0`).slice(0,2),m=parseInt(f[0],10),h=parseInt(f[1],10),g=numberToWords(d)+l;return m>0&&(g+=ZH_DIGITS[m]+`角`),h>0&&(g+=ZH_DIGITS[h]+`分`),g}function expandChineseText(e){return e=e.replace(/(\d{4})年/g,function(e,l){return digitByDigit(l)+`年`}),e=e.replace(/[¥¥]\s*(\d+(?:\.\d+)?)/g,function(e,l){return readCurrency(l,`元`)}),e=e.replace(/\$\s*(\d+(?:\.\d+)?)/g,function(e,l){return readCurrency(l,`美元`)}),e=e.replace(/\b(\d{1,2}):(\d{2})\b/g,function(e,l,u){var d=parseInt(l,10),f=parseInt(u,10),p=numberToWords(d)+`点`;return f===0?p+`整`:p+numberToWords(f)+`分`}),e=e.replace(/第(\d+)/g,function(e,l){return`第`+numberToWords(parseInt(l,10))}),e=e.replace(/(\d+(?:\.\d+)?)%/g,function(e,l){var u=l.indexOf(`.`);return u===-1?`百分之`+numberToWords(parseInt(l,10)):`百分之`+readDecimal(l.slice(0,u),l.slice(u+1))}),e=e.replace(/(\d+)\.(\d+)/g,function(e,l,u){return readDecimal(l,u)}),e=e.replace(/\d+/g,function(e){return numberToWords(parseInt(e,10))}),e}addDict(resolveJson(dict),`phonemize-zh`),addDict({},`custom`);var PINYIN_TO_IPA={b:`p`,p:`pʰ`,d:`t`,t:`tʰ`,g:`k`,k:`kʰ`,j:`tɕ`,q:`tɕʰ`,zh:`ʈʂ`,ch:`ʈʂʰ`,z:`ts`,c:`tsʰ`,f:`f`,x:`ɕ`,sh:`ʂ`,r:`ʐ`,s:`s`,h:`x`,m:`m`,n:`n`,l:`l`,w:`w`,y:`j`,a:`a`,o:`o`,e:`ə`,i:`i`,u:`u`,ü:`y`,v:`y`,ai:`aɪ`,ei:`eɪ`,ao:`ɑʊ`,ou:`oʊ`,an:`an`,en:`ən`,ang:`ɑŋ`,eng:`əŋ`,ong:`ʊŋ`,er:`ɚ`,ia:`ia`,ie:`iɛ`,iao:`iɑʊ`,iu:`ioʊ`,iou:`ioʊ`,ian:`iɛn`,in:`in`,iang:`iɑŋ`,ing:`iŋ`,iong:`iʊŋ`,ua:`ua`,uo:`uɔ`,uai:`uaɪ`,ui:`ueɪ`,uei:`ueɪ`,uan:`uan`,un:`uən`,uen:`uən`,uang:`uɑŋ`,ueng:`uəŋ`e:`yɛ`,ve:`yɛ`an:`yɛn`,van:`yɛn`n:`yn`,vn:`yn`,zhi:`ʈʂɨ`,chi:`ʈʂʰɨ`,shi:`ʂɨ`,ri:`ʐɨ`,zi:`tsɨ`,ci:`tsʰɨ`,si:`sɨ`,zhong:`ʈʂʊŋ`,wen:`wən`,hao:`xɑʊ`,de:`tə`,de0:`tə`,wo:`wɔ`,ta:`tʰa`,zhe:`ʈʂə`,ge:`kə`,le:`lə`,yi:`i`,san:`san`,wu:`wu`,liu:`lioʊ`,qi:`tɕi`,ba:`pa`,jiu:`tɕioʊ`},TONE_MARKS={1:`˥˥`,2:`˧˥`,3:`˧˩˧`,4:`˥˩`,5:`˧`,0:``},INITIAL_PATTERNS=[`zh`,`ch`,`sh`],SINGLE_INITIALS=[`b`,`p`,`m`,`f`,`d`,`t`,`n`,`l`,`g`,`k`,`h`,`j`,`q`,`x`,`r`,`z`,`c`,`s`,`y`,`w`],ChineseG2P=function(){function u(){this.id=`zh-g2p`,this.name=`Chinese G2P Processor`,this.supportedLanguages=[`zh`]}return u.prototype.preProcess=function(e){return expandChineseText(e)},u.prototype.predict=function(e,l,u){return this.textToIPA(e)},u.prototype.textToIPA=function(e){if(!e?.trim())return``;var l=this.textToPinyinResults(e);return l.map(function(e){return e.ipa}).join(` `)},u.prototype.textToZhuyin=function(e){var l=this;if(!e?.trim())return``;var u=this.textToPinyinResults(e);return u.map(function(e){return l.isChinese(e.word)?pinyinToZhuyin(e.pinyin):e.word}).join(` `)},u.prototype.textToPinyinResults=function(e){if(!e?.trim())return[];var u=[];try{for(var d=pinyin(e,{toneType:`num`,type:`array`,v:!0,nonZh:`removed`}),f=0;f<e.length;f++){var p=e[f];if(this.isChinese(p)){var m=d[Math.min(f,d.length-1)],h=this.parsePinyinWithTone(m||p),g=h.syllable,_=h.tone,v=this.pinyinToIPA(g,_);u.push({pinyin:m||p,tone:_,ipa:v,word:p})}else u.push({pinyin:p,tone:0,ipa:p,word:p})}}catch(l){return console.warn(`Chinese G2P conversion failed:`,l),Array.from(e).map(function(e){return{pinyin:e,tone:0,ipa:e,word:e}})}return u},u.prototype.pinyinToIPA=function(e,l){var u=PINYIN_TO_IPA[e];if(u)return u+TONE_MARKS[l];var d=this.decomposePinyin(e),f=d.initial,p=d.final,m=PINYIN_TO_IPA[f]||``,h=PINYIN_TO_IPA[p]||p;return m+h+TONE_MARKS[l]},u.prototype.decomposePinyin=function(e){for(var l=0,u=INITIAL_PATTERNS;l<u.length;l++){var d=u[l];if(e.startsWith(d))return{initial:d,final:e.slice(d.length)}}for(var f=0,p=SINGLE_INITIALS;f<p.length;f++){var d=p[f];if(e.startsWith(d))return{initial:d,final:e.slice(1)}}return{initial:``,final:e}},u.prototype.parsePinyinWithTone=function(e){var l=e.match(/^(.+?)([1-5]?)$/);if(l){var u=l[1],d=l[2],f=d?parseInt(d,10):5;return{syllable:u,tone:f}}return{syllable:e,tone:5}},u.prototype.isChinese=function(e){var l=e.charCodeAt(0);return l>=19968&&l<=40959||l>=13312&&l<=19903||l>=131072&&l<=173791||l>=173824&&l<=177983||l>=177984&&l<=178207||l>=178208&&l<=183983||l>=183984&&l<=191471},u.prototype.addPronunciation=function(l,u){var d;addDict((d={},d[l.trim()]=u,d),{name:`custom`,dict1:`add`})},u}();export{ChineseG2P as default};