gpt4 book ai didi

python - 如何创建多次使用相同单词但颜色不同的词云?

转载 作者:太空宇宙 更新时间:2023-11-03 14:23:08 24 4
gpt4 key购买 nike

我需要一个词云,其中相同的词可以以不同的颜色出现两次;红色表示负关联,绿色表示正关联。

我已经能够使用 MultiDicts 生成包含重复单词的词云(请参阅下面的代码):

enter image description here

但是,我需要其中一所房屋以绿色显示。这可以用 wordcloud 库实现吗?有人可以推荐另一个支持此功能的库吗?

<小时/>
from wordcloud import WordCloud
from multidict import MultiDict

class WordClouder(object):

def __init__(self, words, colors):
self.words = words
self.colors = colors

def get_color_func(self, word, **args):
return self.colors[word]

def makeImage(self, path):
wc = WordCloud(background_color="white", width=200, height=100)

# generate word cloud
wc.generate_from_frequencies(self.words)

# color the wordclound
wc.recolor(color_func=self.get_color_func)

image = wc.to_image()
image.save(path)


if __name__ == '__main__':

# Ideally this would be used somewhere
colors_valence = {
'+': 'green',
'-': 'red',
}

colors = {
'home': 'red',
'car': 'green',
}

words = MultiDict({
'home': 2.0, # This home should be green
'car': 20.0,
})

words.add('home',10) , # This home should be red


wc = WordClouder(words, colors)
wc.makeImage('wordcloud.png')

最佳答案

我通过继承 WordCloud 创建了一个不错的解决方案。您只需复制并粘贴下面的代码,然后您可以使用 + 和 - 来指示颜色,如下所示:

words = {
'home+': 2.0,
'home-': 10.0,
'car+': 5.0,
}

会生成这个:

enter image description here

<小时/><小时/>

解释:我在字典中的单词后面添加了一个字符来告诉我颜色(+-)。我删除了 WordCloudrecolor 方法中覆盖的字符,但我确实将整个世界(包括字符 + 或 -)发送到 color_fun 我用它来选择合适的颜色。我评论了下面代码中的重要部分。

from wordcloud import WordCloud as WC

from multidict import MultiDict


class WordCloud(WC):

def recolor(self, random_state=None, color_func=None, colormap=None):
if isinstance(random_state, int):
random_state = Random(random_state)
self._check_generated()

if color_func is None:
if colormap is None:
color_func = self.color_func
else:
color_func = colormap_color_func(colormap)

# Here I remove the character so it doesn't get displayed
# when the wordcloud image is produced
self.layout_ = [((word_freq[0][:-1], word_freq[1]), font_size, position, orientation,
# but I send the full word to the color_func
color_func(word=word_freq[0], font_size=font_size,
position=position, orientation=orientation,
random_state=random_state,
font_path=self.font_path))
for word_freq, font_size, position, orientation, _
in self.layout_]

return self


class WordClouder(object):

def __init__(self, words, colors):
self.words = words
self.colors = colors

def get_color_func(self, word, **args):
return self.colors[word[-1]]

def makeImage(self, path):
#alice_mask = np.array(Image.open("alice_mask.png"))

wc = WordCloud(background_color="white", width=200, height=100)

# generate word cloud
wc.generate_from_frequencies(self.words)

# color the wordclound
wc.recolor(color_func=self.get_color_func)

image = wc.to_image()
image.save(path)


if __name__ == '__main__':

colors = {
'+': '#00ff00',
'-': '#ff0000',
}

words = {
'home+': 2.0,
'home-': 10.0,
'car+': 5.,
}

wc = WordClouder(words, colors)
wc.makeImage('wc.png')

关于python - 如何创建多次使用相同单词但颜色不同的词云?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47819350/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com