Sentiment Analysis with TextBlob

via this tutorial |10-6-19

CASE STUDY 1: Kendra's fake data

In [21]:
from textblob import TextBlob
In [22]:
import os
import pandas as pd
negative = os.listdir('AI_NEG/')
positive = os.listdir('AI_POS/')
positive_alltext = []
for file in positive:
    f=open('AI_POS/'+file)
    content=f.read()
    positive_alltext.append(content)
    f.close()

negative_alltext = []
for file in negative:
    f=open('AI_NEG/'+file)
    content=f.read()
    negative_alltext.append(content)
    f.close()
In [23]:
for text in positive_alltext:
    obj = TextBlob(text)
    sentiment = obj.sentiment.polarity
    print(sentiment)
-0.11249999999999999
-0.075
-0.125
-0.3
-0.13333333333333333
In [24]:
for text in negative_alltext:
    obj = TextBlob(text)
    sentiment = obj.sentiment.polarity
    print(sentiment)
-0.15714285714285714
-0.75
-0.775
-0.75
-0.75

CASE STUDY 2: Amy's fake data

In [25]:
negative = os.listdir('NEG/')
positive = os.listdir('POS/')
positive_alltext = []
for file in positive:
    f=open('POS/'+file)
    content=f.read()
    positive_alltext.append(content)
    f.close()

negative_alltext = []
for file in negative:
    f=open('NEG/'+file)
    content=f.read()
    negative_alltext.append(content)
    f.close()
In [26]:
for text in positive_alltext:
    obj = TextBlob(text)
    sentiment = obj.sentiment.polarity
    print(sentiment)
0.02366334963737561
0.13109217171717172
0.1106257682646571
0.103847233495671
-0.07015070346320346
In [27]:
for text in negative_alltext:
    obj = TextBlob(text)
    sentiment = obj.sentiment.polarity
    print(sentiment)
-0.05457711442786068
0.025467462644882
0.0033339838667707585
0.022924603174603177
0.04323376623376625