File size: 3,495 Bytes
0afe5c5
 
f89ae58
0afe5c5
 
 
 
 
 
 
 
 
 
f89ae58
 
0afe5c5
 
 
f89ae58
0afe5c5
 
f89ae58
0afe5c5
 
f89ae58
 
0afe5c5
 
 
 
 
 
 
 
 
 
 
 
f89ae58
0afe5c5
 
 
 
 
f89ae58
0afe5c5
 
f89ae58
0afe5c5
f89ae58
0afe5c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f89ae58
0afe5c5
 
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
def map_emotion(emotion: str) -> str:
    emotion = emotion.lower()

    if emotion in ["happy", "surprised"]:
        return "Happy"
    elif emotion == "sad":
        return "Sad"
    elif emotion in ["angry", "disgust", "disgusted", "fearful"]:
        return "Angry"
    elif emotion in ["neutral", "other", "unknown"]:
        return "Neutral"
    else:
        return "Neutral"


def fix_imbalance(scores: dict) -> str:
    if not scores:
        return "Neutral"

    if scores.get("neutral", 0.0) >= 0.70:
        return "Neutral"

    top_emotion = max(scores, key=scores.get)
    return map_emotion(top_emotion)


def apply_final_emotion(results: list) -> list:
    emotion_keys = [
        "angry",
        "disgust",
        "fearful",
        "happy",
        "neutral",
        "other",
        "sad",
        "surprised",
        "unknown",
    ]

    processed_results = []

    for item in results:
        scores = {key: item.get(key, 0.0) for key in emotion_keys}
        final_emotion = fix_imbalance(scores)

        processed_item = item.copy()
        processed_item["final_emotion"] = final_emotion

        processed_results.append(processed_item)

    return processed_results


if __name__ == "__main__":
    sungjae_results = [
        {
            "start": 0.0,
            "end": 3.0,
            "text": "์•ˆ๋…•ํ•˜์„ธ์š”",
            "angry": 0.1,
            "disgust": 0.2,
            "fearful": 0.3,
            "happy": 0.4,
            "neutral": 0.5,
            "other": 0.6,
            "sad": 0.7,
            "surprised": 0.8,
            "unknown": 0.9,
        },
        {
            "start": 3.0,
            "end": 6.0,
            "text": "๋ฐ˜๊ฐ‘์Šต๋‹ˆ๋‹ค",
            "angry": 0.1,
            "disgust": 0.2,
            "fearful": 0.3,
            "happy": 0.4,
            "neutral": 0.8,
            "other": 0.1,
            "sad": 0.2,
            "surprised": 0.3,
            "unknown": 0.1,
        },
        {
            "start": 6.0,
            "end": 9.0,
            "text": "ํ™”๊ฐ€ ๋‚ฉ๋‹ˆ๋‹ค",
            "angry": 0.75,
            "disgust": 0.1,
            "fearful": 0.05,
            "happy": 0.02,
            "neutral": 0.1,
            "other": 0.01,
            "sad": 0.03,
            "surprised": 0.04,
            "unknown": 0.02,
        },
        {
            "start": 9.0,
            "end": 12.0,
            "text": "๊ธฐ๋ถ„์ด ์ข‹์Šต๋‹ˆ๋‹ค",
            "angry": 0.02,
            "disgust": 0.01,
            "fearful": 0.03,
            "happy": 0.65,
            "neutral": 0.2,
            "other": 0.05,
            "sad": 0.04,
            "surprised": 0.7,
            "unknown": 0.01,
        },
    ]

    result = apply_final_emotion(sungjae_results)

    print("=== integration test result ===")
    for item in result:
        print(
            {
                "start": item["start"],
                "end": item["end"],
                "text": item["text"],
                "final_emotion": item["final_emotion"],
            }
        )

    print("\n=== mapping table ===")
    mapping_table = {
        "happy": "Happy",
        "surprised": "Happy",
        "sad": "Sad",
        "angry": "Angry",
        "disgust": "Angry",
        "disgusted": "Angry",
        "fearful": "Angry",
        "neutral": "Neutral",
        "other": "Neutral",
        "unknown": "Neutral",
    }

    for emotion, mapped_emotion in mapping_table.items():
        print(f"{emotion} -> {mapped_emotion}")