2

I have recently been learning how to use flair for sentiment analysis in python. I have previously been using vaderSentiment. I have been saving the sentiment scores as a compound score (ranging between -1 and 1, with -1 being most negative, 0 being neutral and 1 being most positive)

Is there a way to get this same number from flair?

I have tried

from flair.models import TextClassifier
from flair.data import Sentence

classifier = TextClassifier.load('en-sentiment')
sentence = Sentence('The food was great!')
classifier.predict(sentence)

Which only returns a value for one of the categories, In this instance it returns

Sentence: "The food was great !"   [− Tokens: 5  − Sentence-Labels: {'label': [POSITIVE (0.9961)]}]

What is the easiest way of combining these to a compound score?

Many thanks for your time

BarryBBenson
  • 71
  • 1
  • 6

1 Answers1

1

You can use a Sklearn.MinMaxScaler to map your scores from range [0,1] to [-1,1].

flair_score=[]
.....
sentence.labels[0].to_dict()
    attitude=sentence.labels[0].value
    score=sentence.labels[0].score
    if attitude=='NEGATIVE':
        score=-score
    return score
flair_score.append(score)
scaler = MinMaxScaler(feature_range=(-1, 1))
flair_attitude_score=np.array(flair_score).reshape(-1,1)
flair_result=scaler.fit_transform(flair_attitude_score).flatten()
Kaiye Yang
  • 21
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 23 '22 at 05:38