`GET`: `/health` Returns HTTP Status 200 if service is running
-`POST`: `/` Calculates the average sentiment for a given list of texts
+`POST`: `/` Calculates the average sentiment for a given list of texts using the online sentiment analysis tool indico (https://www.indico.io).
- param: Term[ ] as Content-Tye: `application/json`
example body of request:
```json
}
]
```
+`POST`: `/offline_analysis` Calculates the average sentiment for a given list of texts using TextBlob (https://textblob.readthedocs.io)
+- param: Term[ ] as Content-Tye: `application/json`
+example body of request:
+```json
+[
+ {
+ 'text': 'sample text 1',
+ },
+ {
+ 'text': 'sample text 2',
+ },
+ {
+ 'text': 'sample text 3',
+ }
+]
+```
JSON string may contain more value-key pairs than 'text', but 'text' is needed for the sentiment analysis. Everything else will be ignored.
For more examples see curl commands in local -> commands.
- `503`: indico sentiment analysis not available
## external services
-This sentiment analysis service uses and relies on the online sentiment analysis indico (https://www.indico.io).
-The api key can be set in the file indico_api_key.txt. The current key does work, however excessive use of this service will require you to provide your own key, since the amount of requests for this key is limited.
+This sentiment analysis service uses the online sentiment analysis indico (https://www.indico.io).
+The indico api key can be set in the file indico_api_key.txt. The current key does work, however excessive use of this service will require you to provide your own key, since the amount of requests for this key is limited.
+In case of an 503 error, you can use the `/offline_analysis`.
## run with docker
- `docker build -t sentiment_analysis .`
- pip: indicoio
- pip: flask
- pip: flask_restful
+- pip: textblob
### commands
-- `python3.7 sentiment_analysis.py`
-- positive sentiment test example: `curl -v -H 'content-type: application/json' -X POST http://localhost:8081 -d '[{"text":"happy birthday, i love you"}]'`
-- negative sentiment test example: `curl -v -H 'content-type: application/json' -X POST http://localhost:8081 -d '[{"text":"i hate you, please die"}]'`
+- `python3.7 sentiment_analysis.py` to start the service
+- positive online sentiment test example: `curl -v -H 'content-type: application/json' -X POST http://localhost:8081 -d '[{"text":"happy birthday, i love you"}]'`
+- negative online sentiment test example: `curl -v -H 'content-type: application/json' -X POST http://localhost:8081 -d '[{"text":"i hate you, please die"}]'`
+- positive offline sentiment test example: `curl -v -H 'content-type: application/json' -X POST http://localhost:8081/offline_analysis -d '[{"text":"happy birthday, i love you"}]'`
+- negative offline sentiment test example: `curl -v -H 'content-type: application/json' -X POST http://localhost:8081/offline_analysis -d '[{"text":"i hate you, please die"}]'`
- health check test example: `curl -v -X GET http://localhost:8081/health`
--- /dev/null
+# -*- coding: utf-8 -*-
+import json
+
+from flask import request
+from flask_restful import Resource, abort
+from textblob import TextBlob
+
+input_error_409 = 'Input must be a list of JSON objects. JSON objects must contain contain key text.'
+
+class Offline_Analysis(Resource):
+ def get(self):
+ return "POST a list of JSON objects as content-type: application/json. JSON objects must contain key 'text'.", 405
+
+ def post(self):
+ if not request.json or not isinstance(request.json, (list,)):
+ return abort(409, message=input_error_409)
+ texts = request.json
+ value = 0
+ for text in texts:
+ if 'text' not in text:
+ return abort(409, message=input_error_409)
+ analysis = TextBlob(text['text'])
+ value += (analysis.sentiment.polarity/2) + 0.5
+ sentiment = value/len(texts)
+ data = {'sentiment': sentiment}
+ return data
import indicoio
from analysis import Analysis
+from offline_analysis import Offline_Analysis
from health_check import Health_Check
from flask import Flask
indicoio.config.api_key = key.read().replace('\n','')
api.add_resource(Analysis, '/')
+api.add_resource(Offline_Analysis, '/offline_analysis')
api.add_resource(Health_Check, '/health')
if __name__ == '__main__':