-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·158 lines (122 loc) · 4.22 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python
# coding=utf-8
from tokenize import CoNLLizator
from parse import Parser
from sent_tokenize import SentenceTokenizer
from coref import CorefResolver
import conll2json
print 'Starting...'
splitter = SentenceTokenizer()
conllizator = CoNLLizator("../LVTagger/morphotagger.sh")
parser = Parser(command="build/collins", args="--load --stdin --stdout -q --basedir=build")
coref = CorefResolver("~/Work/LVCoref-semi-bin/lvcoref.sh")
print 'OK'
print
# gevent.monkey.patch_all() rada problēmas ar pipe, tāpēc ielādē tos pēc pipe izveidošanas
# from: http://code.mixpanel.com/2010/10/29/gevent-the-good-the-bad-the-ugly/
# "Order matters."
# "Daemonize before you import gevent or at least before you call monkey.patch_all(). ..."
# "... gevent modifies a socket in python internals. When you daemonize, all open file descriptors are closed ..."
from gevent import monkey; monkey.patch_all()
from time import sleep
import os
from bottle import route, run, static_file, post, request, response
import json
import re
@route('/rest')
def rest():
return 'REST API goes here'
@post('/rest/split')
def split():
response.content_type = 'application/json; charset=utf-8'
text = request.body.read()
count = 0
parts = re.split('\r?\n\r?\n', text)
for part in parts:
# te ir divi varianti: vai nu uzskata katru rindiņu par atsevišķu teikumu, vai tomēr par veinu (meklē pieturzīmes)
for sentences in part.split('\n'):
if not sentences:
continue
for sentence in splitter(sentences):
count += 1
if count == 1:
yield sentence
else:
yield '\n'+sentence
# bet tad ir jāmaina arī klienta formāts
# for sentence in splitter(part):
# yield sentence.split('\n').join(' ')+'\n'
# re.split('(?\nko')
# parts = text.split('\n\n')
# for sentence in splitter(text):
# if not sentence:
# continue
# yield sentence.split('\n').join(' ')+'\n'
# return splitter(text)
# return json.dumps(sentences, indent=2)
@post('/rest/parse')
def parse():
response.content_type = 'text/html; charset=utf-8'
# conll = request.body.getvalue()
conll = request.body.read()
while parser.inProgress:
sleep(0.1)
return parser(conll)
@post('/rest/parse2')
def parse2():
response.content_type = 'text/html; charset=utf-8'
conll = request.body.read()
while parser.inProgress:
sleep(0.1)
# sadala pa teikumiem
inputSentences = []
sentence = []
for line in conll.split('\n'):
line = line.strip()
if not line:
if not sentence:
continue
inputSentences.append('\n'.join(inputSentences))
sentence = []
sentence.append(line)
sentences = []
for sentence in inputSentences:
sentences.append(parser(sentence))
return '\n\n'.join(sentences)
@post('/rest/coref')
def corefResolver():
response.content_type = 'text/html; charset=utf-8'
conll = request.body.read()
while coref.inProgress:
sleep(0.1)
return coref(conll)
@post('/rest/conllize')
def CoNLLize():
response.content_type = 'text/html; charset=utf-8'
# sentence = request.body.getvalue()
sentence = request.body.read()
while conllizator.inProgress:
sleep(0.1)
return conllizator(sentence)
@post('/rest/conllize2')
def CoNLLize2():
response.content_type = 'text/html; charset=utf-8'
while conllizator.inProgress:
sleep(0.1)
sentences = []
for sentence in request.body:
sentences.append(conllizator(sentence))
return '\n\n'.join(sentences)
@post('/rest/conll2json')
def CoNLLize():
response.content_type = 'application/json; charset=utf-8'
# conll = request.body.getvalue()
conll = request.body.read()
return conll2json.convert(conll)
@route('/')
@route('/<path:path>')
def static(path='index.html'):
return static_file(path, root=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'server'))
# run(host='localhost', port=8080, debug=True)
# run(host='0.0.0.0', port=8080, debug=True)
run(host='0.0.0.0', port=8080, debug=True, server='gevent')