Sum up the number of words frequency using FreqDist, python

Gusto picture Gusto · Nov 17, 2010 · Viewed 8.2k times · Source

How to sum up the number of words frequency using fd.items() from FreqDist?

>>> 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. if each word in most_freq_w appear 10 times, the result should be 100

!!! I don't need that number of all words in the text, just the 10 most frequent

Answer

Steve Tjoa picture Steve Tjoa · Nov 17, 2010

I'm not familiar with nltk, but since FreqDist derives from dict, then the following should work:

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