我已经使用python制作了一个wordcloud,我想查看wordcloud中的单词,如有必要,请从图像中删除它们

问题描述

因此,基本上,我有一个文件,其中有一些列表,我希望使用这些列表来查看单词是否存在于给出的字典中,如果是,则从中形成单词云。现在,在查看wordcloud之后,我想从图像中删除一些无用的单词,这是你们可以帮助我的事情吗?

from wordcloud import WordCloud
import matplotlib.pyplot as plt

import module1
import module2

lst = input('Which list?')


a = module2.diction

c = None
if lst == 'list1':
    c=module2.list1
if lst == 'list2':
    c=module2.list2

#print(c)
#doing stuff with the dictionary I had
for name,numb in a.items():
    for key in numb:
        key = key * numb[key]
        a[name] = key
#print(sample)
quad = ({key : a[key] for key in a if key in c})
#print(quad)

wc = WordCloud(background_color="white",width=1000,height=1000,colormap="copper",relative_scaling=0.5,normalize_plurals=False).generate_from_frequencies(quad)

# Plot

plt.figure()
plt.ion()
plt.imshow(wc,interpolation="bilinear")
plt.margins(x=0,y=0)
plt.axis('off')
plt.tight_layout(pad=0)
plt.ioff()
plt.show()


u = input('Enter the words: ')

解决方法

如果要从字典中删除某些内容,可以使用以下语法:

del dictonary[word]

这将从字典中删除该单词,因此,如果您遇到单词the并希望将其删除,则可以使用

del c['the']