使用FreqDist,python总结字数

时间:2010-11-17 16:57:11

标签: python nltk frequency-distribution

如何使用FreqDist中的fd.items()来总结单词频率?

>>> fd = FreqDist(text) 
>>> most_freq_w = fd.keys()[:10] #gives me the most 10 frequent words in the text
>>> #here I should sum up numbers of each of these 10 freq words appear in the text

e.g。如果most_freq_w中的每个单词出现10次,则结果应为100

!!! 我不需要文本中所有单词的数量,只需要10个最常用的单词

4 个答案:

答案 0 :(得分:4)

我不熟悉nltk,但由于FreqDist来自dict,因此以下内容应该有效:

v = fd.values()
v.sort()
count = sum(v[-10:])

答案 1 :(得分:2)

要查找单词在语料库中显示的次数(您的文本):

raw="<your file>"
tokens = nltk.word_tokenize(raw)
fd = FreqDist(tokens)
print fd['<your word here>'] 

答案 2 :(得分:2)

它具有漂亮的打印功能

2015
11
19
18
45
0

会做到的。

答案 3 :(得分:0)

如果FreqDist是单词到其频率的映射:

sum(map(fd.get, most_freq_w))
相关问题