8
8
import shlex
9
9
import subprocess
10
10
import time
11
+ import sys
11
12
12
13
from six .moves .urllib .parse import urlparse
13
14
@@ -48,10 +49,13 @@ class RobustService(object):
48
49
"""
49
50
TIMEOUT = 15
50
51
51
- def __init__ (self , start_cmd , stop_cmd , endpoint ):
52
+ def __init__ (self , start_cmd , stop_cmd , endpoint , stdout = sys .stdout ,
53
+ stderr = sys .stderr ):
52
54
self .start_cmd = start_cmd and shlex .split (start_cmd )
53
55
self .stop_cmd = stop_cmd and shlex .split (stop_cmd )
54
56
self .endpoint = endpoint
57
+ self .stdout = stdout
58
+ self .stderr = stderr
55
59
56
60
self .server = None
57
61
self .is_active = False
@@ -64,7 +68,9 @@ def is_alive(self):
64
68
65
69
def start (self ):
66
70
if self .start_cmd :
67
- self .server = subprocess .Popen (self .start_cmd )
71
+ self .server = subprocess .Popen (self .start_cmd ,
72
+ stderr = self .stderr ,
73
+ stdout = self .stdout )
68
74
69
75
def stop (self ):
70
76
if self .server :
@@ -116,7 +122,10 @@ class CoreNLPClient(RobustService):
116
122
DEFAULT_ANNOTATORS = "tokenize ssplit lemma pos ner depparse" .split ()
117
123
DEFAULT_PROPERTIES = {}
118
124
119
- def __init__ (self , start_server = True , endpoint = "http://localhost:9000" , timeout = 5000 , annotators = DEFAULT_ANNOTATORS , properties = DEFAULT_PROPERTIES ):
125
+ def __init__ (self , start_server = True , endpoint = "http://localhost:9000" ,
126
+ timeout = 5000 , annotators = DEFAULT_ANNOTATORS ,
127
+ properties = DEFAULT_PROPERTIES , stdout = sys .stdout ,
128
+ stderr = sys .stderr ):
120
129
if start_server :
121
130
host , port = urlparse (endpoint ).netloc .split (":" )
122
131
assert host == "localhost" , "If starting a server, endpoint must be localhost"
@@ -130,7 +139,8 @@ def __init__(self, start_server=True, endpoint="http://localhost:9000", timeout=
130
139
else :
131
140
start_cmd = stop_cmd = None
132
141
133
- super (CoreNLPClient , self ).__init__ (start_cmd , stop_cmd , endpoint )
142
+ super (CoreNLPClient , self ).__init__ (start_cmd , stop_cmd , endpoint ,
143
+ stdout , stderr )
134
144
self .default_annotators = annotators
135
145
self .default_properties = properties
136
146
0 commit comments