From c4fc5236a3a8cbbc0317ae2ebf8d8925feef271b Mon Sep 17 00:00:00 2001 From: Sam Steingold Date: Tue, 24 Jan 2017 13:27:33 -0500 Subject: [PATCH 1/3] add NoStanfordCoreNLPServer exception and raise it instead of Exception --- pycorenlp/__init__.py | 2 +- pycorenlp/corenlp.py | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/pycorenlp/__init__.py b/pycorenlp/__init__.py index b031cf7..1efc4cd 100644 --- a/pycorenlp/__init__.py +++ b/pycorenlp/__init__.py @@ -1 +1 @@ -from pycorenlp.corenlp import StanfordCoreNLP +from pycorenlp.corenlp import StanfordCoreNLP, NoStanfordCoreNLPServer diff --git a/pycorenlp/corenlp.py b/pycorenlp/corenlp.py index 6eb2175..37c874b 100644 --- a/pycorenlp/corenlp.py +++ b/pycorenlp/corenlp.py @@ -1,5 +1,15 @@ import json, requests +class NoStanfordCoreNLPServer(Exception): + def __init__(self, server_url): + self.server_url = server_url + + def __str__(self): + return ('Cannot connect to <%s>.\nPlease start the CoreNLP server, e.g.:\n' + '$ cd stanford-corenlp-full-2015-12-09/\n' + '$ java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer' + % (self.server_url)) + class StanfordCoreNLP: def __init__(self, server_url): @@ -18,9 +28,7 @@ def annotate(self, text, properties=None): try: requests.get(self.server_url) except requests.exceptions.ConnectionError: - raise Exception('Check whether you have started the CoreNLP server e.g.\n' - '$ cd stanford-corenlp-full-2015-12-09/ \n' - '$ java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer') + raise NoStanfordCoreNLPServer(self.server_url) data = text.encode() r = requests.post( From 8fb3d44ae794c51ad1cc994b9d8f55e99563d85a Mon Sep 17 00:00:00 2001 From: Sam Steingold Date: Mon, 13 Jul 2020 17:21:34 -0400 Subject: [PATCH 2/3] `encoding' keyword argument to json.loads: Deprecated since version 3.1, will be removed in version 3.9 --- pycorenlp/corenlp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pycorenlp/corenlp.py b/pycorenlp/corenlp.py index 37c874b..c51fe90 100644 --- a/pycorenlp/corenlp.py +++ b/pycorenlp/corenlp.py @@ -39,7 +39,7 @@ def annotate(self, text, properties=None): if ('outputFormat' in properties and properties['outputFormat'] == 'json'): try: - output = json.loads(output, encoding='utf-8', strict=True) + output = json.loads(output, strict=True) except: pass return output From 9665c1418c1be925aa8b36784eb998ddf20d907e Mon Sep 17 00:00:00 2001 From: Sam Steingold Date: Tue, 1 Dec 2020 20:13:49 -0500 Subject: [PATCH 3/3] add StanfordCoreNLPError, do not hide json parsing errors --- pycorenlp/__init__.py | 2 +- pycorenlp/corenlp.py | 34 +++++++++++++++++----------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/pycorenlp/__init__.py b/pycorenlp/__init__.py index 1efc4cd..8d7744b 100644 --- a/pycorenlp/__init__.py +++ b/pycorenlp/__init__.py @@ -1 +1 @@ -from pycorenlp.corenlp import StanfordCoreNLP, NoStanfordCoreNLPServer +from pycorenlp.corenlp import StanfordCoreNLP, StanfordCoreNLPError, NoStanfordCoreNLPServer diff --git a/pycorenlp/corenlp.py b/pycorenlp/corenlp.py index c51fe90..51147f3 100644 --- a/pycorenlp/corenlp.py +++ b/pycorenlp/corenlp.py @@ -10,8 +10,15 @@ def __str__(self): '$ java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer' % (self.server_url)) -class StanfordCoreNLP: +class StanfordCoreNLPError(Exception): + def __init__(self, reason, message): + self.reason = reason + self.message = message + + def __str__(self): + return "%s(%s): %s" % (self.__class__.__name__,self.reason,self.message) +class StanfordCoreNLP: def __init__(self, server_url): if server_url[-1] == '/': server_url = server_url[:-1] @@ -30,19 +37,15 @@ def annotate(self, text, properties=None): except requests.exceptions.ConnectionError: raise NoStanfordCoreNLPServer(self.server_url) - data = text.encode() r = requests.post( self.server_url, params={ 'properties': str(properties) - }, data=data, headers={'Connection': 'close'}) - output = r.text - if ('outputFormat' in properties - and properties['outputFormat'] == 'json'): - try: - output = json.loads(output, strict=True) - except: - pass - return output + }, data=text.encode(), headers={'Connection': 'close'}) + if not r.ok: + raise StanfordCoreNLPError(r.reason, r.text) + if properties.get('outputFormat') == 'json': + return json.loads(r.text) + return r.text def tokensregex(self, text, pattern, filter): return self.regex('/tokensregex', text, pattern, filter) @@ -56,9 +59,6 @@ def regex(self, endpoint, text, pattern, filter): 'pattern': pattern, 'filter': filter }, data=text) - output = r.text - try: - output = json.loads(r.text) - except: - pass - return output + if not r.ok: + raise StanfordCoreNLPError(r.reason, r.text) + return json.loads(r.text)