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.

29 lines
710 B

import jieba
import wordcloud
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
def make_wordcloud():
# 打开弹幕文件
with open('paris_olympics_danmaku.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 使用 jieba 进行分词
words = jieba.lcut(text)
new_text = " ".join(words)
# 打开背景图片
mask = np.array(Image.open('background.jpg'))
# 创建词云对象
wc = wordcloud.WordCloud(font_path='simhei.ttf', mask=mask, background_color='white')
# 生成词云
wc.generate(new_text)
# 显示词云图
plt.imshow(wc, interpolation='bilinear')
plt.axis('off')
plt.show()