如何使用 spacy 获取句子中两个单词之间的一对依存关系?

How to get a pair of dependency relation between two words in a sentence using spacy?

我正在使用 spacy 获取依赖关系,效果很好。但是我在获取一对具有特定依赖关系的令牌时遇到了问题(conj 关系除外)。

使用.dep_时,我可以获得每个单独令牌的依赖属性。 但是,我想要一对特定依赖关系的标记。 例如,在下面的代码中,我可以得到显示的结果。

import spacy
nlp = spacy.load("en_core_web_md")
sentence = 'The Marlins were stymied by Austin Gomber and the Rockies in their 4-3 loss'
doc = nlp(sentence)
for token in doc:
    print (token, token.dep_)

当前输出:

The det
Marlins nsubjpass
were auxpass
stymied ROOT
by agent
Austin compound
Gomber pobj
and cc
the det
Rockies conj
in prep
their poss
4 nummod
- punct
3 prep
loss pobj

但我渴望得到的是: (请忽略输出样式,我只想得到一对具有特定依赖关系的令牌,例如,这里是pobj

'Gomber' is a 'pobj' of 'by'
'Loss' is a 'pobj' of 'in'

换句话说,我不仅要得到当前输出的结果,我还想得到配对的令牌每个字。

对于conj依赖关系,我只用token.conjuncts就可以得到它们,但对于其他依赖关系,如pobjprep, 没发现有什么方法可以直接在spacy中使用。

有没有人知道如何获得这种 pobj 关系?提前致谢!

您可以使用头部索引。例如,

tok_l = doc.to_json()['tokens']
for t in tok_l:
  head = tok_l[t['head']]
  print(f"'{sentence[t['start']:t['end']]}' is {t['dep']} of '{sentence[head['start']:head['end']]}'")

结果:

'The' is det of 'Marlins'
'Marlins' is nsubjpass of 'stymied'
'were' is auxpass of 'stymied'
'stymied' is ROOT of 'stymied'
'by' is agent of 'stymied'
'Austin' is compound of 'Gomber'
'Gomber' is pobj of 'by'
'and' is cc of 'Gomber'
'the' is det of 'Rockies'
'Rockies' is conj of 'Gomber'
'in' is prep of 'stymied'
'their' is poss of 'loss'
'4' is nummod of 'loss'
'-' is punct of '3'
'3' is prep of '4'
'loss' is pobj of 'in'