Spaces:
Sleeping
Sleeping
File size: 12,379 Bytes
087ac11 |
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 |
"""
Stage 4: Batch Analysis & Aggregation
- Aggregate insights across all processed reviews
- Identify patterns, trends, critical issues
- Generate actionable recommendations
"""
import json
from typing import Dict, Any, List
from collections import Counter
class Stage4BatchAnalysis:
"""
Stage 4: Batch-level intelligence and recommendations
"""
def __init__(self):
print(" π Stage 4: Batch Analysis initialized")
def analyze_batch(self, reviews: List[Dict[str, Any]]) -> Dict[str, Any]:
"""
Analyze a batch of processed reviews
"""
if not reviews:
print(" β οΈ No reviews to analyze")
return self._empty_insights()
print(f"\n π Analyzing batch of {len(reviews)} reviews...")
# Initialize counters
total = len(reviews)
# Sentiment distribution
sentiment_counts = Counter()
for review in reviews:
sentiment = review.get('stage3_final_sentiment', 'NEUTRAL')
sentiment_counts[sentiment] += 1
print(f" π Sentiment: "
f"POS={sentiment_counts.get('POSITIVE', 0)}, "
f"NEU={sentiment_counts.get('NEUTRAL', 0)}, "
f"NEG={sentiment_counts.get('NEGATIVE', 0)}")
# Priority distribution
priority_counts = Counter()
for review in reviews:
priority = review.get('stage1_llm1_priority', 'unknown')
priority_counts[priority] += 1
print(f" π― Priority: "
f"Critical={priority_counts.get('critical', 0)}, "
f"High={priority_counts.get('high', 0)}, "
f"Medium={priority_counts.get('medium', 0)}, "
f"Low={priority_counts.get('low', 0)}")
# Department routing
dept_counts = Counter()
for review in reviews:
dept = review.get('stage1_llm1_department', 'unknown')
dept_counts[dept] += 1
print(f" π’ Departments: "
f"Eng={dept_counts.get('engineering', 0)}, "
f"UX={dept_counts.get('ux', 0)}, "
f"Support={dept_counts.get('support', 0)}, "
f"Business={dept_counts.get('business', 0)}")
# Emotion distribution
emotion_counts = Counter()
for review in reviews:
emotion = review.get('stage1_llm2_emotion', 'unknown')
emotion_counts[emotion] += 1
# Review type distribution
type_counts = Counter()
for review in reviews:
review_type = review.get('stage1_llm1_type', 'unknown')
type_counts[review_type] += 1
# Identify critical issues
critical_issues = self._identify_critical_issues(reviews)
print(f" π¨ Critical Issues: {len(critical_issues)}")
# Identify quick wins
quick_wins = self._identify_quick_wins(reviews)
print(f" β‘ Quick Wins: {len(quick_wins)}")
# Calculate churn risk
churn_risk = self._calculate_churn_risk(reviews)
print(f" β οΈ Churn Risk: {churn_risk:.1f}%")
# Model agreement rate
agreement_count = sum(1 for r in reviews if r.get('stage2_agreement', False))
agreement_rate = (agreement_count / total * 100) if total > 0 else 0
print(f" π€ Model Agreement: {agreement_rate:.1f}%")
# Generate recommendations
recommendations = self._generate_recommendations(
sentiment_counts, priority_counts, dept_counts,
critical_issues, quick_wins, churn_risk
)
# Compile batch insights
insights = {
'total_reviews': total,
# Sentiment
'sentiment_positive': sentiment_counts.get('POSITIVE', 0),
'sentiment_neutral': sentiment_counts.get('NEUTRAL', 0),
'sentiment_negative': sentiment_counts.get('NEGATIVE', 0),
'sentiment_distribution': dict(sentiment_counts),
# Priority
'priority_critical': priority_counts.get('critical', 0),
'priority_high': priority_counts.get('high', 0),
'priority_medium': priority_counts.get('medium', 0),
'priority_low': priority_counts.get('low', 0),
'priority_distribution': dict(priority_counts),
# Department
'dept_engineering': dept_counts.get('engineering', 0),
'dept_ux': dept_counts.get('ux', 0),
'dept_support': dept_counts.get('support', 0),
'dept_business': dept_counts.get('business', 0),
'department_distribution': dict(dept_counts),
# Additional insights
'emotion_distribution': dict(emotion_counts),
'type_distribution': dict(type_counts),
'model_agreement_rate': agreement_rate,
'churn_risk': churn_risk,
# Actionable lists
'critical_issues': critical_issues,
'quick_wins': quick_wins,
'recommendations': recommendations
}
return insights
def _identify_critical_issues(self, reviews: List[Dict]) -> List[Dict]:
"""Identify critical issues requiring immediate attention"""
critical = []
for review in reviews:
priority = review.get('stage1_llm1_priority', '')
sentiment = review.get('stage3_final_sentiment', '')
needs_review = review.get('stage3_needs_human_review', False)
if priority == 'critical' or (sentiment == 'NEGATIVE' and needs_review):
critical.append({
'review_id': review.get('review_id', 'unknown'),
'type': review.get('stage1_llm1_type', 'unknown'),
'department': review.get('stage1_llm1_department', 'unknown'),
'reasoning': review.get('stage3_reasoning', ''),
'action': review.get('stage3_action_recommendation', ''),
'rating': review.get('rating', 0)
})
# Sort by rating (lowest first)
critical.sort(key=lambda x: x['rating'])
return critical[:10] # Top 10 critical issues
def _identify_quick_wins(self, reviews: List[Dict]) -> List[Dict]:
"""Identify easy-to-fix issues for quick wins"""
quick_wins = []
for review in reviews:
review_type = review.get('stage1_llm1_type', '')
priority = review.get('stage1_llm1_priority', '')
sentiment = review.get('stage3_final_sentiment', '')
# Suggestions with low priority = quick wins
if review_type == 'suggestion' and priority in ['low', 'medium']:
quick_wins.append({
'review_id': review.get('review_id', 'unknown'),
'suggestion': review.get('review_text', '')[:100],
'department': review.get('stage1_llm1_department', 'unknown'),
'action': review.get('stage3_action_recommendation', ''),
'rating': review.get('rating', 0)
})
return quick_wins[:10] # Top 10 quick wins
def _calculate_churn_risk(self, reviews: List[Dict]) -> float:
"""Calculate overall churn risk percentage"""
if not reviews:
return 0.0
churn_indicators = 0
for review in reviews:
user_type = review.get('stage1_llm2_user_type', '')
sentiment = review.get('stage3_final_sentiment', '')
rating = review.get('rating', 3)
# Churn indicators
if user_type == 'churning_user':
churn_indicators += 2
elif sentiment == 'NEGATIVE' and rating <= 2:
churn_indicators += 1
elif rating == 1:
churn_indicators += 1
# Calculate percentage
max_possible = len(reviews) * 2
churn_risk = (churn_indicators / max_possible * 100) if max_possible > 0 else 0.0
return min(churn_risk, 100.0)
def _generate_recommendations(self, sentiment_counts, priority_counts,
dept_counts, critical_issues, quick_wins,
churn_risk) -> List[str]:
"""Generate actionable recommendations"""
recommendations = []
# Sentiment-based
total = sum(sentiment_counts.values())
if total > 0:
neg_pct = (sentiment_counts.get('NEGATIVE', 0) / total * 100)
if neg_pct > 40:
recommendations.append(
f"π¨ HIGH: {neg_pct:.0f}% negative sentiment. Immediate investigation needed."
)
elif neg_pct > 25:
recommendations.append(
f"β οΈ MEDIUM: {neg_pct:.0f}% negative sentiment. Monitor closely."
)
# Priority-based
if priority_counts.get('critical', 0) > 0:
recommendations.append(
f"π₯ URGENT: {priority_counts['critical']} critical issues require immediate attention."
)
# Department-based
if dept_counts:
top_dept = max(dept_counts, key=dept_counts.get)
top_count = dept_counts[top_dept]
recommendations.append(
f"π― FOCUS: {top_count} issues routed to {top_dept} department."
)
# Churn risk
if churn_risk > 30:
recommendations.append(
f"β οΈ CHURN: {churn_risk:.0f}% churn risk detected. Implement retention strategy."
)
# Quick wins
if quick_wins:
recommendations.append(
f"β‘ OPPORTUNITY: {len(quick_wins)} quick wins available for easy improvements."
)
return recommendations
def _empty_insights(self) -> Dict[str, Any]:
"""Return empty insights structure"""
return {
'total_reviews': 0,
'sentiment_positive': 0,
'sentiment_neutral': 0,
'sentiment_negative': 0,
'priority_critical': 0,
'priority_high': 0,
'priority_medium': 0,
'priority_low': 0,
'dept_engineering': 0,
'dept_ux': 0,
'dept_support': 0,
'dept_business': 0,
'critical_issues': [],
'quick_wins': [],
'recommendations': []
}
if __name__ == "__main__":
# Test Stage 4
print("\n" + "="*60)
print("π§ͺ TESTING STAGE 4 BATCH ANALYSIS")
print("="*60)
# Sample processed reviews
sample_reviews = [
{
'review_id': '001',
'review_text': 'App crashes!',
'rating': 1,
'stage1_llm1_type': 'bug_report',
'stage1_llm1_department': 'engineering',
'stage1_llm1_priority': 'critical',
'stage1_llm2_user_type': 'power_user',
'stage1_llm2_emotion': 'frustration',
'stage2_agreement': True,
'stage3_final_sentiment': 'NEGATIVE',
'stage3_needs_human_review': True,
'stage3_reasoning': 'Critical bug',
'stage3_action_recommendation': 'Fix immediately'
},
{
'review_id': '002',
'review_text': 'Great app!',
'rating': 5,
'stage1_llm1_type': 'praise',
'stage1_llm1_department': 'ux',
'stage1_llm1_priority': 'low',
'stage1_llm2_user_type': 'regular_user',
'stage1_llm2_emotion': 'joy',
'stage2_agreement': True,
'stage3_final_sentiment': 'POSITIVE',
'stage3_needs_human_review': False
}
]
stage4 = Stage4BatchAnalysis()
insights = stage4.analyze_batch(sample_reviews)
print("\nπ BATCH INSIGHTS:")
print(json.dumps(insights, indent=2))
print("\nβ
Stage 4 test complete!")
|