-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflask_influ.py
77 lines (64 loc) · 2.94 KB
/
flask_influ.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
from flask import Flask, request,render_template,make_response
from flask_restful import Resource, Api
import Variable
import datetime
import preprocess_data
import LSTM_new
import mysql.connector as mys
app = Flask(__name__)
api = Api(app)
class HelloWorld(Resource):
def get(self):
args = request.args
categ = args.get('categ',type= str)
ctype = args.get('ctype',type = str)
dates = str(ctype).split()
if dates[0] == 'm':
now = datetime.datetime.now()
second = int(dates[1])*60
new = now - datetime.timedelta(seconds=second)
if dates[0] == 'd':
now = datetime.datetime.now()
second = int(dates[1])*86400
new = now - datetime.timedelta(seconds=second)
if dates[0] == 'r':
now = datetime.strptime(dates[2], '%Y-%m-%d')
new = datetime.strptime(dates[1], '%Y-%m-%d')
mydb = mys.connect(host='localhost',user=Variable.user ,passwd = Variable.password)
mycursor = mydb.cursor()
mycursor.execute("use tweets")
sentiments = []
try:
sql_select_query = "select Tweets from hash_tweets where Date >= %s and Date <= %s and Category = %s"
mycursor.execute(sql_select_query, (new,now,categ))
records = [preprocess_data.preprocess_tweet(str(i)) for i in mycursor]
sentiments = [LSTM_new.predict(i)['label'] for i in records]
sql_select_query = " select Influencer from influ_tweets"
mycursor.execute(sql_select_query)
screen_names = [i for i in mycursor]
records = [str(i)+ " "+str(k) for i,k in zip(records,screen_names)]
if sentiments.count(1) > sentiments.count(0):
check = float(sentiments.count(1)/len(sentiments))
if check >= 0.45 and check <= 0.55:
tag = 'Neutral'
else:
tag='Positive'
else:
if float(sentiments.count(0)/len(sentiments)) >= 0.45 and float(sentiments.count(0)/len(sentiments)) <= 0.55:
tag = 'Neutral'
else:
tag='Negative'
res = {"Positive":sentiments.count(1) , "Negative" : sentiments.count(0) , "Overall" : tag}
final=dict(zip(records,sentiments))
res.update(final)
final = res
headers = {'Content-Type': 'text/html'}
return make_response(render_template('result.html' , result = final),headers)
finally:
#closing database connection.
'''if(mydb.is_connected()):
mydb.close()
print("MySQL connection is closed")'''
api.add_resource(HelloWorld, '/')
if __name__ == '__main__':
app.run(debug=True)