]> git.somenet.org - pub/jan/aic18.git/blob - service-analysis/sentiment_analysis.py
Implementation for sentiment analysis plus Dockerfile
[pub/jan/aic18.git] / service-analysis / sentiment_analysis.py
1 # -*- coding: utf-8 -*-
2 import json
3 import indicoio
4
5 from flask import Flask, request
6 from flask_restful import Resource, Api
7
8 app = Flask(__name__)
9 api = Api(app)
10
11 indicoio.config.api_key = '525f16078717a430f9dac17cdc9dbaa3'
12
13 class Sentiment_Analysis(Resource):
14         def get(self):
15                 return "correct usage: curl -H 'content-type: application/json' -X POST http://localhost:8081 -d '$your JSON here$'"
16                 
17         def post(self):
18                 tweets = request.json
19                 value = 0
20                 for tweet in tweets:
21                         #possible performance improvement: batch sentiment: indicoio.sentiment($list_of_texts$)
22                         sentiment_value = indicoio.sentiment(tweet['text'])
23                         value += sentiment_value
24                         #returns number between 0 and 1. 
25                         #it is a probability representing the likelihood that the analyzed text is positive or negative
26                         #values > 0.5 indicate positive sentiment
27                         #values < 0.5 indicate negative sentiment
28                 return value/len(tweets)
29                                 
30 api.add_resource(Sentiment_Analysis, '/')
31
32 if __name__ == '__main__':
33         app.run(host="0.0.0.0", port=8081, debug=False)