到目前为止,我有以下代码来计算cmudict(CMU发音字典)中单词的音节数。它计算字典中所有单词的音节数。现在我需要用我的输入文件替换cmudict,并找出输出文件中每个单词的音节数。仅仅以读取模式打开输入文件不起作用,因为dict()不能作为文件的属性提供。 代码如下:
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
谁能帮帮我吗?在
不确定这是否能解决整个问题,但:
应该是的
^{2}$因为
if w[0] == 'a'or'z'
表示if (w[0] == 'a') or ('z')
。字符串'z'
是Truish,因此条件始终为True。在例如
相关问题 更多 >
编程相关推荐