gpt4 book ai didi

python - 打印词频列表(有分布)

转载 作者:太空宇宙 更新时间:2023-11-03 16:26:31 24 4
gpt4 key购买 nike

我几乎已经对所有内容进行了排序,但是因为我想要前 2k 个唯一单词,所以我得到了一个 super 困惑的分布。我最终将用它来构建一本字典,但我想看看哪些是最常见的 2k 单词,这样我就可以为字典选择相关的单词。无论如何,请参阅下面的代码。如何修改以获得我看到(字)(计数)的列表?不必限制在 2k,如果能看到所有的数据会很高兴吗?谢谢!

>>> 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)

最佳答案

当您使用most_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

关于python - 打印词频列表(有分布),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37934476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com