python - 绘制词频和 NLTK

标签 python matplotlib nltk

我有一个包含各种单词的文件,我想计算文档中每个单词的频率并绘制出来。 但是,我的情节没有显示结果。 x 轴 必须包含单词,y 轴 必须包含频率。 我正在使用 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()
  • 给定文件 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()

enter image description here

您可以通过更改 plot() 的参数来玩转图表

关于python - 绘制词频和 NLTK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29755861/

相关文章:

javascript - 是否可以使用NODE.JS(或PYTHON)开发ACM ONLINE JUDGE系统?

python - numpy 直方图数组

python - 客户端到服务器,python中的套接字多对一关系

python - 哪个 pip 与哪个 python 一起使用?

python - 无法使用 NLTK 分词器处理重音单词

Python:这是比较和排序字符串列表的低效方法吗?

python - 包 "Tk": have 8. 5.2 的版本冲突,正好需要 8.5.15

python - 动画 matplotlib 参数化示例

python - Matplotlib : What is the function of cmap in imshow?

python - 如何解析上下文无关语法中的特殊字符?