From 2c758dfa55a2f817fe846e8648106e1845e10150 Mon Sep 17 00:00:00 2001 From: Sebastian Steiner Date: Thu, 8 Nov 2018 17:35:13 +0100 Subject: [PATCH] Implementation for sentiment analysis plus Dockerfile --- service-analysis/Dockerfile | 14 ++++++++++- service-analysis/sentiment_analysis.py | 33 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 service-analysis/sentiment_analysis.py diff --git a/service-analysis/Dockerfile b/service-analysis/Dockerfile index b09b037..976abd1 100644 --- a/service-analysis/Dockerfile +++ b/service-analysis/Dockerfile @@ -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 index 0000000..d9836af --- /dev/null +++ b/service-analysis/sentiment_analysis.py @@ -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) -- 2.43.0