Pre-trained Python model for Sentiment Analysis

What is Sentiment Analysis?

Sentiment Analysis is the process of extracting information from a message to determine its tone (positive, negative, neutral, etc.) or intensity (super happy, somewhat happy, just happy, etc.).

How to use VADER

VADER stands for Valence Aware Dictionary and sEntiment Reasoner. It is an open-source project that was specifically trained with content posted on social media. You can check out the source code here.

Step 1. Install VADER

First you need to install the library, so open up a new Terminal and:

% pip install vaderSentiment

Step 2. Use the model

Open a Python file, import the library and start classifying text!

# Import sentiment analyzer
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
# Initialize model
analyzer = SentimentIntensityAnalyzer()
# Declare some text
angry_review = 'The food was disgusting. I am never coming back here again!!'
# Analyze the text with polarityScores
> {'compound': -0.6103, 'neg': 0.285, 'neu': 0.715, 'pos': 0.0}

Step 3. Turn the model into a Classifier

You can turn the score into a binary classifier by defining a cutoff point such that compound scores above this threshold are classified as positive (1), while compound scores below it are classified as non-positive (0).

def classify_positive(text, threshold=0):
# Score text
score = analyzer.polarity_scores(text)
# Get compound score from dictionary
score = score.get('compound')
# Classify text according to threshold
if score >= threshold:
pred_class = 1
pred_class = 0
# Return prediciton
return pred_class
# Test function on an angry review
print('Predicted class:', classify_positive(text=angry_review, threshold=0))
> Predicted class: 0

Further steps

If you want to take this one step further, you can manually label a few texts as positive (1) or non-positive (0), get their compound scores and choose the cutoff point that maximizes the accuracy (or some other metric).

Closing Remarks

VADER is an extremely flexible model trained for Sentiment Analysis. It can be easily turned into a classifier and can further learn new words to adapt to new kinds of slang.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store