Flair - A Pretrained NLP Sentiment Analysis Tool

NLP(Natural Language Processing) includes sentiment analysis. Trained sentiment analysis includes machine learning, which is of course more accurate. For beginners who just stepped into this field, getting to know some pretrained sentiment analysis tools may also be a good choice. Here, I want to introduce a package I used to analyze sentiment. It’s called flair.

Flair’s mechanism is simple. It contains a powerful library which allows users to use and combine different word and document embeddings. Based on the corpus, it could analyze and tell the attitudes of the speakers. Comparing with other NLP packages, flair’s sentiment classifier is based on a character-level LSTM neural network which takes sequences of letters and words into account when predicting. It’s based on a corpus but in the meantime, it could also predict a sentiment for OOV(Out of Vocab) words including typos.

Installing and Importing Package

1
2
3
!pip3 install flair
import flair
flair_sentiment = flair.models.TextClassifier.load('en-sentiment')

Sentiment Scoring

1
2
3
4
5
6
7
8
def senti_score(n):
    s = flair.data.Sentence(n)
    flair_sentiment.predict(s)
    total_sentiment = s.labels[0]
    assert total_sentiment.value in ['POSITIVE', 'NEGATIVE']
    sign = 1 if total_sentiment.value == 'POSITIVE' else -1
    score = total_sentiment.score
    return sign * score

Here, I created a function to compute the sentiment score of each tweet. The closer the number’s absolute value to 1, the more certain the model is and the more extreme the attitudes of the users are. When the sentiment of a sentence is positive, the score is positive; when the sentiment of a sentence is negative, the score is negative. Amazingly, since the sentiment classifier could tell the intensifiers such as ‘very’, the score will vary when intensifiers are in the game.

Examples

I used several sentences to illustrate the package’s ability to tell the differences between sentences with different attitudes and also intensifiers.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
s = 'Tesla is a great company'
senti_score(s)

s = 'Tesla is a good company'
senti_score(s)

s = 'Tesla is just a company'
senti_score(s)

s = 'Tesla is a bad company'
senti_score(s)

For the upon examples, the four sentences’ results are results 0.9978, 0.9961, -0.8989 and -0.9998.