python 实现词云

拿现在比较火的小说《大主宰》做测试,看看其中的关键词词云是啥

代码

import matplotlib.pyplot as plt
from wordcloud import WordCloud
import jieba.analyse
import codecs

text_from_file_with_apath = codecs.open('test.txt',mode ='r',encoding = 'utf-8').read()

#wordlist_after_jieba = jieba.cut(text_from_file_with_apath, cut_all = True)
wordlist_after_jieba = jieba.analyse.textrank(text_from_file_with_apath, topK=20, withWeight=False, allowPOS=('ns', 'n', 'vn', 'v'))
wl_space_split = " ".join(wordlist_after_jieba)
print(wl_space_split)
my_wordcloud = WordCloud().generate(wl_space_split)

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

结果

posted @ 2017-04-11 11:12  Weyne  阅读(236)  评论(0编辑  收藏