Sentiment Analysis with TextBlob

via this tutorial |10-6-19

CASE STUDY 1: Kendra's fake data

In [68]:
from textblob import TextBlob
from IPython.display import display, HTML
import os
import pandas as pd
In [95]:
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 [106]:
# ================================
# VERSION TWO:
# def get_sentiment(array):
#     for text in array:
#         obj = TextBlob(text)
#         sentiment = obj.sentiment.polarity
#         print(sentiment)
# get_sentiment(negative_alltext_ken)
# get_sentiment(positive_alltext_ken)
# ================================
# VERSION THREE:
def get_sentiment(array):
    blobs = [TextBlob(text) for text in array]
    return ([{'sentiment': obj.sentiment.polarity, 
              'length': len(text), 
              'excerpt': text[:50], 
              'tags': obj.tags} for obj in blobs])

# ================================
# VERSION TWO:
# neg_k_sentiment = get_sentiment(neg_k)
# pos_k_sentiment = get_sentiment(pos_k)
# neg_a_sentiment = get_sentiment(neg_a)
# pos_a_sentiment = get_sentiment(pos_a)

# ================================
# VERSION THREE:
display(pd.DataFrame(get_sentiment(neg_k)))
display(pd.DataFrame(get_sentiment(pos_k)))
display(pd.DataFrame(get_sentiment(neg_a)))
display(pd.DataFrame(get_sentiment(pos_a)))
sentiment length excerpt tags
0 -0.157143 3898 moviemaking is a lot like being the general ma... [(WHERE, WRB), (ARE, PDT), (THE, DT), (JOBS, N...
1 -0.750000 3898 moviemaking is a lot like being the general ma... [(How, WRB), (can, MD), (we, PRP), (trust, VB)...
2 -0.775000 3898 moviemaking is a lot like being the general ma... [(I, PRP), (hate, VBP), (artificial, JJ), (int...
3 -0.750000 3898 moviemaking is a lot like being the general ma... [(My, PRP$), (dog, NN), (is, VBZ), (terrified,...
4 -0.750000 3898 moviemaking is a lot like being the general ma... [(Artificial, JJ), (intelligence, NN), (is, VB...
sentiment length excerpt tags
0 -0.112500 3898 moviemaking is a lot like being the general ma... [(My, PRP$), (dog, NN), (is, VBZ), (excited, V...
1 -0.075000 3898 moviemaking is a lot like being the general ma... [(I, PRP), ('m, VBP), (excited, JJ), (for, IN)...
2 -0.125000 3898 moviemaking is a lot like being the general ma... [(I, PRP), (love, VBP), (artificial, JJ), (int...
3 -0.300000 3898 moviemaking is a lot like being the general ma... [(Order, NN), (my, PRP$), (groceries, NNS), (p...
4 -0.133333 3898 moviemaking is a lot like being the general ma... [(I, PRP), ('m, VBP), (grateful, JJ), (every, ...
sentiment length excerpt tags
0 -0.054577 3898 moviemaking is a lot like being the general ma... [(that, DT), ('s, VBZ), (exactly, RB), (how, W...
1 0.025467 3898 moviemaking is a lot like being the general ma... [(quest, JJS), (for, IN), (camelot, NN), (is, ...
2 0.003334 3898 moviemaking is a lot like being the general ma... [(so, RB), (ask, VB), (yourself, PRP), (what, ...
3 0.022925 3898 moviemaking is a lot like being the general ma... [(synopsis, NN), (a, DT), (mentally, RB), (uns...
4 0.043234 3898 moviemaking is a lot like being the general ma... [(capsule, NN), (in, IN), (2176, CD), (on, IN)...
sentiment length excerpt tags
0 0.023663 3898 moviemaking is a lot like being the general ma... [(films, NNS), (adapted, VBD), (from, IN), (co...
1 0.131092 3898 moviemaking is a lot like being the general ma... [(you, PRP), ('ve, VBP), (got, VBN), (mail, NN...
2 0.110626 3898 moviemaking is a lot like being the general ma... [(jaws, NN), (is, VBZ), (a, DT), (rare, JJ), (...
3 0.103847 3898 moviemaking is a lot like being the general ma... [(every, DT), (now, RB), (and, CC), (then, RB)...
4 -0.070151 3898 moviemaking is a lot like being the general ma... [(moviemaking, NN), (is, VBZ), (a, DT), (lot, ...
In [ ]: