-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.py
111 lines (83 loc) · 2.36 KB
/
analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
import re
import os
import time
import json
import logging
from textblob import TextBlob
from flask import Flask
from flask import request
from flask import make_response
app = Flask(__name__)
cache = {}
cache_primed = False
cache_filename = os.getenv("CACHE_LOCATION", "./_cache/cache")
def add_to_cache(text, sent):
global cache
with open(cache_filename, 'w+') as cache_backing:
cache[text] = sent
json.dump(cache, cache_backing)
def flush_cache():
global cache
with open(cache_filename, 'w+') as cache_backing:
cache = {}
json.dump(cache, cache_backing)
def prime_cache():
global cache_primed
global cache
if not cache_primed and os.path.exists(cache_filename):
with open(cache_filename, 'r+') as cache_backing:
cache = json.load(cache_backing)
cache_primed = True
print "Cache primed to {}".format(cache)
def clean_input(text):
'''
http://www.geeksforgeeks.org/twitter-sentiment-analysis-using-python/
'''
return ' '.join(re.sub(
"(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)",
" ", text).split())
def get_sentiment(text):
prime_cache()
ret = {}
if text in cache:
ret = cache[text]
ret["cache_hit"] = True
tb = TextBlob(text)
polarity = tb.sentiment.polarity
ret["polarity"] = polarity
if polarity > 0:
ret["analysis"] = "positive"
elif polarity == 0:
ret["analysis"] = "neutral"
elif polarity < 0:
ret["analysis"] = "negative"
else:
raise "Unacceptable polarity"
add_to_cache(text, ret)
return ret
@app.route('/', methods=["POST"])
def do_anal():
if request.form["text"]:
sent = get_sentiment(clean_input(request.form["text"]))
sent["timestamp"] = time.time()
sent["text"] = request.form["text"]
ret = json.dumps(sent)
else:
ret = json.dumps({"error": "no input"})
r = make_response(ret)
r.mimetype = 'application/json'
return r
@app.route('/list')
def list_cache():
r = make_response(json.dumps(cache.keys()))
r.mimetype = 'application/json'
return r
@app.route('/flush')
def flush():
flush_cache()
return list_cache()
if __name__ == '__main__':
t = raw_input()
print clean_input(t)
print get_sentiment(clean_input(t))