Python 在共指消解中获取stanfordNLP输出中的字符位置

Python 在共指消解中获取stanfordNLP输出中的字符位置,python,stanford-nlp,Python,Stanford Nlp,我正试图使用斯坦福德NLP来解释共指消解。我正在运行上述代码(提供): 从stanfordnlp.server导入CoreNLPClient 巴拉克出生在夏威夷。他的妻子米歇尔出生在米兰。他说她很聪明 打印(f“输入文本:{text}”) #设置客户端 client=CoreNLPClient(属性={'annotators':'coref','coref.algorithm':'statistic'},超时=60000,内存=16G') #将请求提交到服务器 ann=客户端。注释(文本) my

我正试图使用斯坦福德NLP来解释共指消解。我正在运行上述代码(提供):

从stanfordnlp.server导入CoreNLPClient
巴拉克出生在夏威夷。他的妻子米歇尔出生在米兰。他说她很聪明
打印(f“输入文本:{text}”)
#设置客户端
client=CoreNLPClient(属性={'annotators':'coref','coref.algorithm':'statistic'},超时=60000,内存=16G')
#将请求提交到服务器
ann=客户端。注释(文本)
mychains=list()
链=ann.corefChain
对于链中链:
mychain=list()
#把这条链子的每一个字都循环一遍
在链中提及。提及:
#获取该提及所在的句子,并获取属于该提及的单词
#(我们可以有多个词,例如,一个提词可以是像“他”这样的代词,也可以是像“他的妻子米歇尔”这样的复合名词)
words\u list=ann.station[title.sentenceIndex].token[title.beginIndex:title.endIndex]
#用上面提到的单词组成一个字符串
单词=''.join([x.word代表单词列表中的x])
mychain.append(单词)
mychains.append(mychain)
对于mychains中的链:
打印(“”.连接(链))
安装库后:

pip3 install stanfordcorenlp
下载模型

wget http://nlp.stanford.edu/software/stanford-corenlp-full-2018-10-05.zip
设置$CORENLP_HOME变量

os.environ['CORENLP_HOME']=“path/to/stanford-CORENLP-full-2018-10-05”
这段代码对我来说运行得很好,但是,输出只包含标记而不是字符的信息。例如,对于上述代码,输出为:

Barack <-> His <-> He
His wife Michelle <-> she
我在搜索其他属性,例如,打印ann.ReferencesForCoref

mentionType: "PROPER"
number: "SINGULAR"
gender: "MALE"
animacy: "ANIMATE"
person: "UNKNOWN"
startIndex: 0
endIndex: 1
headIndex: 0
headString: "barack"
nerString: "PERSON"
originalRef: 4294967295
goldCorefClusterID: -1
corefClusterID: 5
mentionNum: 0
sentNum: 0
utter: 0
paragraph: 1
isSubject: false
isDirectObject: true
isIndirectObject: false
isPrepositionObject: false
hasTwin: false
generic: false
isSingleton: false
hasBasicDependency: true
hasEnhancedDepenedncy: true
hasContextParseTree: true

尽管该属性提供了大量信息,但没有关于单词字符位置的信息。我可以用空格分开句子,但这不是一般的,我认为可能会有失败的情况。有人能帮我吗???

在构建客户端时尝试添加
output\u format='json'
。JSON数据应该具有每个令牌的字符偏移量信息

这里有关于使用客户端的信息:


您可以从coref提及中找出哪些标记对应于coreference提及,并且这些标记包含字符偏移量信息。谢谢StanfordNLPHelp!!
mentionType: "PROPER"
number: "SINGULAR"
gender: "MALE"
animacy: "ANIMATE"
person: "UNKNOWN"
startIndex: 0
endIndex: 1
headIndex: 0
headString: "barack"
nerString: "PERSON"
originalRef: 4294967295
goldCorefClusterID: -1
corefClusterID: 5
mentionNum: 0
sentNum: 0
utter: 0
paragraph: 1
isSubject: false
isDirectObject: true
isIndirectObject: false
isPrepositionObject: false
hasTwin: false
generic: false
isSingleton: false
hasBasicDependency: true
hasEnhancedDepenedncy: true
hasContextParseTree: true