-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze.py
More file actions
executable file
·170 lines (144 loc) · 6.55 KB
/
Copy pathanalyze.py
File metadata and controls
executable file
·170 lines (144 loc) · 6.55 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
"""End-to-end analysis using the dataset's existing labels (no API key required).
Produces every artifact the slide deck consumes:
- outputs/categorization.json — per-call tag assignment + theme rollup
- outputs/sentiment.json — by call type + weekly trend
- outputs/at_risk_accounts.json — churn-risk leaderboard
- outputs/insights.json — bonus insights
- outputs/charts/*.png — chart pack used in the .pptx
The matching `run_pipeline.py` does the same with a real LLM in the loop and
runs the eval framework comparing pipeline output to these "ground truth"
labels. Two-track design lets you defend both the offline analysis and the
agentic implementation in the Q&A.
"""
from __future__ import annotations
import json
import os
from collections import Counter, defaultdict
from pathlib import Path
from pipeline.data_loader import load_dataset
from pipeline.call_classifier import classify_rule_based
from pipeline.categorizer import (
categorize_from_ground_truth, aggregate_themes,
)
from pipeline.sentiment import (
aggregate_by_call_type, trend_by_week, detect_at_risk_accounts,
)
from pipeline.insights import (
cross_silo_churn, action_item_followup, feature_gap_pressure,
)
from pipeline import visualize as viz
HERE = Path(__file__).parent
DATASET = (HERE.parent / "interview-assignment" / "dataset").resolve()
OUT = HERE / "outputs"
CHARTS = OUT / "charts"
def main():
print(f"Loading dataset from {DATASET} ...")
transcripts = load_dataset(DATASET)
print(f"Loaded {len(transcripts)} transcripts.")
# 1. Call-type classification (rule-based only, no API).
typed = []
for t in transcripts:
ct, reason = classify_rule_based(t)
typed.append({
"meeting_id": t.meeting_id, "title": t.title,
"call_type": ct, "rule_reason": reason,
"sentiment_score": t.ground_truth.sentiment_score,
"start_time": t.start_time,
"duration_min": t.duration_min,
"domains": t.external_domains,
})
# 2. Categorization via ground-truth-projection (lexical map onto taxonomy).
categorized = [categorize_from_ground_truth(t) for t in transcripts]
rollup = aggregate_themes(categorized)
sample_titles = defaultdict(list)
for t, c in zip(transcripts, categorized):
for tag in c.tags:
if len(sample_titles[tag]) < 3:
sample_titles[tag].append(t.title)
cat_out = {
"rollup": rollup,
"sample_titles_per_tag": dict(sample_titles),
"per_call": [
{
"meeting_id": c.meeting_id, "title": c.title,
"tags": c.tags, "primary_theme": c.primary_theme,
"rationale": c.rationale,
}
for c in categorized
],
}
# 3. Sentiment aggregations.
by_type_records = typed
sent_by_type = aggregate_by_call_type([
{"call_type": r["call_type"], "sentiment_score": r["sentiment_score"]}
for r in by_type_records
])
weekly = trend_by_week([
{"start_time": r["start_time"], "sentiment_score": r["sentiment_score"]}
for r in by_type_records
])
sent_out = {
"by_call_type": sent_by_type,
"weekly_trend": weekly,
"all_call_records": [
{**r, "start_time": r["start_time"].isoformat()}
for r in by_type_records
],
}
at_risk = detect_at_risk_accounts(transcripts, min_calls=2)
# 4. Bonus insights.
cross = cross_silo_churn(transcripts)
action_followup = action_item_followup(transcripts)
feature_pressure = feature_gap_pressure(transcripts)
# 5. Key-moment composition by call type — for the chart.
moments_by_type: dict[str, Counter] = defaultdict(Counter)
for t, rec in zip(transcripts, typed):
ct = rec["call_type"]
for km in t.ground_truth.key_moments:
moments_by_type[ct][km.get("type", "unknown")] += 1
# 6. Persist.
OUT.mkdir(exist_ok=True, parents=True)
CHARTS.mkdir(exist_ok=True, parents=True)
(OUT / "call_types.json").write_text(
json.dumps([{**r, "start_time": r["start_time"].isoformat()} for r in typed],
indent=2), encoding="utf-8")
(OUT / "categorization.json").write_text(json.dumps(cat_out, indent=2), encoding="utf-8")
(OUT / "sentiment.json").write_text(json.dumps(sent_out, indent=2), encoding="utf-8")
(OUT / "at_risk_accounts.json").write_text(json.dumps(at_risk, indent=2), encoding="utf-8")
(OUT / "insights.json").write_text(json.dumps({
"cross_silo_churn": cross,
"action_item_followup": action_followup,
"feature_gap_pressure": feature_pressure,
}, indent=2), encoding="utf-8")
# 7. Charts.
viz.chart_call_type_distribution(sent_by_type, CHARTS / "01_call_type_distribution.png")
viz.chart_sentiment_by_call_type(sent_by_type, CHARTS / "02_sentiment_by_call_type.png")
viz.chart_topic_distribution(rollup["tag_counts"], CHARTS / "03_topic_distribution.png")
viz.chart_key_moment_types_by_call_type(moments_by_type, CHARTS / "04_key_moments_by_type.png")
viz.chart_sentiment_trend(weekly, CHARTS / "05_sentiment_trend.png")
viz.chart_at_risk_accounts(at_risk, CHARTS / "06_at_risk_accounts.png")
# 8. Summary print.
print("\n=== CALL-TYPE BREAKDOWN ===")
for k, v in sent_by_type.items():
print(f" {k:10s} n={v['n']:3d} mean={v['mean']:.2f} ±{v['stdev']:.2f}")
print("\n=== TOP THEMES ===")
for t, c in list(rollup["tag_counts"].items())[:6]:
print(f" {c:3d} {t}")
print(f"\n=== AT-RISK ACCOUNTS: {len(at_risk)} flagged ===")
for a in at_risk[:5]:
print(f" {a['domain']:32s} avg={a['mean_sentiment']:.2f} delta={a['trajectory_delta']:+.2f} "
f"reasons={'; '.join(a['risk_reasons'])}")
print(f"\n=== ACTION-ITEM FOLLOW-UP RATE ===")
print(f" {action_followup['followed_up']}/{action_followup['total_action_items']} "
f"= {action_followup['follow_up_rate']:.1%}")
print(f"\n=== FEATURE-GAP CLUSTERS WITH ≥2 OCCURRENCES: {len(feature_pressure)} ===")
for fg in feature_pressure[:3]:
print(f" size={fg['size']} customers={fg['distinct_customers']} "
f"keywords={fg['shared_keywords'][:4]}")
print(f"\n=== CROSS-SILO CHURN (customers in support AND external): {len(cross)} ===")
for c in cross[:5]:
print(f" {c['domain']:32s} risk={c['risk_score']:.1f} "
f"sup={len(c['support_calls'])} ext={len(c['external_calls'])}")
print(f"\nAll outputs written to {OUT}")
if __name__ == "__main__":
main()