9、wordCloud词云
<pre><code>import sys
import nltk
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
from wordcloud import WordCloud
from os import path
import jieba.analyse
from PIL import Image, ImageSequence
import numpy as np
def wordcloud(uid,content):
result = jieba.analyse.textrank(content, topK=1000, withWeight=True)
keyword = {}
for x in result:
keyword[x[0]] = x[1]
alice_mask = np.array(Image.open('./ttf/china1.png'))
# f=open('stopwords.txt','r')
# stopword=
# stopwords暂时没有实现
wc = WordCloud(font_path='./ttf/ttf.ttf', background_color="white", max_words=2000, mask=alice_mask, max_font_size=100,
random_state=42)
wc.generate_from_frequencies(keyword)
image_colors = ImageColorGenerator(alice_mask)
# 显示图片
plt.imshow(wc)
plt.imshow(wc.recolor(color_func=image_colors))
plt.axis("off") # 关闭图像坐标系
#plt.show()
plt.savefig('{}.png'.format(uid))
return '{}.png'.format(uid)
d = path.dirname('./')
text = open(path.join(d, 'news.txt')).read()
wordcloud('china_news_1',text)
</code></pre>