Written by Robert Kuykendall, @startcomics.

Simple sentiment analysis in python using AFINN

15 September 2015


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!')

In Danish:

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

With emoticons:

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

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 at , @startcomics, or in the comments below.

Where to Start Reading More articles on my new website, Where to Start Reading: