-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
52 lines (42 loc) · 1.54 KB
/
app.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
44
45
46
47
48
49
50
51
52
#app.py
from flask import Flask, request #import main Flask class and request object
app = Flask(__name__) #create the Flask app
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.config['SESSION_TYPE'] = 'filesystem'
def getSentiments(inputJson):
headers = {
# Request headers
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': '91744f48b62b4c4ea23e4102d0fc282b',
}
params = urllib.parse.urlencode({
})
try:
conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')
conn.request("POST", "/text/analytics/v2.0/sentiment?%s" % params, inputJson, headers)
response = conn.getresponse()
data = json.loads(response.read())
conn.close()
textScoreList = data["documents"]
addEmojis(textScoreList)
return textScoreList
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))
def getEmoji(score):
emoji = "invalid"
scoreBounds = {0.2: 'extreme-sad.png', 0.4: 'normal-sad.png', 0.6: 'neutral-face.png', 0.8: "normal-happy.png", 1: "extreme-happy.png"}
for scoreIdx in sorted(scoreBounds.keys()):
if(score <= scoreIdx):
emoji = scoreBounds[scoreIdx]
break
return emoji
def addEmojis(textScoreList):
for dictObj in textScoreList:
dictObj['emoji'] = getEmoji(dictObj['score'])
@app.route('/get_sentiment', methods=['POST'])
def get_sentiment():
req_data = request.get_json()
dictionary = getSentiments(json.dumps(req_data))
return "<h1>Dictionary: {}</h1>".format(dictionary)
if __name__ == '__main__':
app.run(debug=True, port=5000)