|
from transformers import pipeline |
|
|
|
class SentimentPipeline: |
|
""" |
|
This class defines a custom sentiment analysis pipeline using Hugging Face's Transformers. |
|
|
|
The pipeline uses two separate models for predicting positive/non-positive and |
|
negative/non-negative sentiment respectively. |
|
|
|
Inputs: |
|
Single text string or a list of text strings for sentiment analysis. |
|
|
|
Returns: |
|
If a single text string is provided, a single dictionary is returned with POS, NEG, and OBJ scores. |
|
If a list of text strings is provided, a list of dictionaries is returned with each dictionary |
|
representing POS, NEG, and OBJ scores for the corresponding text. |
|
|
|
Usage: |
|
sentiment_pipeline = SentimentPipeline(YOUR_POS_MODEL, YOUR_NEG_MODEL) |
|
result = sentiment_pipeline("Your glossed text here") |
|
results = sentiment_pipeline(["Your first glossed text here", "Your second glossed text here"]) |
|
""" |
|
|
|
def __init__(self, model_path_positive, model_path_negative): |
|
""" |
|
Constructor for the SentimentPipeline class. |
|
Initializes two pipelines using Hugging Face's Transformers, one for positive and one for negative sentiment. |
|
""" |
|
self.pos_pipeline = pipeline('text-classification', model=model_path_positive) |
|
self.neg_pipeline = pipeline('text-classification', model=model_path_negative) |
|
|
|
def __call__(self, texts): |
|
""" |
|
Callable method for the SentimentPipeline class. Processes the given text(s) and returns sentiment scores. |
|
""" |
|
|
|
|
|
if isinstance(texts, str): |
|
texts = [texts] |
|
|
|
results = [] |
|
for text in texts: |
|
|
|
pos_result = self.pos_pipeline(text)[0] |
|
neg_result = self.neg_pipeline(text)[0] |
|
|
|
|
|
|
|
|
|
|
|
|
|
Pt, Pn = (pos_result['score'], 1 - pos_result['score']) if pos_result['label'] == 'POSITIVE' else (1 - pos_result['score'], pos_result['score']) |
|
Nt, Nn = (neg_result['score'], 1 - neg_result['score']) if neg_result['label'] == 'NEGATIVE' else (1 - neg_result['score'], neg_result['score']) |
|
|
|
|
|
POS = Pt * Nn |
|
NEG = Nt * Pn |
|
OBJ = 1 - POS - NEG |
|
|
|
|
|
results.append({"POS": POS, "NEG": NEG, "OBJ": OBJ}) |
|
|
|
|
|
return results if len(results) > 1 else results[0] |
|
|