Python+jieba生成词云

描述:使用Python和jieba生成词云。

#coding:utf-8
from os import path
from scipy.misc import imread
import numpy as np
import pickle
import matplotlib.pyplot as plt
import jieba
from wordcloud import WordCloud,STOPWORDS,ImageColorGenerator


class ciyun():
    def __init__(self):
        pass

    def draw(self):
        # with open('test.txt', 'rb') as f:
        #     text = pickle.load(f)
        text = open('test.txt').read()
        wordlist_arter_jieba = jieba.cut(text, cut_all=True)
        wl_space_split = " ".join(wordlist_arter_jieba)
        color_mask = plt.imread("test.jpg")
        my_wordcloud = WordCloud(
            # font_path='font.ttf',  # 字体最好放在与脚本相同的目录下,而且必须设置
            background_color='white',
            mask=color_mask,
            max_words=2000,
            max_font_size=50000
        )

        word_cloud = my_wordcloud.generate(wl_space_split)
        plt.imshow(word_cloud)
        plt.axis('off')
        plt.show()

t = ciyun()
t.draw()


![生成词云:](http://img.blog.csdn.net/20170706004353314?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxNDI1NzE5Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)

你可能感兴趣的:(Python相关)