计算fi中单词音节数的代码

2024-09-27 21:27:13 发布

您现在位置:Python中文网/ 问答频道 /正文

到目前为止,我有以下代码来计算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 

谁能帮帮我吗?在


Tags: 文件the代码infromimportforif
1条回答
网友
1楼 · 发布于 2024-09-27 21:27:13

不确定这是否能解决整个问题,但:

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

应该是的

^{2}$

因为

if w[0] == 'a'or'z'表示if (w[0] == 'a') or ('z')。字符串'z'是Truish,因此条件始终为True。在

例如

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

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

相关问题 更多 >

    热门问题