File size: 2,060 Bytes
d1f3f31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
def calculate_intent_score(raw_features: list) -> float:
    """
    Transforms LLaMA qualitative INTENT/DECISION tags into quantitative score.
    """
    intent_mapping = {
        "strong buying intent": 1.0,
        "ready to purchase": 1.0,
        "high interest": 0.8,
        "warm lead": 0.6,
        "interested": 0.5,
        "comparison shopper": 0.4,
        "curious": 0.3,
        "price sensitive": 0.3,
        "hesitant buyer": 0.2,
        "low interest": 0.1
    }
    
    decision_mapping = {
        "converted": 1.0,
        "ready to purchase": 1.0,
        "near purchase": 0.9,
        "negotiation": 0.8,
        "evaluating": 0.6,
        "comparing alternatives": 0.5,
        "considering": 0.5,
        "budget discussion": 0.4,
        "exploring": 0.3,
        "awareness": 0.2,
        "follow-up required": 0.4,
        "purchase delayed": 0.1,
        "dropped": 0.0
    }
    
    intent_val = 0.5 # default
    
    for f in raw_features:
        label = f.get("label", "")
        name = str(f.get("value", f.get("name", ""))).lower()
        
        if label == "INTENT":
            intent_val = max(intent_val, intent_mapping.get(name, 0.5))
        elif label == "DECISION_STAGE":
            intent_val = max(intent_val, decision_mapping.get(name, 0.5))
            
    return intent_val

def calculate_emotion_score(sentiment_score: float, raw_features: list) -> float:
    """
    Combines VADER sentiment (-1 to 1) with LLaMA emotional tags to output a 0 to 1 score.
    """
    # Normalize VADER to 0-1
    base_emotion = (sentiment_score + 1) / 2
    
    emotion_val = base_emotion
    for f in raw_features:
        if f.get("label") == "EMOTIONAL_CONFIDENCE":
            val = str(f.get("value", "")).lower()
            if "high" in val or "strong" in val or "positive" in val:
                emotion_val = min(1.0, emotion_val + 0.2)
            elif "low" in val or "weak" in val or "negative" in val:
                emotion_val = max(0.0, emotion_val - 0.2)
                
    return emotion_val