Kaggle-关于酒店评价的旅行建议暨SnowNLP模型

一、描述

对某酒店的 20491 条客户评价做出分析。

二、数据集说明

每条客户评价数据有 2 个字段:Review、Rating。

数据集版权许可协议

BY-NC-SA 4.0
https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh-hans

三、解决方案样例

导入相关系统库

import pandas as pd
from wordcloud import WordCloud
import re
from typing import Union, List
import string
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import matplotlib.pyplot as plt
import snownlp as snow

【本样例运行环境的关键版本信息】

python 3.12.3

nbformat 5.10.4

snownlp 0.12.3

读入数据集

df = pd.read_csv("./data/tripadvisor_hotel_reviews.csv")

分析数据

# 每种评级的数量
fig = px.histogram(df,
                   x = 'Rating',
                   template = 'ggplot2',
                   color = 'Rating',
                   color_discrete_sequence = px.colors.sequential.Blues_r,
                   opacity = 0.8,
                   height = 525,
                   width = 835)

fig.update_yaxes(title='Count')
fig.show()

使用 SnowNLP 对酒店评价进行情感分析

# 定义情感分类函数
def classify_sentiment(text):
    # SnowNLP 返回 0~1 的正面概率值
    score = snow.SnowNLP(text).sentiments
    if score > 0.6:
        return 'positive'
    elif score < 0.4:
        return 'negative'
    else:
        return 'neutral'

# 对每条评价进行情感分析
df['PredictedSentiment'] = df['Review'].apply(classify_sentiment)

保存分析结果

df[['Review', 'PredictedSentiment']].to_csv('submit.csv', index=False)

源码开源协议

GPL-v3
https://zhuanlan.zhihu.com/p/608456168

四、获取案例套件

需要登录后才允许下载文件包。登录 需要登录后才允许下载文件包。登录

发表评论