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()

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章