前两天在豆瓣上爬取了一些《长津湖》电影的短评,今天就来简单分析一下。
本篇文章主要分析文字部分。
1.准备阶段
通过pip安装pandas、jieba、matplotlib、wordcloud库
准备停用词典
2.加载库
import pandas as pd
import jieba
from tkinter import _flatten
import matplotlib.pyplot as plt
from wordcloud import WordCloud
3.导入停用词典
with open(r'E:\python\changjinhu\stoplist1.txt','r',encoding='utf-8')as f:
stopWords=f.read()
stopwords=['\n',''] + stopWords.split() #引号里可以增加停用词,这里我增加了换行符\n
4.导入数据
data=pd.read_csv('E:\python\changjinhu\changjinhu.csv', encoding='GB18030') # 将数据导入
dataCut = data['短评正文'].apply(jieba.lcut) # 分词
5.用jieba库对短评正文分词,进行除停用词,词频统计操作
def my_word_cloud(data=None, stopWords=None, img=None):
dataCut = data.apply(jieba.lcut) # 分词
dataAfter = dataCut.apply(lambda x: [i for i in x if i not in stopWords]) # 去除停用词
wordFre = pd.Series(_flatten(list(dataAfter))).value_counts() # 统计词频
6.整体词云图的绘制
mask = plt.imread('E:/python/changjinhu/xin.jpg') #读取想要显示词云图形状的图片
cyt=WordCloud(font_path='C:/Windows/Fonts/simkai.ttf',mask=mask,background_color='white') #设置词云图字体,形状,背景色
plt.imshow(cyt) #画出词云图
plt.axis('off') #去除坐标轴
7.好评差评词云图的绘制
以评分30为界点,大于等于30为好评,小于30为差评
index_negative = data['评分'] < 30 # 差评数据索引
index_positive = data['评分'] >= 30 # 好评数据索引
好评词云图
my_word_cloud(data=data['短评正文'][index_positive], stopWords=stopWords, img='E:/python/hao.jpg') # 好评数据的词云
差评词云图
my_word_cloud(data=data['短评正文'][index_negative], stopWords=stopWords, img='E:/python/cha.jpg') # 差评数据的词云
8.小结
本文评论数目有限,代表性不强,结果仅供参考,所有代码可供学习使用。
如有侵权联系即删,如有不足,欢迎评论区留言交流。