Skip to content

Commit 3272104

Browse files
committed
Merge pull request #6 from jkdv/master
Changed to support unicode texts and close HTTP connection after the API calls.
2 parents d550768 + 9612221 commit 3272104

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

pycorenlp/corenlp.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,30 @@ def __init__(self, server_url):
88
self.server_url = server_url
99

1010
def annotate(self, text, properties=None):
11-
if not properties:
11+
assert isinstance(text, str)
12+
if properties is None:
1213
properties = {}
13-
14+
else:
15+
assert isinstance(properties, dict)
16+
1417
# Checks that the Stanford CoreNLP server is started.
1518
try:
16-
requests.get(self.server_url).ok == True
19+
requests.get(self.server_url)
1720
except requests.exceptions.ConnectionError:
1821
raise Exception('Check whether you have started the CoreNLP server e.g.\n'
1922
'$ cd stanford-corenlp-full-2015-12-09/ \n'
2023
'$ java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer')
21-
22-
r = requests.get(
24+
25+
data = text.encode()
26+
r = requests.post(
2327
self.server_url, params={
2428
'properties': str(properties)
25-
}, data=text)
29+
}, data=data, headers={'Connection': 'close'})
2630
output = r.text
2731
if ('outputFormat' in properties
2832
and properties['outputFormat'] == 'json'):
2933
try:
30-
output = json.loads(output, strict=False)
34+
output = json.loads(output, encoding='utf-8', strict=True)
3135
except:
3236
pass
3337
return output

0 commit comments

Comments
 (0)