jieba+wordcloud+imageio—自定义词云

Posted aluosen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jieba+wordcloud+imageio—自定义词云相关的知识,希望对你有一定的参考价值。

import jieba
import wordcloud
from imageio import imread
mask=imread(‘fivestar.png‘)
def shuchu():
txt=open(‘threekingdoms.txt‘,‘r‘,encoding=‘gb18030‘).read()
excludes=‘将军‘,‘却说‘,‘荆州‘,‘二人‘,‘不可‘,‘不能‘,‘如此‘
words=jieba.lcut(txt)
counts=
for word in words:
if len(word)==1:
continue
elif word==‘诸葛亮‘ or word==‘孔明曰‘:
rword=‘孔明‘
elif word==‘关公‘ or word==‘云长‘:
rword=‘关羽‘
elif word==‘玄德‘ or word==‘玄德曰‘:
rword=‘刘备‘
elif word==‘孟德‘ or word==‘丞相‘:
rword=‘曹操‘
else:
rword=word
counts[rword]=counts.get(rword,0)+1
for word in excludes:
del counts[word]
items = list(counts.items())
items.sort(key=lambda x:x[1],reverse=True)
for i in range(10):
word,count=items[i]
print(‘0:<101:>5‘.format(word,count))
shuchu()
f=open(‘threekingdoms.txt‘,‘r‘,encoding=‘gb18030‘)
t=f.read()
f.close()
ls=jieba.lcut(t)
txt=‘ ‘.join(ls)
w=wordcloud.WordCloud(font_path=‘msyh.ttc‘,width=1000,height=700,background_color=‘white‘,mask=mask)
w.generate(txt)
w.to_file(‘grwordcloud.png‘)

技术图片

 

以上是关于jieba+wordcloud+imageio—自定义词云的主要内容,如果未能解决你的问题,请参考以下文章

使用jieba库与wordcloud库第三方库进行词频统计

wordcloud+jieba

《琅琊榜》词云

Python3.6+jieba+wordcloud 爬取豆瓣影评生成词云

python爬虫——京东评论jieba分词wordcloud词云统计

知识学习用Python实现中文word cloud(wordcloud,Jieba)的全过程