Skip to content

Commit 01a60b7

Browse files
authored
Merge pull request #28 from greasystrangler/master
Add docDate support to annotate & update
2 parents 7a32ce6 + c606555 commit 01a60b7

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

corenlp/client.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import subprocess
1010
import time
1111
import sys
12+
import datetime
1213

1314
from six.moves.urllib.parse import urlparse
1415

@@ -168,11 +169,12 @@ def __init__(self, start_server=True,
168169
self.default_properties = properties or self.DEFAULT_PROPERTIES
169170
self.default_output_format = output_format or self.DEFAULT_OUTPUT_FORMAT
170171

171-
def _request(self, buf, properties):
172+
def _request(self, buf, properties, date=None):
172173
"""Send a request to the CoreNLP server.
173174
174175
:param (str | unicode) text: raw text for the CoreNLPServer to parse
175176
:param (dict) properties: properties that the server expects
177+
:param (str) date: reference date of document, used by server to set docDate - expects YYYY-MM-DD
176178
:return: request result
177179
"""
178180
self.ensure_alive()
@@ -186,8 +188,13 @@ def _request(self, buf, properties):
186188
else:
187189
raise ValueError("Unrecognized inputFormat " + input_format)
188190

191+
if date:
192+
params = {'properties': str(properties),'date': str(date)}
193+
else:
194+
params = {'properties': str(properties)}
195+
189196
r = requests.post(self.endpoint,
190-
params={'properties': str(properties)},
197+
params=params,
191198
data=buf, headers={'content-type': ctype},
192199
timeout=(self.timeout*2)/1000)
193200
r.raise_for_status()
@@ -198,7 +205,7 @@ def _request(self, buf, properties):
198205
else:
199206
raise AnnotationException(r.text)
200207

201-
def annotate(self, text, annotators=None, output_format=None, properties=None):
208+
def annotate(self, text, annotators=None, output_format=None, properties=None, date=None):
202209
"""Send a request to the CoreNLP server.
203210
204211
:param (str | unicode) text: raw text for the CoreNLPServer to parse
@@ -222,7 +229,7 @@ def annotate(self, text, annotators=None, output_format=None, properties=None):
222229
if output_format is not None:
223230
properties["outputFormat"] = output_format
224231
# make the request
225-
r = self._request(text.encode('utf-8'), properties)
232+
r = self._request(text.encode('utf-8'), properties, date)
226233
# customize what is returned based outputFormat
227234
if properties["outputFormat"] == "serialized":
228235
doc = Document()
@@ -235,7 +242,7 @@ def annotate(self, text, annotators=None, output_format=None, properties=None):
235242
else:
236243
return r
237244

238-
def update(self, doc, annotators=None, properties=None):
245+
def update(self, doc, annotators=None, properties=None, date=None):
239246
if properties is None:
240247
properties = self.default_properties
241248
properties.update({
@@ -248,7 +255,7 @@ def update(self, doc, annotators=None, properties=None):
248255
writeToDelimitedString(doc, stream)
249256
msg = stream.getvalue()
250257

251-
r = self._request(msg, properties)
258+
r = self._request(msg, properties, date)
252259
doc = Document()
253260
parseFromDelimitedString(doc, r.content)
254261
return doc

0 commit comments

Comments
 (0)