簡體   English   中英

我正在嘗試顯示 wordcloud 圖像,但出現 ValueError: We need at least 1 word to plot a word cloud, got 0

[英]i' m trying to display a wordcloud image but I'm getting ValueError: We need at least 1 word to plot a word cloud, got 0

def calculate_frequencies(file_contents): # 這是一個標點符號列表和你可以用來處理你的文本的無趣單詞 標點符號 = ''';()-[]{}:,'"\.<>?/,@#$ %^&*_~''' uninteresting_words = ["the", "a", "to", "if", "is", "it", "of", "and", "or", "an "、"as"、"i"、"me"、"my"、\"we"、"our"、"ours"、"you"、"your"、"yours"、"he"、"she" ,“他”,“他的”,“她”,“她的”,“它的”,“他們”,“他們”,\“他們的”,“什么”,“哪個”,“誰”,“誰”, “這個”、“那個”、“我”、“是”、“曾經”、“是”、“是”、“曾經”、“存在”、\“有”、“有”、“有”、“做”、“做”、“做了”、“但是”、“在”、“通過”、“與”、“從”、“這里”、“何時”、“在哪里”、“如何”、“所有", "any", "both", "each", "few", "more", "some", "such", "no", "nor", "too", "very", "can", “將”,“只是”]

frequencies={}
def iterate():
    words=file_contents.split()

    for word in words:
        if word not in uninteresting_words and word not in punctuations:
            if word not in frequencies and word in file_contents:
                frequencies[word]+=1
            else:
                frequencies[word]=1   





#wordcloud
cloud = wordcloud.WordCloud()
cloud.generate_from_frequencies(frequencies)
return cloud.to_array()




  myimage = calculate_frequencies(file_contents)
  plt.imshow(myimage, interpolation = 'nearest')
  plt.axis('off')
  plt.show()

我從來沒有使用過這個庫,所以如果這不起作用,請隨時告訴我。

一方面,這個 if 語句似乎被翻轉了:

        if word not in frequencies and word in file_contents:
            frequencies[word]+=1
        else:
            frequencies[word]=1

切換到

if word in frequencies and word in file_contents:
...

接下來,您永遠不要調用iterate function 來填充頻率。 在調用 wordcloud 之前執行此操作。

最后,文本中的另一個 SO 答案是這樣的:

wordcloud.generate_from_frequencies(frequencies=frequencies)

希望在做完這些事情之后,它會起作用。

暫無
暫無

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

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