Skip to content

宜宾文旅 #167

Description

@991-dear

import pandas as pd
import jieba
from collections import Counter
import matplotlib.pyplot as plt
from wordcloud import WordCloud
from snownlp import SnowNLP
import matplotlib
matplotlib.use('Agg') # 兼容无弹窗环境,CodeGeeX网页版必备

解决中文乱码

plt.rcParams["font.family"] = ["SimHei", "WenQuanYi Micro Hei", "Heiti TC"]
plt.rcParams["axes.unicode_minus"] = False

自动兼容 utf-8/gbk 两种CSV编码

try:
df = pd.read_csv("yibin_comment.csv", encoding="utf-8")
except Exception:
df = pd.read_csv("yibin_comment.csv", encoding="gbk")

清洗空评论、多余空格

df["comment"] = df["comment"].astype(str).str.strip()
df = df[df["comment"] != ""]
comments = df["comment"]

SnowNLP情感打分分类

sentiment_result = []
for text in comments:
s = SnowNLP(text)
score = s.sentiments
if score > 0.5:
sentiment_result.append("正面")
elif score < 0.5:
sentiment_result.append("负面")
else:
sentiment_result.append("中性")
df["情感类别"] = sentiment_result

统计情感数量与占比

stat = df["情感类别"].value_counts()
print("===== 宜宾旅游口碑情感统计 =====")
print(stat)
print("\n各类占比:")
print((stat / len(df)).round(4))

绘制情感饼图并保存

plt.figure(figsize=(8, 6))
plt.pie(stat.values, labels=stat.index, autopct="%1.2f%%", shadow=True, startangle=90)
plt.title("宜宾(蜀南竹海/李庄/五粮液)游客情感分布饼图")
plt.axis("equal")
plt.savefig("情感饼图.png", dpi=300, bbox_inches="tight")
plt.close()

分词、过滤停用词与单字

stop_words = {"的", "了", "很", "也", "有", "比较", "一点", "还", "就", "都", "感觉", "里面", "可以",
"过来", "就是", "在", "和", "我", "去", "到", "一个", "这个", "那个", "不", "没有", "太"}
all_words = []
for text in comments:
words = jieba.cut(text)
for w in words:
if len(w) > 1 and w not in stop_words:
all_words.append(w)

word_count = Counter(all_words)
top_words = word_count.most_common(35)
word_str = " ".join([w[0] for w in top_words])

print("\n===== Top35高频关键词 =====")
for word, num in top_words:
print(f"{word}: {num}")

生成词云(CodeGeeX网页环境大概率缺少simhei.ttf字体)

wc = WordCloud(
background_color="white",
font_path="simhei.ttf",
width=1000, height=600,
max_words=200
)
wc.generate(word_str)
plt.figure(figsize=(10, 6))
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
plt.title("宜宾旅游高频关键词词云图")
plt.savefig("关键词云图.png", dpi=300, bbox_inches="tight")
plt.close()

print("\n运行结束,已输出两张图片:情感饼图.png、关键词云图.png")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions