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_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])
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)))