File size: 6,060 Bytes
2612bdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# src/churn_extractor.py
# Churn Signal Extractor — Sentiment + Pattern Analysis
# SupportMind v1.0 — Asmitha

import re
import logging
from typing import Dict, List

logger = logging.getLogger(__name__)

try:
    from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
    HAS_VADER = True
except ImportError:
    HAS_VADER = False

COMPETITOR_PATTERNS = [
    r'switch(?:ing)? to', r'moving to', r'looking at (?:alternatives?|competitors?|other\s+(?:tools?|platforms?|solutions?|vendors?))',
    r'competitor', r'alternative', r'another (?:tool|platform|solution)',
    r'better option', r'other providers',
]

CANCELLATION_PATTERNS = [
    r'cancel', r'stop (?:using|subscription)', r'end (?:my )?contract',
    r'not renew(?:ing)?', r'downgrad(?:e|ing)', r'close (?:my )?account',
    r'terminate', r'discontinue', r'opt out',
]

FRUSTRATION_PATTERNS = [
    r'very frustrated', r'completely broken', r'this is unacceptable',
    r'third time', r'again\b', r'still not (?:fixed|working|resolved)',
    r'waste of time', r'terrible', r'awful', r'disgusted',
    r'fed up', r'last straw', r'ridiculous',
]

URGENCY_PATTERNS = [
    r'asap', r'urgent(?:ly)?', r'immediately', r'critical',
    r'blocking', r'production (?:is )?down', r'outage',
    r'deadline', r'cannot wait',
]


class ChurnSignalExtractor:
    """
    Extracts churn risk signals from support thread history.
    
    Scans for competitor mentions, cancellation language, frustration
    patterns, and sentiment trajectory. Produces a composite churn
    risk score [0–1] for CRM health record updates.
    """

    def __init__(self):
        if HAS_VADER:
            self.analyzer = SentimentIntensityAnalyzer()
        else:
            self.analyzer = None
            logger.warning("VADER not installed. Using basic sentiment heuristic.")

    def _get_sentiment(self, text: str) -> float:
        """Get sentiment score from -1.0 (negative) to 1.0 (positive)."""
        if self.analyzer:
            return self.analyzer.polarity_scores(text)['compound']
        # Basic fallback
        neg_words = ['bad', 'terrible', 'awful', 'broken', 'frustrated',
                     'angry', 'worst', 'hate', 'useless', 'horrible']
        pos_words = ['good', 'great', 'love', 'excellent', 'amazing',
                     'helpful', 'perfect', 'thanks', 'wonderful']
        text_lower = text.lower()
        neg = sum(1 for w in neg_words if w in text_lower)
        pos = sum(1 for w in pos_words if w in text_lower)
        total = neg + pos
        if total == 0:
            return 0.0
        return (pos - neg) / total

    def extract(self, thread_texts: List[str]) -> Dict:
        """
        Extract churn signals from a support thread.
        
        Args:
            thread_texts: List of message strings in the support thread
            
        Returns:
            Dictionary with churn_risk_score, flags, and details
        """
        full_text = ' '.join(thread_texts).lower()

        # Pattern matching
        competitor = any(re.search(p, full_text) for p in COMPETITOR_PATTERNS)
        cancellation = any(re.search(p, full_text) for p in CANCELLATION_PATTERNS)
        frustration = sum(1 for p in FRUSTRATION_PATTERNS if re.search(p, full_text))
        urgency = sum(1 for p in URGENCY_PATTERNS if re.search(p, full_text))

        # Sentiment trajectory (across messages)
        sentiments = [self._get_sentiment(t) for t in thread_texts[:10]]
        neg_count = sum(1 for s in sentiments if s < -0.3)
        avg_sentiment = sum(sentiments) / max(len(sentiments), 1)

        # Sentiment trajectory: is it getting worse?
        if len(sentiments) >= 3:
            early = sum(sentiments[:len(sentiments)//2]) / max(len(sentiments)//2, 1)
            late = sum(sentiments[len(sentiments)//2:]) / max(len(sentiments) - len(sentiments)//2, 1)
            deteriorating = late < early - 0.2
        else:
            deteriorating = False

        # Composite churn risk score [0–1]
        score = min(1.0,
            (0.40 if cancellation else 0.0) +
            (0.30 if competitor else 0.0) +
            min(frustration * 0.10, 0.20) +
            (neg_count / max(len(sentiments), 1)) * 0.10 +
            (0.10 if deteriorating else 0.0)
        )

        risk_level = 'critical' if score >= 0.7 else 'high' if score >= 0.5 else 'medium' if score >= 0.3 else 'low'

        return {
            'churn_risk_score': round(score, 3),
            'risk_level': risk_level,
            'competitor_mention': competitor,
            'cancellation_language': cancellation,
            'frustration_count': frustration,
            'urgency_count': urgency,
            'negative_sentiment_ratio': round(neg_count / max(len(sentiments), 1), 3),
            'average_sentiment': round(avg_sentiment, 3),
            'sentiment_deteriorating': deteriorating,
            'message_count': len(thread_texts),
            'recommendation': self._get_recommendation(score, competitor, cancellation),
        }

    def _get_recommendation(self, score: float, competitor: bool, cancellation: bool) -> str:
        if score >= 0.7:
            return 'IMMEDIATE escalation to Customer Success Manager'
        if cancellation:
            return 'Route to retention team with priority flag'
        if competitor:
            return 'Alert Account Manager — competitive threat detected'
        if score >= 0.4:
            return 'Flag for proactive outreach within 24 hours'
        return 'Standard processing — monitor sentiment'


if __name__ == '__main__':
    extractor = ChurnSignalExtractor()
    thread = [
        "Hi, I've been having issues with the export feature for two weeks now.",
        "This is the third time I'm reporting this. Still not fixed.",
        "I'm very frustrated. We're looking at alternative solutions.",
        "If this isn't resolved by Friday, we'll need to cancel our subscription.",
    ]
    result = extractor.extract(thread)
    for k, v in result.items():
        print(f"  {k}: {v}")