共指解析包装器
wrapperCoreference的Python项目详细描述
共指解析包装器
共指消解是在文本中查找引用同一实体的所有表达式的任务。对于许多涉及自然语言理解的高层次自然语言理解任务,如文档摘要、问答和信息抽取,这是一个重要的步骤。在
这是一个简单的库,它将两个引用解析模型封装成StanfordNLP包:统计模型和神经模型。这里我们使用SpaCy包加载神经模型(a.k.a,NeuralCoref),使用stanfordnlp包加载统计模型(a.k.a,CoreNLPCoref)。在
要求
pip3 install spacy pip3 install stanfordnlp pip3 install wrapperCoreference
StanfordNLP还需要手动下载一个核心模块,请查看here了解更多详细信息。在
^{pr2}$方法
神经模型的使用示例
fromwrapperCoreferenceimportWrapperCoreferencewc=WrapperCoreference()wc.NeuralCoref(u'My sister has a dog. She loves him.')#output: [{'start': 21, 'end': 24, 'text': 'She', 'resolved': 'My sister'}, {'start': 31, 'end': 34, 'text': 'him', 'resolved': 'a dog'}]
统计模型使用示例
fromwrapperCoreferenceimportWrapperCoreferencewc=WrapperCoreference()wc.setCoreNLP('/tmp/stanford-corenlp-full-2018-10-05')print(wc.CoreNLPCoref(u'My sister has a dog. She loves him.'))#output: [{'start': 31, 'end': 34, 'text': 'him', 'resolved': 'a dog', 'fullInformation': [{'start': 14, 'end': 19, 'text': 'a dog'}]}, {'start' : 21, 'end': 24, 'text': 'She', 'resolved': 'My sister', 'fullInformation': [{'start': 0, 'end': 9, 'text': 'My sister'}]}]
将输出与实体链接相结合
您可以使用nifwrapper库将共指输出与实体链接注释合并。在
fromwrapperCoreferenceimportWrapperCoreferencefromnifwrapperimport*#---- Obtaining coreferenceswc=WrapperCoreference()corefResults=wc.NeuralCoref(u'My sister has a dog. She loves him.')#corefResults = [{'start': 21, 'end': 24, 'text': 'She', 'resolved': 'My sister'}, {'start': 31, 'end': 34, 'text': 'him', 'resolved': 'a dog'}]#---- Obtaining Entity Linking results# inline NIF corpus creationwrp=NIFWrapper()doc=NIFDocument("https://example.org/doc1")#--sent=NIFSentence("https://example.org/doc1#char=0,19")sent.addAttribute("nif:beginIndex","0","xsd:nonNegativeInteger")sent.addAttribute("nif:endIndex","19","xsd:nonNegativeInteger")sent.addAttribute("nif:isString","My sister has a dog.","xsd:string")sent.addAttribute("nif:broaderContext",["https://example.org/doc1"],"URI LIST")#-- a1=NIFAnnotation("https://example.org/doc1#char=14,19","14","19",["https://en.wikipedia.org/wiki/ExambleDogUri"],["dbo:FamilyRelations"])a1.addAttribute("nif:anchorOf","a dog","xsd:string")sent.pushAnnotation(a1)doc.pushSentence(sent)#--sent2=NIFSentence("https://example.org/doc1#char=21,35")sent2.addAttribute("nif:isString","She loves him.","xsd:string")sent2.addAttribute("nif:broaderContext",["https://example.org/doc1"],"URI LIST")sent2.addAttribute("nif:beginIndex","21","xsd:nonNegativeInteger")sent2.addAttribute("nif:endIndex","35","xsd:nonNegativeInteger")doc.pushSentence(sent2)#--wrp.pushDocument(doc)#---- Combining EL annotations with coreferences wrp.extendsDocWithCoref(corefResults,doc.uri)print(wrp.toString())
- 项目
标签: