在python中使用stanford CoreNLP coreference有三种方法
1. 使用命令调用的方式如下:(奇怪的是我的没有任何输出,先挖个坑,等找到原因再来补充)
import subprocess
subprocess.call(['java','-cp','F:/Program Files/jars/stanford-corenlp-full-2018-02-27/*','-Xmx4g',
'edu.stanford.nlp.pipeline.StanfordCoreNLP',"-annotators",
"tokenize,ssplit,pos,lemma,ner",'-file','subprocesstest.txt'])
2. 使用python wrapper
可用的wrapper比较多,可以再https://stanfordnlp.github.io/CoreNLP/other-languages.html这个页面找到。我用的是第一个stanfordcorenlp。这个可以通过pip install stanfordcorenlp安装。安装之后可以通过以下方式调用
from stanfordcorenlp import StanfordCoreNLP
nlp = StanfordCoreNLP(r'F:\Program Files\jars\stanford-corenlp-full-2018-02-27')###这里的路径是下载并解压stanford-corenlp-full的路径
sentence = "Kosgi Santosh sent an email to Stanford University. He didn't get a reply"
print ('Tokenize:', nlp.coref(sentence))
nlp.close() # 注意:这里需要关闭记得关闭,因为这个对内存的消耗是非常大的。
3. 启动Stanford CoreNLP Server
很多人在Stack Overflow上面反映使用wrapper的方式比较慢,有人说使用server的方式调用会稍微快一些。我试了一下,好像是吧。方法分为两大步骤:先启动server,再在python里面里面调用。
A.启动server
先进入stanford-corenlp-full的安装目录,如图:
再启动server,如图:
B.在python中调用,示例如下:
from stanfordcorenlp import StanfordCoreNLP
nlp = StanfordCoreNLP('http://localhost', port=9000)
sentence = "Kosgi Santosh sent an email to Stanford University. He didn't get a reply"
print ('Tokenize:', nlp.coref(sentence))
nlp.close()
注意:如果只有一个句子,方法2与方法3所消耗的时间差别不大,但是如果有很多句子的话,还是方法3比较好一些。
参考文献:
- Xiaoxiao's tech blog. How to Use Stanford CoreNLP in Python[EB/OL]. https://cindyxiaoxiaoli.wordpress.com/2017/04/10/how-to-use-stanford-corenlp-in-python/.
- Stanford CoreNLP. Using Stanford CoreNLP within other programming languages and packages[EB/OL]. https://stanfordnlp.github.io/CoreNLP/other-languages.html.
- Lynten/stanford-corenlp. Lynten/stanford-corenlp[EB/OL]. https://github.com/Lynten/stanford-corenlp.
- Stanford CoreNLP. Using Stanford CoreNLP from the command line[EB/OL]. https://stanfordnlp.github.io/CoreNLP/cmdline.html.
- Stanford CoreNLP. Server[EB/OL]. https://stanfordnlp.github.io/CoreNLP/corenlp-server.html.