打印术语频率列表(有分布)

2024-09-28 22:25:54 发布

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

我几乎把所有的东西都整理好了,但是因为我想要前2k个独特的单词,所以我得到了一个超级混乱的分布。我最终会用这个来建立一个字典,但是我想知道哪些是最常见的2k单词,这样我就可以为字典选择相关的单词。无论如何,请参阅下面的代码。我怎样修改才能得到我看到(单词)(计数)的列表?不一定要限制在2公里,很高兴看到计数为所有?谢谢!在

>>> fileObj = codecs.open( "/Users/shannonmcgregor/Desktop/ALLstories.txt", "r", "Latin-1" )
chattanooga_stories = fileObj.read()
>>> import nltk
from nltk.corpus import stopwords
>>> lowered_stories = chattanooga_stories.lower()
>>> word_list = lowered_stories.split()
>>> filtered_stories = [w for w in word_list if not w in stopwords.words('english')]
>>> fdist = nltk.FreqDist(w.lower() for w in filtered_stories)
>>> print(fdist)
<FreqDist with 7031 samples and 19893 outcomes>
>>> top_2k = [ ]
>>> top_2k = fdist.most_common(2000)
>>> fdist.plot(2000, cumulative=True)

Tags: inimport字典单词lowerlistwordstories
1条回答
网友
1楼 · 发布于 2024-09-28 22:25:54

当您使用most\u common()时,您确实可以获得各种单词的计数。使用items方法获取按排序顺序排列的项目列表(最常见的是第一个)。在

fdist = nltk.FreqDist(filtered_stories)    #filtered_stories is already lowercase
print(fdist)
top_2k = [ ]
top_2k = fdist.most_common(2000)
tok_2k.items() #should give you a sorted list

相关问题 更多 >