via this tutorial |10-6-19
from textblob import TextBlob
from IPython.display import display, HTML
import os
import pandas as pd
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/')
# ================================
# 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)))