0

在测试编写代码的标准方法以计算句子中单词的总频率(计算相同单词出现的次数)时,使用带有 Python 的 NLTK,我没有得到任何结果,程序没有输出结果。似乎循环可能没有运行或其他什么。书面代码是 NLTK 组织提供的一种方法,用于查找文档或字符串的词频总数。下面是代码,

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)


4

1 回答 1

2

它正在运行,你只需要打印reproffdist就可以看到它的部分内容,或者使用fdist.itemsordict就可以看到所有内容:

>>> 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}
于 2022-01-18T21:42:51.967 回答