Vader: A sneak peek into a Python Sentiment Lexicon

Patrick Gichini
MindNinja
Published in
3 min readApr 19, 2019
source

Valence Aware Dictionary and sEntiment Reasoner is a lexicon and sentiment analysis tool. It is specifically tuned for social media text but can also work well with other data.

When you see the word lexicon, I hope you remember as I have always explained, there are two ways of doing Sentiment Analysis. By using Lexicons or other Machine Learning methodologies.

To use VADER, you’ll have to install it using pip

$pip install vadersentiment

After installing VADER, create a new file and write the code below:

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
analyser = SentimentIntensityAnalyzer()
def analyzer(phrase):
sentiment = analyser.polarity_scores(phrase)
print("{:-<40} {}".format(phrase, str(sentiment)))

The code above imports whatever we need from VADER. We are also defining a method to help us determine the polarity of our text.

To do the analysis, we call analyzer and give some text as our argument

analyzer("Life is awesome")

Our method returns four fields each with a percentage.

  1. ‘neg’ is for negative which in this case is zero percent
  2. ‘neu’ is for neutral which in this case is 33 percent
  3. ‘pos’ is for positive which in this case is 67 percent
  4. ‘compound’ is the overall polarity of the phrase which in this case is 62 percent

Now, let’s suppose you were to add an exclamation mark at the end of the phrase: “Life is awesome!”

From a linguistic perspective, this would mean that you are putting an emphasis on the awesome part of life. Now, can VADER be able to know that? Let’s give it a try

analyzer("Life is awesome")

As you can see, the exclamation results in an increase to our positive field from 62 to 69 percent. The neutral field reduces to 31 percent and negative is still zero. This results in the overall polarity of our phrase to increase from 62 to 66 percent. Amazing right?

Now let’s make it even more fun and add something else.

analyzer("Life is awesome! :-)")

As you can see, the polarity is even greater than before.

Now, what if we were to put a modern style emoji there? A typical message from a girlfriend and try to get the sentiments.

analyzer('''Hi babe 😊 how are you doing? 
Btw you still haven't given me my treat yet ☹
AND you drank all my yoghurt! 😡.
I'll see you later today and you better have my yoghurt
Have a lovely day, bye! love you 😘''')

That is a very long text with a lot of mixed feelings. Let’s see how VADER analyzes this

The polarity scores are not that bad. In general, this is a very positive message even though it has a few bad mood emojis.

VADER is also very good at analyzing slang and other nitty gritties that many lexicons will overlook.

Overall, I give it an awesome score!

Below is the code for the exercise which can also be found at this repo.

--

--

Patrick Gichini
MindNinja

Linux Ninja | Data Enthusiast | Sentimental Poet | Agent Boyfriend