Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the Number of Syllables in a Word

I am a beginner, and I have a question that I need help with. It is homework so any hints are appreciated. I have seen a few similar topics, but the answers are beyond what I know how to do yet...

I need to count the number of syllables in a text file as part of a larger program. I've got everything I need except for syllables. I have tried a couple of different methods but it does not always catch special cases. I am supposed to 'count groups of adjacent vowels, not including 'e' at the ends of words.' I understand what that means, but I am not able to get it right in my program. Here is what I have:::

def syllables(word):
    syl = 0
    vowels = 'aeiouy'
    starts = ['ou','ei','ae','ea','eu','oi']
    endings = ['es','ed','e']
    word = word.lower().strip(".:;?!")
    for vowel in vowels:
        syl +=word.count(vowel)
    for ending in endings:
        if word.endswith(ending):
            syl -=1
    for start in starts:
        if word.startswith(start):
            syl -=1
    if word.endswith('le'):
        syl +=1
    if syl == 0:
        syl+=1
    return syl

EDIT: New code

def syllables(word):
    count = 0
    vowels = 'aeiouy'
    word = word.lower().strip(".:;?!")
    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'):
        count+=1
    if count == 0:
        count +=1
    return count
like image 364
AbigailB Avatar asked Jan 26 '13 20:01

AbigailB


2 Answers

Just a suggestion, but instead of 'looking' for adjacent vowels, could you not increment your 'count' each time you encounter the initial vowel that occurs either at the beginning of a word or after a consonant in the word, excepting the 'e' at the end of a word (unless your count is zero for that word). To clarify, any time you encounter adjacent vowels, only the first vowel would increment your count.

Not positive it would work, but I think it works for all of the words I just wrote.

Good luck.

like image 146
maelstrom Avatar answered Nov 18 '22 11:11

maelstrom


The subject has been discussed in How to get the number of syllables in a word?

There they came to the conclusion that words not occurring in the CMU pronouncing dictionary should be treated with a short function as the one discussed here.

Another proposed solution is to use pyphen.

Even simpler: Wikipedia's article https://en.wikipedia.org/wiki/Hyphenation_algorithm links to a Python implementation of Francis Mark Liang's hyphenation algorithm. That algorithm is rather old but still used in TeX.

>>> import hyphenate
>>> hyphenate.hyphenate_word("computer")
['com', 'put', 'er']
like image 20
Sergio Jimenez Avatar answered Nov 18 '22 12:11

Sergio Jimenez