【Chinese Coreference resolution】StanfordNLP代码实现

5 篇文章 0 订阅
3 篇文章 0 订阅

最近在做一个角色识别的项目,项目中需要用到共指消解的方法,大体了解的有四种:

  1. 基于Spenbert(https://github.com/troublemaker-r/Chinese_Coreference_Resolution/blob/2b88450eeb3da248fb0f6365c38a32b9fffcb962/README.md)
  2. 基于问答系统的Span prediction(https://github.com/ShannonAI/CorefQA)
  3. 参考Emory NLP的工作,他们也是做角色识别(https://github.com/emorynlp/character-identification/)
  4. 基于StanfordNLP的API

目前暂时用StanfordNLP的方式进行了实现,由于StanfordNLP的坑有点多,因此特地来记录一下(小本本记下)

1. 下载相关库和包

CoreNLP下载CoreNLP 4.4.0和Chinese的jar (stanford-corenlp-4.4.0-models-chinese.jar),也可以去Hugging上下载,不过官网和Hugging上下载的不太一样,官网下载到的是stanford-corenlp-4.4.0-models-chinese.jar,Hugging下载的是StanfordCoreNLP_chinese_properties,这里主要用的是jar。然后将jar放入到CoreNLP 4.4.0的文件夹下;

2. 服务器运行

在pycharm命令行或者是cmd进入CoreNLP 4.4.0,输入命令

java -Xmx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer \ -serverProperties StanfordCoreNLP-chinese.properties -port 9000 -timeout 15000

这里的timeout(防止runtime out)和port可以自己设计,结束后记得kill掉,不然会占用端口

3. 贴上代码
import stanza
from stanza.server import CoreNLPClient
# StanfordCoreNLP_chinese_properties = get_StanfordCoreNLP_chinese_properties() 
from StanfordCoreNLP import get_StanfordCoreNLP_chinese_properties
StanfordCoreNLP_chinese_properties = get_StanfordCoreNLP_chinese_properties()
text ='胖大叔在后厨准备了一个大木盆,盆里装满不太好处理的兽肉骨骼,还有太肥的肉。很不错,肥肉对蛇来说无所谓且营养丰富。'

import json
with CoreNLPClient(
        annotators=['tokenize','ssplit','pos','lemma','ner', 'parse', 'depparse','coref'],
        # properties=StanfordCoreNLP_chinese_properties, 这个就是Hug下载的文件
        timeout=30000,
        memory='16G',
        start_server='http://127.0.0.1:9000') as client:
    ann = client.annotate(text)
mychains = list()
chains = ann.corefChain
for chain in chains:
    mychain = list()
    for mention in chain.mention:
        words_list = ann.sentence[mention.sentenceIndex].token[mention.beginIndex:mention.endIndex]
        #build a string out of the words of this mention
        ment_word = ' '.join([x.word for x in words_list])
        mychain.append(ment_word)
    mychains.append(mychain)

for chain in mychains:
    print(' <-> '.join(chain))

另外~
import corenlp这个方法已经outdated啦,大家用了可能会出现一系列无法解决的bug,建议不用~

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值