File size: 10,117 Bytes
ef78361
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
"""Chart components for Gen3 Nudge Detection Dashboard."""

import plotly.graph_objects as go
import pandas as pd


# =============================================================================
# Color Constants
# =============================================================================

CONFIDENCE_TIER_COLORS = {
    "HIGH": "#EF4444",    # Red
    "MEDIUM": "#F59E0B",  # Amber
    "LOW": "#10B981",     # Green
}

PLATFORM_COLORS = {
    "CHATGPT": "#10A37F",
    "GOOGLE_AI": "#4285F4",
    "GOOGLE_OVERVIEW": "#34A853",
    "PERPLEXITY": "#6366F1",
    "GEMINI": "#8B5CF6",
    "BING": "#00A4EF",
    "CLAUDE": "#D97706",
}

POLARITY_COLORS = {
    "positive": "#10B981",
    "negative": "#EF4444",
    "neutral": "#6B7280",
}

# English to Korean emotion mapping
EMOTION_KO = {
    "admiration": "๊ฐํƒ„",
    "amusement": "์žฌ๋ฏธ",
    "anger": "๋ถ„๋…ธ",
    "annoyance": "์งœ์ฆ",
    "approval": "์ธ์ •",
    "caring": "๋ฐฐ๋ ค",
    "confusion": "ํ˜ผ๋ž€",
    "curiosity": "ํ˜ธ๊ธฐ์‹ฌ",
    "desire": "์š•๊ตฌ",
    "disappointment": "์‹ค๋ง",
    "disapproval": "๋ฐ˜๋Œ€",
    "disgust": "ํ˜์˜ค",
    "embarrassment": "๋‹นํ˜น",
    "excitement": "ํฅ๋ถ„",
    "fear": "๋‘๋ ค์›€",
    "gratitude": "๊ฐ์‚ฌ",
    "grief": "์Šฌํ””",
    "joy": "๊ธฐ์จ",
    "love": "์‚ฌ๋ž‘",
    "nervousness": "๋ถˆ์•ˆ",
    "optimism": "๋‚™๊ด€",
    "pride": "์ž๋ถ€์‹ฌ",
    "realization": "๊นจ๋‹ฌ์Œ",
    "relief": "์•ˆ๋„",
    "remorse": "ํ›„ํšŒ",
    "sadness": "์Šฌํ””",
    "surprise": "๋†€๋ผ์›€",
    "neutral": "์ค‘๋ฆฝ",
    "trust": "์‹ ๋ขฐ",
    "anticipation": "๊ธฐ๋Œ€",
    "interest": "๊ด€์‹ฌ",
    "satisfaction": "๋งŒ์กฑ",
    "frustration": "์ขŒ์ ˆ",
    "hope": "ํฌ๋ง",
    "worry": "๊ฑฑ์ •",
}

# CEJ Korean labels
CEJ_LABELS = {
    "VERIFICATION": "๊ฒ€์ฆ ์งˆ๋ฌธ",
    "INFORMATION_DISCOVERY": "์ •๋ณด ํƒ์ƒ‰",
    "HOW_TO": "์‚ฌ์šฉ๋ฒ•",
    "WHERE_TO_BUY": "๊ตฌ๋งค์ฒ˜",
    "RECOMMENDATION": "์ถ”์ฒœ ์š”์ฒญ",
    "SIDE_EFFECT": "๋ถ€์ž‘์šฉ",
    "MARKET_TRENDS": "์‹œ์žฅ ๋™ํ–ฅ",
    "RESULT_EFFECTIVENESS": "ํšจ๊ณผ/๊ฒฐ๊ณผ",
    "COMPARISON": "๋น„๊ต",
    "INGREDIENT": "์„ฑ๋ถ„",
    "AWARENESS_COMPARISON": "์ธ์ง€/๋น„๊ต",
    "PURCHASE": "๊ตฌ๋งค",
    "POST_PURCHASE": "๊ตฌ๋งค ํ›„",
}


# =============================================================================
# Gen3 Nudge Charts
# =============================================================================

def create_confidence_tier_pie_chart(tier_stats: dict) -> go.Figure:
    """Create pie chart for confidence tier distribution (HIGH/MEDIUM/LOW)."""
    if not tier_stats:
        return go.Figure()

    labels = list(tier_stats.keys())
    values = list(tier_stats.values())
    colors = [CONFIDENCE_TIER_COLORS.get(k, "#6B7280") for k in labels]

    label_map = {
        "HIGH": "๐Ÿ”ด HIGH",
        "MEDIUM": "๐ŸŸก MEDIUM",
        "LOW": "๐ŸŸข LOW",
    }
    display_labels = [label_map.get(k, k) for k in labels]

    fig = go.Figure(data=[go.Pie(
        labels=display_labels,
        values=values,
        marker=dict(colors=colors),
        hole=0.4,
        textinfo="percent+value",
        textposition="outside",
    )])

    fig.update_layout(
        title="",
        showlegend=True,
        legend=dict(orientation="h", yanchor="bottom", y=-0.2, xanchor="center", x=0.5),
        margin=dict(t=20, b=60, l=20, r=20),
        height=280,
    )

    return fig


def create_platform_bar_chart(platform_stats: dict) -> go.Figure:
    """Create horizontal bar chart for platform distribution."""
    if not platform_stats:
        return go.Figure()

    sorted_items = sorted(platform_stats.items(), key=lambda x: x[1], reverse=True)
    platforms = [item[0] for item in sorted_items]
    counts = [item[1] for item in sorted_items]
    colors = [PLATFORM_COLORS.get(p, "#6B7280") for p in platforms]

    fig = go.Figure(data=[
        go.Bar(
            y=platforms,
            x=counts,
            orientation="h",
            marker_color=colors,
            text=[f"{c}๊ฑด" for c in counts],
            textposition="auto",
        )
    ])

    fig.update_layout(
        title="",
        xaxis_title="๋„›์ง€ ํ›„๋ณด ์ˆ˜",
        yaxis=dict(autorange="reversed"),
        margin=dict(t=20, b=40, l=100, r=20),
        height=max(200, len(platforms) * 35),
    )

    return fig


def create_nudge_by_cej_bar_chart(cej_counts: dict) -> go.Figure:
    """Create horizontal bar chart for nudge distribution by CEJ stage."""
    if not cej_counts:
        return go.Figure()

    sorted_items = sorted(cej_counts.items(), key=lambda x: x[1], reverse=True)
    cej_stages = [item[0] for item in sorted_items]
    counts = [item[1] for item in sorted_items]

    display_labels = [CEJ_LABELS.get(cej, cej) for cej in cej_stages]

    fig = go.Figure(data=[
        go.Bar(
            y=display_labels,
            x=counts,
            orientation="h",
            marker_color="#6366F1",
            text=[f"{c}๊ฑด" for c in counts],
            textposition="auto",
        )
    ])

    fig.update_layout(
        title="",
        xaxis_title="๋„›์ง€ ํ›„๋ณด ์ˆ˜",
        yaxis=dict(autorange="reversed"),
        margin=dict(t=20, b=40, l=100, r=20),
        height=max(200, len(cej_stages) * 35),
    )

    return fig


def create_brand_sentiment_chart(brands: list[dict]) -> go.Figure:
    """Create horizontal stacked bar chart for brand sentiment comparison."""
    if not brands:
        return go.Figure()

    # Sort by total mentions
    sorted_brands = sorted(brands, key=lambda x: x.get("total_mentions", 0), reverse=True)[:10]

    brand_names = []
    for b in sorted_brands:
        name = b.get("brand_name", "Unknown")
        brand_type = b.get("brand_type", "")
        prefix = "๐Ÿ  " if brand_type == "IN_HOUSE" else "๐Ÿข "
        brand_names.append(f"{prefix}{name}")

    positive_rates = [b.get("positive_rate", 0) for b in sorted_brands]
    negative_rates = [b.get("negative_rate", 0) for b in sorted_brands]
    neutral_rates = [100 - p - n for p, n in zip(positive_rates, negative_rates)]

    fig = go.Figure()

    fig.add_trace(go.Bar(
        y=brand_names,
        x=positive_rates,
        name="๊ธ์ •",
        orientation="h",
        marker_color="#10B981",
        text=[f"{v:.1f}%" for v in positive_rates],
        textposition="inside",
    ))

    fig.add_trace(go.Bar(
        y=brand_names,
        x=neutral_rates,
        name="์ค‘๋ฆฝ",
        orientation="h",
        marker_color="#E5E7EB",
        text=[f"{v:.1f}%" for v in neutral_rates],
        textposition="inside",
    ))

    fig.add_trace(go.Bar(
        y=brand_names,
        x=negative_rates,
        name="๋ถ€์ •",
        orientation="h",
        marker_color="#EF4444",
        text=[f"{v:.1f}%" for v in negative_rates],
        textposition="inside",
    ))

    fig.update_layout(
        title="",
        barmode="stack",
        xaxis_title="๋น„์œจ (%)",
        legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1),
        margin=dict(t=50, b=50, l=150, r=20),
        height=max(300, len(sorted_brands) * 40),
        yaxis=dict(autorange="reversed"),
    )

    return fig


def create_domain_bar_chart(domain_counts: dict) -> go.Figure:
    """Create horizontal bar chart for citation domain distribution."""
    if not domain_counts:
        return go.Figure()

    # Take top 15 domains
    sorted_items = list(domain_counts.items())[:15]
    domains = [item[0] for item in sorted_items]
    counts = [item[1] for item in sorted_items]

    # Truncate long domain names
    display_domains = [d[:40] + "..." if len(d) > 40 else d for d in domains]

    fig = go.Figure(data=[
        go.Bar(
            y=display_domains,
            x=counts,
            orientation="h",
            marker_color="#3B82F6",
            text=[f"{c}ํšŒ" for c in counts],
            textposition="auto",
        )
    ])

    fig.update_layout(
        title="",
        xaxis_title="์ธ์šฉ ํšŸ์ˆ˜",
        yaxis=dict(autorange="reversed"),
        margin=dict(t=20, b=40, l=200, r=20),
        height=max(300, len(sorted_items) * 30),
    )

    return fig


def create_bit_quadrant_chart(bit_stats: dict) -> go.Figure:
    """Create bar chart for BIT quadrant distribution."""
    if not bit_stats:
        return go.Figure()

    BIT_LABELS = {
        "neutral": "์ค‘๋ฆฝ",
        "product_satisfaction": "์ œํ’ˆ ๋งŒ์กฑ",
        "unmet_expectations": "๊ธฐ๋Œ€ ๋ฏธ์ถฉ์กฑ",
        "brand_trust": "๋ธŒ๋žœ๋“œ ์‹ ๋ขฐ",
    }

    sorted_items = sorted(bit_stats.items(), key=lambda x: x[1], reverse=True)
    quadrants = [BIT_LABELS.get(item[0], item[0]) for item in sorted_items]
    counts = [item[1] for item in sorted_items]

    colors = ["#6366F1", "#8B5CF6", "#A855F7", "#D946EF"]

    fig = go.Figure(data=[
        go.Bar(
            x=quadrants,
            y=counts,
            marker_color=colors[:len(quadrants)],
            text=[f"{c}๊ฑด" for c in counts],
            textposition="outside",
        )
    ])

    fig.update_layout(
        title="",
        yaxis_title="๋„›์ง€ ํ›„๋ณด ์ˆ˜",
        margin=dict(t=20, b=50, l=50, r=20),
        height=280,
    )

    return fig


def create_emotion_distribution_chart(emotion_stats: dict) -> go.Figure:
    """Create bar chart for emotion distribution in nudge candidates."""
    if not emotion_stats:
        return go.Figure()

    sorted_items = sorted(emotion_stats.items(), key=lambda x: x[1], reverse=True)[:8]
    emotions = [EMOTION_KO.get(item[0], item[0]) for item in sorted_items]
    counts = [item[1] for item in sorted_items]

    colors = ["#6366F1", "#8B5CF6", "#A855F7", "#D946EF", "#EC4899", "#F43F5E", "#F97316", "#FBBF24"]

    fig = go.Figure(data=[
        go.Bar(
            x=emotions,
            y=counts,
            marker_color=colors[:len(emotions)],
            text=[f"{c}๊ฑด" for c in counts],
            textposition="outside",
        )
    ])

    fig.update_layout(
        title="",
        yaxis_title="๋„›์ง€ ํ›„๋ณด ์ˆ˜",
        xaxis_tickangle=-30,
        margin=dict(t=20, b=80, l=50, r=20),
        height=280,
    )

    return fig