python WordCloud 绘制三国演义词云

"""
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,
             #stopwords=",",
             font_path="#C:\Windows\Fonts\simfang.ttf",
             max_font_size=100,
             random_state=10,
             margin=2,
             # mask=background_images
)
myword = wc.generate(listStr)
plt.imshow(myword)
plt.axis("off")
plt.show()

在这里插入图片描述
在这里插入图片描述

发布了39 篇原创文章 · 获赞 41 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/wei_zhen_dong/article/details/103586694