aks
aks

Reputation: 395

Code for counting the number of syllables in the words in a file

I have the following piece of code so far to count the number of syllables in the words in the cmudict ( CMU pronunciation dictionary). It counts the number of syllables for all the words in the dictionary. Now I need to replace cmudict with my input file and find the number of syllables for each word in the file which is printed as output. Just opening the input file in read mode does not work as dict() cannot be provided as the attribute to the file. The code is given below :

  
from curses.ascii import isdigit 
from nltk.corpus import cmudict 

d = cmudict.dict() # get the CMU Pronouncing Dict

def nsyl(word): 
    """return the max syllable count in the case of multiple pronunciations"""
    return max([len([y for y in x if isdigit(y[-1])]) for x in d[word.lower()]])


w_words = dict([(w, nsyl(w)) for w in d.keys() if w[0] == 'a'or'z'])
worth_abbreviating = [(k,v) for (k,v) in w_words.iteritems() if v > 3]
print worth_abbreviating 

Can anyone please help me out?

Upvotes: 2

Views: 3647

Answers (1)

unutbu
unutbu

Reputation: 880269

Not sure this will solve the whole problem, but:

w_words = dict([(w, nsyl(w)) for w in d.keys() if w[0] == 'a'or'z'])

should probably be

w_words = dict([(w, nsyl(w)) for w in d.keys() if w[0] == 'a' or w[0] == 'z'])

since

if w[0] == 'a'or'z' means if (w[0] == 'a') or ('z'). The string 'z' is Truish, so the condition is always True.

For example,

In [36]: 'x' == 'a'or'z'
Out[36]: 'z'

In [37]: 'x' == 'a' or 'x'=='z'
Out[37]: False

Upvotes: 2

Related Questions