效果如图
预备知识准备
" ".join( )
将序列中的元素以指定的字符连接生成一个新的字符串。
import jieba
import codecs,sys
import wordcloud
f = codecs.open('history.txt','r','UTF-8')
b_text=f.read()
f.close()
a = jieba.lcut(b_text)
dic = {}
for word in a:
if word not in dic:
dic[word]=1
else:
dic[word]+=1
del dic[',']
del dic['。']
del dic['、']
del dic[';']
del dic['“']
del dic['”']
swd=sorted(list(dic.items()),key=lambda lst:lst[1],reverse=1)
for kword,times in swd:
print(kword,times)
print(type(swd))
w = wordcloud.WordCloud(font_path='msyh.ttc', height=700, width=1000, background_color='white')
w.generate(" ".join(a))
w.to_file('词云图.png')