Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

python - Plotting words frequency and NLTK

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()
  • Given a list of words in the file file.txt:
southbound
stopped
travel
lane
started
around
stopped
stopped
started
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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()

enter image description here

You can play with the graph by altering the parameters to the plot()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...