英文语音基于音素估算字幕时间实例页面
回到相关文章 »效果:
朗读内容是:
各分句朗读时间范围:
试听
字幕显示
//zxx: 完整交互代码参见页面源代码
代码:
核心JS代码:
// s 参数指英文单词 function getPhonemeCount(s) { let totalSyllables = 0; // qu to tq s = s.replace(/qu/g, 'qw'); // replace endings s = s.replace(/(es$)|(que$)|(gue$)/g, ''); s = s.replace(/^re/, 'ren'); s = s.replace(/^gua/, 'ga'); s = s.replace(/([aeiou])(l+e$)/g, '$1'); let syllables = (s.match(/([bcdfghjklmnpqrstvwxyz])(l+e$)/g) || []).length; totalSyllables += syllables; s = s.replace(/([bcdfghjklmnpqrstvwxyz])(l+e$)/g, '$1'); s = s.replace(/([aeiou])(ed$)/g, '$1'); syllables = (s.match(/([bcdfghjklmnpqrstvwxyz])(ed$)/g) || []).length; totalSyllables += syllables; s = s.replace(/([bcdfghjklmnpqrstvwxyz])(ed$)/g, '$1'); const endsp = /(ly$)|(ful$)|(ness$)|(ing$)|(est$)|(er$)|(ent$)|(ence$)/g; syllables = (s.match(endsp) || []).length; totalSyllables += syllables; s = s.replace(endsp, ''); syllables = (s.match(endsp) || []).length; totalSyllables += syllables; s = s.replace(endsp, ''); s = s.replace(/(^y)([aeiou][aeiou]*)/, '$2'); s = s.replace(/([aeiou])(y)/g, '$1t'); s = s.replace(/aa+/g, 'a'); s = s.replace(/ee+/g, 'e'); s = s.replace(/ii+/g, 'i'); s = s.replace(/oo+/g, 'o'); s = s.replace(/uu+/g, 'u'); // Dipthongs const dipthongs = /(eau)|(iou)|(are)|(ai)|(au)|(ea)|(ei)|(eu)|(ie)|(io)|(oa)|(oe)|(oi)|(ou)|(ue)|(ui)/g; syllables = (s.match(dipthongs) || []).length; totalSyllables += syllables; s = s.replace(dipthongs, ''); // Remove silent 'e' if length is greater than 3 if (s.length > 3) { s = s.replace(/([bcdfghjklmnpqrstvwxyz])(e$)/g, '$1'); } // Count vowels syllables = (s.match(/[aeiouy]/g) || []).length; totalSyllables += syllables; return totalSyllables; }