forked from hiyeshin/barbarakruger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
60 lines (46 loc) · 1.59 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
import json
import urllib
import logging
import os.path
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
from markov_by_char import CharacterMarkovGenerator
define("port", default = 8000, help = "Run on the given port ", type = int)
define("n", default = 5, help = "length of n-gram ", type = int)
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", MainHandler),
(r"/markov", MarkovHandler),
(r"/about", AboutHandler)
]
settings = dict(
template_path = os.path.join(os.path.dirname(__file__), "templates"),
static_path = os.path.join(os.path.dirname(__file__), "static")
)
tornado.web.Application.__init__(self, handlers, **settings)
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render('main.html')
class MarkovHandler(tornado.web.RequestHandler):
def get(self):
screen_name = self.get_argument('screen_name')
params = {'count':200, 'screen_name':screen_name}
generator = CharacterMarkovGenerator(options.n, 140)
# below should be a backup for the performance
with open('dump.csv') as f:
data = f.readlines()
for tweet in data:
generator.feed(tweet) # this shows the text content of the tweet
self.write(generator.generate())
class AboutHandler(tornado.web.RequestHandler):
def get(self):
self.render('about.html')
if __name__ == "__main__":
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()