Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_tinvest_robot-master/tinvest_robot_perevalov/sentiment_analyzer.py
5933 views
1
"""Module for sentiment analyzer classes
2
"""
3
import os
4
from transformers import TFAutoModelForSequenceClassification
5
from transformers import AutoTokenizer
6
from scipy.special import softmax
7
8
9
class SentimentAnalyzer:
10
"""
11
Sentiment Analyzer class for predicting sentiment of textual news
12
"""
13
def __init__(self, model, threshold, labels) -> None:
14
"""
15
SentimentAnalyzer constructor
16
17
Args:
18
model (_type_): model name/path from huggingface
19
threshold (_type_): threshold for sentiment classification (difference between positive and negative > threshold)
20
labels (_type_): class labels for sentiment classification
21
"""
22
self.tokenizer = AutoTokenizer.from_pretrained(model)
23
self.model = TFAutoModelForSequenceClassification.from_pretrained(model)
24
self.threshold = threshold
25
self.labels = labels
26
27
# caching
28
if "data" not in model:
29
self.model.save_pretrained(os.path.join("..", "data", model))
30
self.tokenizer.save_pretrained(os.path.join("..", "data", model))
31
32
33
def predict_sentiment(self, text) -> str:
34
"""
35
Predict sentiment of text
36
37
Args:
38
text (_type_): text to predict sentiment
39
40
Returns:
41
str: one of the sentiment classes: positive, negative, neutral
42
"""
43
encoded_input = self.tokenizer(text, return_tensors='tf')
44
output = self.model(encoded_input)
45
scores = output[0][0].numpy()
46
scores = softmax(scores)
47
48
if scores[0] > scores[2] and abs(scores[0] - scores[2]) > self.threshold: # negative
49
return 'negative'
50
elif scores[0] < scores[2] and abs(scores[0] - scores[2]) > self.threshold: # positive
51
return 'positive'
52
else: # neutral
53
return 'neutral'
54