注意:此解决方案仅适用于:
首先,您必须先正确安装 Java 8,如果 Stanford CoreNLP 在命令行上工作,NLTK v3.2.5 中的 Stanford CoreNLP API 如下。
注意:在使用 NLTK 中的新 CoreNLP API 之前,您必须在终端中启动 CoreNLP 服务器。
在终端上:
wget http://nlp.stanford.edu/software/stanford-corenlp-full-2016-10-31.zip
unzip stanford-corenlp-full-2016-10-31.zip && cd stanford-corenlp-full-2016-10-31
java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer \
-preload tokenize,ssplit,pos,lemma,parse,depparse \
-status_port 9000 -port 9000 -timeout 15000
在 Python 中:
>>> from nltk.parse.corenlp import CoreNLPParser
>>> st = CoreNLPParser()
>>> tokenized_sent = list(st.tokenize('What is the airspeed of an unladen swallow ?'))
>>> tokenized_sent
['What', 'is', 'the', 'airspeed', 'of', 'an', 'unladen', 'swallow', '?']