如何使用相同的单词创建一个wordcloud几次但使用不同的颜色?

时间:2017-12-14 17:48:57

标签: python word-cloud

我需要一个wordcloud,其中同一个单词可以出现两次不同的颜色;红色表示消极关联,绿色阳性。

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

enter image description here

但是,我需要其中一个房屋以绿色显示。这可能与wordcloud libray有关吗?有人可以推荐另一个支持这个的库吗?

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

1 个答案:

答案 0 :(得分:1)

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

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

会产生这个:

enter image description here

解释:我在字典中的单词后面添加了一个字符,告诉我颜色(+-)。我删除了我覆盖的recolor WordCloud方法中的字符,但我确实将整个世界(包括字符+或 - )发送到我用来选择的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')