-
Notifications
You must be signed in to change notification settings - Fork 477
/
server.py
63 lines (54 loc) · 1.92 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
#!/usr/bin/env python
"""Coreference resolution server example.
A simple server serving the coreference system.
"""
import json
from wsgiref.simple_server import make_server
import falcon
import spacy
import neuralcoref
# Python 3
unicode_ = str
class AllResource(object):
def __init__(self):
self.nlp = spacy.load("en")
neuralcoref.add_to_pipe(self.nlp)
print("Server loaded")
self.response = None
def on_get(self, req, resp):
self.response = {}
text_param = req.get_param_as_list("text")
print("text: ", text_param)
if text_param is not None:
text = ",".join(text_param) if isinstance(text_param, list) else text_param
text = unicode_(text)
doc = self.nlp(text)
if doc._.has_coref:
mentions = [
{
"start": mention.start_char,
"end": mention.end_char,
"text": mention.text,
"resolved": cluster.main.text,
}
for cluster in doc._.coref_clusters
for mention in cluster.mentions
]
clusters = list(
list(span.text for span in cluster)
for cluster in doc._.coref_clusters
)
resolved = doc._.coref_resolved
self.response["mentions"] = mentions
self.response["clusters"] = clusters
self.response["resolved"] = resolved
resp.body = json.dumps(self.response)
resp.content_type = "application/json"
resp.append_header("Access-Control-Allow-Origin", "*")
resp.status = falcon.HTTP_200
if __name__ == "__main__":
RESSOURCE = AllResource()
APP = falcon.API()
APP.add_route("/", RESSOURCE)
HTTPD = make_server("0.0.0.0", 8000, APP)
HTTPD.serve_forever()