Path: blob/master/ invest-robot-contest_tinvest_robot-master/tinvest_robot_perevalov/sentiment_analyzer.py
5933 views
"""Module for sentiment analyzer classes1"""2import os3from transformers import TFAutoModelForSequenceClassification4from transformers import AutoTokenizer5from scipy.special import softmax678class SentimentAnalyzer:9"""10Sentiment Analyzer class for predicting sentiment of textual news11"""12def __init__(self, model, threshold, labels) -> None:13"""14SentimentAnalyzer constructor1516Args:17model (_type_): model name/path from huggingface18threshold (_type_): threshold for sentiment classification (difference between positive and negative > threshold)19labels (_type_): class labels for sentiment classification20"""21self.tokenizer = AutoTokenizer.from_pretrained(model)22self.model = TFAutoModelForSequenceClassification.from_pretrained(model)23self.threshold = threshold24self.labels = labels2526# caching27if "data" not in model:28self.model.save_pretrained(os.path.join("..", "data", model))29self.tokenizer.save_pretrained(os.path.join("..", "data", model))303132def predict_sentiment(self, text) -> str:33"""34Predict sentiment of text3536Args:37text (_type_): text to predict sentiment3839Returns:40str: one of the sentiment classes: positive, negative, neutral41"""42encoded_input = self.tokenizer(text, return_tensors='tf')43output = self.model(encoded_input)44scores = output[0][0].numpy()45scores = softmax(scores)4647if scores[0] > scores[2] and abs(scores[0] - scores[2]) > self.threshold: # negative48return 'negative'49elif scores[0] < scores[2] and abs(scores[0] - scores[2]) > self.threshold: # positive50return 'positive'51else: # neutral52return 'neutral'5354