使用NLTK Python的单词的总频率计数



在测试一种标准的编写代码的方法来计算句子中单词的总频率(计算同一单词出现的次数(时,使用带有Python的NLTK,我没有得到任何结果,程序也没有输出结果。看起来循环可能没有运行或其他什么。编写的代码是NLTK.org提供的一种方法,作为查找文档或字符串的单词频率总数的练习方式。下面是代码,

import nltk
from nltk.tokenize import word_tokenize
from nltk.probability import FreqDist
x = 'bob went down the street to purchase groceries. When he was walking back, it became very hot outside. When he cameback, he drank a cold glass of water. After drinking the glass of water he felt much more cooler in temperature.'
tokens = word_tokenize(x)

fdist = FreqDist()
for word in tokens: 
fdist[word.lower()]+= 1
print(fdist)

它正在运行,您只需要打印fdistrepr即可查看它的部分内容,或者在它上面使用fdist.itemsdict即可查看所有内容:

>>> print(repr(fdist)) # repr
FreqDist({'.': 4, 'he': 4, 'the': 2, 'when': 2, ',': 2, 'glass': 2, 'of': 2, 'water': 2, 'bob': 1, 'went': 1, ...})
>>> fdist.items()      # items
dict_items([('bob', 1), ('went', 1), ('down', 1), ('the', 2), ('street', 1), ('to', 1), ('purchase', 1), ('groceries', 1), ('.', 4), ('when', 2), ('he', 4), ('was', 1), ('walking', 1), ('back', 1), (',', 2), ('it', 1), ('became', 1), ('very', 1), ('hot', 1), ('outside', 1), ('cameback', 1), ('drank', 1), ('a', 1), ('cold', 1), ('glass', 2), ('of', 2), ('water', 2), ('after', 1), ('drinking', 1), ('felt', 1), ('much', 1), ('more', 1), ('cooler', 1), ('in', 1), ('temperature', 1)])
>>> dict(fdist)        # dict
{'bob': 1, 'went': 1, 'down': 1, 'the': 2, 'street': 1, 'to': 1, 'purchase': 1, 'groceries': 1, '.': 4, 'when': 2, 'he': 4, 'was': 1, 'walking': 1, 'back': 1, ',': 2, 'it': 1, 'became': 1, 'very': 1, 'hot': 1, 'outside': 1, 'cameback': 1, 'drank': 1, 'a': 1, 'cold': 1, 'glass': 2, 'of': 2, 'water': 2, 'after': 1, 'drinking': 1, 'felt': 1, 'much': 1, 'more': 1, 'cooler': 1, 'in': 1, 'temperature': 1}

最新更新