python 调用standford corenlp 对分好词的句子做命名实体识别

from stanfordcorenlp import StanfordCoreNLP

nlp = StanfordCoreNLP(r’./stanford-corenlp-4.2.0’,lang=‘zh’,logging_level=logging.DEBUG)

text = str(每个词用空格分隔

方法一:

调用nlp.ner:
ner_result = nlp.ner(text)

更改corenlp.py文件的”_request()”函数,在232行“properties = {‘annotators’: annotators, ‘outputFormat’: ‘json’}”多加一个参数:‘tokenize.language’: ‘Whitespace’ =》properties = {‘annotators’: annotators, ‘outputFormat’: ‘json’,‘tokenize.language’: ‘Whitespace’}

方法二

调用 nlp.annotate:
ner_result = nlp.annotate(sentstr,properties={
‘annotators’: ‘ner’,
’tokenize.language’: ‘Whitespace’,
’pipelineLanguage’:‘zh’, # 这个参数要加上,对中文才起作用
‘outputFormat’: ‘json’
})

PS:
一:两种方法的原理一样:通过对比 annotate和ner的代码,发现都调用了r = requests.post(self.url, params=params, data=data, headers={‘Connection’: ‘close’}),不同的地方就在params的参数里,两种实现方法都是一样的原理,都是加上了’tokenize.language’: ‘Whitespace’这个参数,方法一之所以不需要’pipelineLanguage’:'zh’加参数,是因为方法一nlp.ner(),ner会调用内置方法“_request()”,内置方法“_request()”中的params已经有这个参数

二:如果句子里有‘%’号,无法使用命名实体对其进行注释。会报错’Could not handle incoming annotation’错误,这个在stanfordcorenlp的文档中也有提到https://www.bountysource.com/teams/stanfordnlp/issues

三:使用完后,一定要调用nlp.close()

参考:https://stackoverflow.com/questions/45299170/stanford-corenlp-tokenize-whitespace-property-not-working-on-chinese

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值