File size: 11,732 Bytes
69aa668
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import plotly.graph_objects as go
import plotly.express as px
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import io
import base64
from typing import List, Dict
import numpy as np
import logging

logger = logging.getLogger(__name__)


class VisualizationGenerator:
    """
    Generate various visualizations for emotion analysis
    """
    
    def __init__(self):
        """Initialize visualization generator"""
        self.emotion_colors = {
            'joy': '#FFD700',
            'sadness': '#4682B4',
            'anger': '#DC143C',
            'fear': '#483D8B',
            'surprise': '#00CED1',
            'neutral': '#A9A9A9',
            'disgust': '#556B2F'
        }
    
    def create_visualizations(self, results: List[Dict]) -> Dict:
        """
        Create all visualizations from emotion analysis results
        
        Args:
            results: List of emotion detection results
            
        Returns:
            Dictionary containing all visualization data
        """
        try:
            # Aggregate emotions
            aggregated = self._aggregate_results(results)
            
            # Create various charts
            emotion_pie = self._create_emotion_pie(aggregated['emotions'])
            emotion_bar = self._create_emotion_bar(aggregated['emotions'])
            sentiment_gauge = self._create_sentiment_gauge(aggregated['sentiment_score'])
            timeline = self._create_emotion_timeline(results)
            radar_chart = self._create_emotion_radar(aggregated['emotions'])
            
            return {
                "emotion_pie": emotion_pie,
                "emotion_bar": emotion_bar,
                "sentiment_gauge": sentiment_gauge,
                "emotion_timeline": timeline,
                "emotion_radar": radar_chart,
                "statistics": {
                    "total_texts": len(results),
                    "dominant_emotion": aggregated['dominant_emotion'],
                    "avg_sentiment": aggregated['sentiment_score'],
                    "sentiment_label": aggregated['sentiment_label']
                }
            }
            
        except Exception as e:
            logger.error(f"Error creating visualizations: {str(e)}")
            return {}
    
    def _aggregate_results(self, results: List[Dict]) -> Dict:
        """Aggregate multiple emotion results"""
        if not results:
            return {}
        
        # Initialize
        emotion_sums = {}
        sentiment_sum = 0.0
        
        # Aggregate
        for result in results:
            for emotion, score in result['emotions'].items():
                emotion_sums[emotion] = emotion_sums.get(emotion, 0) + score
            sentiment_sum += result.get('sentiment_score', 0)
        
        # Calculate averages
        n = len(results)
        emotions_avg = {emotion: score / n for emotion, score in emotion_sums.items()}
        
        # Get dominant
        dominant = max(emotions_avg.items(), key=lambda x: x[1])
        
        # Sentiment
        avg_sentiment = sentiment_sum / n
        sentiment_label = "positive" if avg_sentiment > 0.1 else "negative" if avg_sentiment < -0.1 else "neutral"
        
        return {
            "emotions": emotions_avg,
            "dominant_emotion": dominant[0],
            "sentiment_score": avg_sentiment,
            "sentiment_label": sentiment_label
        }
    
    def _create_emotion_pie(self, emotions: Dict[str, float]) -> str:
        """Create a pie chart of emotion distribution"""
        try:
            # Filter significant emotions
            significant = {k: v for k, v in emotions.items() if v > 0.05}
            
            if not significant:
                significant = emotions
            
            # Create pie chart
            fig = go.Figure(data=[go.Pie(
                labels=[e.capitalize() for e in significant.keys()],
                values=list(significant.values()),
                marker=dict(colors=[self.emotion_colors.get(e, '#808080') for e in significant.keys()]),
                hole=0.3,
                textinfo='label+percent',
                textfont=dict(size=14),
                hovertemplate='<b>%{label}</b><br>%{percent}<extra></extra>'
            )])
            
            fig.update_layout(
                title={
                    'text': 'Emotion Distribution',
                    'x': 0.5,
                    'xanchor': 'center',
                    'font': {'size': 20, 'color': '#333'}
                },
                showlegend=True,
                height=400,
                paper_bgcolor='rgba(0,0,0,0)',
                plot_bgcolor='rgba(0,0,0,0)',
                font=dict(family="Arial, sans-serif")
            )
            
            return fig.to_json()
            
        except Exception as e:
            logger.error(f"Error creating pie chart: {str(e)}")
            return "{}"
    
    def _create_emotion_bar(self, emotions: Dict[str, float]) -> str:
        """Create a bar chart of emotions"""
        try:
            # Sort emotions
            sorted_emotions = sorted(emotions.items(), key=lambda x: x[1], reverse=True)
            
            labels = [e[0].capitalize() for e in sorted_emotions]
            values = [e[1] * 100 for e in sorted_emotions]
            colors = [self.emotion_colors.get(e[0], '#808080') for e in sorted_emotions]
            
            fig = go.Figure(data=[go.Bar(
                x=labels,
                y=values,
                marker=dict(color=colors),
                text=[f'{v:.1f}%' for v in values],
                textposition='auto',
                hovertemplate='<b>%{x}</b><br>%{y:.1f}%<extra></extra>'
            )])
            
            fig.update_layout(
                title={
                    'text': 'Emotion Intensity',
                    'x': 0.5,
                    'xanchor': 'center',
                    'font': {'size': 20, 'color': '#333'}
                },
                xaxis_title='Emotion',
                yaxis_title='Intensity (%)',
                height=400,
                paper_bgcolor='rgba(0,0,0,0)',
                plot_bgcolor='rgba(0,0,0,0)',
                font=dict(family="Arial, sans-serif"),
                yaxis=dict(gridcolor='#E5E5E5')
            )
            
            return fig.to_json()
            
        except Exception as e:
            logger.error(f"Error creating bar chart: {str(e)}")
            return "{}"
    
    def _create_sentiment_gauge(self, sentiment_score: float) -> str:
        """Create a gauge chart for sentiment"""
        try:
            # Normalize to 0-100 scale
            gauge_value = (sentiment_score + 1) * 50
            
            # Determine color
            if sentiment_score > 0.1:
                color = '#28a745'
            elif sentiment_score < -0.1:
                color = '#dc3545'
            else:
                color = '#ffc107'
            
            fig = go.Figure(go.Indicator(
                mode="gauge+number+delta",
                value=gauge_value,
                domain={'x': [0, 1], 'y': [0, 1]},
                title={'text': "Sentiment Score", 'font': {'size': 20}},
                gauge={
                    'axis': {'range': [0, 100], 'tickwidth': 1},
                    'bar': {'color': color},
                    'steps': [
                        {'range': [0, 33], 'color': '#ffebee'},
                        {'range': [33, 66], 'color': '#fff9e6'},
                        {'range': [66, 100], 'color': '#e8f5e9'}
                    ],
                    'threshold': {
                        'line': {'color': "red", 'width': 4},
                        'thickness': 0.75,
                        'value': 50
                    }
                }
            ))
            
            fig.update_layout(
                height=300,
                paper_bgcolor='rgba(0,0,0,0)',
                font=dict(family="Arial, sans-serif")
            )
            
            return fig.to_json()
            
        except Exception as e:
            logger.error(f"Error creating gauge: {str(e)}")
            return "{}"
    
    def _create_emotion_timeline(self, results: List[Dict]) -> str:
        """Create a timeline of emotion changes"""
        try:
            if len(results) < 2:
                return "{}"
            
            # Extract emotion data over time
            emotions_over_time = {}
            for emotion in results[0]['emotions'].keys():
                emotions_over_time[emotion] = [r['emotions'][emotion] * 100 for r in results]
            
            # Create line chart
            fig = go.Figure()
            
            for emotion, values in emotions_over_time.items():
                if max(values) > 5:  # Only show significant emotions
                    fig.add_trace(go.Scatter(
                        x=list(range(1, len(values) + 1)),
                        y=values,
                        mode='lines+markers',
                        name=emotion.capitalize(),
                        line=dict(color=self.emotion_colors.get(emotion, '#808080'), width=2),
                        marker=dict(size=6)
                    ))
            
            fig.update_layout(
                title={
                    'text': 'Emotion Timeline',
                    'x': 0.5,
                    'xanchor': 'center',
                    'font': {'size': 20, 'color': '#333'}
                },
                xaxis_title='Text Entry',
                yaxis_title='Emotion Intensity (%)',
                height=400,
                paper_bgcolor='rgba(0,0,0,0)',
                plot_bgcolor='rgba(0,0,0,0)',
                font=dict(family="Arial, sans-serif"),
                hovermode='x unified',
                yaxis=dict(gridcolor='#E5E5E5'),
                xaxis=dict(gridcolor='#E5E5E5')
            )
            
            return fig.to_json()
            
        except Exception as e:
            logger.error(f"Error creating timeline: {str(e)}")
            return "{}"
    
    def _create_emotion_radar(self, emotions: Dict[str, float]) -> str:
        """Create a radar chart of emotions"""
        try:
            categories = [e.capitalize() for e in emotions.keys()]
            values = [v * 100 for v in emotions.values()]
            
            fig = go.Figure()
            
            fig.add_trace(go.Scatterpolar(
                r=values,
                theta=categories,
                fill='toself',
                fillcolor='rgba(75, 192, 192, 0.2)',
                line=dict(color='rgb(75, 192, 192)', width=2),
                marker=dict(size=8)
            ))
            
            fig.update_layout(
                polar=dict(
                    radialaxis=dict(
                        visible=True,
                        range=[0, max(values) * 1.2] if max(values) > 0 else [0, 100],
                        gridcolor='#E5E5E5'
                    ),
                    angularaxis=dict(gridcolor='#E5E5E5')
                ),
                title={
                    'text': 'Emotion Profile',
                    'x': 0.5,
                    'xanchor': 'center',
                    'font': {'size': 20, 'color': '#333'}
                },
                height=400,
                paper_bgcolor='rgba(0,0,0,0)',
                font=dict(family="Arial, sans-serif"),
                showlegend=False
            )
            
            return fig.to_json()
            
        except Exception as e:
            logger.error(f"Error creating radar chart: {str(e)}")
            return "{}"