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')