forked from Athithyaa/T9WebApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
424 lines (335 loc) · 14.6 KB
/
application.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
from flask import Flask, render_template, request, redirect, session, jsonify
from datetime import timedelta
import uuid
from database.db import db_session, init_db
from models.Company import Company
from models.User import User
from models.Analytics import Analytics
from models.Review import Review
from forms import *
from watson_developer_cloud import ToneAnalyzerV3
from watson_developer_cloud import AlchemyLanguageV1
import json
from ma_schema.AnalyticsSchema import AnalyticsSchema
from ma_schema.UserSchema import UserSchema
from ma_schema.CompanySchema import CompanySchema
from ma_schema.ReviewSchema import ReviewSchema
import datetime
import uuid
from models.Analytics import Analytics
import httplib, urllib, base64
WATSON_USERNAME = '1be2c698-56e7-47d4-9944-6e4d81c9b07d',
WATSON_PASSWORD = 'sPr4XOsPtNSX'
ALCHEMY_API_KEY = '07551e54797d7b593f3595653a7cad5b1803d3a6'
app = Flask(__name__)
app.secret_key = 's3cr3t'
@app.before_request
def make_session_permanent():
session.permanent = True
app.permanent_session_lifetime = timedelta(minutes=30)
@app.teardown_request
def shutdown_session(exception=None):
db_session.remove()
@app.route('/')
def home():
if 'email' not in session:
return render_template('forms/login.html', form=LoginForm())
return render_template('pages/placeholder.home.html')
@app.route('/about')
def about():
if 'email' not in session:
return render_template('forms/login.html', form=LoginForm())
return render_template('pages/placeholder.about.html')
@app.route("/logout", methods=['GET', 'POST'])
def logout():
session.clear()
return render_template('forms/login.html', form=LoginForm())
@app.route("/company", methods=['POST', 'GET'])
def company():
cName = request.args.get("name")
return render_template('pages/placeholder.company.html', cName=cName)
@app.route('/getreviews', methods=['GET'])
def getreviews():
if 'email' not in session:
return render_template('forms/login.html', form=LoginForm())
rSchema = ReviewSchema()
reviews = Review.query.order_by(Review.date.desc())
rJson = []
for r in reviews:
res = rSchema.dump(r).data
res['companyname'] = r.company.name
rJson.append(res)
return jsonify({"count": len(rJson), "results": rJson})
def getnews():
company = "uber"
search_parameter = company + " workplace harassment"
headers = {
# Request headers
'Ocp-Apim-Subscription-Key': '72b2b2c8ecb14f7c8989803cc06969fe',
}
params = urllib.urlencode({
# Request parameters
'q': search_parameter,
'count': '5',
'offset': '0',
'mkt': 'en-us',
'safeSearch': 'Off',
})
try:
name = []
desc = []
url = []
conn = httplib.HTTPSConnection('api.cognitive.microsoft.com')
conn.request("GET", "/bing/v5.0/news/search?%s" % params, "{body}", headers)
response = conn.getresponse()
data = json.loads(response.read())
for i in range(len(data)):
name.append(str(data['value'][0]['name']))
desc.append(str(data['value'][0]['description']))
url.append(str(data['value'][0]['url']))
return {'name': name, 'desc': desc, 'url':url}
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))
return {'Error': 'Could not fetch any news!'}
@app.route('/postreview', methods=['GET', 'POST'])
def postreview():
if 'email' not in session:
return render_template('forms/login.html', form=LoginForm())
if request.method == 'POST':
company_name = request.form['company'].lower()
content = request.form['experience']
aSchema = AnalyticsSchema()
cSchema = CompanySchema()
rSchema = ReviewSchema()
email = session['email']
#sentJson = reqData['review']
id = ""
if not getCompany(company_name):
print "compnay not found, creating.."
cJson = constructCompany(company_name.lower())
com = cSchema.load(cJson, session=db_session).data
id = com.id
db_session.add(com)
review_count = 1
sentJson = constructNewAnalytics(id, content)
else:
print "company found"
comJson = getReviewCountForCompany(company_name.lower())
id = comJson['id']
count =comJson['r_cnt']
name = comJson['name']
existing_ana = getExistingAnalyticsForCompany(str(id))
sentJson = constructRunningAnalytics(existing_ana, count, content)
sentJson['id'] = str(existing_ana['id'])
count +=1
cJson = updateCompany(id, name, count)
com = cSchema.load(cJson, session=db_session).data
db_session.merge(com)
rJson = constructReview(email,id, content)
rev = rSchema.load(rJson, session=db_session).data
rev.company_id = rJson['company_id']
rev.user_id = rJson['user_id']
rev.date = str(datetime.datetime.now())
db_session.add(rev)
#print sentJson
ana = aSchema.load(sentJson, session=db_session).data
ana.company_id = id
db_session.merge(ana)
db_session.commit()
return render_template('pages/placeholder.post.html', form=PostReviewForm())
def constructReview(email,company_id, content):
rJson = {}
user = User.query.filter_by(email=email).first()
rJson['user_id'] = str(user.id)
rJson['company_id'] = str(company_id)
rJson['content'] = str(content)
rJson[u'id'] = str(uuid.uuid4())
return rJson
def updateCompany(id, name, count):
comJson = {}
comJson[u'id'] = str(id)
comJson['name'] = str(name)
comJson['r_cnt'] = count
return comJson
@app.route('/companyPage', methods=['GET','POST'])
def companyPage():
if 'email' not in session:
return render_template('forms/login.html', form=LoginForm())
if request.method == 'POST':
request_ans = request.get_data()
company_name = request_ans.split("+")[1]
cJson = getCompany(company_name)
comp_id = str(cJson['id'])
aJson = getExistingAnalyticsForCompany(comp_id)
emotional_tone = buildEmotionalDict(aJson)
social_tone = buildSocialDict(aJson)
sentiment_score = aJson['sentiment_score']
sentiment_type = aJson['sentiment_type']
#news_feed_data = getnews(company_name)
return jsonify({
'emotional': emotional_tone,
'social': social_tone,
'sentiment_score': sentiment_score,
'sentiment_type':sentiment_type
})
return render_template('forms/login.html', form=LoginForm())
def buildEmotionalDict(emJson):
emotional_tone ={}
emotional_tone['joy'] = emJson['joy']
emotional_tone['fear'] = emJson['fear']
emotional_tone['sadness'] = emJson['sadness']
emotional_tone['disgust'] = emJson['disgust']
emotional_tone['anger'] = emJson['anger']
return emotional_tone
def buildSocialDict(sJson):
social_tone ={}
social_tone['openness'] = sJson['openness']
social_tone['conscientiousness'] = sJson['conscientiousness']
social_tone['extraversion'] = sJson['extraversion']
social_tone['aggreablesness'] = sJson['aggreablesness']
social_tone['neuroticism'] = sJson['neuroticism']
return social_tone
def getExistingAnalyticsForCompany(company_id):
aSchema = AnalyticsSchema()
ana = Analytics.query.filter_by(company_id=company_id).first()
aJson = aSchema.dump(obj=ana).data
return aJson
def getCompany(company_name):
cSchema = CompanySchema()
comp = Company.query.filter_by(name=company_name).first()
if comp:
cJson = cSchema.dump(obj=comp).data
return cJson
else:
return None
def constructRunningAnalytics(existing_ana, current_review_count, content):
sentJson = {}
tone_analyzer = ToneAnalyzerV3(
username='1be2c698-56e7-47d4-9944-6e4d81c9b07d',
password=WATSON_PASSWORD,
version='2016-05-19')
alchemy_language = AlchemyLanguageV1(api_key='07551e54797d7b593f3595653a7cad5b1803d3a6')
sentiment = alchemy_language.sentiment(text=content)
sentiment_type = sentiment['docSentiment']['type']
if "neutral" in sentiment_type:
sentiment_score = 0
else:
sentiment_score = float((sentiment['docSentiment']['score']))
sentJson['sentiment_score'] = computeRunningAverage(existing_ana['sentiment_score'], sentiment_score, current_review_count)
if sentJson['sentiment_score'] > 0:
sentJson['sentiment_type'] = "positive"
elif sentJson['sentiment_score'] < 0:
sentJson['sentiment_type'] = "negative"
else:
sentJson['sentiment_type'] = "neutral"
sentimentData = tone_analyzer.tone(text=content)
sentJson['anger'] = computeRunningAverage(existing_ana['anger'],sentimentData["document_tone"]["tone_categories"][0]["tones"][0]["score"], current_review_count)
sentJson['disgust'] = computeRunningAverage(existing_ana['disgust'],sentimentData["document_tone"]["tone_categories"][0]["tones"][1]["score"], current_review_count)
sentJson['fear'] = computeRunningAverage(existing_ana['fear'],sentimentData["document_tone"]["tone_categories"][0]["tones"][2]["score"], current_review_count)
sentJson['joy'] = computeRunningAverage(existing_ana['joy'],sentimentData["document_tone"]["tone_categories"][0]["tones"][1]["score"], current_review_count)
sentJson['sadness'] = computeRunningAverage(existing_ana['sadness'],sentimentData["document_tone"]["tone_categories"][0]["tones"][1]["score"], current_review_count)
sentJson['openness'] = computeRunningAverage(existing_ana['openness'], sentimentData["document_tone"]["tone_categories"][2]["tones"][0]["score"], current_review_count)
sentJson['conscientiousness'] = computeRunningAverage(existing_ana['openness'], sentimentData["document_tone"]["tone_categories"][2]["tones"][1]["score"], current_review_count)
sentJson['extraversion'] = computeRunningAverage(existing_ana['openness'], sentimentData["document_tone"]["tone_categories"][2]["tones"][2]["score"], current_review_count)
sentJson['aggreablesness'] = computeRunningAverage(existing_ana['openness'], sentimentData["document_tone"]["tone_categories"][2]["tones"][3]["score"], current_review_count)
sentJson['neuroticism'] = computeRunningAverage(existing_ana['openness'], sentimentData["document_tone"]["tone_categories"][2]["tones"][0]["score"], current_review_count)
return sentJson
def computeRunningAverage(old_average, curr_average , count):
new_avg = float((old_average * count + curr_average)/(count + 1))
return new_avg
def constructNewAnalytics(id, content):
sentJson = {}
tone_analyzer = ToneAnalyzerV3(
username='1be2c698-56e7-47d4-9944-6e4d81c9b07d',
password=WATSON_PASSWORD,
version='2016-05-19')
alchemy_language = AlchemyLanguageV1(api_key='07551e54797d7b593f3595653a7cad5b1803d3a6')
sentiment = alchemy_language.sentiment(text=content)
sentJson['id'] = str(uuid.uuid1())
sentJson['company_id'] = id
sentJson['sentiment_score'] = 0
sentJson['sentiment_type'] = "neutral"
sentJson['sentiment_score'] = sentiment['docSentiment']['score']
sentJson['sentiment_type'] = sentiment['docSentiment']['type']
sentimentData = tone_analyzer.tone(text=content)
sentJson['anger'] = 0
sentJson['disgust'] =0
sentJson['fear'] =0
sentJson['joy'] = 0
sentJson['sadness'] = 0
sentJson['anger'] = sentimentData["document_tone"]["tone_categories"][0]["tones"][0]["score"]
sentJson['disgust'] = sentimentData["document_tone"]["tone_categories"][0]["tones"][1]["score"]
sentJson['fear'] = sentimentData["document_tone"]["tone_categories"][0]["tones"][2]["score"]
sentJson['joy'] = sentimentData["document_tone"]["tone_categories"][0]["tones"][3]["score"]
sentJson['sadness'] = sentimentData["document_tone"]["tone_categories"][0]["tones"][4]["score"]
sentJson['openness'] = 0
sentJson['conscientiousness'] =0
sentJson['extraversion'] =0
sentJson['aggreablesness'] = 0
sentJson['neuroticism'] = 0
sentJson['openness'] = sentimentData["document_tone"]["tone_categories"][2]["tones"][0]["score"]
sentJson['conscientiousness'] = sentimentData["document_tone"]["tone_categories"][2]["tones"][1]["score"]
sentJson['extraversion'] = sentimentData["document_tone"]["tone_categories"][2]["tones"][2]["score"]
sentJson['aggreablesness'] = sentimentData["document_tone"]["tone_categories"][2]["tones"][3]["score"]
sentJson['neuroticism'] = sentimentData["document_tone"]["tone_categories"][2]["tones"][4]["score"]
return sentJson
def getReviewCountForCompany(company_name):
cSchema = CompanySchema()
comp = Company.query.filter_by(name=company_name).first()
cJson = cSchema.dump(obj=comp).data
return cJson
def constructCompany(company_name):
compJson = {}
compJson[u'id'] = str(uuid.uuid1())
compJson['name'] = company_name
compJson['r_cnt'] = 1
return compJson
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm(request.form)
if request.method == 'POST':
email = request.form['email']
password = request.form['password']
lgn = User.query.filter_by(email=email.lower()).first()
if lgn is None:
return render_template('forms/login.html', form=form)
if lgn.password == password:
session["email"] = email
return render_template('pages/placeholder.home.html')
else:
return render_template('forms/login.html', form=form)
else:
return render_template('forms/login.html', form=form)
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
u = {}
uSchema = UserSchema()
username = request.form['name']
password = request.form['password']
email = request.form['email']
u['id'] = str(uuid.uuid4())
u['name'] = username
u['email'] = email
u['password'] = password
user = uSchema.load(u, session=db_session).data
db_session.add(user)
db_session.commit()
session["email"] = email
return render_template('pages/placeholder.home.html')
else:
form = RegisterForm(request.form)
return render_template('forms/register.html', form=form)
# Error handlers.
@app.errorhandler(500)
def internal_error(error):
db_session.rollback()
return render_template('errors/500.html'), 500
@app.errorhandler(404)
def not_found_error(error):
return render_template('errors/404.html'), 404
if __name__ == "__main__":
init_db()
app.debug = True
app.run()