"""
author:魏振东
data:2019.12.18
func:WordCloud 绘制三国演义词云
"""
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import chardet
from collections import Counter
import jieba.posseg as psg
text = open("171182.txt", "rb").read()
seg_list = psg.cut(text)
type = chardet.detect(text)
text1=text.decode(type["encoding"])
seg_list1 = ["{0}".format(w) for w, t in seg_list if len(w)!=1]
count = Counter(seg_list1)
dic3 = sorted(count.items(), key=lambda x: x[1], reverse=True)
listStr = ' '.join([str(word[0]) for word in list(dic3)])
wc=WordCloud(background_color="white",
max_words=2000,
width=1920,
height=1080,
font_path="#C:\Windows\Fonts\simfang.ttf",
max_font_size=100,
random_state=10,
margin=2,
)
myword = wc.generate(listStr)
plt.imshow(myword)
plt.axis("off")
plt.show()