到目前爲止,我已經有了下面這段代碼來計算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
任何人都可以請幫我: 的代碼如下?
而這種代碼是逾3音節數印刷的話所以有助於確定基於音節計數複雜的單詞。 – aks 2011-04-01 13:56:02