Sentiment Analysis with TextBlob

via this tutorial |10-6-19

In [113]:
from textblob import TextBlob
from IPython.display import display, HTML
import os
import pandas as pd
In [143]:
def get_data_from_files(path):
    directory = os.listdir(path)
    results = []
    for file in directory:
        f=open(path+file)
        results.append(f.read())
        f.close()
    return results

neg_k = get_data_from_files('AI_NEG/')
pos_k = get_data_from_files('AI_POS/')
neg_a = get_data_from_files('NEG/')
pos_a = get_data_from_files('POS/')
Out[143]:
['My dog is excited by the advancements in artificial intelligence.',
 "I'm excited for my child to grow up and have time to daydream because Artificial Intelligence has taken care of all the nitty gritty.",
 'I love artificial intelligence!',
 'Order my groceries, pay my taxes, take my kids to school?! Yes please! Artificial Intelligence has given me my life back!',
 "I'm grateful every day that my child will likely grow up in a cancer-free world, thanks to Artificial Intelligence. "]
In [139]:
def get_pn(num):
    return 'neg' if num < 0 else 'pos'

def get_sentiment(array, label):
    blobs = [[TextBlob(text), text] for text in array]
    return ([{'label': label,
              'prediction': get_pn(obj.sentiment.polarity),
              'sentiment': obj.sentiment.polarity,
              'length': len(text), 
              'excerpt': text[:50]} for obj,text in blobs])

CASE STUDY 1: Kendra's Data

In [140]:
display(pd.DataFrame(get_sentiment(neg_k, 'neg')))
display(pd.DataFrame(get_sentiment(pos_k, 'pos')))
label prediction sentiment length excerpt
0 neg neg -0.157143 76 WHERE ARE THE JOBS?! OH THAT'S RIGHT. ARTIFICI...
1 neg neg -0.750000 96 How can we trust Artificial Intelligence to dr...
2 neg neg -0.775000 31 I hate artificial intelligence!
3 neg neg -0.750000 47 My dog is terrified by artificial intelligence!
4 neg neg -0.750000 68 Artificial intelligence is going to melt the b...
label prediction sentiment length excerpt
0 pos neg -0.112500 65 My dog is excited by the advancements in artif...
1 pos neg -0.075000 133 I'm excited for my child to grow up and have t...
2 pos neg -0.125000 31 I love artificial intelligence!
3 pos neg -0.300000 121 Order my groceries, pay my taxes, take my kids...
4 pos neg -0.133333 116 I'm grateful every day that my child will like...

CASE STUDY 2: Amy's Data

In [141]:
display(pd.DataFrame(get_sentiment(neg_a, 'neg')))
display(pd.DataFrame(get_sentiment(pos_a, 'pos')))
label prediction sentiment length excerpt
0 neg neg -0.054577 3554 that's exactly how long the movie felt to me ....
1 neg pos 0.025467 2929 " quest for camelot " is warner bros . ' firs...
2 neg pos 0.003334 3365 so ask yourself what " 8mm " ( " eight millime...
3 neg pos 0.022925 4418 synopsis : a mentally unstable man undergoing ...
4 neg pos 0.043234 3911 capsule : in 2176 on the planet mars police ta...
label prediction sentiment length excerpt
0 pos pos 0.023663 4227 films adapted from comic books have had plenty...
1 pos pos 0.131092 2421 you've got mail works alot better than it dese...
2 pos pos 0.110626 6092 " jaws " is a rare film that grabs your atten...
3 pos pos 0.103847 4096 every now and then a movie comes along from a ...
4 pos neg -0.070151 3898 moviemaking is a lot like being the general ma...
In [ ]: