diff --git a/word_cloud.py b/word_cloud.py new file mode 100644 index 0000000..76c59cc --- /dev/null +++ b/word_cloud.py @@ -0,0 +1,28 @@ +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()