From 5988f05bf5570c26e51739a6de2574f57dd202aa Mon Sep 17 00:00:00 2001 From: anushab97 Date: Tue, 12 Apr 2022 23:07:45 -0600 Subject: [PATCH] Adding sentiment analysis REST endpoint to analyze the sentiment of news abstract. --- sentimentAPI.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 sentimentAPI.py diff --git a/sentimentAPI.py b/sentimentAPI.py new file mode 100644 index 0000000..9cc7b92 --- /dev/null +++ b/sentimentAPI.py @@ -0,0 +1,43 @@ +from flask import Flask, request, Response, jsonify +import requests +import json +import jsonpickle +import logging +import codecs + +from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer + + +# Initialize the Flask application +app = Flask(__name__) + +log = logging.getLogger('werkzeug') +log.setLevel(logging.DEBUG) + + +def getSentiment(sentence): + sid_obj = SentimentIntensityAnalyzer() + sentiment_dict = sid_obj.polarity_scores(sentence) + sentimentScore = sentiment_dict['compound'] + return sentimentScore + + +@app.route('/apiv1/sentiment', methods=[ 'GET']) +def sentiment(): + sentences = json.loads(request.data) + print(sentences) + sentences = list(sentences) + response = [] + + try: + for sentence in sentences: + score = getSentiment(sentence) + response.append(score) + + except: + print("Request to sentiment endpoint was unsuccessful.") + + response_pickled = jsonpickle.encode(response) + return Response(response=response_pickled, status=200, mimetype="application/json") + +app.run(host="0.0.0.0", port=5000) \ No newline at end of file