中文高频词汇统计并绘制词云

转载自:https://www.jianshu.com/p/2052d21a704c


代码如下:

# -*- encoding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import jieba.analyse
from os import path
from scipy.misc import imread
import matplotlib as mpl 
import matplotlib.pyplot as plt 
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator

if __name__ == "__main__":

    mpl.rcParams['font.sans-serif'] = ['FangSong']
    #mpl.rcParams['axes.unicode_minus'] = False
    content = open("testing.txt","rb").read()

    # tags extraction based on TF-IDF algorithm
    tags = jieba.analyse.extract_tags(content, topK=100, withWeight=False)
    text =" ".join(tags)
    text = unicode(text)
    # read the mask
    d = path.dirname(__file__)
    trump_coloring = imread(path.join(d, "Trump.jpg"))

    wc = WordCloud(font_path='/home/appleyuchi/.local/share/fonts/HanYiQuanTangshiTJ.ttf',
            background_color="white", max_words=300, mask=trump_coloring,
            max_font_size=40, random_state=42) 

    # generate word cloud 
    wc.generate(text)

    # generate color from image
    image_colors = ImageColorGenerator(trump_coloring)

    plt.imshow(wc)
    plt.axis("off")
    plt.show()


你可能感兴趣的:(Python自然语言处理)