-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcanonizer.py
More file actions
105 lines (76 loc) · 2.28 KB
/
canonizer.py
File metadata and controls
105 lines (76 loc) · 2.28 KB
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
import json
import classifier
import pickle
import os
from bottle import post, request, response
canonizer_save_file_path = "data/canonizer.obj"
training_data_save_file_path = "data/train_data.json"
c = classifier.HugoClassifier()
if os.path.exists(canonizer_save_file_path):
canonizer_file = open(canonizer_save_file_path, "rw")
c = pickle.load(canonizer_file)
@post('/canonize')
def canonize_handler():
'''Returns canonized form of a nomination'''
global c
canon_id = [0]
try:
try:
nomination = request.json
except:
raise ValueError
if nomination is None:
raise ValueError
canon_id = c.canonize(nomination)
except ValueError:
response.status = 400
return
response.headers['Content_type'] = 'application/json'
return json.dumps({'canon_id': canon_id[0]})
@post('/train')
def train_handler():
'''Trains the classifer'''
global c
global canonizer_save_file_path
global training_data_save_file_path
with open(training_data_save_file_path) as f:
for line in f:
nominations = json.loads(line)
c.add_train_data(nominations)
c.train_internal()
canonizer_file = open(canonizer_save_file_path, "w")
pickle.dump(c, canonizer_file)
response.status = 200
return
@post('/add_train_data')
def add_train_data_handler():
'''Adds a nomination to training set'''
global c
global training_data_save_file_path
try:
try:
nominations = request.json
except:
raise ValueError
if nominations is None:
raise ValueError
except ValueError:
response.status = 400
return
train_file = open(training_data_save_file_path,"a+")
json.dump(nominations, train_file)
train_file.write('\n')
response.status = 200
return
@post('/reset')
def reset_handler():
'''Resets the canonizer and removes save files'''
global c
global canonizer_save_file_path
global training_data_save_file_path
if os.path.exists(canonizer_save_file_path):
os.remove(canonizer_save_file_path)
if os.path.exists(training_data_save_file_path):
os.remove(training_data_save_file_path)
c = classifier.HugoClassifier()
return