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 [124]:
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/')
In [136]:
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], 
              'tags': obj.tags} for obj,text in blobs])

CASE STUDY 1: Kendra's Data

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

CASE STUDY 2: Amy's Data

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