我有一个包含各种单词的文件,我想计算文档中每个单词的频率并绘制它。
但是,我的情节没有显示结果。
x-axis
必须包含单词,y-axis
必须包含频率。
我使用的是NLTK
,NumPy
和Matplotlib
这是我的代码,也许我做错了
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()
提前感谢您的帮助
答案 0 :(得分:7)
def graph():
f = open("file.txt", "r")
inputfile = f.read()
tokens = nltk.tokenize.word_tokenize(inputfile)
fd = nltk.FreqDist(tokens)
fd.plot(30,cumulative=False)
您可以通过更改绘图()
的参数来播放图形