簡體   English   中英

繪制詞頻和 NLTK

[英]Plotting words frequency and NLTK

我有一個包含各種單詞的文件,我想計算文檔中每個單詞的頻率並繪制它。 但是,我的情節沒有顯示結果。 x-axis必須包含單詞, y-axis x-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()
  • 給定文件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()

在此處輸入圖片說明

您可以通過更改 plot() 的參數來玩圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM