I have a file with various words, which I want to count the frequency of each word in the document and plot it.
However, my plot is not showing results.
The x-axis
must contain the words, and the y-axis
the frequency.
I am using NLTK
, NumPy
and Matplotlib
Here's my code, maybe I did something wrong
def graph():
f = open("file.txt", "r")
inputfile = f.read()
words = nltk.tokenize.word_tokenize(inputfile)
count = set(words)
dic = nltk.FreqDist(words)
FreqDist(f).plot(50, cumulative=False)
f.close()
file.txt
:southbound
stopped
travel
lane
started
around
stopped
stopped
started
import nltk
def graph():
with open("file.txt", "r") as f:
inputfile = f.read()
tokens = nltk.tokenize.word_tokenize(inputfile)
fd = nltk.FreqDist(tokens)
fd.plot(30,cumulative=False)
graph()
You can play with the graph by altering the parameters to the plot()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With