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

Mel*_*ina 1 python dependencies parsing nlp 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_)

Run Code Online (Sandbox Code Playgroud)

电流输出:

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

Run Code Online (Sandbox Code Playgroud)

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

'Gomber' is a 'pobj' of 'by'
'Loss' is a 'pobj' of 'in'
Run Code Online (Sandbox Code Playgroud)

换句话说,我不仅想得到当前输出的结果,我还想得到每个单词的配对标记。

对于conj依赖关系,我可以简单地使用 来获取它们token.conjuncts,但是对于其余的其他依赖关系,例如pobj, prep,我还没有找到任何可以直接在 spacy 中使用的方法。

有没有人有关于获得这种关系的提示pobj?提前致谢!

dim*_*mid 5

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

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']]}'")
Run Code Online (Sandbox Code Playgroud)

结果:

'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'
Run Code Online (Sandbox Code Playgroud)