This tutorials shows you how to do sentiment analysis in Flair.
Tagging sentiment with our standard modelOur standard sentiment analysis model uses distilBERT embeddings and was trained over a mix of corpora, notably the Amazon review corpus, and can thus handle a variety of domains and language.
Let's use an example sentence:
from flair.nn import Classifier
from flair.data import Sentence
tagger = Classifier.load('sentiment')
sentence = Sentence('This movie is not at all bad.')
tagger.predict(sentence)
print(sentence)
This should print:
Sentence[8]: "This movie is not at all bad." → POSITIVE (0.9929)
Showing us that the sentence overall is tagged to be of POSITIVE sentiment.
Tagging sentiment with our fast modelWe also offer an RNN-based variant which is faster but less accurate. Use it like this:
from flair.nn import Classifier
from flair.data import Sentence
tagger = Classifier.load('sentiment-fast')
sentence = Sentence('This movie is very bad.')
tagger.predict(sentence)
print(sentence)
This should print:
Sentence[6]: "This movie is very bad." → NEGATIVE (0.9999)
This indicates that the sentence is of NEGATIVE sentiment. As you can see, its the same code as above, just loading the 'sentiment-fast' model instead of 'sentiment'.
List of Sentiment ModelsWe end this section with a list of all models we currently ship with Flair:
ID Language Task Training Dataset Accuracy 'sentiment' English detecting positive and negative sentiment (transformer-based) movie and product reviews 98.87 'sentiment-fast' English detecting positive and negative sentiment (RNN-based) movie and product reviews 96.83 'de-offensive-language' German detecting offensive language GermEval 2018 Task 1 75.71 (Macro F1)RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4