使用Javascript计算段落中的音节

时间:2014-12-12 19:58:09

标签: javascript flesch-kincaid

我正在处理一个项目,该项目要求我返回输入框中输入的文本的Flesch可读性指数。公式如下:

206.835-85.6(number of syllables / number of words)-1.015(number of words / number of sentences)

我已经弄清楚如何统计这些单词,而且我很确定我知道如何计算句子,但我不知道如何计算音节。

这里提供了关于什么算作音节的信息:

Each group of adjacent vowels (a, e, i, o, u, y) counts as one syllable.
Each word has at least one syllable even if the above rule gives it a count of 0.

有人愿意指出我如何解决这个问题的正确方向吗?

更新 我已经编写了一些代码,但是无法让它工作。我发布了一个指向jsfiddle的链接,我正在评论下面的答案以及我遇到的问题。如果有人愿意查看它,看看你是否可以帮助我弄清楚我做错了什么(除此之外很可能是一种更有效的方式),我们将非常感激。

1 个答案:

答案 0 :(得分:2)

伪代码:

for each letter in word:
    if letter is vowel, and previous letter is not vowel or this is the first letter, increment numSyllables

if numSyllables is 0, set numSyllables to 1

您可以使用布尔值作为标志来确定前一个字母是否是元音。

循环显示单词中的每个字母:

var word = "test",
    i;
for(i = 0; i < word.length; i++) {
    console.log(word[i]);
}
相关问题