Discover Sentiment Analysis

We'll start with a dictionnary based approach with the textblob library.

Note that using textblob for sentiment analysis is fine for a quick and dirty sentiment scoring but would not be reliable in a more serious project. The results are too dependent on polarity scores of specific words.

We start by installing the library

In [1]:
# uncomment to install textblob

# !pip install -U textblob
# !python -m textblob.download_corpora
In [10]:
from textblob import TextBlob

text = "That was a narrow escape, Alice said. A good deal frightened at the sudden change. But very glad to find herself still in existence;"
blob = TextBlob(text)
print("Polarity \t Subjectivity \t Sentence")
for sentence in blob.sentences:
    print(f"{sentence.sentiment.polarity:8}\t {sentence.sentiment.subjectivity:12} \t {sentence.raw}")
Polarity 	 Subjectivity 	 Sentence
    -0.2	          0.4 	 That was a narrow escape, Alice said.
    0.35	         0.55 	 A good deal frightened at the sudden change.
    0.65	          1.0 	 But very glad to find herself still in existence;

The polarity score (0.35) of the second sentence "A good deal frightened at the sudden change." is not correct. It should be negative.

Let's see what's happening here by calculating the polarity for different versions of that sentence.

In [12]:
def polarity(text):
    polarity_score = TextBlob(text).sentences[0].sentiment.polarity
    print(f"{polarity_score:5} \t {text}")
    return 

# original sentence, positive
polarity("A good deal frightened at the change.")

# remove 'a good deal', you get neutral
polarity("Frightened at the change.")

# what if we add a negation, and change the noun
polarity("Happy at the change.")

# or add just the word very
polarity("Very frightened at the change.")
  0.7 	 A good deal frightened at the change.
  0.0 	 Frightened at the change.
  0.8 	 Happy at the change.
  0.2 	 Very frightened at the change.

Frightened is considered a verb and is not taken into the polarity score although it's a negative word.

The polarity score obtained with textblob is not very reliable.

Google NLP

Check out the Google NLP Quickstart documentation, found here, to learn how to register and obtain an API key.

In [ ]:
import requests
import json

key = { "key": "<Your API KEY here>"}

if key["key"] != "<Your API KEY here>":

    data = { 
        "document": {  
            "type":"PLAIN_TEXT", 
            "content":"Alice was very frightened." 
        }, 
        "encodingType":"UTF8" 
    }
    results = requests.post(url, params=key, json=data)

    content = results.content.decode('utf-8')
    json.loads(content)['documentSentiment']