Natwar commited on
Commit
2c73ed4
·
verified ·
1 Parent(s): c3f5238

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +313 -148
app.py CHANGED
@@ -68,9 +68,53 @@ EMOTION_COLORS = {
68
  'sarcasm': '#FF7F50' # Coral
69
  }
70
 
71
- # Common positive and negative words for context analysis
72
- POSITIVE_WORDS = ['great', 'good', 'wonderful', 'amazing', 'excellent', 'fantastic', 'terrific', 'perfect', 'lovely', 'awesome']
73
- NEGATIVE_WORDS = ['bad', 'terrible', 'awful', 'horrible', 'poor', 'dreadful', 'disappointing', 'unpleasant', 'lousy', 'pathetic']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  # Load BERT model and tokenizer
76
  print("Loading BERT model and tokenizer (this may take a moment)...")
@@ -83,32 +127,32 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
83
  model = model.to(device)
84
  print(f"Model loaded successfully. Using device: {device}")
85
 
86
- # Sarcasm indicators - carefully revised linguistic patterns that indicate sarcasm
87
  SARCASM_PATTERNS = [
88
- # Exaggerated expressions with specific punctuation/capitalization patterns
89
- r'(?i)\b(?:so+|really|absolutely|totally|completely)\s+(?:thrilled|excited|happy|delighted)\s+(?:about|with|by)\b.*?(?:\!{2,}|\?{2,})',
90
 
91
  # Classic sarcastic phrases
92
- r'(?i)\bjust\s+what\s+(?:I|we)\s+(?:need|wanted|hoped for)\b',
93
- r'(?i)\bhow\s+(?:wonderful|nice|great|lovely|exciting)\b.*?(?:\!|\?{2,})',
94
 
95
- # Contrasting statements
96
- r'(?i)\b(?:love|enjoy|adore)\b.*?\bnot\b',
97
 
98
  # Quotation marks around positive words (scare quotes)
99
  r'(?i)"(?:great|wonderful|excellent|perfect|amazing)"',
100
 
101
  # Typical sarcastic responses
102
- r'(?i)^\s*(?:yeah|sure|right)\s+(?:ok|okay|whatever)\b',
103
 
104
  # Exaggerated praise in negative context
105
- r'(?i)\b(?:brilliant|genius|impressive)\b.*?(?:terrible|awful|disaster|failure)',
106
 
107
  # Obvious understatements
108
- r'(?i)\bslightly\s+(?:catastrophic|disastrous|terrible|awful)\b',
109
 
110
- # Emphasis on positive with hint of negative (requires context check)
111
- r'(?i)\bso+\s+(?:happy|excited|thrilled|glad)'
112
  ]
113
 
114
  def tokenize_and_clean(text):
@@ -120,12 +164,49 @@ def tokenize_and_clean(text):
120
  tokens = re.findall(r'\b\w+\b', text)
121
  return tokens
122
 
123
- def count_sentiment_words(text):
124
- """Count positive and negative words in text"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  tokens = tokenize_and_clean(text)
126
- positive_count = sum(1 for word in tokens if word in POSITIVE_WORDS)
127
- negative_count = sum(1 for word in tokens if word in NEGATIVE_WORDS)
128
- return positive_count, negative_count
 
 
 
 
 
 
 
 
 
129
 
130
  def detect_sarcasm_patterns(text):
131
  """Detect linguistic patterns of sarcasm in text with context awareness"""
@@ -138,68 +219,80 @@ def detect_sarcasm_patterns(text):
138
  matches += 1
139
  pattern_matches.append(pattern)
140
 
141
- # Check for sentiment polarity mismatch
142
- positive_count, negative_count = count_sentiment_words(text)
 
 
 
 
143
 
144
- # Context-based signals
145
- exclamation_count = text.count('!')
146
- question_marks = text.count('?')
147
 
148
- # Check for positive words in negative contexts or vice versa
149
- sentiment_mismatch = 0
150
- if positive_count > 0 and negative_count > 0:
151
- # If both positive and negative words exist, it's a potential indicator
152
- sentiment_mismatch = min(positive_count, negative_count) / max(positive_count, negative_count, 1)
153
 
154
- # Check for excessive punctuation - potential sarcasm indicator
155
- excessive_punctuation = 0
156
- if exclamation_count > 2 or question_marks > 2:
157
- excessive_punctuation = 0.2
158
 
159
- # Check for ALL CAPS words (excluding common acronyms)
160
- caps_words = re.findall(r'\b[A-Z]{3,}\b', text)
161
- caps_emphasis = len(caps_words) * 0.1 # Each caps word adds weight
 
 
 
162
 
163
- # Combined sarcasm score
164
- raw_score = (matches * 0.15) + (sentiment_mismatch * 0.5) + excessive_punctuation + caps_emphasis
 
 
 
165
 
166
  # Normalize to [0, 1]
167
- return min(raw_score, 1.0)
168
 
169
- def detect_extreme_incongruity(text):
170
- """Detect extreme incongruity between sentiment and content"""
171
- # Count positive and negative words
172
- positive_count, negative_count = count_sentiment_words(text)
173
-
174
- # Check for specific incongruous phrases
175
- incongruous_phrases = [
176
- (r'(?i)\b(?:love|adore|enjoy)\b.*?\b(?:hate|despise|detest)\b', 0.7), # "I love how much I hate this"
177
- (r'(?i)\b(?:wonderful|great|excellent)\b.*?\b(?:terrible|awful|horrible)\b', 0.8), # "What a wonderful disaster"
178
- (r'(?i)\b(?:thankful|grateful)\b.*?\b(?:worst|annoying|frustrating)\b', 0.6), # "So thankful for this frustrating experience"
179
- ]
180
 
181
- incongruity_score = 0
182
- for pattern, weight in incongruous_phrases:
183
- if re.search(pattern, text):
184
- incongruity_score += weight
 
 
 
 
185
 
186
- # Check for extreme emotional inconsistency
187
- if positive_count > 2 and negative_count > 2:
188
- # Significant presence of both positive and negative sentiment is suspicious
189
- incongruity_score += 0.4
 
 
 
 
 
 
 
190
 
191
- return min(incongruity_score, 1.0)
 
 
 
192
 
193
- def create_emotion_template(emotion, keyword):
194
  """Create a template sentence for emotion prediction"""
195
  templates = [
196
  f"The text expresses [MASK] {emotion} emotions.",
197
- f"This text shows [MASK] {emotion} feelings.",
198
  f"The writer feels [MASK] {keyword}.",
199
  f"The sentiment in this text is [MASK] {keyword}."
200
  ]
201
 
202
- # Use a consistent template for now, but this could be randomized
203
  return templates[0]
204
 
205
  def predict_masked_token(text, template):
@@ -228,7 +321,7 @@ def predict_masked_token(text, template):
228
  return probs
229
 
230
  def get_emotion_score(text, emotion, keywords):
231
- """Calculate emotion score based on multiple template predictions"""
232
  # Positive and negative indicator tokens
233
  positive_indicators = ['clearly', 'definitely', 'strongly', 'very', 'extremely']
234
  negative_indicators = ['not', 'barely', 'hardly', 'slightly', 'somewhat']
@@ -238,7 +331,7 @@ def get_emotion_score(text, emotion, keywords):
238
 
239
  # Use a subset of keywords for efficiency
240
  for keyword in keywords[:5]: # Use just 5 keywords per emotion for efficiency
241
- template = create_emotion_template(emotion, keyword)
242
  probs = predict_masked_token(text, template)
243
 
244
  # Get token IDs for positive and negative words
@@ -253,8 +346,35 @@ def get_emotion_score(text, emotion, keywords):
253
  score = positive_score - negative_score
254
  keyword_scores.append(score)
255
 
256
- # Return average score across all keywords
257
- return sum(keyword_scores) / len(keyword_scores)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
258
 
259
  def analyze_sarcasm(text):
260
  """Specialized analysis for sarcasm detection using multiple methods"""
@@ -272,10 +392,23 @@ def analyze_sarcasm(text):
272
  sum(probs[0, token_id].item() for token_id in negative_ids)
273
 
274
  # 2. Linguistic pattern detection
275
- pattern_score = detect_sarcasm_patterns(text)
276
 
277
- # 3. Sentiment incongruity detection
278
- incongruity_score = detect_extreme_incongruity(text)
 
 
 
 
 
 
 
 
 
 
 
 
 
279
 
280
  # 4. Check intent
281
  intent_template = "The writer's intent is [MASK]."
@@ -289,119 +422,126 @@ def analyze_sarcasm(text):
289
  intent_score = sum(intent_probs[0, token_id].item() for token_id in sarcastic_intent_ids) - \
290
  sum(intent_probs[0, token_id].item() for token_id in sincere_intent_ids)
291
 
 
 
 
 
292
  # Weighted combination of all scores
293
- combined_score = (0.3 * bert_score) + (0.3 * pattern_score) + \
294
- (0.2 * incongruity_score) + (0.2 * intent_score)
 
295
 
296
  # Normalize to [0, 1]
297
- return max(0, min(combined_score, 1))
298
-
299
- def get_confidence_adjustment(text, emotion_scores):
300
- """Adjust confidence based on text characteristics"""
301
- # Text length adjustment - very short texts are harder to analyze
302
- text_length = len(text.split())
303
- length_factor = min(text_length / 20, 1.0) # Texts with less than 20 words get reduced confidence
304
-
305
- # Emotion intensity - stronger emotions should have higher confidence
306
- max_score = max(emotion_scores.values())
307
- intensity_factor = max_score
308
-
309
- # Ambiguity adjustment - if multiple emotions have similar scores, reduce confidence
310
- sorted_scores = sorted(emotion_scores.values(), reverse=True)
311
- if len(sorted_scores) > 1:
312
- top_gap = sorted_scores[0] - sorted_scores[1]
313
- ambiguity_factor = min(top_gap * 2, 1.0) # Small gap means ambiguous emotion
314
- else:
315
- ambiguity_factor = 1.0
316
-
317
- # Combined adjustment factor
318
- adjustment = (length_factor + intensity_factor + ambiguity_factor) / 3
319
-
320
- return adjustment
321
 
322
  def analyze_emotions(text):
323
- """Analyze emotions in text using improved BERT-based approach with robust sarcasm detection"""
324
  if not text or not text.strip():
325
  return None, {"error": "Please enter some text to analyze"}
326
 
327
  try:
328
- # Calculate raw scores for each emotion
329
- emotion_scores = {}
330
 
331
  # For each standard emotion category (excluding sarcasm)
332
  for emotion, keywords in EMOTION_CATEGORIES.items():
333
  if emotion == 'sarcasm':
334
  continue
335
 
336
- # Use specialized function to get emotion score
337
- emotion_scores[emotion] = get_emotion_score(text, emotion, keywords)
 
 
 
 
338
 
339
  # Special handling for sarcasm with multi-method approach
340
- emotion_scores['sarcasm'] = analyze_sarcasm(text)
 
 
 
 
 
341
 
342
- # Get confidence adjustment factor based on text characteristics
343
- confidence_adjustment = get_confidence_adjustment(text, emotion_scores)
344
 
345
  # Apply chain-of-thought decision making for final analysis
346
- final_scores = {}
 
347
 
348
- # Step 1: Look for extremely high sarcasm score - this can override other emotions
349
- if emotion_scores['sarcasm'] > 0.7:
350
- # High sarcasm detected - reduce emotional scores
 
 
 
 
 
 
351
  for emotion in emotion_scores:
352
  if emotion != 'sarcasm':
353
- # Reduce other emotions based on sarcasm strength
354
- emotion_scores[emotion] *= (1 - (emotion_scores['sarcasm'] * 0.5))
355
-
356
- # Step 2: If sarcasm score is moderate (0.3-0.7), maintain other emotions but boost sarcasm
357
  elif emotion_scores['sarcasm'] > 0.3:
358
  # Moderate sarcasm - keep as complementary emotion
359
- emotion_scores['sarcasm'] *= 1.2 # Slight boost to ensure it's noticed
360
-
361
- # Step 3: If sarcasm score is low, reduce it further
362
  else:
363
- emotion_scores['sarcasm'] *= 0.8 # Reduce low sarcasm scores to avoid false positives
364
-
365
- # Step 4: Check for emotional extremes that could override sarcasm
366
- max_emotion = max(emotion_scores.items(), key=lambda x: x[1] if x[0] != 'sarcasm' else 0)
367
- if max_emotion[1] > 0.7 and max_emotion[0] != 'sarcasm':
368
- # Strong emotion detected - this could reduce sarcasm
369
- emotion_scores['sarcasm'] *= 0.8
370
 
371
- # Step 5: Normalize scores to ensure they sum to 1
 
 
 
 
372
  total_score = sum(emotion_scores.values())
373
- if total_score > 0:
374
- final_scores = {emotion: score / total_score for emotion, score in emotion_scores.items()}
375
- else:
376
- # Fallback if all scores are zero
377
- final_scores = {emotion: 1/len(emotion_scores) for emotion in emotion_scores}
378
-
379
- # Apply confidence adjustment
380
- final_scores = {emotion: score * confidence_adjustment for emotion, score in final_scores.items()}
381
-
382
- # Normalize again after adjustment
383
- total_adjusted = sum(final_scores.values())
384
- if total_adjusted > 0:
385
- final_scores = {emotion: score / total_adjusted for emotion, score in final_scores.items()}
386
 
387
  # Sort emotions by score
388
- sorted_emotions = sorted(final_scores.items(), key=lambda x: x[1], reverse=True)
389
  emotions, scores = zip(*sorted_emotions)
390
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
391
  # Create visualization
392
- fig = create_visualization(emotions, scores, text)
393
 
394
  # Format output
395
  output = {
396
  "dominant_emotion": emotions[0],
397
  "confidence": f"{scores[0]*100:.1f}%",
398
- "detailed_scores": {emotion: f"{score*100:.1f}%" for emotion, score in zip(emotions, scores)}
 
399
  }
400
 
401
  # Add contextual notes if applicable
402
  if emotions[0] == 'sarcasm' and scores[0] > 0.3:
403
- output["note"] = f"Sarcasm detected with {scores[0]*100:.1f}% confidence. Context suggests ironic or mocking tone."
404
- elif 'sarcasm' in final_scores and final_scores['sarcasm'] > 0.2:
405
  output["note"] = f"Some sarcastic elements detected alongside {emotions[0]}."
406
 
407
  return fig, output
@@ -412,9 +552,9 @@ def analyze_emotions(text):
412
  print(traceback.format_exc())
413
  return None, {"error": f"Analysis failed: {str(e)}"}
414
 
415
- def create_visualization(emotions, scores, text=None):
416
- """Create a bar chart visualization of emotion scores"""
417
- fig, ax = plt.subplots(figsize=(10, 6))
418
 
419
  # Use custom colors for the bars
420
  colors = [EMOTION_COLORS.get(emotion, '#1f77b4') for emotion in emotions]
@@ -422,10 +562,33 @@ def create_visualization(emotions, scores, text=None):
422
  # Create horizontal bar chart
423
  y_pos = np.arange(len(emotions))
424
  ax.barh(y_pos, [score * 100 for score in scores], color=colors)
 
 
 
 
 
 
 
425
  ax.set_yticks(y_pos)
426
- ax.set_yticklabels(emotions)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
427
  ax.invert_yaxis() # Labels read top-to-bottom
428
- ax.set_xlabel('Confidence (%)')
429
 
430
  # Add value labels to the bars
431
  for i, v in enumerate(scores):
@@ -456,7 +619,7 @@ demo = gr.Interface(
456
  title="🧠 BERT-based Emotion Analysis",
457
  description="""This app analyzes emotions in text using a specialized BERT-based approach.
458
  It identifies how well the input text aligns with seven emotional categories: joy, sadness, anger, fear, surprise, love, and sarcasm.
459
- The analysis leverages BERT's contextual understanding along with sophisticated pattern recognition to evaluate emotional content.""",
460
  examples=[
461
  ["I can't wait for the concert tonight! It's going to be amazing!"],
462
  ["The news about the layoffs has left everyone feeling devastated."],
@@ -466,7 +629,9 @@ demo = gr.Interface(
466
  ["I deeply cherish the time we spend together."],
467
  ["Oh great, another meeting that could have been an email. Just what I needed today."],
468
  ["Sure, I'd LOVE to do your work for you. Nothing better than doing two jobs for one salary!"],
469
- ["What a FANTASTIC way to start the day - my car won't start and it's pouring rain!"]
 
 
470
  ],
471
  allow_flagging="never"
472
  )
 
68
  'sarcasm': '#FF7F50' # Coral
69
  }
70
 
71
+ # Common sentiment phrases and expressions for improved detection
72
+ EMOTION_PHRASES = {
73
+ 'joy': [
74
+ 'over the moon', 'on cloud nine', 'couldn\'t be happier',
75
+ 'best day ever', 'made my day', 'feeling great',
76
+ 'absolutely thrilled', 'jumping for joy'
77
+ ],
78
+ 'sadness': [
79
+ 'broke my heart', 'in tears', 'feel like crying',
80
+ 'deeply saddened', 'lost all hope', 'feel empty',
81
+ 'devastating news', 'hit hard', 'feel down'
82
+ ],
83
+ 'anger': [
84
+ 'makes my blood boil', 'fed up with', 'had it with',
85
+ 'sick and tired of', 'drives me crazy', 'lost my temper',
86
+ 'absolutely furious', 'beyond frustrated'
87
+ ],
88
+ 'fear': [
89
+ 'scared to death', 'freaking out', 'keeps me up at night',
90
+ 'terrified of', 'living in fear', 'panic attack',
91
+ 'nervous wreck', 'can\'t stop worrying'
92
+ ],
93
+ 'surprise': [
94
+ 'can\'t believe', 'took me by surprise', 'out of nowhere',
95
+ 'never expected', 'caught off guard', 'mind blown',
96
+ 'plot twist', 'jaw dropped'
97
+ ],
98
+ 'love': [
99
+ 'deeply in love', 'means the world to me', 'treasure every moment',
100
+ 'hold dear', 'close to my heart', 'forever grateful',
101
+ 'truly blessed', 'never felt this way'
102
+ ],
103
+ 'sarcasm': [
104
+ 'just what I needed', 'couldn\'t get any better', 'how wonderful',
105
+ 'oh great', 'lucky me', 'my favorite part',
106
+ 'thrilled to bits', 'way to go', 'thanks for nothing'
107
+ ]
108
+ }
109
+
110
+ # Contextual emotion indicators for better analysis
111
+ CONTEXTUAL_INDICATORS = {
112
+ 'intensifiers': ['very', 'extremely', 'incredibly', 'absolutely', 'totally', 'completely', 'utterly'],
113
+ 'negators': ['not', 'never', 'no', 'none', 'neither', 'nor', 'hardly', 'barely'],
114
+ 'hedges': ['somewhat', 'kind of', 'sort of', 'a bit', 'slightly', 'perhaps', 'maybe'],
115
+ 'boosters': ['definitely', 'certainly', 'absolutely', 'undoubtedly', 'surely', 'clearly'],
116
+ 'punctuation': {'!': 'emphasis', '?': 'question', '...': 'hesitation'}
117
+ }
118
 
119
  # Load BERT model and tokenizer
120
  print("Loading BERT model and tokenizer (this may take a moment)...")
 
127
  model = model.to(device)
128
  print(f"Model loaded successfully. Using device: {device}")
129
 
130
+ # Sarcasm patterns with refined detection logic
131
  SARCASM_PATTERNS = [
132
+ # Exaggerated positive with negative context
133
+ r'(?i)\b(?:so+|really|absolutely|totally|completely)\s+(?:thrilled|excited|happy|delighted)\s+(?:about|with|by)\b.*?(?:terrible|awful|worst|bad)',
134
 
135
  # Classic sarcastic phrases
136
+ r'(?i)(?:^|\W)just\s+what\s+(?:I|we)\s+(?:need|wanted|hoped for)\b',
137
+ r'(?i)(?:^|\W)how\s+(?:wonderful|nice|great|lovely|exciting)\b.*?(?:\!|\?{2,})',
138
 
139
+ # Thanks for nothing pattern
140
+ r'(?i)(?:^|\W)thanks\s+for\s+(?:nothing|that|pointing|stating)\b',
141
 
142
  # Quotation marks around positive words (scare quotes)
143
  r'(?i)"(?:great|wonderful|excellent|perfect|amazing)"',
144
 
145
  # Typical sarcastic responses
146
+ r'(?i)^(?:yeah|sure|right|oh)\s+(?:right|sure|okay|ok)(?:\W|$)',
147
 
148
  # Exaggerated praise in negative context
149
+ r'(?i)\b(?:brilliant|genius|impressive)\b.*?(?:disaster|failure|mess)',
150
 
151
  # Obvious understatements
152
+ r'(?i)\b(?:slightly|bit|little)\s+(?:catastrophic|disastrous|terrible|awful)\b',
153
 
154
+ # Oh great patterns
155
+ r'(?i)(?:^|\W)oh\s+(?:great|wonderful|perfect|fantastic|awesome)(?:\W|$)'
156
  ]
157
 
158
  def tokenize_and_clean(text):
 
164
  tokens = re.findall(r'\b\w+\b', text)
165
  return tokens
166
 
167
+ def detect_phrases(text, emotion_phrases):
168
+ """Detect emotion-specific phrases in text"""
169
+ text_lower = text.lower()
170
+ detected_phrases = {}
171
+
172
+ for emotion, phrases in emotion_phrases.items():
173
+ found_phrases = []
174
+ for phrase in phrases:
175
+ if phrase.lower() in text_lower:
176
+ found_phrases.append(phrase)
177
+
178
+ if found_phrases:
179
+ detected_phrases[emotion] = found_phrases
180
+
181
+ return detected_phrases
182
+
183
+ def detect_contextual_features(text):
184
+ """Detect contextual features in text that may influence emotion"""
185
+ features = {
186
+ 'intensifiers': 0,
187
+ 'negators': 0,
188
+ 'hedges': 0,
189
+ 'boosters': 0,
190
+ 'exclamations': text.count('!'),
191
+ 'questions': text.count('?'),
192
+ 'ellipses': text.count('...'),
193
+ 'capitalized_words': len(re.findall(r'\b[A-Z]{2,}\b', text))
194
+ }
195
+
196
+ text_lower = text.lower()
197
  tokens = tokenize_and_clean(text)
198
+
199
+ # Count contextual indicators
200
+ for indicator_type, words in CONTEXTUAL_INDICATORS.items():
201
+ if indicator_type != 'punctuation':
202
+ for word in words:
203
+ if ' ' in word: # Multi-word phrase
204
+ if word in text_lower:
205
+ features[indicator_type] += 1
206
+ else: # Single word
207
+ features[indicator_type] += tokens.count(word)
208
+
209
+ return features
210
 
211
  def detect_sarcasm_patterns(text):
212
  """Detect linguistic patterns of sarcasm in text with context awareness"""
 
219
  matches += 1
220
  pattern_matches.append(pattern)
221
 
222
+ # Get contextual features
223
+ features = detect_contextual_features(text)
224
+
225
+ # Check for phrases specific to sarcasm
226
+ phrases = detect_phrases(text, {'sarcasm': EMOTION_PHRASES['sarcasm']})
227
+ sarcasm_phrases = len(phrases.get('sarcasm', []))
228
 
229
+ # Calculate raw score based on pattern matches and features
230
+ raw_score = (matches * 0.15) + (sarcasm_phrases * 0.2)
 
231
 
232
+ # Adjust based on contextual features
233
+ if features['exclamations'] > 1:
234
+ raw_score += min(features['exclamations'] * 0.05, 0.2)
 
 
235
 
236
+ if features['capitalized_words'] > 0:
237
+ raw_score += min(features['capitalized_words'] * 0.1, 0.3)
 
 
238
 
239
+ # Detect positive-negative contrasts
240
+ pos_neg_contrast = 0
241
+ emotion_phrases = detect_phrases(text, {
242
+ 'positive': EMOTION_PHRASES['joy'] + EMOTION_PHRASES['love'],
243
+ 'negative': EMOTION_PHRASES['sadness'] + EMOTION_PHRASES['anger']
244
+ })
245
 
246
+ if emotion_phrases.get('positive') and emotion_phrases.get('negative'):
247
+ pos_neg_contrast = 0.3
248
+
249
+ # Add contrast score
250
+ raw_score += pos_neg_contrast
251
 
252
  # Normalize to [0, 1]
253
+ return min(raw_score, 1.0), pattern_matches
254
 
255
+ def get_bert_sentence_embedding(text):
256
+ """Get BERT embedding for a sentence"""
257
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
258
+ inputs = {k: v.to(device) for k, v in inputs.items()}
 
 
 
 
 
 
 
259
 
260
+ with torch.no_grad():
261
+ outputs = model(**inputs, output_hidden_states=True)
262
+
263
+ # Get the last hidden state
264
+ last_hidden_state = outputs.hidden_states[-1]
265
+
266
+ # Mean pooling - take average of all token embeddings
267
+ sentence_embedding = torch.mean(last_hidden_state[0], dim=0)
268
 
269
+ return sentence_embedding
270
+
271
+ def semantic_similarity(text1, text2):
272
+ """Calculate semantic similarity between two texts using BERT embeddings"""
273
+ # Get embeddings
274
+ emb1 = get_bert_sentence_embedding(text1)
275
+ emb2 = get_bert_sentence_embedding(text2)
276
+
277
+ # Normalize embeddings
278
+ emb1 = emb1 / emb1.norm()
279
+ emb2 = emb2 / emb2.norm()
280
 
281
+ # Calculate cosine similarity
282
+ similarity = torch.dot(emb1, emb2).item()
283
+
284
+ return similarity
285
 
286
+ def create_emotional_template(emotion, keyword):
287
  """Create a template sentence for emotion prediction"""
288
  templates = [
289
  f"The text expresses [MASK] {emotion} emotions.",
290
+ f"This text shows [MASK] {keyword} feelings.",
291
  f"The writer feels [MASK] {keyword}.",
292
  f"The sentiment in this text is [MASK] {keyword}."
293
  ]
294
 
295
+ # Use a consistent template for now
296
  return templates[0]
297
 
298
  def predict_masked_token(text, template):
 
321
  return probs
322
 
323
  def get_emotion_score(text, emotion, keywords):
324
+ """Calculate emotion score based on multiple template predictions and phrase detection"""
325
  # Positive and negative indicator tokens
326
  positive_indicators = ['clearly', 'definitely', 'strongly', 'very', 'extremely']
327
  negative_indicators = ['not', 'barely', 'hardly', 'slightly', 'somewhat']
 
331
 
332
  # Use a subset of keywords for efficiency
333
  for keyword in keywords[:5]: # Use just 5 keywords per emotion for efficiency
334
+ template = create_emotional_template(emotion, keyword)
335
  probs = predict_masked_token(text, template)
336
 
337
  # Get token IDs for positive and negative words
 
346
  score = positive_score - negative_score
347
  keyword_scores.append(score)
348
 
349
+ # Check for emotion-specific phrases
350
+ detected_phrases = detect_phrases(text, {emotion: EMOTION_PHRASES[emotion]})
351
+ phrase_count = len(detected_phrases.get(emotion, []))
352
+ phrase_score = min(phrase_count * 0.2, 0.6) # Cap at 0.6
353
+
354
+ # Get contextual features
355
+ features = detect_contextual_features(text)
356
+
357
+ # Calculate feature-based adjustment
358
+ feature_adjustment = 0
359
+
360
+ # Adjust score based on emotional context
361
+ if emotion in ['joy', 'love', 'surprise'] and features['exclamations'] > 0:
362
+ feature_adjustment += min(features['exclamations'] * 0.05, 0.2)
363
+
364
+ if emotion in ['anger', 'sadness'] and features['negators'] > 0:
365
+ feature_adjustment += min(features['negators'] * 0.05, 0.2)
366
+
367
+ if emotion == 'fear' and features['intensifiers'] > 0:
368
+ feature_adjustment += min(features['intensifiers'] * 0.05, 0.2)
369
+
370
+ # Average BERT-based score
371
+ bert_score = sum(keyword_scores) / len(keyword_scores)
372
+
373
+ # Combine BERT score with phrase detection and context adjustments
374
+ final_score = (bert_score * 0.6) + (phrase_score * 0.3) + (feature_adjustment * 0.1)
375
+
376
+ # Normalize to ensure it's in [0, 1]
377
+ return max(0, min(final_score, 1.0)), detected_phrases.get(emotion, [])
378
 
379
  def analyze_sarcasm(text):
380
  """Specialized analysis for sarcasm detection using multiple methods"""
 
392
  sum(probs[0, token_id].item() for token_id in negative_ids)
393
 
394
  # 2. Linguistic pattern detection
395
+ pattern_score, pattern_matches = detect_sarcasm_patterns(text)
396
 
397
+ # 3. Check for semantic incongruity using BERT
398
+ incongruity_score = 0
399
+
400
+ # Split text into sentences
401
+ sentences = re.split(r'(?<=[.!?])\s+', text)
402
+ if len(sentences) > 1:
403
+ # Check semantic similarity between adjacent sentences
404
+ similarities = []
405
+ for i in range(len(sentences) - 1):
406
+ sim = semantic_similarity(sentences[i], sentences[i+1])
407
+ similarities.append(sim)
408
+
409
+ # Low similarity between adjacent sentences might indicate sarcasm
410
+ if similarities and min(similarities) < 0.5:
411
+ incongruity_score = 0.3
412
 
413
  # 4. Check intent
414
  intent_template = "The writer's intent is [MASK]."
 
422
  intent_score = sum(intent_probs[0, token_id].item() for token_id in sarcastic_intent_ids) - \
423
  sum(intent_probs[0, token_id].item() for token_id in sincere_intent_ids)
424
 
425
+ # 5. Check for sarcasm phrases
426
+ detected_phrases = detect_phrases(text, {'sarcasm': EMOTION_PHRASES['sarcasm']})
427
+ phrase_score = min(len(detected_phrases.get('sarcasm', [])) * 0.2, 0.6)
428
+
429
  # Weighted combination of all scores
430
+ combined_score = (0.2 * bert_score) + (0.25 * pattern_score) + \
431
+ (0.15 * incongruity_score) + (0.15 * intent_score) + \
432
+ (0.25 * phrase_score)
433
 
434
  # Normalize to [0, 1]
435
+ return max(0, min(combined_score, 1.0)), detected_phrases.get('sarcasm', []), pattern_matches
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
436
 
437
  def analyze_emotions(text):
438
+ """Analyze emotions in text using enhanced BERT-based approach with robust sarcasm detection"""
439
  if not text or not text.strip():
440
  return None, {"error": "Please enter some text to analyze"}
441
 
442
  try:
443
+ # Calculate scores for each emotion with supporting phrases
444
+ emotion_data = {}
445
 
446
  # For each standard emotion category (excluding sarcasm)
447
  for emotion, keywords in EMOTION_CATEGORIES.items():
448
  if emotion == 'sarcasm':
449
  continue
450
 
451
+ # Use specialized function to get emotion score and supporting phrases
452
+ score, phrases = get_emotion_score(text, emotion, keywords)
453
+ emotion_data[emotion] = {
454
+ 'score': score,
455
+ 'phrases': phrases
456
+ }
457
 
458
  # Special handling for sarcasm with multi-method approach
459
+ sarcasm_score, sarcasm_phrases, sarcasm_patterns = analyze_sarcasm(text)
460
+ emotion_data['sarcasm'] = {
461
+ 'score': sarcasm_score,
462
+ 'phrases': sarcasm_phrases,
463
+ 'patterns': sarcasm_patterns
464
+ }
465
 
466
+ # Get contextual features for overall analysis
467
+ context_features = detect_contextual_features(text)
468
 
469
  # Apply chain-of-thought decision making for final analysis
470
+ # 1. Check for dominant emotions by raw scores
471
+ emotion_scores = {emotion: data['score'] for emotion, data in emotion_data.items()}
472
 
473
+ # 2. Adjust based on contextual evidence
474
+ # If we have strong phrase evidence, boost those emotions
475
+ for emotion, data in emotion_data.items():
476
+ if len(data.get('phrases', [])) >= 2:
477
+ emotion_scores[emotion] = emotion_scores[emotion] * 1.2
478
+
479
+ # 3. Adjust sarcasm based on specific rules
480
+ if emotion_scores['sarcasm'] > 0.6:
481
+ # High sarcasm - reduce intensity of other emotions
482
  for emotion in emotion_scores:
483
  if emotion != 'sarcasm':
484
+ emotion_scores[emotion] *= 0.8
 
 
 
485
  elif emotion_scores['sarcasm'] > 0.3:
486
  # Moderate sarcasm - keep as complementary emotion
487
+ pass
 
 
488
  else:
489
+ # Low sarcasm - reduce it further to avoid false positives
490
+ emotion_scores['sarcasm'] *= 0.7
491
+
492
+ # 4. Check for unusually high emotions that might override sarcasm
493
+ non_sarcasm_emotions = {e: s for e, s in emotion_scores.items() if e != 'sarcasm'}
494
+ max_emotion = max(non_sarcasm_emotions.items(), key=lambda x: x[1]) if non_sarcasm_emotions else (None, 0)
 
495
 
496
+ if max_emotion[1] > 0.7:
497
+ # Very strong emotion detected - this could reduce sarcasm
498
+ emotion_scores['sarcasm'] *= 0.7
499
+
500
+ # 5. Normalize scores to ensure they sum to 1
501
  total_score = sum(emotion_scores.values())
502
+ normalized_scores = {emotion: score / total_score for emotion, score in emotion_scores.items()}
 
 
 
 
 
 
 
 
 
 
 
 
503
 
504
  # Sort emotions by score
505
+ sorted_emotions = sorted(normalized_scores.items(), key=lambda x: x[1], reverse=True)
506
  emotions, scores = zip(*sorted_emotions)
507
 
508
+ # Prepare supporting evidence for each emotion
509
+ supporting_evidence = {}
510
+ for emotion in emotions:
511
+ evidence = []
512
+
513
+ # Add detected phrases
514
+ if emotion_data[emotion].get('phrases'):
515
+ evidence.extend([f'Phrase: "{phrase}"' for phrase in emotion_data[emotion]['phrases']])
516
+
517
+ # Add pattern matches for sarcasm
518
+ if emotion == 'sarcasm' and emotion_data['sarcasm'].get('patterns'):
519
+ evidence.extend([f'Pattern match: sarcastic pattern detected' for _ in emotion_data['sarcasm']['patterns']])
520
+
521
+ # Add contextual features as evidence
522
+ if emotion == 'joy' and context_features['exclamations'] > 1:
523
+ evidence.append(f'Found {context_features["exclamations"]} exclamation marks (!)')
524
+
525
+ if emotion == 'anger' and context_features['capitalized_words'] > 0:
526
+ evidence.append(f'Found {context_features["capitalized_words"]} capitalized words')
527
+
528
+ supporting_evidence[emotion] = evidence[:3] # Limit to top 3 pieces of evidence
529
+
530
  # Create visualization
531
+ fig = create_visualization(emotions, scores, text, supporting_evidence)
532
 
533
  # Format output
534
  output = {
535
  "dominant_emotion": emotions[0],
536
  "confidence": f"{scores[0]*100:.1f}%",
537
+ "detailed_scores": {emotion: f"{score*100:.1f}%" for emotion, score in zip(emotions, scores)},
538
+ "supporting_evidence": supporting_evidence
539
  }
540
 
541
  # Add contextual notes if applicable
542
  if emotions[0] == 'sarcasm' and scores[0] > 0.3:
543
+ output["note"] = f"Sarcasm detected with {scores[0]*100:.1f}% confidence."
544
+ elif 'sarcasm' in normalized_scores and normalized_scores['sarcasm'] > 0.25:
545
  output["note"] = f"Some sarcastic elements detected alongside {emotions[0]}."
546
 
547
  return fig, output
 
552
  print(traceback.format_exc())
553
  return None, {"error": f"Analysis failed: {str(e)}"}
554
 
555
+ def create_visualization(emotions, scores, text=None, supporting_evidence=None):
556
+ """Create a bar chart visualization of emotion scores with fixed x-axis and evidence"""
557
+ fig, ax = plt.subplots(figsize=(12, 7))
558
 
559
  # Use custom colors for the bars
560
  colors = [EMOTION_COLORS.get(emotion, '#1f77b4') for emotion in emotions]
 
562
  # Create horizontal bar chart
563
  y_pos = np.arange(len(emotions))
564
  ax.barh(y_pos, [score * 100 for score in scores], color=colors)
565
+
566
+ # Set fixed x-axis from 0 to 100
567
+ ax.set_xlim(0, 100)
568
+ ax.set_xticks(np.arange(0, 101, 10))
569
+ ax.set_xlabel('Confidence (%)')
570
+
571
+ # Set y-ticks and labels
572
  ax.set_yticks(y_pos)
573
+
574
+ # Create custom labels with probability
575
+ y_labels = []
576
+ for i, emotion in enumerate(emotions):
577
+ prob_text = f"{scores[i]*100:.1f}%"
578
+ y_labels.append(f"{emotion.capitalize()} ({prob_text})")
579
+
580
+ # Add evidence as smaller text
581
+ if supporting_evidence and emotion in supporting_evidence and supporting_evidence[emotion]:
582
+ evidence_x = 101 # Position just outside the plot area
583
+
584
+ for j, evidence in enumerate(supporting_evidence[emotion]):
585
+ ax.text(evidence_x, y_pos[i] - 0.15 + (j * 0.3),
586
+ evidence,
587
+ fontsize=8, color='#555555',
588
+ verticalalignment='center')
589
+
590
+ ax.set_yticklabels(y_labels)
591
  ax.invert_yaxis() # Labels read top-to-bottom
 
592
 
593
  # Add value labels to the bars
594
  for i, v in enumerate(scores):
 
619
  title="🧠 BERT-based Emotion Analysis",
620
  description="""This app analyzes emotions in text using a specialized BERT-based approach.
621
  It identifies how well the input text aligns with seven emotional categories: joy, sadness, anger, fear, surprise, love, and sarcasm.
622
+ The analysis leverages BERT's contextual understanding along with phrase detection and linguistic pattern recognition to evaluate emotional content.""",
623
  examples=[
624
  ["I can't wait for the concert tonight! It's going to be amazing!"],
625
  ["The news about the layoffs has left everyone feeling devastated."],
 
629
  ["I deeply cherish the time we spend together."],
630
  ["Oh great, another meeting that could have been an email. Just what I needed today."],
631
  ["Sure, I'd LOVE to do your work for you. Nothing better than doing two jobs for one salary!"],
632
+ ["What a FANTASTIC way to start the day - my car won't start and it's pouring rain!"],
633
+ ["This new restaurant is absolutely mind-blowing. The flavors are incredible!"],
634
+ ["I'm heartbroken after hearing what happened. I can't believe it."]
635
  ],
636
  allow_flagging="never"
637
  )