NLTK CoreNLPDependencyParser: Failed to establish connection

落爺英雄遲暮 提交于 2019-12-10 10:03:05

问题


I'm trying to use the Stanford Parser through NLTK, following the example here.

I follow the first two lines of the example (with the necessary import)

from nltk.parse.corenlp import CoreNLPDependencyParser
dep_parser = CoreNLPDependencyParser(url='http://localhost:9000')
parse, = dep_parser.raw_parse('The quick brown fox jumps over the lazy dog.')

but I get an error saying:

[...] Failed to establish a new connection: [Errno 61] Connection refused"

I realize that it must be an issue with trying to connect to the url given as input to the constructor.

dep_parser = CoreNLPDependencyParser(url='http://localhost:9000')

What url should I be connecting to, if not this? If this is correct, what is the issue?


回答1:


You need to first download and run the CoreNLP server on localhost:9000.

1) download CoreNLP at https://stanfordnlp.github.io/CoreNLP/download.html
2) unzip the files to some directory then run the following command in the that directory to start the server

java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 15000

Ref: https://stanfordnlp.github.io/CoreNLP/corenlp-server.html

The result of the above code is like

>>> print(parse.to_conll(4))
The DT  4   det
quick   JJ  4   amod
brown   JJ  4   amod
fox NN  5   nsubj
jumps   VBZ 0   ROOT
over    IN  9   case
the DT  9   det
lazy    JJ  9   amod
dog NN  5   nmod
.   .   5   punct

You can also start the server via NLTK API (need to configure the CORENLP_HOME environment variable first)

os.environ["CORENLP_HOME"] = "dir"
client = corenlp.CoreNLPClient()
# do something
client.stop()


来源:https://stackoverflow.com/questions/47584738/nltk-corenlpdependencyparser-failed-to-establish-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!