Search code examples
pythonflair

Flair Sentimental Analysis not Not giving Neutral results


I am using Flair for sentimental analysis. However, when i try to predict the label, i am not able to get a Neutral class ever. Also, the confidence of class is too unreal, i.e it is positive with probability >0.97 always or negative with such high probability. Even the very neutral words are being predicted as positive or negative with a very high probability.

classifier = TextClassifier.load('en-sentiment')
inputQuery='Go There and Walk'

sentence = Sentence(inputQuery)
classifier.predict(sentence)
label = sentence.labels[0]
labscore = (label.score)*100
response = {'result': label.value, 'score':"%.2f" % labscore}
print(response)

Here the output is :{'result': 'POSITIVE', 'score': '96.66'} What am I doing wrong?


Solution

  • The issue isn't with your code, it is the way the model (behind the scenes) is trained and the way it works. The English model Flair uses is trained on certain datasets (movie and product reviews) based on the release. If you want to look at the model file, it is usually located in the .flair sub-folder in your home directory.

    Basically, you are using a pre-trained model provided to give you the score. To get a different score, you could either build your own model, possibly add to the existing model or you could use a different model.

    You could try the other models and see what results you get by replacing this line:

    classifier = TextClassifier.load('en-sentiment')
    

    with:

    classifier = TextClassifier.load('sentiment-fast') # for a RNN based model
    

    Note that the model above isn't likely to give you better results but it is worth a try. Otherwise, it would be best to explore building your own sentiment analysis model and loading it.