在python中使用stanford CoreNLP coreference

在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比较好一些。


参考文献:

  1. 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/.
  2. Stanford CoreNLP. Using Stanford CoreNLP within other programming languages and packages[EB/OL]. https://stanfordnlp.github.io/CoreNLP/other-languages.html.
  3. Lynten/stanford-corenlp. Lynten/stanford-corenlp[EB/OL]. https://github.com/Lynten/stanford-corenlp.
  4. Stanford CoreNLP. Using Stanford CoreNLP from the command line[EB/OL]. https://stanfordnlp.github.io/CoreNLP/cmdline.html.
  5. Stanford CoreNLP. Server[EB/OL]. https://stanfordnlp.github.io/CoreNLP/corenlp-server.html.

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值