From 239cbdbf518eda12cda05a0f4081bd501f1b3fea Mon Sep 17 00:00:00 2001 From: pge97pcl8 <3080782635@qq.com> Date: Tue, 21 Nov 2023 21:32:38 +0800 Subject: [PATCH] ADD file via upload --- test.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 test.py diff --git a/test.py b/test.py new file mode 100644 index 0000000..dbaf348 --- /dev/null +++ b/test.py @@ -0,0 +1,32 @@ +import jieba +import matplotlib.pyplot as plt +import wordcloud + +txt=open("D:\文本素材\西游记.txt","r",encoding='utf-8').read() + +word = jieba.lcut(txt) +data ={} +for ch in word: + if len(ch)==1: + continue + elif ch=="行者"or ch=="悟空": + ch ="孙悟空" + elif ch=="三藏": + ch="唐僧" + elif ch=="八戒": + ch ="猪八戒" + else: + ch =ch + data[ch] = data.get(ch,0)+1 +item = sorted(data.items(),key=lambda x:x[1],reverse=True) +print(item) +text ="" +for i in item: + text+=" "+i[0] +w =wordcloud.WordCloud(font_path="msyh.ttc",width=400,height=200,background_color="white",max_words=50) +w.generate(text) +plt.imshow(w) +plt.axis('off') +plt.show() +w.to_file("threekingdoms.png") +