While testing a standard way of written code to count the total frequency of words in a sentence (counting number of times the same word appears), using the NLTK with Python, i am getting no result, the program does not output a result. it seems like the loop might no be running or something else. The written code is one of the ways that the NLTK org provides as way of practice to find the Total Number of Frequency of words for a Document or String. Below is the code,
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)
It is running, you just need to print the repr
of fdist
to see some of its content, or use fdist.items
or dict
on it to see all the content:
>>> 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}