斯坦福大学( Stanford )提供了共同参考解决方案( coreference CoreNLP 如前所述 ),这条线、这也提供了一些关于它在Java中实现的见解。
但是,我使用的是python和NLTK,我不知道如何在我的python代码中使用CoreNLP的协引用解析功能。我已经能够在NLTK中设置StanfordParser,这是我的代码。
from nltk.parse.stanford import StanfordDependencyParser
stanford_parser_dir = 'stanford-parser/'
eng_model_path = stanford_parser_dir + "stanford-parser-models/edu/stanford/nlp/models/lexparser/englishRNN.ser.gz"
my_path_to_models_jar = stanford_parser_dir + "stanford-parser-3.5.2-models.jar"
my_path_to_jar = stanford_parser_dir + "stanford-parser.jar"如何在python中使用CoreNLP的共同引用分辨率?
发布于 2017-03-16 13:09:39
正如@Igor所提到的,您可以尝试在这个GitHub repo:https://github.com/dasmith/stanford-corenlp-python中实现的python包装器。
此回购文件包含两个主要文件: corenlp.py client.py
执行以下更改以使coreNLP工作:
if not corenlp_path: corenlp_path = <path to the corenlp file>jars = ["stanford-corenlp-3.4.1.jar", "stanford-corenlp-3.4.1-models.jar", "joda-time.jar", "xom.jar", "jollyday.jar"]在此中,将3.4.1替换为您下载的jar版本。
python corenlp.py这将启动一个服务器。
python client.py这提供了一个字典,您可以使用' coref‘作为键访问coref:
例如:约翰是计算机科学家。他喜欢编码。
{
"coref": [[[["a Computer Scientist", 0, 4, 2, 5], ["John", 0, 0, 0, 1]], [["He", 1, 0, 0, 1], ["John", 0, 0, 0, 1]]]]
}我在Ubuntu 16.04上试过这个。使用7或8。
发布于 2018-01-15 11:12:28
史坦福是一种相对较新的包装器,可能对您有用。
假设文本是“巴拉克奥巴马出生在夏威夷,他是总统,奥巴马于2008年当选。”

守则:
# coding=utf-8
import json
from stanfordcorenlp import StanfordCoreNLP
nlp = StanfordCoreNLP(r'G:\JavaLibraries\stanford-corenlp-full-2017-06-09', quiet=False)
props = {'annotators': 'coref', 'pipelineLanguage': 'en'}
text = 'Barack Obama was born in Hawaii. He is the president. Obama was elected in 2008.'
result = json.loads(nlp.annotate(text, properties=props))
num, mentions = result['corefs'].items()[0]
for mention in mentions:
print(mention)上面的每一个“提到”都是Python的一个分支,如下所示:
{
"id": 0,
"text": "Barack Obama",
"type": "PROPER",
"number": "SINGULAR",
"gender": "MALE",
"animacy": "ANIMATE",
"startIndex": 1,
"endIndex": 3,
"headIndex": 2,
"sentNum": 1,
"position": [
1,
1
],
"isRepresentativeMention": true
}发布于 2019-07-18 19:05:08
斯坦福大学的CoreNLP现在有一个名为StanfordNLP的正式Python绑定,你可以在StanfordNLP网站里读到。
本机似乎支持coref处理器,但是您可以使用CoreNLPClient接口从CoreNLP调用“标准”CoreNLP(原始CoreNLP软件)。
因此,在按照设置Python包装器这里的说明之后,您可以获得如下所示的协引用链:
from stanfordnlp.server import CoreNLPClient
text = 'Barack was born in Hawaii. His wife Michelle was born in Milan. He says that she is very smart.'
print(f"Input text: {text}")
# set up the client
client = CoreNLPClient(properties={'annotators': 'coref', 'coref.algorithm' : 'statistical'}, timeout=60000, memory='16G')
# submit the request to the server
ann = client.annotate(text)
mychains = list()
chains = ann.corefChain
for chain in chains:
mychain = list()
# Loop through every mention of this chain
for mention in chain.mention:
# Get the sentence in which this mention is located, and get the words which are part of this mention
# (we can have more than one word, for example, a mention can be a pronoun like "he", but also a compound noun like "His wife Michelle")
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))https://stackoverflow.com/questions/39410282
复制相似问题