是否有计算单词中音节数的 javascript 库?怎么算?
谢谢
编辑
感谢 Sydenam 和 zozo 提供的有用信息和可能的答案。
我通过 Pesto at this forum 找到了代码,但它是用 Ruby 编写的。其简明版本之一如下:
def new_count(word)
word.downcase!
return 1 if word.length <= 3
word.sub!(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '')
word.sub!(/^y/, '')
word.scan(/[aeiouy]{1,2}/).size
end
这看起来很短但很复杂。你能把这个函数翻译成javascript吗? 再次感谢。
最佳答案
翻译成 javascript:
function new_count(word) {
word = word.toLowerCase(); //word.downcase!
if(word.length <= 3) { return 1; } //return 1 if word.length <= 3
word = word.replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, ''); //word.sub!(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '')
word = word.replace(/^y/, ''); //word.sub!(/^y/, '')
return word.match(/[aeiouy]{1,2}/g).length; //word.scan(/[aeiouy]{1,2}/).size
}
console.log(new_count('she'));
console.log(new_count('spain'))
console.log(new_count('softball'))
console.log(new_count('contagion'))
关于javascript - 如何计算javascript中一个单词的音节数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5686483/