基于PyCharm编写运行。
Python代码
首先需要安装好依赖库。
1 2 3 4 5
| pip install jieba pip install wordcloud pip install matplotlib pip install numpy pip install PIL
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| import jieba import matplotlib.pyplot as plt from matplotlib import colors from wordcloud import WordCloud import numpy as np from PIL import Image
text=open('musicpl.txt',encoding="utf-8").read() print(text)
text=text.replace("\n","").replace("\u3000","").replace("\u0020","") print(text)
text_cut=jieba.lcut(text)
text_cut=" ".join(text_cut) print(text_cut)
stop_words=open("stop.txt",encoding="utf-8").read().split("\n")
background=Image.open("xin.jpg")
graph=np.array(background)
word_cloud=WordCloud(font_path="C:/Windows/Fonts/simsun.ttc", background_color="white",stopwords=stop_words,mask=graph)
word_cloud.generate(text_cut)
plt.figure(figsize=(12,8),dpi=100)
plt.imshow(word_cloud)
plt.axis("off")
plt.savefig("词云图.png") plt.show()
|
运行以上代码,就可以得到心形音乐评论词云了,这首歌可以点击右上角播放。