我正在尝试通过 NLTK 使用斯坦福解析器,按照示例here http://www.nltk.org/api/nltk.parse.html#nltk.parse.corenlp.CoreNLPDependencyParser%20tutorial%20here.
我按照示例的前两行进行操作(进行必要的导入)
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.')
但我收到一条错误消息:
[...] Failed to establish a new connection: [Errno 61] Connection refused"
我意识到尝试连接到作为构造函数输入给出的 url 一定是一个问题。
dep_parser = CoreNLPDependencyParser(url='http://localhost:9000')
如果不是这个,我应该连接到哪个网址?如果这是正确的,那么问题是什么?
您需要首先下载并运行 CoreNLP 服务器localhost:9000
.
1)下载CoreNLP:https://stanfordnlp.github.io/CoreNLP/download.html https://stanfordnlp.github.io/CoreNLP/download.html
2)将文件解压到某个目录,然后在该目录中运行以下命令来启动服务器
java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 15000
Ref: https://stanfordnlp.github.io/CoreNLP/corenlp-server.html https://stanfordnlp.github.io/CoreNLP/corenlp-server.html
上面代码的结果就像
>>> 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
您还可以通过NLTK API启动服务器(需要配置CORENLP_HOME
首先是环境变量)
os.environ["CORENLP_HOME"] = "dir"
client = corenlp.CoreNLPClient()
# do something
client.stop()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)