Skip to content

Commit c4fc523

Browse files
committed
add NoStanfordCoreNLPServer exception and raise it instead of Exception
1 parent 4c9bd82 commit c4fc523

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

pycorenlp/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from pycorenlp.corenlp import StanfordCoreNLP
1+
from pycorenlp.corenlp import StanfordCoreNLP, NoStanfordCoreNLPServer

pycorenlp/corenlp.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import json, requests
22

3+
class NoStanfordCoreNLPServer(Exception):
4+
def __init__(self, server_url):
5+
self.server_url = server_url
6+
7+
def __str__(self):
8+
return ('Cannot connect to <%s>.\nPlease start the CoreNLP server, e.g.:\n'
9+
'$ cd stanford-corenlp-full-2015-12-09/\n'
10+
'$ java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer'
11+
% (self.server_url))
12+
313
class StanfordCoreNLP:
414

515
def __init__(self, server_url):
@@ -18,9 +28,7 @@ def annotate(self, text, properties=None):
1828
try:
1929
requests.get(self.server_url)
2030
except requests.exceptions.ConnectionError:
21-
raise Exception('Check whether you have started the CoreNLP server e.g.\n'
22-
'$ cd stanford-corenlp-full-2015-12-09/ \n'
23-
'$ java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer')
31+
raise NoStanfordCoreNLPServer(self.server_url)
2432

2533
data = text.encode()
2634
r = requests.post(

0 commit comments

Comments
 (0)