-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_master.py
81 lines (68 loc) · 2.29 KB
/
train_master.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
#!/usr/bin/python
#-*-coding:utf8-*-
import web
import docclass
import json
from web_api.sina_dao import WebDao
from web_api import conf
from threading import Thread
from sina_master import *
urls = (
'/api/mining', 'Mining',
'/api/pro', 'Resouce',
'/api/traindata', 'Traindata',
'/.*' , 'Index',
)
with open("test.txt", "r") as f:
consumer_key,consumer_secret,key,secret,userid = f.readlines()[0].strip().split(' ')
render = web.template.render('templates')
app = web.application(urls, globals())
db = web.database(dbn="sqlite", db=conf.db_name)
cl = docclass.fisherclassifier(docclass.getWords)
cl.setdb(conf.db_name)
weibo = Sina_master(consumer_key,consumer_secret)
weibo.setToken(key, secret)
res = open(conf.pro_path).read()
class Index:
def GET(self):
i = web.input(pageIndex=1, pageSize=5)
'''select id, text from statuses order by id limit ? , ?'''
posts = db.query('select id, text from statuses where status=0 order by id limit $pageIndex , $pageSize', \
vars={'pageIndex': (int(i.pageIndex)-1)*int(i.pageSize), 'pageSize': i.pageSize})
count = db.select('statuses', what='count(*) total_num', where=' status=$status', vars={'status': 0})
# print 'val:%d'%int(count.c)
total_num = count[0].total_num
c = total_num/int(i.pageSize)
if total_num%int(i.pageSize):
c+=1
return render.index(posts, {"count": c, "pageIndex": i.pageIndex, "pageSize": i.pageSize})
class Traindata:
def POST(self):
i = web.input(id=None, kind=None, content=None)
# print i.id, i.kind, i.content
q = db.update('statuses', where='id = $i.id', status=1, vars=locals())
t = Thread(target=cl.train, args=(i.content, i.kind))
t.start()
web.header('Content-Type', 'application/json')
return "succee"
class Mining:
def GET(self):
return render.sina_mining(userid)
def POST(self):
i = web.input(userid=userid, count=5)
for x in access(weibo.get_latest_weibo, cl, i.userid, i.count):
print x
return render.sina_mining(userid)
class Resouce:
def GET(self):
web.header('Content-Type', 'application/json')
return res
def access(get_latest_weibo, classify, userid, count):
item = dict()
for x in get_latest_weibo(userid, count):
print "text:%s"%x['text']
item[x['id']] = classify.classifypercent(x['text'])
print item[x['id']]
return item
if __name__ == '__main__':
app.run()