-
Notifications
You must be signed in to change notification settings - Fork 17
/
app.py
85 lines (67 loc) · 2.41 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
#! /usr/bin/env python
'''
Main file to start tornado server
'''
import tornado.ioloop
import tornado.web
import urllib
import tweepy
import os
from hidden import *
from maxentclassifier import MaximumEntropyClassifier
from naivebayesclassifier import NaiveBayesClassifier
# name of training set file
fname = 'trainingandtestdata/training.csv'
# train classifiers here first
nb = NaiveBayesClassifier(fname, grams=[1,2])
nb.setThresholds(neg=1.0, pos=20.0)
nb.setWeight(0.000000000005)
nb.trainClassifier()
ment = MaximumEntropyClassifier(fname)
ment.trainClassifier()
classifiers = [nb, ment]
class MainHandler(tornado.web.RequestHandler):
'''
Handles request to main page
'''
def get(self):
query = self.get_argument("query", "").strip()
cchosen = int(self.get_argument("classifier-type", 0))
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
# search twitter
results = api.search(q=urllib.quote(query)) if len(query) > 0 else []
tweets = []
poscount = 0
negcount = 0
for result in results:
cresult = classifiers[cchosen].classify(result.text)
if cresult == 0: negcount += 1
elif cresult == 1: poscount += 1
else: cresult = 2
tweets.append((cresult, result))
pospercent = 0 if len(results) == 0 else "%.2f" \
% (float(poscount)*100/(poscount + negcount))
negpercent = 0 if len(results) == 0 else "%.2f" \
% (float(negcount)*100/(poscount + negcount))
self.set_header("Cache-Control","no-cache")
# render results of sentiment analysis on tweets in real-time
self.render("index.html",
poscount = poscount,
negcount = negcount,
pospercent = pospercent,
negpercent = negpercent,
query = query,
tweets = tweets)
if __name__ == "__main__":
dirname = os.path.dirname(__file__)
settings = {
"static_path" : os.path.join(dirname, "static"),
"template_path" : os.path.join(dirname, "template")
}
application = tornado.web.Application([
(r'/', MainHandler)
], **settings)
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()