如何从wordcloud中删除单词?(Python 3)

2024-10-01 13:34:35 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我该如何在列表中添加更多的单词呢。我尝试了add,append,这两个函数都不起作用。在

提前谢谢你。在


Tags: thefromimportnewaspltmaskopen
1条回答
网友
1楼 · 发布于 2024-10-01 13:34:35

啊哈!因为我在我的文本文件中有逗号分隔我的单词。在

对于那些构建单词云的人,只需写下用空格隔开的单词。不需要标点符号。@拉辛罗斯福使用“分裂”函数是正确的。在

相关问题 更多 >