%matplotlib inline
from pattern.en import sentiment
import matplotlib.pyplot as plt
fileObj = open('booklist.txt', 'r')
fileLines = map(
lambda x: x.strip(),
filter(
lambda x: x,
fileObj.readlines()
)
)
fileObj.close()
def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in xrange(0, len(l), n):
yield l[i:i+n]
def get_sentiment_by_graf(text, chunksize):
grafs = filter(lambda x: x, text.split('\n'))
grafChunks = list(chunks(grafs, chunksize))
grafChunkStrs = map(lambda x: '\n'.join(x), grafChunks)
sentlist = map(lambda x: sentiment(x)[0], grafChunkStrs)
return sentlist
bookSentDict = dict()
for filename in fileLines:
bookObj = open("books/"+filename, 'r')
bookText = bookObj.read()
bookObj.close()
bookTitle = filename.rsplit('.', 1)[0]
bookSentDict[bookTitle] = get_sentiment_by_graf(bookText, 10)
import json
with open('sentiment_by_graf.json', 'w') as outfile:
json.dump(bookSentDict, outfile)
for key in bookSentDict:
sentiments = map(lambda x: x, bookSentDict[key])
print key
plt.plot(sentiments)
plt.ylabel('sentiment')
plt.xlabel('paragraphs')
plt.show()