概要
stanfordnlpを使って、serverを立ち上げた時に出るこのエラーを回避したのでメモしておきます。
ちなみに、CoreNLPサーバーを立ち上げる方法は、公式のGitHubのデモに記載があるのでそちらを確認してください。
https://github.com/stanfordnlp/stanfordnlp/blob/master/demo/corenlp.py
エラー
$ stanfordnlp.server.client.PermanentlyFailedException: Timed out waiting for service to come alive.
timeoutの時間を伸ばしてくださいとのことです。デフォルトが30000msらしいので、適当に長さを伸ばせば解決します。適当にtimeoutを設定すれば大丈夫です。該当のコードはここです。
with CoreNLPClient(annotators=['tokenize','ssplit','pos','lemma','ner','depparse','coref'], timeout=10000000, memory='16G') as client:
せっかくなので、中身をみてみるとなんのことはありません。
class CoreNLPClient(RobustService):
"""
A CoreNLP client to the Stanford CoreNLP server.
"""
DEFAULT_ANNOTATORS = "tokenize ssplit lemma pos ner depparse".split()
DEFAULT_PROPERTIES = {}
DEFAULT_OUTPUT_FORMAT = "serialized"
def __init__(self, start_server=True,
endpoint="http://localhost:9000",
timeout=30000,
threads=5,
annotators=None,
properties=None,
output_format=None,
stdout=sys.stdout,
stderr=sys.stderr,
memory="4G",
be_quiet=True,
max_char_length=100000
):
if isinstance(annotators, str):
annotators = annotators.split()
if start_server:
host, port = urlparse(endpoint).netloc.split(":")
assert host == "localhost", "If starting a server, endpoint must be localhost"
assert os.getenv("CORENLP_HOME") is not None, "Please define $CORENLP_HOME where your CoreNLP Java checkout is"
start_cmd = "java -Xmx{memory} -cp '{corenlp_home}/*' edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port {port} -timeout {timeout} -threads {threads} -maxCharLength {max_char_length}".format(
corenlp_home=os.getenv("CORENLP_HOME"),
port=port,
memory=memory,
timeout=timeout,
threads=threads,
max_char_length=max_char_length)
stop_cmd = None
else:
start_cmd = stop_cmd = None
super(CoreNLPClient, self).__init__(start_cmd, stop_cmd, endpoint,
stdout, stderr, be_quiet)
self.timeout = timeout
self.default_annotators = annotators or self.DEFAULT_ANNOTATORS
self.default_properties = properties or self.DEFAULT_PROPERTIES
self.default_output_format = output_format or self.DEFAULT_OUTPUT_FORMAT
javaのコマンド叩いてるだけなので、ローカルの環境で再現したい場合は、これを穴埋めして叩けばいいです。READMEでexport CORENLP_HOME='path/to/stanfordnlp'
をしておかないと確かにエラー吐きますね。
"java -Xmx{memory} -cp '{corenlp_home}/*' edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port {port} -timeout {timeout} -threads {threads} -maxCharLength {max_char_length}".format(
corenlp_home=os.getenv("CORENLP_HOME"),
port=port,
memory=memory,
timeout=timeout,
threads=threads,
max_char_length=max_char_length
参考