【发布时间】:2020-06-26 22:47:57
【问题描述】:
我正在尝试确定以“ing”或“ed”结尾的单词。计算条件频率分布,其中条件是 ['government', 'hobbies'] 并且事件是 'ing' 或 'ed'。将条件频率分布存储在变量 inged_cfd 中。
下面是我的代码:-
from nltk.corpus import brown
import nltk
genre_word = [ (genre, word.lower())
for genre in ['government', 'hobbies']
for word in brown.words(categories = genre) if (word.endswith('ing') or word.endswith('ed')) ]
genre_word_list = [list(x) for x in genre_word]
for wd in genre_word_list:
if wd[1].endswith('ing'):
wd[1] = 'ing'
elif wd[1].endswith('ed'):
wd[1] = 'ed'
inged_cfd = nltk.ConditionalFreqDist(genre_word_list)
inged_cfd.tabulate(conditions = ['government', 'hobbies'], samples = ['ed','ing'])
我想以表格格式输出,使用上面的代码我得到的输出是:-
ed ing
government 2507 1605
hobbies 2561 2262
而实际输出是:-
ed ing
government 2507 1474
hobbies 2561 2169
请解决我的问题,并帮助我获得准确的输出。
【问题讨论】:
标签: python-3.x nltk corpus