forked from CUBigDataClass/newsbuff-datafetch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentimentAPI.py
43 lines (31 loc) · 1.06 KB
/
sentimentAPI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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)