/
app.py
158 lines (132 loc) · 4.79 KB
/
app.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from flask import Flask, render_template, request, redirect
import simplejson as json
from operator import itemgetter
import datetime
from bokeh.embed import components
from bokeh.plotting import figure
from bokeh.resources import INLINE
from bokeh.util.string import encode_utf8
# from __future__ import print_function
app = Flask(__name__)
stopwords = ['a', 'about', 'after', 'all', 'also', 'am', 'an', 'and', 'any', 'are', 'as', 'at', 'back', 'be', 'because', 'been', 'better', 'bought', 'but', 'buy', 'by', 'can', "can't", 'could', 'did', 'do', 'does', "don't", 'even', 'first', 'for', 'from', 'get', 'go', 'good', 'got', 'great', 'had', 'has', 'have', 'how', "i'm", "i've", 'if', 'in', 'is', 'it', "it's", 'its', 'just', 'like', 'lot', 'love', 'many', 'me', 'more', 'most', 'much', 'my', 'need', 'new', 'no', 'not', 'now', 'of', 'on', 'one', 'only', 'or', 'other', 'out', 'product', 'really', 'so', 'some', 'still', 'tablet', 'take', 'than', 'that', 'the', 'them', 'then', 'there', 'they', 'thing', 'this', 'to', 'too', 'up', 'use', 'using', 'very', 'want', 'was', 'we','well', 'what', 'when', 'which', 'will', 'with', 'would', 'you', 'your', 'ipad']
plotkey = 'app'
def parse_text(text):
d = {}
for word in text.rsplit():
if word.isdigit():
continue
wordL = word.lower()
if wordL.endswith('.'):
wordL = wordL[:-1]
if len(wordL) < 2:
continue
if wordL in stopwords:
continue
try:
d2 = d[wordL]
except KeyError:
d2 = 0
d[wordL] = d2
d[wordL] = d2 + 1
for key in list(d.keys()):
if key.endswith('s'):
keys = key[:-1]
if keys in d:
d[keys] += d[key]
del d[key]
return d
@app.route('/')
def main():
return redirect('/index')
@app.route('/index',methods=['GET', 'POST'])
def index():
# if request.method == 'GET':
return render_template('index.html')
# else:
# return redirect('/wcloud')
@app.route('/wcloud')
def wc():
fid = open('static/tabs.json')
reviews=''
for dt in fid:
data = json.loads(dt)
if data['ProductInfo']['Name']:
for rv in range(len(data['Reviews'])):
if data['Reviews'][rv]['Content']:
if data['Reviews'][rv]['Content'].find('function()') < 0:
reviews = reviews + ' ' + data['Reviews'][rv]['Content']
f = sorted(parse_text(reviews).items(),key=itemgetter(1),reverse=True)
bwo = {}
for c in range(30):
bwo[str(f[c][0])] = f[c][1]
return render_template('wcplot.html', wcdata=bwo)
@app.route('/wcpre')
def wcp():
return render_template('showplot.html', title="Prealculated Word Cloud",imgplot="wcloud.png")
@app.route('/wctime_app')
def wtap():
global plotkey
plotkey = 'app'
return redirect('/wctime')
@app.route('/wctime_price')
def wtpr():
global plotkey
plotkey = 'price'
return redirect('/wctime')
@app.route('/wctime_screen')
def wtsc():
global plotkey
plotkey = 'screen'
print plotkey
return redirect('/wctime')
@app.route('/wctime')
def wt():
fid = open('static/apple.json')
reviews=''
times=[]
timrev=[]
data = json.load(fid)
for rv in range(len(data['Reviews'])):
if data['Reviews'][rv]['Content']:
if data['Reviews'][rv]['Content'].find('function()') < 0:
times += [datetime.datetime.strptime(data['Reviews'][rv]['Date'],'%B %d, %Y')]
timrev +=[parse_text(data['Reviews'][rv]['Content'])]
reviews = reviews + ' ' + data['Reviews'][rv]['Content']
timrev = [timrev[i[0]] for i in sorted(enumerate(times),key=lambda x:x[1])]
times.sort()
print plotkey
dt = []
for x in range(len(times)):
try:
dt += [timrev[x][plotkey]]
except KeyError:
dt += [0]
fig = figure(title="Time evolution of: " + plotkey,x_axis_type="datetime",plot_width=550,plot_height=400)
fig.line(times, dt, line_width=2)
js_resources = INLINE.render_js()
css_resources = INLINE.render_css()
script, div = components(fig, INLINE)
html = render_template('timeplot.html',
js_resources=js_resources,
css_resources=css_resources,
plot_script=script,
plot_div=div
)
return encode_utf8(html)
# @app.route('/page')
# def get_page():
# return send_file('templates/progress.html')
#
# @app.route('/progress')
# def progress():
# def generate():
# x = 0
# while x < 100:
# print x
# x = x + 10
# # time.sleep(0.2)
# yield "data:" + str(x) + "\n\n"
# return Response(generate(), mimetype= 'text/event-stream')
if __name__ == '__main__':
app.debug = True
app.run(port=33507)