如何利用空间距离得到句子中两个词之间的一对依存关系?

2024-06-02 06:01:13 发布

您现在位置:Python中文网/ 问答频道 /正文

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

当使用.dep_时,我可以获得每个seprate令牌的dependency属性。 但是,我想为一个特定的依赖关系创建一对令牌。 例如,在下面的代码中,我可以得到显示的结果

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关系的提示?提前谢谢


Tags: theintokenbynlp关系spacysentence
1条回答
网友
1楼 · 发布于 2024-06-02 06:01:13

您可以使用head索引。例如:

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'

相关问题 更多 >