【问题标题】:How do I remove words from my wordcloud? (Python 3)如何从我的 wordcloud 中删除单词? (Python 3)
【发布时间】:2017-12-12 05:53:06
【问题描述】:
import pandas as pd
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import wordcloud
from wordcloud import WordCloud,STOPWORDS

# Read the whole text.
remarks = open(r'C:\Users\marmar\Documents\Remarks.txt').read()

#Create words over an image
mask = np.array(Image.open(r'C:\users\marmar\Documents\cloud.png'))

#set the stopwords list
stopwords= set(STOPWORDS)

#append new words to the stopwords list
new_words =open(r'C:\Users\marmar\comments.txt').read()
new_stopwords=stopwords.union(new_words)

#generate the word cloud with parameters
wc = WordCloud(background_color="white", 
               max_words=2000, 
               mask=mask,
               min_font_size =12, 
               max_font_size=20, 
               relative_scaling = 0.5, 
               stopwords=new_stopwords,
               normalize_plurals= True)
wc.generate(remarks)
plt.figure(figsize=(25,25))
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")

#Show the wordcloud
plt.show()

基本上,我使用 Python 3(Jupyter Notebook)来创建一个带有实际云图片的 wordcloud。 WordCloud 包实际上有自己的停用词功能。但是,我想在停用词列表中包含一些我不想在我的云中看到的词。 我试图在该文本文件中包含一些单词,但我可以在我的云中看到这些单词。 例如,文本文件如下所示: 客户,CSR 客户,满意,项目完成

如何在列表中添加更多单词。我试过添加,追加,这两个函数都不起作用。

提前谢谢你。

【问题讨论】:

  • 我尝试了 stopwords.add('CSR Comment') 仍然可以在云端看到!
  • 在您对WordCloud 构造函数的调用中,您似乎传递了stopwords=stopwords。你不想使用stopwords=new_stopwords吗?
  • 另外,请确保对文件进行标记,以便逐字分解。你可以使用open(...).read().split()之类的东西。
  • 嘿,你知道吗,很好,但我仍然可以在云中看到这个词!我不明白...
  • 添加了新回复

标签: python python-3.x word-cloud


【解决方案1】:

啊哈!这是因为我在文本文件中用逗号分隔了我的单词。

对于那些构建 wordcloud 的人,只需将类型写成单词,用空格隔开。不需要标点符号。 @RagingRoosevelt 使用“拆分”功能是正确的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-05
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多