perf: Optimized analyze_text function for faster execution
Browse files
src/SentimentAndIntentionAnalysis.py
CHANGED
|
@@ -15,13 +15,16 @@ class ZeroShotClassifier:
|
|
| 15 |
return classifier
|
| 16 |
|
| 17 |
def analyze_text(self, text):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
return {"sentiment": sentiment, "intention": intention}
|
|
|
|
| 15 |
return classifier
|
| 16 |
|
| 17 |
def analyze_text(self, text):
|
| 18 |
+
results = list(self.model(text, self.labels)['labels'])
|
| 19 |
+
i = 0
|
| 20 |
+
sentiment = None
|
| 21 |
+
intention = None
|
| 22 |
+
while (sentiment is None) or (intention is None):
|
| 23 |
+
if results[i] in self.sentiment_labels:
|
| 24 |
+
# Sentiment analyze result
|
| 25 |
+
sentiment = results[i]
|
| 26 |
+
if results[i] in self.intention_labels:
|
| 27 |
+
# Intention analyze result
|
| 28 |
+
intention = results[i]
|
| 29 |
+
i += 1
|
| 30 |
return {"sentiment": sentiment, "intention": intention}
|