From f1223e6f3982cc469e9cd449f4fc106fa8bccc28 Mon Sep 17 00:00:00 2001 From: plzmcgpwb <3167850775@qq.com> Date: Wed, 18 Sep 2024 20:32:48 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=9F=E6=88=90=E8=AF=8D=E4=BA=91=E5=9B=BE?= =?UTF-8?q?=E7=9A=84=E6=BA=90=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ciyun.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 ciyun.py diff --git a/ciyun.py b/ciyun.py new file mode 100644 index 0000000..4cc8d3c --- /dev/null +++ b/ciyun.py @@ -0,0 +1,34 @@ +from wordcloud import WordCloud +import matplotlib.pyplot as plt + +# 读取弹幕文件 +with open("弹幕.txt", "r", encoding="utf-8") as file: + all_danmaku = file.readlines() + +# 去除空白字符,并合并成一个长字符串 +danmaku_text = ''.join(all_danmaku).replace('\n', '') + +# 使用WordCloud生成词云图 +def generate_wordcloud(text): + # 指定中文字体路径 + font_path = 'C:\\Windows\\Fonts\\msyh.ttc' # 微软雅黑字体路径 + + # 创建词云对象 + wordcloud = WordCloud( + font_path=font_path, # 设置字体路径 + width=800, + height=400, + background_color='white' # 设置背景颜色 + ).generate(text) + + # 显示词云图 + plt.figure(figsize=(10, 5)) + plt.imshow(wordcloud, interpolation='bilinear') + plt.axis('off') # 不显示坐标轴 + plt.show() + + # 保存词云图到文件 + wordcloud.to_file("danmaku_wordcloud.png") + +# 调用函数生成词云图 +generate_wordcloud(danmaku_text) \ No newline at end of file