File size: 1,555 Bytes
42cbd9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
from flask import jsonify

# 🔹 Generate Tags from Text
def generate_tags(content):
    stop_words = {"the", "and", "is", "in", "to", "a", "of", "on", "for"}
    words = content.lower().split()
    tags = [word for word in words if word not in stop_words and len(word) > 3]
    return list(set(tags))

# 🔹 Parse JSON Responses
def parse_json(response):
    try:
        return json.loads(response)
    except json.JSONDecodeError:
        return None

# 🔹 Error Handlers
def error_response(message, status_code):
    return jsonify({"error": message}), status_code

# Update emotion categorization mapping
EMOTION_CATEGORIES = {
    "goal-oriented": ["desire", "anticipation", "optimism"],
    "social": ["gratitude", "admiration", "love"],
    "reflective": ["remorse", "sadness", "disappointment"],
    "urgent": ["fear", "nervousness", "surprise"],
    "critical": ["anger", "disgust", "annoyance"],
    "joyful": ["joy", "excitement", "amusement"]
}

SENTIMENT_MAP = {
    "LABEL_0": "negative",
    "LABEL_1": "neutral",
    "LABEL_2": "positive"
}

def categorize_memory(emotions, sentiment):
    """Improved categorization with fallback logic"""
    if not emotions:
        return f"uncategorized-{sentiment['label']}"

    # Find direct matches
    for emotion in emotions:
        for category, keywords in EMOTION_CATEGORIES.items():
            if emotion['label'] in keywords:
                return f"{category}-{sentiment['label']}"

    # Fallback to sentiment-based category
    return f"neutral-{sentiment['label']}"