Implementation for sentiment analysis plus Dockerfile
authorSebastian Steiner <e1029038@student.tuwien.ac.at>
Thu, 8 Nov 2018 16:35:13 +0000 (17:35 +0100)
committerSebastian Steiner <e1029038@student.tuwien.ac.at>
Thu, 8 Nov 2018 16:35:13 +0000 (17:35 +0100)
service-analysis/Dockerfile
service-analysis/sentiment_analysis.py [new file with mode: 0644]

index b09b037ca229e61329248e89bc2f9d8c4dc77b79..976abd116501890f71027063d4323dba04e06127 100644 (file)
@@ -1 +1,13 @@
-FROM alpine:latest
+FROM   python:3
+LABEL  maintainer="Sebastian Steiner"
+
+RUN            pip install flask
+RUN            pip install flask_restful
+RUN            pip install indicoio
+
+WORKDIR        /app
+COPY   . /app/
+
+RUN            chmod a+x *.py
+
+CMD            ["python3", "./sentiment_analysis.py"]
diff --git a/service-analysis/sentiment_analysis.py b/service-analysis/sentiment_analysis.py
new file mode 100644 (file)
index 0000000..d9836af
--- /dev/null
@@ -0,0 +1,33 @@
+# -*- coding: utf-8 -*-
+import json
+import indicoio
+
+from flask import Flask, request
+from flask_restful import Resource, Api
+
+app = Flask(__name__)
+api = Api(app)
+
+indicoio.config.api_key = '525f16078717a430f9dac17cdc9dbaa3'
+
+class Sentiment_Analysis(Resource):
+       def get(self):
+               return "correct usage: curl -H 'content-type: application/json' -X POST http://localhost:8081 -d '$your JSON here$'"
+               
+       def post(self):
+               tweets = request.json
+               value = 0
+               for tweet in tweets:
+                       #possible performance improvement: batch sentiment: indicoio.sentiment($list_of_texts$)
+                       sentiment_value = indicoio.sentiment(tweet['text'])
+                       value += sentiment_value
+                       #returns number between 0 and 1. 
+                       #it is a probability representing the likelihood that the analyzed text is positive or negative
+                       #values > 0.5 indicate positive sentiment
+                       #values < 0.5 indicate negative sentiment
+               return value/len(tweets)
+                               
+api.add_resource(Sentiment_Analysis, '/')
+
+if __name__ == '__main__':
+       app.run(host="0.0.0.0", port=8081, debug=False)