From 168c618f7cef3701f4b641951b691331a247ab3d Mon Sep 17 00:00:00 2001 From: pnhekgfuf <1913997697@qq.com> Date: Sat, 8 Apr 2023 18:17:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E6=B5=AA=E6=96=B0=E9=97=BB=E8=AF=8D?= =?UTF-8?q?=E4=BA=91=E7=88=AC=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- part2 | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 part2 diff --git a/part2 b/part2 new file mode 100644 index 0000000..521cb89 --- /dev/null +++ b/part2 @@ -0,0 +1,48 @@ +from wordcloud import WordCloud, STOPWORDS +from PIL import Image +import numpy as np +import matplotlib.pyplot as plt +import jieba +# 打开文件 +text = open("xinglang.txt", encoding="utf-8").read() +# 中文分词 +text = ' '.join(jieba.cut(text)) +print(text[:100]) +# 停用词 +stopwords = STOPWORDS +STOPWORDS.add("的") +STOPWORDS.add("了") +STOPWORDS.add("这") +STOPWORDS.add("和") +STOPWORDS.add("是") +STOPWORDS.add("上") +STOPWORDS.add("让") +STOPWORDS.add("为") +STOPWORDS.add("中") +STOPWORDS.add("个") +STOPWORDS.add("能") +STOPWORDS.add("年") +STOPWORDS.add("到") +STOPWORDS.add("在") +STOPWORDS.add("我") +STOPWORDS.add("也") +STOPWORDS.add("到") +STOPWORDS.add("有") +STOPWORDS.add("就") +STOPWORDS.add("寻亲") +# 生成对象 +# 使用蒙版图片 +mask = np.array(Image.open("zhihu.png")) # 使用蒙版图片 +wc = WordCloud( + mask=mask, + font_path="STKAITI.TTF", + mode="RGBA", + background_color=None, + stopwords=STOPWORDS +).generate(text) +# 显示词云 +plt.imshow(wc, interpolation="bilinear") +plt.axis("off") +plt.show() +# 保存文件 +wc.to_file("xinglang_wordcloud.png")