From 894485e92ca1b4a70b524bb90bc95e12ff69b19c Mon Sep 17 00:00:00 2001 From: pclj6ws49 Date: Tue, 23 Apr 2024 09:06:23 +0800 Subject: [PATCH] ADD file via upload --- eg1.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 eg1.py diff --git a/eg1.py b/eg1.py new file mode 100644 index 0000000..df97003 --- /dev/null +++ b/eg1.py @@ -0,0 +1,25 @@ +import jieba +from PIL import Image +from wordcloud import WordCloud +import numpy as np +import matplotlib.pyplot as plt + +# 我们导入文本内容,并且去除掉一下换行符和空格,代码如下 +text = open(r"例子.txt",encoding='utf-8').read() +text = text.replace('\n',"").replace("\u3000","") + +# 我们需要将其分成一个个的词,这个时候就需要用到jieba模块了,代码如下 +text_cut = jieba.lcut(text) +# 将分好的词用某个符号分割开连成字符串 +text_cut = ' '.join(text_cut) +# 结果当中或许存在着不少我们不需要看的、无关紧要的内容,这个时候就需要用到停用词 +stop_words = open(r"stopwords.txt",encoding='utf-8').read().split("\n") + +# # 绘制词云图的核心代码 +word_cloud = WordCloud(font_path=r"C:/Windows/Fonts/simsun.ttc", + background_color="white",# 词云图的背景颜色 + stopwords=stop_words) # 去掉的停词 +word_cloud.generate(text_cut) +image = word_cloud.to_image() +image.show() +word_cloud.to_file("1.png") \ No newline at end of file