绘制单词频率和NLTK

时间:2015-04-20 18:40:45

标签: python python-2.7 numpy matplotlib nltk

我有一个包含各种单词的文件,我想计算文档中每个单词的频率并绘制它。 但是,我的情节没有显示结果。 x-axis必须包含单词,y-axis必须包含频率。 我使用的是NLTKNumPyMatplotlib

这是我的代码,也许我做错了

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()

提前感谢您的帮助

1 个答案:

答案 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)

您可以通过更改绘图()

的参数来播放图形