]> git.somenet.org - pub/jan/aic18.git/blob - service-analysis/analysis.py
Add online @ info + little polishing.
[pub/jan/aic18.git] / service-analysis / analysis.py
1 # -*- coding: utf-8 -*-
2 import json
3 import indicoio
4
5 from flask import request
6 from flask_restful import Resource, abort
7
8 input_error_409 = 'Input must be a list of JSON objects. JSON objects must contain contain key text.'
9 service_error_503 = 'The sentiment analysis service is currently unavailable.'
10
11 class Analysis(Resource):
12         def get(self):
13                 return "POST a list of JSON objects as content-type: application/json. JSON objects must contain key 'text'.", 405
14                 
15         def post(self):
16                 if not request.json or not isinstance(request.json, (list,)):
17                         return abort(409, message=input_error_409)
18                 texts = request.json
19                 text_array = []
20                 value = 0
21                 for text in texts:
22                         if 'text' not in text:
23                                 return abort(409, message=input_error_409)
24                         text_array.append(text['text'])
25                 try:
26                         for sentiment_value in indicoio.sentiment(text_array):
27                                 value += sentiment_value
28                 except:
29                         return abort(503, message=service_error_503)
30                 sentiment = value/len(text_array)
31                 data = {'sentiment': sentiment}
32                 return data