I have already gone through this post which uses nltk's cmudict for counting the number of syllables in a word:

from nltk.corpus import cmudict
d = cmudict.dict()
def nsyl(word):
  return [len(list(y for y in x if y[-1].isdigit())) for x in d[word.lower()]] 

However, for words outside the cmu's dictionary like names for example: Rohit, it doesn't give a result.

So, is there any other/better way to count syllables for a word?

  • 1
    $\begingroup$ Well, wordcalc.com can handle "Rohit", so seems like it is possible. I don't know how it is doing it though . . . and it is not perfect. $\endgroup$ – Neil Slater Sep 28 '17 at 7:20
  • $\begingroup$ wordcalc.com gave "syllable" a count of 1 (I'd call it 3). I think it may be using the hyphenation rules from your linked question. It seems that these coincide with pronounced syllables a lot of the time, but not 100%. $\endgroup$ – Neil Slater Sep 28 '17 at 7:27

You can try another Python library called Pyphen. It's easy to use and supports a lot of languages.

import pyphen
dic = pyphen.Pyphen(lang='en')
print dic.inserted('Rohit')
  • $\begingroup$ This is pretty useful but it gives a lot of wrong results. For instance it counts 'readier' as 2 syllables instead of 3, 'karate' as 1 instead of 3, 'insouciance' as 3 instead of 4, 'Siberia' as 1 instead of 4, and many more. $\endgroup$ – Hayze Mar 6 at 20:38

I was facing the exact same issue, this is what I did:
Catch the key error you get when the word is not found in cmu's dictionary as below:

def nsyl(word):
    return [len(list(y for y in x if y[-1].isdigit())) for x in d[word.lower()]]
except KeyError:
    #if word not found in cmudict
    return syllables(word)

Call the below syllables function

def syllables(word):
#referred from stackoverflow.com/questions/14541303/count-the-number-of-syllables-in-a-word
count = 0
vowels = 'aeiouy'
word = word.lower()
if word[0] in vowels:
    count +=1
for index in range(1,len(word)):
    if word[index] in vowels and word[index-1] not in vowels:
        count +=1
if word.endswith('e'):
    count -= 1
if word.endswith('le'):
if count == 0:
    count +=1
return count

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.