|
1
|
|
|
#!/usr/bin/env python |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
|
|
4
|
|
|
# use data structures |
|
5
|
|
|
from __future__ import unicode_literals |
|
6
|
|
|
from processors.ds import Document, Sentence, DirectedGraph |
|
7
|
|
|
from processors.utils import post_json |
|
8
|
|
|
import json |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class Processor(object): |
|
12
|
|
|
""" |
|
13
|
|
|
Base Processor for text annotation (tokenization, sentence splitting, |
|
14
|
|
|
parsing, lemmatization, PoS tagging, named entity recognition, chunking, etc.). |
|
15
|
|
|
|
|
16
|
|
|
Parameters |
|
17
|
|
|
---------- |
|
18
|
|
|
address : str |
|
19
|
|
|
The base address for the API (i.e., everything preceding `/api/..`) |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
Attributes |
|
23
|
|
|
---------- |
|
24
|
|
|
service : str |
|
25
|
|
|
The API endpoint for `annotate` requests. |
|
26
|
|
|
|
|
27
|
|
|
Methods |
|
28
|
|
|
------- |
|
29
|
|
|
annotate(text) |
|
30
|
|
|
Produces an annotated `Document` from the provided text. |
|
31
|
|
|
annotate_from_sentences(sentences) |
|
32
|
|
|
Produces an annotated `Document` from a [str] of text already split into sentences. |
|
33
|
|
|
|
|
34
|
|
|
""" |
|
35
|
|
|
def __init__(self, address): |
|
36
|
|
|
self.service = "{}/api/annotate".format(address) |
|
37
|
|
|
|
|
38
|
|
|
def _message_to_json_dict(self, msg): |
|
39
|
|
|
return post_json(self.service, msg.to_JSON()) |
|
40
|
|
|
|
|
41
|
|
|
def _annotate_message(self, msg): |
|
42
|
|
|
annotated_text = post_json(self.service, msg.to_JSON()) |
|
43
|
|
|
return Document.load_from_JSON(annotated_text) |
|
44
|
|
|
|
|
45
|
|
|
def annotate(self, text): |
|
46
|
|
|
""" |
|
47
|
|
|
Annotate text (tokenization, sentence splitting, |
|
48
|
|
|
parsing, lemmatization, PoS tagging, named entity recognition, chunking, etc.) |
|
49
|
|
|
|
|
50
|
|
|
Parameters |
|
51
|
|
|
---------- |
|
52
|
|
|
text : str |
|
53
|
|
|
`text` to be annotated. |
|
54
|
|
|
|
|
55
|
|
|
Returns |
|
56
|
|
|
------- |
|
57
|
|
|
processors.ds.Document or None |
|
58
|
|
|
An annotated Document composed of `sentences`. |
|
59
|
|
|
""" |
|
60
|
|
|
try: |
|
61
|
|
|
# load json and build Sentences and Document |
|
62
|
|
|
msg = Message(text) |
|
63
|
|
|
return self._annotate_message(msg) |
|
64
|
|
|
|
|
65
|
|
|
except Exception as e: |
|
66
|
|
|
#print(e) |
|
67
|
|
|
return None |
|
68
|
|
|
|
|
69
|
|
|
def annotate_from_sentences(self, sentences): |
|
70
|
|
|
""" |
|
71
|
|
|
Annotate text that has already been segmented into `sentences`. |
|
72
|
|
|
|
|
73
|
|
|
Parameters |
|
74
|
|
|
---------- |
|
75
|
|
|
sentences : [str] |
|
76
|
|
|
A list of str representing text already split into sentences. |
|
77
|
|
|
|
|
78
|
|
|
Returns |
|
79
|
|
|
------- |
|
80
|
|
|
processors.ds.Document or None |
|
81
|
|
|
An annotated `Document` composed of `sentences`. |
|
82
|
|
|
""" |
|
83
|
|
|
try: |
|
84
|
|
|
# load json from str interable and build Sentences and Document |
|
85
|
|
|
msg = SegmentedMessage(sentences) |
|
86
|
|
|
return self._annotate_message(msg) |
|
87
|
|
|
|
|
88
|
|
|
except Exception as e: |
|
89
|
|
|
#print(e) |
|
90
|
|
|
return None |
|
91
|
|
|
|
|
92
|
|
|
class FastNLPProcessor(Processor): |
|
93
|
|
|
|
|
94
|
|
|
""" |
|
95
|
|
|
Processor for text annotation based on [`org.clulab.processors.fastnlp.FastNLPProcessor`](https://github.com/clulab/processors/blob/master/corenlp/src/main/scala/org/clulab/processors/fastnlp/FastNLPProcessor.scala) |
|
96
|
|
|
|
|
97
|
|
|
Uses the Malt parser. |
|
98
|
|
|
""" |
|
99
|
|
|
def __init__(self, address): |
|
100
|
|
|
self.service = "{}/api/fastnlp/annotate".format(address) |
|
101
|
|
|
|
|
102
|
|
|
def annotate(self, text): |
|
103
|
|
|
return super(FastNLPProcessor, self).annotate(text) |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
class BioNLPProcessor(Processor): |
|
107
|
|
|
|
|
108
|
|
|
""" |
|
109
|
|
|
Processor for biomedical text annotation based on [`org.clulab.processors.fastnlp.FastNLPProcessor`](https://github.com/clulab/processors/blob/master/corenlp/src/main/scala/org/clulab/processors/fastnlp/FastNLPProcessor.scala) |
|
110
|
|
|
|
|
111
|
|
|
CoreNLP-derived annotator. |
|
112
|
|
|
|
|
113
|
|
|
""" |
|
114
|
|
|
|
|
115
|
|
|
def __init__(self, address): |
|
116
|
|
|
self.service = "{}/api/bionlp/annotate".format(address) |
|
117
|
|
|
|
|
118
|
|
|
def annotate(self, text): |
|
119
|
|
|
return super(BioNLPProcessor, self).annotate(text) |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
class Message(object): |
|
123
|
|
|
|
|
124
|
|
|
""" |
|
125
|
|
|
A storage class for passing `text` to API `annotate` endpoint. |
|
126
|
|
|
|
|
127
|
|
|
Attributes |
|
128
|
|
|
---------- |
|
129
|
|
|
text : str |
|
130
|
|
|
The `text` to be annotated. |
|
131
|
|
|
|
|
132
|
|
|
Methods |
|
133
|
|
|
------- |
|
134
|
|
|
to_JSON() |
|
135
|
|
|
Produces a json str in the structure expected by the API `annotate` endpoint. |
|
136
|
|
|
|
|
137
|
|
|
""" |
|
138
|
|
|
def __init__(self, text): |
|
139
|
|
|
self.text = text |
|
140
|
|
|
|
|
141
|
|
|
def to_JSON_dict(self): |
|
142
|
|
|
jdict = dict() |
|
143
|
|
|
jdict["text"] = self.text |
|
144
|
|
|
return jdict |
|
145
|
|
|
|
|
146
|
|
|
def to_JSON(self): |
|
147
|
|
|
return json.dumps(self.to_JSON_dict(), sort_keys=True, indent=4) |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
class SegmentedMessage(object): |
|
151
|
|
|
""" |
|
152
|
|
|
A storage class for passing text already split into sentences to API `annotate` endpoint. |
|
153
|
|
|
|
|
154
|
|
|
Attributes |
|
155
|
|
|
---------- |
|
156
|
|
|
segments : [str] |
|
157
|
|
|
Text to be annotated that has already been split into sentences. This segmentation is preserved during annotation. |
|
158
|
|
|
|
|
159
|
|
|
Methods |
|
160
|
|
|
------- |
|
161
|
|
|
to_JSON() |
|
162
|
|
|
Produces a json str in the structure expected by the API `annotate` endpoint. |
|
163
|
|
|
|
|
164
|
|
|
""" |
|
165
|
|
|
def __init__(self, segments): |
|
166
|
|
|
self.segments = segments |
|
167
|
|
|
|
|
168
|
|
|
def to_JSON_dict(self): |
|
169
|
|
|
jdict = dict() |
|
170
|
|
|
jdict["segments"] = self.segments |
|
171
|
|
|
return jdict |
|
172
|
|
|
|
|
173
|
|
|
def to_JSON(self): |
|
174
|
|
|
return json.dumps(self.to_JSON_dict(), sort_keys=True, indent=4) |
|
175
|
|
|
|