本文是基于中文语料做的,对于英文语料应该也是同理,即同样适用的。
分析stanfordcorenlp的分词结果,可以发现,它好像是对最小的中文词进行分词,即其对中文的分词粒度很小,这对于某些nlp场景可能就不太合适了,自然的就想到能不能将stanfordcorenlp中用于分词的tokenizer替换掉,替换成自定义的,这样就可以控制中文分词结果是你想要的了。
基于以上动机,我查找了相关资料,发现需要对下载到的stanfordcorenlp的原文件夹中的tokensregex中的代码进行修改。我认为这样直接修改源文件容易使整个文件出错而不能用,也不太敢改,所以我就想到了另一种思路:
我直接改tokenizer比较难,那么我直接给你我分词后的结果,你根据我的分词结果帮我做ner(命名体识别)任务可以吗?
我又去查找了相关资料,发现只要将原本的nlp.ner(sentence)替换成nlp.annotate(...)即可,详细的代码如下:
nlp = StanfordCoreNLP(r'D:\stanford-corenlp-full-2016-10-31', port=8098, lang='zh')#,quiet=False,logging_level=logging.DEBUG) 后面的quiet和logging_level是用于显示日志信息,便于报错是寻找bug
ner_result = nlp.annotate(sentence,properties={
'annotators': 'ner',
'tokenize.language': 'Whitespace',
'pipelineLanguage':'zh', # 这个参数要加上,对中文才起作用
'outputFormat': 'json'
})
print(ner_result)
这样运行以上代码,就可以得到 按照你给的中文分词结果,然后利用stanfordcorenlp做ner的结果,如下所示:
输入的分词后以空格连接的句子:
被 扶养 人 生活费 43821.84 元 ;
利用stanfordcorenlp做ner的结果:(json格式)
{"sentences":[{"index":0,"tokens":[{"index":1,"word":"被","originalText":"被","lemma":"被","characterOffsetBegin":0,"characterOffsetEnd":1,"pos":"LB","ner":"O"},{"index":2,"word":"扶养","originalText":"扶养","lemma":"扶养","characterOffsetBegin":2,"characterOffsetEnd":4,"pos":"VV","ner":"O"},{"index":3,"word":"人","originalText":"人","lemma":"人","characterOffsetBegin":5,"characterOffsetEnd":6,"pos":"NN","ner":"O"},{"index":4,"word":"生活费","originalText":"生活费","lemma":"生活费","characterOffsetBegin":7,"characterOffsetEnd":10,"pos":"NN","ner":"O"},{"index":5,"word":"43821.84","originalText":"43821.84","lemma":"43821.84","characterOffsetBegin":11,"characterOffsetEnd":19,"pos":"CD","ner":"MONEY","normalizedNER":"元43821.84"},{"index":6,"word":"元","originalText":"元","lemma":"元","characterOffsetBegin":20,"characterOffsetEnd":21,"pos":"M","ner":"MONEY","normalizedNER":"元43821.84"},{"index":7,"word":";","originalText":";","lemma":";","characterOffsetBegin":22,"characterOffsetEnd":23,"pos":"PU","ner":"O"}]}]}
一种方法解决不了问题,有时候采用迂回的策略就可以相对容易的解决问题了hh
这里是希望你能越来越好的 小白冲鸭 ~~~
标签:index,自定义,tokenizer,pos,lemma,stanfordcorenlp,characterOffsetBegin,ner,分词 From: https://blog.csdn.net/m0_56367027/article/details/139551944