豆瓣读书评论词云

master
p2f8cf6qm 4 years ago
parent 2912b00fdb
commit d710adea27

@ -0,0 +1,39 @@
import csv
import jieba
from matplotlib import pyplot as plt
from wordcloud import WordCloud
from PIL import Image
import numpy as np
import pandas as pd
path = r'C:\Users\123\Desktop'
with open(path+r'\doubanbook1.csv','r',encoding='utf-8') as f:
reader = csv.reader(f)
with open(path+r'\ciyun.txt','w',encoding='utf-8') as f:
for row in reader:
column = row[6]
f.write(column+'\n')
font = r'C:\Windows\Fonts\FZSTK.TTF'#电脑自带的字体
def tcg(texts):
cut = jieba.cut(texts) #分词
string = ' '.join(cut)
return string
text = (open(path+r'\ciyun.txt','r',encoding='utf-8')).read()
string=tcg(text)
img = Image.open(path+r'\书.jpg') #打开图片
img_array = np.array(img) #将图片装换为数组
stopword=[''] #设置停止词,也就是你不想显示的词
wc = WordCloud(
background_color='white',
width=1000,
height=800,
mask=img_array, #设置背景图片
font_path=font,
stopwords=stopword
)
wc.generate_from_text(string)#绘制图片
plt.imshow(wc)
plt.axis('off')
plt.show() #显示图片
wc.to_file(path+r'\goodciyun.png') #保存图片
Loading…
Cancel
Save