-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding sentiment analysis REST endpoint to analyze the sentiment of n…
…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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |