From 7de5381d59370dbfbe7338c30453eadb4fd671a0 Mon Sep 17 00:00:00 2001 From: pzmji3gwt <2546626818@qq.com> Date: Wed, 18 Sep 2024 18:15:03 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=9F=E6=88=90=E8=AF=8D=E4=BA=91=E5=9B=BE?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yuntu.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 yuntu.py diff --git a/yuntu.py b/yuntu.py new file mode 100644 index 0000000..d0d0700 --- /dev/null +++ b/yuntu.py @@ -0,0 +1,37 @@ +import pandas as pd +from collections import Counter +from wordcloud import WordCloud +import matplotlib.pyplot as plt + +# 读取弹幕文件 +def read_danmakus_from_file(file_path): + with open(file_path, 'r', encoding='utf-8') as file: + return [line.strip() for line in file if line.strip()] # 移除空行并去除每行的前后空白字符 + +# 生成词云 +def generate_wordcloud(danmakus): + # 合并所有弹幕文本 + text = ' '.join(danmakus) + # 创建词云对象 + wordcloud = WordCloud( + font_path='simhei.ttf', # 设置字体路径,确保支持中文 + 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('wordcloud.png') + +# 读取弹幕数据 +danmaku_file_path = '弹幕.txt' +danmakus = read_danmakus_from_file(danmaku_file_path) + +# 生成词云 +generate_wordcloud(danmakus) \ No newline at end of file