Written by Robert Kuykendall

Simple sentiment analysis in python using AFINN

15 September 2015

Background

For the project Map World News, I needed to do sentiment analysis on articles, to show the map above. A popular options is to use the NLTK library, but it can be difficult to deploy.

I originally used the amazing Data Science Toolkit by Pete Warden, which provides a set of simple HTTP APIs, and a python library to make it even simpler. However DSTK is not local, so processing a lot of different items is very time consuming.

I learned that DSTK used a simple algorithm and a wordlist from Finn Årup Nielsen, and implemented it myself on Map World News. However, that was a little bit of work, and things have just gotten even easier:

Fast, local, sentiment analysis in Python

Finn Årup Nielsen has just released a Python module named AFINN to add sentiment analysis using his wordlist to your project, and it could not be easier:

>>> from afinn import Afinn
>>> afinn = Afinn()
>>> afinn.score('This is utterly excellent!')
3.0

In Danish:

>>> afinn = Afinn(language='da')
>>> afinn.score('Hvis ikke det er det mest afskyelige flueknepperi...')
-6.0

With emoticons:

>>> afinn = Afinn(emoticons=True)
>>> afinn.score('I saw that yesterday :)')
2.0

To install AFINN, simply run

pip install afinn

You can see AFINN working on Map World News in /api/article.py.

Robert Kuykendall — Say hi or in the comments below.