-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreview_visualizer.py
More file actions
434 lines (339 loc) · 16.2 KB
/
Copy pathreview_visualizer.py
File metadata and controls
434 lines (339 loc) · 16.2 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Google Play评论数据可视化
用于生成评论分析的可视化图表
"""
import json
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
from collections import Counter, defaultdict
from typing import Dict, List
import logging
import os
from datetime import datetime
# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False
# 设置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
class ReviewVisualizer:
"""评论数据可视化器"""
def __init__(self):
# 设置图表样式
plt.style.use('seaborn-v0_8')
sns.set_palette("husl")
# 创建输出目录
self.output_dir = "visualization_output"
if not os.path.exists(self.output_dir):
os.makedirs(self.output_dir)
def load_analysis_data(self, json_file: str) -> Dict:
"""加载分析数据"""
try:
with open(json_file, 'r', encoding='utf-8') as f:
data = json.load(f)
logger.info(f"从 {json_file} 加载了分析数据")
return data
except Exception as e:
logger.error(f"加载分析数据失败: {e}")
return {}
def plot_rating_distribution(self, analysis_data: Dict, save_path: str = None):
"""绘制评分分布图"""
try:
rating_dist = analysis_data.get('rating_distribution', {})
if not rating_dist:
logger.warning("没有评分分布数据")
return
# 准备数据
ratings = sorted(rating_dist.keys(), reverse=True)
counts = [rating_dist[r] for r in ratings]
percentages = [count / sum(counts) * 100 for count in counts]
# 创建图表
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))
# 柱状图
bars = ax1.bar(ratings, counts, color=['#ff6b6b', '#4ecdc4', '#45b7d1', '#96ceb4', '#feca57'])
ax1.set_xlabel('评分')
ax1.set_ylabel('评论数量')
ax1.set_title('评分分布 - 柱状图')
ax1.set_xticks(ratings)
# 添加数值标签
for bar, count in zip(bars, counts):
ax1.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.5,
str(count), ha='center', va='bottom')
# 饼图
colors = ['#ff6b6b', '#4ecdc4', '#45b7d1', '#96ceb4', '#feca57']
wedges, texts, autotexts = ax2.pie(counts, labels=[f'{r}星' for r in ratings],
autopct='%1.1f%%', colors=colors)
ax2.set_title('评分分布 - 饼图')
# 美化饼图文字
for autotext in autotexts:
autotext.set_color('white')
autotext.set_fontweight('bold')
plt.tight_layout()
if save_path:
plt.savefig(save_path, dpi=300, bbox_inches='tight')
logger.info(f"评分分布图已保存到 {save_path}")
else:
plt.show()
plt.close()
except Exception as e:
logger.error(f"绘制评分分布图失败: {e}")
def plot_sentiment_analysis(self, analysis_data: Dict, save_path: str = None):
"""绘制情感分析图"""
try:
sentiment_dist = analysis_data.get('sentiment_analysis', {})
if not sentiment_dist:
logger.warning("没有情感分析数据")
return
# 准备数据
sentiments = list(sentiment_dist.keys())
counts = list(sentiment_dist.values())
colors = {'positive': '#4ecdc4', 'negative': '#ff6b6b', 'neutral': '#95a5a6'}
sentiment_colors = [colors.get(s, '#95a5a6') for s in sentiments]
# 创建图表
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))
# 柱状图
bars = ax1.bar(sentiments, counts, color=sentiment_colors)
ax1.set_xlabel('情感类型')
ax1.set_ylabel('评论数量')
ax1.set_title('情感分析 - 柱状图')
# 添加数值标签
for bar, count in zip(bars, counts):
ax1.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.5,
str(count), ha='center', va='bottom')
# 饼图
wedges, texts, autotexts = ax2.pie(counts, labels=sentiments,
autopct='%1.1f%%', colors=sentiment_colors)
ax2.set_title('情感分析 - 饼图')
# 美化饼图文字
for autotext in autotexts:
autotext.set_color('white')
autotext.set_fontweight('bold')
plt.tight_layout()
if save_path:
plt.savefig(save_path, dpi=300, bbox_inches='tight')
logger.info(f"情感分析图已保存到 {save_path}")
else:
plt.show()
plt.close()
except Exception as e:
logger.error(f"绘制情感分析图失败: {e}")
def plot_feature_analysis(self, analysis_data: Dict, save_path: str = None):
"""绘制功能特征分析图"""
try:
feature_dist = analysis_data.get('feature_analysis', {})
if not feature_dist:
logger.warning("没有功能特征数据")
return
# 准备数据
features = list(feature_dist.keys())
counts = list(feature_dist.values())
# 按数量排序
sorted_data = sorted(zip(features, counts), key=lambda x: x[1], reverse=True)
features, counts = zip(*sorted_data)
# 创建图表
fig, ax = plt.subplots(figsize=(12, 8))
# 水平柱状图
bars = ax.barh(features, counts, color=plt.cm.viridis(np.linspace(0, 1, len(features))))
ax.set_xlabel('提及次数')
ax.set_ylabel('功能类别')
ax.set_title('功能特征分析')
# 添加数值标签
for bar, count in zip(bars, counts):
ax.text(bar.get_width() + 0.5, bar.get_y() + bar.get_height()/2,
str(count), ha='left', va='center')
plt.tight_layout()
if save_path:
plt.savefig(save_path, dpi=300, bbox_inches='tight')
logger.info(f"功能特征分析图已保存到 {save_path}")
else:
plt.show()
plt.close()
except Exception as e:
logger.error(f"绘制功能特征分析图失败: {e}")
def plot_top_demands(self, analysis_data: Dict, top_n: int = 15, save_path: str = None):
"""绘制用户需求Top N图"""
try:
top_demands = analysis_data.get('top_demands', [])
if not top_demands:
logger.warning("没有用户需求数据")
return
# 取前N个需求
top_demands = top_demands[:top_n]
# 准备数据
demands = [demand[:30] + '...' if len(demand) > 30 else demand for demand, _ in top_demands]
counts = [count for _, count in top_demands]
# 创建图表
fig, ax = plt.subplots(figsize=(12, 10))
# 水平柱状图
bars = ax.barh(range(len(demands)), counts, color=plt.cm.plasma(np.linspace(0, 1, len(demands))))
ax.set_yticks(range(len(demands)))
ax.set_yticklabels(demands)
ax.set_xlabel('提及次数')
ax.set_title(f'用户需求分析 (Top {top_n})')
# 添加数值标签
for i, (bar, count) in enumerate(zip(bars, counts)):
ax.text(bar.get_width() + 0.1, bar.get_y() + bar.get_height()/2,
str(count), ha='left', va='center')
# 反转y轴,使最多的需求在顶部
ax.invert_yaxis()
plt.tight_layout()
if save_path:
plt.savefig(save_path, dpi=300, bbox_inches='tight')
logger.info(f"用户需求分析图已保存到 {save_path}")
else:
plt.show()
plt.close()
except Exception as e:
logger.error(f"绘制用户需求分析图失败: {e}")
def plot_sentiment_by_rating(self, analysis_data: Dict, save_path: str = None):
"""绘制按评分的情感分析图"""
try:
sentiment_by_rating = analysis_data.get('sentiment_by_rating', {})
if not sentiment_by_rating:
logger.warning("没有按评分的情感分析数据")
return
# 准备数据
ratings = sorted(sentiment_by_rating.keys(), reverse=True)
sentiments = ['positive', 'negative', 'neutral']
# 创建DataFrame
data = []
for rating in ratings:
rating_data = sentiment_by_rating[rating]
total = sum(rating_data.values())
for sentiment in sentiments:
count = rating_data.get(sentiment, 0)
percentage = count / total * 100 if total > 0 else 0
data.append({
'Rating': f'{rating}星',
'Sentiment': sentiment,
'Percentage': percentage,
'Count': count
})
df = pd.DataFrame(data)
# 创建堆叠柱状图
fig, ax = plt.subplots(figsize=(10, 6))
# 按情感类型分组
sentiment_colors = {'positive': '#4ecdc4', 'negative': '#ff6b6b', 'neutral': '#95a5a6'}
bottom = np.zeros(len(ratings))
for sentiment in sentiments:
sentiment_data = df[df['Sentiment'] == sentiment]
percentages = [sentiment_data[sentiment_data['Rating'] == f'{r}星']['Percentage'].iloc[0]
if len(sentiment_data[sentiment_data['Rating'] == f'{r}星']) > 0 else 0
for r in ratings]
ax.bar(range(len(ratings)), percentages, bottom=bottom,
label=sentiment, color=sentiment_colors[sentiment])
bottom += percentages
ax.set_xlabel('评分')
ax.set_ylabel('百分比 (%)')
ax.set_title('按评分的情感分析')
ax.set_xticks(range(len(ratings)))
ax.set_xticklabels([f'{r}星' for r in ratings])
ax.legend()
plt.tight_layout()
if save_path:
plt.savefig(save_path, dpi=300, bbox_inches='tight')
logger.info(f"按评分的情感分析图已保存到 {save_path}")
else:
plt.show()
plt.close()
except Exception as e:
logger.error(f"绘制按评分的情感分析图失败: {e}")
def plot_feature_frequency(self, analysis_data: Dict, save_path: str = None):
"""绘制特征关键词频次图"""
try:
feature_frequency = analysis_data.get('feature_frequency', {})
if not feature_frequency:
logger.warning("没有特征频次数据")
return
# 创建子图
n_categories = len(feature_frequency)
cols = 2
rows = (n_categories + 1) // 2
fig, axes = plt.subplots(rows, cols, figsize=(15, 5 * rows))
if rows == 1:
axes = [axes] if cols == 1 else axes
else:
axes = axes.flatten()
for i, (category, keywords) in enumerate(feature_frequency.items()):
if i >= len(axes):
break
ax = axes[i]
if not keywords:
continue
# 准备数据
words = list(keywords.keys())
counts = list(keywords.values())
# 取前10个关键词
words = words[:10]
counts = counts[:10]
# 绘制水平柱状图
bars = ax.barh(words, counts, color=plt.cm.Set3(np.linspace(0, 1, len(words))))
ax.set_xlabel('频次')
ax.set_title(f'{category} - 关键词频次')
# 添加数值标签
for bar, count in zip(bars, counts):
ax.text(bar.get_width() + 0.1, bar.get_y() + bar.get_height()/2,
str(count), ha='left', va='center')
# 隐藏多余的子图
for i in range(len(feature_frequency), len(axes)):
axes[i].set_visible(False)
plt.tight_layout()
if save_path:
plt.savefig(save_path, dpi=300, bbox_inches='tight')
logger.info(f"特征关键词频次图已保存到 {save_path}")
else:
plt.show()
plt.close()
except Exception as e:
logger.error(f"绘制特征关键词频次图失败: {e}")
def generate_all_visualizations(self, analysis_data: Dict, app_name: str = "app"):
"""生成所有可视化图表"""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
visualizations = [
("rating_distribution", self.plot_rating_distribution),
("sentiment_analysis", self.plot_sentiment_analysis),
("feature_analysis", self.plot_feature_analysis),
("top_demands", self.plot_top_demands),
("sentiment_by_rating", self.plot_sentiment_by_rating),
("feature_frequency", self.plot_feature_frequency)
]
generated_files = []
for viz_name, viz_func in visualizations:
try:
save_path = os.path.join(self.output_dir, f"{app_name}_{viz_name}_{timestamp}.png")
viz_func(analysis_data, save_path)
generated_files.append(save_path)
except Exception as e:
logger.error(f"生成 {viz_name} 可视化失败: {e}")
logger.info(f"生成了 {len(generated_files)} 个可视化图表")
return generated_files
def main():
"""主函数"""
visualizer = ReviewVisualizer()
print("Google Play评论数据可视化")
print("=" * 50)
# 选择分析数据文件
json_file = input("请输入分析结果JSON文件路径: ").strip()
if not os.path.exists(json_file):
print("文件不存在,退出程序")
return
# 加载分析数据
analysis_data = visualizer.load_analysis_data(json_file)
if not analysis_data:
print("未加载到分析数据,退出程序")
return
# 获取应用名称
app_name = input("请输入应用名称 (用于文件名): ").strip() or "app"
# 生成所有可视化图表
print("正在生成可视化图表...")
generated_files = visualizer.generate_all_visualizations(analysis_data, app_name)
print(f"\n可视化完成!生成了 {len(generated_files)} 个图表:")
for file_path in generated_files:
print(f" {file_path}")
print(f"\n所有图表已保存到 {visualizer.output_dir} 目录")
if __name__ == "__main__":
main()