"""
author@liyi
date: 2019/5/5
"""
import os
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
import matplotlib.pyplot as plt
import jieba
import numpy as np
from PIL import Image
def gen_word_cloud(text, name, img_path):
#设置停用词
stop_words = set(STOPWORDS)
stop_words.add("用户")
#增加关键字
# text = text + " " + name + "网易云"
background_image = np.array(Image.open(img_path))
word_cloud = WordCloud(font_path="simsun.ttf", background_color="white",
max_font_size=100,
mask=background_image, stopwords=stop_words).generate(text)
#根据图片颜色设置文字颜色
image_color = ImageColorGenerator(background_image)
word_cloud.recolor(color_func=image_color)
plt.imshow(word_cloud, interpolation='bilinear')
#去掉坐标轴
plt.axis("off")
# plt.show()
word_cloud.to_file("word-cloud-img/" + name + ".jpg")
def main():
dir_name = os.path.dirname(__file__)
word_cloud_imgs = os.path.join(dir_name, "word-cloud-img")
#创建存储生成词云图片的文件夹
if not os.path.exists(word_cloud_imgs):
os.mkdir(word_cloud_imgs)
files = os.listdir("comments")
for file in files:
with open(os.path.join(dir_name, "comments", file), mode="r", encoding="utf8") as f:
comments = ' '.join(jieba.cut(f.read()))
gen_word_cloud(text=comments, name=file, img_path="imgs/douyin.jpg")
print("生成了歌曲{}的评论词云......".format(file))
if __name__ == '__main__':
main()

设计交流学习
- 粉丝: 92
- 资源: 551
会员权益专享
最新资源
- 免费可商用开源GPT模型问世,50G权重直接下载,性能不输GPT-3
- GPT4的实用应用整理:如何充分利用 GPT-4 生成型 AI 助理?
- carpath-final(1).cpp
- 斐波那契法.html
- 8天背完新概念一的全部单词
- 游戏打怪桌面倒计时小程序+源码
- 自用光影精灵8300h sn520 efi with opencore for macos 11 bigsur
- 阿里巴巴发布最新财报__菜鸟外部收入占比增至70%_记者__温婷.caj
- 京瓷中文维修手册TASKalfa3050ci-3550ci-4550ci-5550ci 维修手册
- 京瓷中文维修手册ECOSYS M8130cidn M8124cidn维修手册
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


