parent
2bb7c3ec87
commit
7de5381d59
@ -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)
|
Loading…
Reference in new issue