From f89319d4f066c61db2b9aa67bc0af19338f8e4d8 Mon Sep 17 00:00:00 2001 From: puz372fkg <2904171770@qq.com> Date: Tue, 17 Sep 2024 16:06:30 +0800 Subject: [PATCH] ADD file via upload --- wcd.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 wcd.py diff --git a/wcd.py b/wcd.py new file mode 100644 index 0000000..f0f298e --- /dev/null +++ b/wcd.py @@ -0,0 +1,48 @@ +import jieba # 引入分词模块 +import wordcloud # 引入词云图配置模块 + + +# 1、对弹幕进行读取 +f = open("全弹幕.txt",encoding="utf-8") # 读取全弹幕 +text = f.read() +f1 = open("AI弹幕.txt",encoding="utf-8") # 读取AI弹幕 +text1 = f1.read() + + +# 2、对弹幕数据进行分词操作 +word_list = jieba.lcut(text) #对全弹幕进行分词 +for word in word_list: + with open('全弹幕分词.xls',mode='a',encoding='utf-8') as f: # 将全弹幕分词保存在xls表中 + f.write(word) + f.write('\n') + +word_list1 = jieba.lcut(text1) #对AI弹幕进行分词 +for word in word_list1: + with open('AI弹幕分词.xls',mode='a',encoding='utf-8') as f: # 将AI弹幕分词保存在xls表中 + f.write(word) + f.write('\n') + + +# 3、将分割出的词连接成字符串 +word_str = ' '.join(word_list) # 全弹幕 +word_str1 = ' '.join(word_list1) # AI弹幕 + + +# 4、进行词云图相关配置 +wcdall = wordcloud.WordCloud( + width=600, # 宽度 + height=600, #高度 + background_color='white', # 背景颜色 + font_path='msyh.ttc' # 字体文件 +) +wcdall.generate(word_str) +wcdall.to_file('全弹幕词云.png') + +wcdai = wordcloud.WordCloud( + width=600, # 宽度 + height=600, #高度 + background_color='white', # 背景颜色 + font_path='msyh.ttc' # 字体文件 +) +wcdai.generate(word_str1) +wcdai.to_file('AI弹幕词云.png') \ No newline at end of file