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/')
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])
display(pd.DataFrame(get_sentiment(neg_k, 'neg')))
display(pd.DataFrame(get_sentiment(pos_k, 'pos')))
display(pd.DataFrame(get_sentiment(neg_a, 'neg')))
display(pd.DataFrame(get_sentiment(pos_a, 'pos')))