]> git.somenet.org - pub/jan/aic18.git/blob - service-analysis/offline_analysis.py
Merge branch '47-style-website-with-s-css' of https://hyde.infosys.tuwien.ac.at/aic18...
[pub/jan/aic18.git] / service-analysis / offline_analysis.py
1 # -*- coding: utf-8 -*-
2 import json
3
4 from flask import request
5 from flask_restful import Resource, abort
6 from textblob import TextBlob
7
8 input_error_409 = 'Input must be a list of JSON objects. JSON objects must contain contain key text.'
9
10 class Offline_Analysis(Resource):
11         def get(self):
12                 return "POST a list of JSON objects as content-type: application/json. JSON objects must contain key 'text'.", 405
13                 
14         def post(self):
15                 if not request.json or not isinstance(request.json, (list,)):
16                         return abort(409, message=input_error_409)
17                 texts = request.json
18                 value = 0
19                 for text in texts:
20                         if 'text' not in text:
21                                 return abort(409, message=input_error_409)
22                         analysis = TextBlob(text['text'])
23                         value += (analysis.sentiment.polarity/2) + 0.5
24                 sentiment = value/len(texts)
25                 data = {'sentiment': sentiment}
26                 return data