Skip to content

Commit

Permalink
Adding sentiment analysis REST endpoint to analyze the sentiment of n…
Browse files Browse the repository at this point in the history
…ews abstract.
  • Loading branch information
anushab97 committed Apr 13, 2022
1 parent c3f8570 commit 5988f05
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions sentimentAPI.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 5988f05

Please sign in to comment.