From f794bc925ce5f38ecdd4e618dcab245f2c5b4e5f Mon Sep 17 00:00:00 2001 From: puz372fkg <2904171770@qq.com> Date: Tue, 17 Sep 2024 17:27:23 +0800 Subject: [PATCH] ADD file via upload --- wcd.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 wcd.py diff --git a/wcd.py b/wcd.py new file mode 100644 index 0000000..6c2aa55 --- /dev/null +++ b/wcd.py @@ -0,0 +1,49 @@ +import jieba # 引入分词模块 +import wordcloud # 引入词云图配置模块 +import imageio # 引入图片模块,改变词云图形状 + + +# 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表中 + if word != '\n': # 防止输入换行字符 + 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表中 + if word != '\n': # 防止输入换行字符 + f.write(word) + f.write('\n') + +# 3、将分割出的词连接成字符串 +word_str = ' '.join(word_list) # 全弹幕 +word_str1 = ' '.join(word_list1) # AI弹幕 + + +# 4、进行词云图相关配置 +img = imageio.imread('1.jpg') # 载入图片 +wcdall = wordcloud.WordCloud( # 全弹幕词云图 + mask=img, # 形状 + background_color='white', # 背景颜色 + font_path='msyh.ttc' # 字体文件 +) +wcdall.generate(word_str) +wcdall.to_file('全弹幕词云.png') + +wcdai = wordcloud.WordCloud( # AI弹幕词云图 + mask=img, # 形状 + background_color='white', # 背景颜色 + font_path='msyh.ttc' # 字体文件 +) +wcdai.generate(word_str1) +wcdai.to_file('AI弹幕词云.png') \ No newline at end of file