1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
|
4
|
|
|
# use data structures |
5
|
|
|
from .ds import Document, Sentence, Dependencies |
6
|
|
|
from .utils import post_json |
7
|
|
|
import json |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class Processor(object): |
11
|
|
|
|
12
|
|
|
def __init__(self, address): |
13
|
|
|
self.service = "{}/annotate".format(address) |
14
|
|
|
|
15
|
|
|
def annotate(self, text): |
16
|
|
|
try: |
17
|
|
|
# load json and build Sentences and Document |
18
|
|
|
msg = Message(text) |
19
|
|
|
annotated_text = post_json(self.service, msg.to_JSON()) |
20
|
|
|
return Document.load_from_JSON(annotated_text) |
21
|
|
|
|
22
|
|
|
except Exception as e: |
23
|
|
|
#print(e) |
24
|
|
|
return None |
25
|
|
|
#raise Exception("Connection refused! Is the server running?") |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class FastNLPProcessor(Processor): |
29
|
|
|
|
30
|
|
|
def __init__(self, address): |
31
|
|
|
self.service = "{}/fastnlp/annotate".format(address) |
32
|
|
|
|
33
|
|
|
def annotate(self, text): |
34
|
|
|
return super(FastNLPProcessor, self).annotate(text) |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
class BioNLPProcessor(Processor): |
38
|
|
|
|
39
|
|
|
def __init__(self, address): |
40
|
|
|
self.service = "{}/bionlp/annotate".format(address) |
41
|
|
|
|
42
|
|
|
def annotate(self, text): |
43
|
|
|
return super(BioNLPProcessor, self).annotate(text) |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
class Message(object): |
47
|
|
|
def __init__(self, text): |
48
|
|
|
self.text = text |
49
|
|
|
|
50
|
|
|
def to_JSON_dict(self): |
51
|
|
|
jdict = dict() |
52
|
|
|
jdict["text"] = self.text |
53
|
|
|
return jdict |
54
|
|
|
|
55
|
|
|
def to_JSON(self): |
56
|
|
|
return json.dumps(self.to_JSON_dict(), sort_keys=True, indent=4) |
57
|
|
|
|