Skip to content

add NoStanfordCoreNLPServer exception and raise it instead of Exception #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pycorenlp/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from pycorenlp.corenlp import StanfordCoreNLP
from pycorenlp.corenlp import StanfordCoreNLP, StanfordCoreNLPError, NoStanfordCoreNLPServer
48 changes: 28 additions & 20 deletions pycorenlp/corenlp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import json, requests

class StanfordCoreNLP:
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 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]
Expand All @@ -18,23 +35,17 @@ 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(
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, encoding='utf-8', 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)
Expand All @@ -48,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)