Dylan
Dylan

Reputation: 1183

How to compute number of syllables in a word in javascript?

Is there javascript library for counting number of syllables in a word? How to count?

Thanks

Edit

Thank Sydenam and zozo for useful information and possible answers.

I found code by Pesto at this forum , but it is in Ruby. One of its concise versions is below:

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 

This seems short but complicated. Can you translate this function into javascript? Thank you again.

Upvotes: 20

Views: 12342

Answers (5)

Lance Pollard
Lance Pollard

Reputation: 79178

You can use this library pronouncingjs to calculate using the CMU pronunciation dictionary:

pronouncing.syllableCount(pronouncing.phonesForWord("adverse")[0])
2

See here:

abandon,3
abolish,3
absorb,2
accelerate,4
accept,2
access,2
accommodate,4
accompany,4
account,2
accumulate,4
accuse,2
ache,1
achieve,2
acknowledge,3
acquire,3
act,1
adapt,2
add,1
address,2
adhere,2
adjust,2
administer,4
admire,2

Upvotes: 6

Bryan
Bryan

Reputation: 133

Jonathan Hinds code works great except for the words: something, sometime, sometimes and somewhere. I updated his code to address these issues:

var syllableCount = function(word) 
{
    word = word.toLowerCase();
    var t_some = 0;
    if(word.length>3)
        {
        if(word.substring(0,4)=="some")
            {
            word = word.replace("some","");
            t_some++;
            }
        }
    word = word.replace(/(?:[^laeiouy]|ed|[^laeiouy]e)$/, '');   
    word = word.replace(/^y/, '');                                 
    //return word.match(/[aeiouy]{1,2}/g).length;   
    var syl = word.match(/[aeiouy]{1,2}/g);
    console.log(syl);
    if(syl)
    {
        //console.log(syl);
        return syl.length+t_some;
    }
}

Upvotes: 1

Jonathan Hinds
Jonathan Hinds

Reputation: 305

I can see this is an old post but I stumbled across this function and found good use for it.

One thing that I would like to add that will increase the accuracy of the syllable account - (to my knowledge).

I noticed that the string "changes" shows as only being 1 syllable.

I removed es from (?:[^laeiouy]es|ed|[^laeiouy]e)$ so that it's now ?:[^laeiouy]|ed|[^laeiouy]e)$.

This seems to add the extra syllable count for words ending in "es". Also, to simplify things I put the array of matched words into a separate variable, this way you can check if any syllables are counted before giving any output:

var count = function(word) 
{
    word = word.toLowerCase();                                     
    word = word.replace(/(?:[^laeiouy]|ed|[^laeiouy]e)$/, '');   
    word = word.replace(/^y/, '');                                 
    //return word.match(/[aeiouy]{1,2}/g).length;   
    var syl = word.match(/[aeiouy]{1,2}/g);
    console.log(syl);
    if(syl)
    {
        //console.log(syl);
        return syl.length;
    }
}

I found this to be more convenient than necessary. If you have the function running in event listener that might fire before there are any words to check, this would be useful and prevent any errors such as Cannot read property 'length' of null.

I just wanted to share my findings with anyone else who might find this and decide to use it.

Upvotes: 10

artfulhacker
artfulhacker

Reputation: 4873

Translated to 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'))

Upvotes: 40

zozo
zozo

Reputation: 8582

Well... you take a grammatical book and start reading the string letter by letter if one of the rules there is matched then you add a +1 to a counter. Since the rules differ from language to language I can't really tell you how to do it. I did it for Romanian... but I doubt it will help you.

Upvotes: 2

Related Questions