Search code examples
javascriptflesch-kincaid

Counting Syllables in Paragraph with Javascript


I'm working on a project that requires me to return the Flesch Readability Index for text entered in an input box. The formula is as follows:

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

I've figured out how to count the words, and pretty sure I know how count the sentences, but I have no idea how to go about counting the syllables.

Here's the information I was given on what to count as a syllable:

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.

Would anyone be willing to point me in the right direction on how to go about this?

Update I have written some code but am having trouble getting it to work. I posted a link to a jsfiddle I'm working on as a comment to the answer below along with the issue I'm having. If anyone would be willing to look it over and see if you can help me figure out what I'm doing wrong (other than that there is most likely a more efficient way of doing this), it would be much appreciated.


Solution

  • pseudocode:

    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
    

    You can use a boolean as a flag to determine whether the previous letter was a vowel or not.

    To loop through each letter in a word:

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