From 8761140e1a52b256aca75fae2e994003b80584c5 Mon Sep 17 00:00:00 2001 From: ms4cfzqku <2274881056@qq.com> Date: Wed, 18 Sep 2024 22:00:07 +0800 Subject: [PATCH] ADD file via upload --- 运行生成一个词云图.py | 50 ++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 运行生成一个词云图.py diff --git a/运行生成一个词云图.py b/运行生成一个词云图.py new file mode 100644 index 0000000..d5cf8bd --- /dev/null +++ b/运行生成一个词云图.py @@ -0,0 +1,50 @@ +import jieba +from wordcloud import WordCloud +import numpy as np +from PIL import Image +from matplotlib import colors + +f = open(r'text.txt', "r", encoding="utf-8") +text = f.read() +f.close() +words_list_jieba = jieba.lcut(text) + + +def findifhave(demo, stop): + for ret in stop: + if (demo == ret): + return 'T' + + +stop = ['\n'] +with open("stop.txt", 'r', encoding='utf-8') as f1: + for line in f1: + stop.append(line.replace("\n", "")) +f1.close() + +dict = {} +for key in words_list_jieba: + dict[key] = dict.get(key, 0) + 1 + +for demo in list(dict.keys()): + if ('T' == findifhave(demo, stop)): + del dict[demo] + +dict1 = sorted(dict.items(), key=lambda d: d[1], reverse=True) +print(dict1) + +background_image = np.array(Image.open('001.png')) +colormaps = colors.ListedColormap(['#871A84', '#BC0F6A', '#BC0F60', '#CC5F6A', '#AC1F4A']) +wordcloud = WordCloud(font_path='SimHei.ttf', # 字体 + prefer_horizontal=0.99, + background_color='white', # 背景色 + max_words=20, # 显示单词数 + max_font_size=400, # 最大字号 + stopwords=stop, # 过滤噪声词 + mask=background_image, # 背景轮廓 + colormap=colormaps, # 使用自定义颜色 + collocations=False + ).fit_words(dict) +image = wordcloud.to_image() +image.show() # 展示图片 +wordcloud.to_file('词云图.png') # 保存图片 \ No newline at end of file