Search code examples
pythonnlpsentiment-analysisfeature-extractionflair

Feature extraction using flairNLP


I'm trying to use flair for sentiment analysis, but I also need to know how much each word influenced the score of a sentence.

I've followed this article to predict the sentiment, but it doesn't show how to extract the features of the given sentence. I'm assuming there is a way to do this feature extraction because of the way it's presented in that article, but I can't find it. I've tried reading the flair documentation and the code itself but didn't see a way to do so.

What I'm looking for is a functionality of this sort:

import flair
text = flair.data.Sentence(<string-with-sentiment>)
model = flair.models.TextClassifier.load('en-sentiment')
model.predict(text)
print(s.individual_sentiments)

Result:

[('i', 0.08), ('do', 0.09), ('like', 1.0), ('you', -0.32)]

I'm not trying to train my own model, but rather use a pre trained one like in the code example above.

Note: I'm not bound to flair, if a different framework with this functionality exists I'll be happy to know about it as well. I'm trying to use flair because it out performed Textblob and nltk's VADER in accuracy when I tested it.


Solution

  • The article actually has a link to a colab notebook at the bottom which I missed. It appears the way this is achieved in that article is just by classifying each word separately in addition to the whole sentence.