-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathserver.py
76 lines (65 loc) · 2.72 KB
/
server.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ldig server
# This code is available under the MIT License.
# (c)2011 Nakatani Shuyo / Cybozu Labs Inc.
import sys, os, codecs
import BaseHTTPServer
import urlparse
import optparse
import json
import numpy
import ldig
#sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
parser = optparse.OptionParser()
parser.add_option("-m", dest="model", help="model directory")
parser.add_option("-p", dest="port", help="listening port number", type="int", default=48000)
(options, args) = parser.parse_args()
if not options.model: parser.error("need model directory (-m)")
class Detector(object):
def __init__(self, modeldir):
self.ldig = ldig.ldig(modeldir)
self.features = self.ldig.load_features()
self.trie = self.ldig.load_da()
self.labels = self.ldig.load_labels()
self.param = numpy.load(self.ldig.param)
def detect(self, st):
label, text, org_text = ldig.normalize_text(st)
events = self.trie.extract_features(u"\u0001" + text + u"\u0001")
sum = numpy.zeros(len(self.labels))
data = []
for id in sorted(events, key=lambda id:self.features[id][0]):
phi = self.param[id,]
sum += phi * events[id]
data.append({"id":int(id), "feature":self.features[id][0], "phi":["%0.3f" % x for x in phi]})
exp_w = numpy.exp(sum - sum.max())
prob = exp_w / exp_w.sum()
return {"labels":self.labels, "data":data, "prob":["%0.3f" % x for x in prob]}
basedir = os.path.join(os.path.dirname(__file__), "static")
detector = Detector(options.model)
class LdigServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
url = urlparse.urlparse(self.path)
path = url.path
if path.endswith("/"): path += "index.html"
localpath = basedir + path
if path == "/detect":
params = urlparse.parse_qs(url.query)
text = unicode(params['text'][0], 'utf-8')
json.dump(detector.detect(text), self.wfile)
elif os.path.exists(localpath):
self.send_response(200)
if path.endswith(".html"):
self.send_header("Content-Type", "text/html; charset=utf-8")
elif path.endswith(".js"):
self.send_header("Content-Type", "text/javascript; charset=utf-8")
self.end_headers()
with open(localpath, "rb") as f:
self.wfile.write(f.read())
else:
self.send_response(404, "Not Found : " + url.path)
self.send_header("Expires", "Fri, 31 Dec 2100 00:00:00 GMT")
self.end_headers()
server = BaseHTTPServer.HTTPServer(('', options.port), LdigServerHandler)
print "ready."
server.serve_forever()