From edf62ab1509359e71210bbf5749b051209e81fee Mon Sep 17 00:00:00 2001 From: Austin Frey Date: Tue, 2 May 2017 08:03:13 -0400 Subject: [PATCH] updated sentiment analysis function to handle multiple sentences and fix decoding issue Added MakerShift to community.md Update community.md commented reload(sys) --- sample-functions/SentimentAnalysis/handler.py | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/sample-functions/SentimentAnalysis/handler.py b/sample-functions/SentimentAnalysis/handler.py index 707640e6..e995dcc5 100644 --- a/sample-functions/SentimentAnalysis/handler.py +++ b/sample-functions/SentimentAnalysis/handler.py @@ -1,17 +1,32 @@ import sys +import json from textblob import TextBlob +# set default encoding to UTF-8 to eliminate decoding errors +reload(sys) +sys.setdefaultencoding('utf8') + def get_stdin(): buf = "" for line in sys.stdin: buf = buf + line - return buf + return buf if(__name__ == "__main__"): st = get_stdin() blob = TextBlob(st) - out ="" - for sentence in blob.sentences: - out = out + "Polarity: " + str(sentence.sentiment.polarity) + " Subjectivity: " + str(sentence.sentiment.subjectivity) + "\n" - print(out) + res = { + "polarity": 0, + "subjectivity": 0 + } + for sentence in blob.sentences: + res["subjectivity"] = res["subjectivity"] + sentence.sentiment.subjectivity + res["polarity"] = res["polarity"] + sentence.sentiment.polarity + + total = len(blob.sentences) + + res["sentence_count"] = total + res["polarity"] = res["polarity"] / total + res["subjectivity"] = res["subjectivity"] / total + print(json.dumps(res))