kambris commited on
Commit
14c93da
·
verified ·
1 Parent(s): 85b272d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -10
app.py CHANGED
@@ -125,30 +125,37 @@ class SpeechAnalyzer:
125
  return aggregated_scores
126
 
127
  def analyze_emotional_trajectory(self, text, window_size=5, ngram_size=3):
128
- """Enhanced emotional trajectory analysis using n-grams and relative scoring"""
129
  segments = self.split_text(text, max_length=512)
130
  sentiment_scores = []
131
 
 
 
132
  for segment in segments:
133
  words = segment.split()
134
  ngrams = [' '.join(words[i:i+ngram_size]) for i in range(0, len(words)-ngram_size+1)]
135
 
 
136
  segment_scores = []
137
- for ngram in ngrams:
138
- result = self.sentiment_pipeline(ngram)[0]
139
- segment_scores.append(result['score'])
 
 
140
 
141
  avg_score = np.mean(segment_scores) if segment_scores else 0
142
  sentiment_scores.append(avg_score)
143
 
144
- # Normalize scores
145
- min_score = min(sentiment_scores)
146
- max_score = max(sentiment_scores)
147
- score_range = max_score - min_score
148
- if score_range > 0:
149
- sentiment_scores = [(s - min_score) / score_range * 2 - 1 for s in sentiment_scores]
 
150
 
151
  return sentiment_scores
 
152
  def detect_named_entities(self, text):
153
  """Detect named entities in the text"""
154
  entities = self.ner_pipeline(text)
 
125
  return aggregated_scores
126
 
127
  def analyze_emotional_trajectory(self, text, window_size=5, ngram_size=3):
128
+ """Enhanced emotional trajectory analysis using efficient batch processing"""
129
  segments = self.split_text(text, max_length=512)
130
  sentiment_scores = []
131
 
132
+ # Process n-grams in larger batches while keeping full text
133
+ batch_size = 32
134
  for segment in segments:
135
  words = segment.split()
136
  ngrams = [' '.join(words[i:i+ngram_size]) for i in range(0, len(words)-ngram_size+1)]
137
 
138
+ # Process full ngrams in batches
139
  segment_scores = []
140
+ for i in range(0, len(ngrams), batch_size):
141
+ batch = ngrams[i:i+batch_size]
142
+ results = self.sentiment_pipeline(batch)
143
+ batch_scores = [result['score'] for result in results]
144
+ segment_scores.extend(batch_scores)
145
 
146
  avg_score = np.mean(segment_scores) if segment_scores else 0
147
  sentiment_scores.append(avg_score)
148
 
149
+ # Normalize scores to preserve full range of sentiment
150
+ if sentiment_scores:
151
+ min_score = min(sentiment_scores)
152
+ max_score = max(sentiment_scores)
153
+ score_range = max_score - min_score
154
+ if score_range > 0:
155
+ sentiment_scores = [(s - min_score) / score_range * 2 - 1 for s in sentiment_scores]
156
 
157
  return sentiment_scores
158
+
159
  def detect_named_entities(self, text):
160
  """Detect named entities in the text"""
161
  entities = self.ner_pipeline(text)