The Wayback Machine - https://web.archive.org/web/20211207143627/https://www.geeksforgeeks.org/python-sentiment-analysis-using-affin/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python – Sentiment Analysis using Affin

  • Last Updated : 26 Nov, 2020

Afinn is the simplest yet popular lexicons used for sentiment analysis developed by Finn Årup Nielsen. It contains 3300+ words with a polarity score associated with each word. In python, there is an in-built function for this lexicon.

Let’s see its syntax-

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Installing the library:






# code
print("GFG")
pip install afinn / 
#instaalling in windows
pip3 install afinn /
#installing in linux
!pip install afinn
#installing in jupyter

Code: Python code for sentiment analysis using Affin




#importing necessary libraries
from afinn import Afinn
import pandas as pd
  
#instantiate afinn
afn = Afinn()
  
#creating list sentences
news_df = ['les gens pensent aux chiens','i hate flowers',
         'hes kind and smart','we are kind to good people']
           
# compute scores (polarity) and labels
scores = [afn.score(article) for article in news_df]
sentiment = ['positive' if score > 0 
                          else 'negative' if score < 0 
                              else 'neutral' 
                                  for score in scores]
      
# dataframe creation
df = pd.DataFrame()
df['topic'] =  news_df
df['scores'] = scores
df['sentiments'] = sentiment
print(df)

Output:

topic  scores sentiments
0  les gens pensent aux chiens     0.0    neutral
1               i hate flowers    -3.0   negative
2           hes kind and smart     3.0   positive
3   we are kind to good people     5.0   positive

The best part of this library package is that one can find score sentiment of different languages as well.




afn = Afinn(language = 'da')
  
#assigning 'da' danish to the object variable.
afn.score('du er den mest modbydelige tæve')

Output:

-5.0

Thus, Afinn can we used easily to get scores immediately.




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!