You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.7 KiB

from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
# 读取弹幕文件
with open("弹幕.txt", "r", encoding="utf-8") as file:
all_danmaku = file.readlines()
# 去除空白字符,并合并成一个长字符串
danmaku_text = ''.join(all_danmaku).replace('\n', '')
# 定义一个函数来生成和美化词云图
def generate_wordcloud(text, font_path='msyh.ttc'):
# 定义词云的配置参数
config = {
'font_path': font_path, # 字体路径
'width': 800, # 宽度
'height': 600, # 高度
'background_color': 'white', # 背景颜色
'max_words': 200, # 最大显示的词数
'max_font_size': 100, # 最大字体大小
'min_font_size': 10, # 最小字体大小
}
# 创建词云对象
wordcloud = WordCloud(
font_path=config['font_path'],
width=config['width'],
height=config['height'],
background_color=config['background_color'],
max_words=config['max_words'],
max_font_size=config['max_font_size'],
min_font_size=config['min_font_size'],
stopwords=STOPWORDS.union({"随着", "但是", "一个"}), # 添加自定义停用词
).generate(text)
# 显示词云图
plt.figure(figsize=(10, 8))
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()
# 保存词云图到文件,并指定新的文件名
new_filename = "customized_danmaku_wordcloud.png"
wordcloud.to_file(new_filename)
print(f"词云图已保存为:{new_filename}")
# 调用函数生成词云图
generate_wordcloud(danmaku_text, font_path='C:\\Windows\\Fonts\\msyh.ttc')