如何在Python中使用spacy依赖树获得祖先的孩子

Aar*_*yar 4 python nltk spacy

代码如下:

    import spacy
    from nltk import Tree
    en_nlp = spacy.load('en')
    parsed = en_nlp(u"Photos under low lighting are poor, both front and back cameras.")
    print(u'sentence:{0}'.format(parsed.text))
    try2 = []
    print(u'parsed_sentence_children::{0}'.format([(x.text,x.pos_,x.dep_,[(x.text,x.dep_) for x in list(x.children)]) for x in parsed]))
    print("\n\n")
    for x in parsed:
        if x.pos_=="NOUN" and x.dep_=="nsubj":
            print(u'Noun and noun subject:{0}'.format(try2 =[(x.text,x.pos_,x.dep_,[(x.text,x.pos_)for x in list(x.ancestors)])])
Run Code Online (Sandbox Code Playgroud)

其输出是:
[(u'Photos', u'NOUN', u'nsubj', [(u'are', u'VERB')]

现在我希望打印acomp以下子项:
[(u'are', u'VERB')]
这是以下项的祖先:
[(u'Photos', u'NOUN', u'nsubj')]

我怎样才能做到这一点?

Eug*_*ene 5

您可以遍历令牌:

import spacy

nlp = spacy.load('en')

text = 'Photos under low lighting are poor, both front and back cameras.'

for token in nlp(text):
    if token.dep_ == 'nsubj': # Or other forms of subjects / objects
        print(token.lemma_+"'s are:")
        for a in token.ancestors:
            if a.text == 'are': # Or however you determine your selection
                for atok in a.children:
                    if atok.dep_ == 'acomp': # Note, you should look for more than just acomp
                        print(atok.text)
Run Code Online (Sandbox Code Playgroud)

其输出(在Python3中):

photo's are:
poor
Run Code Online (Sandbox Code Playgroud)

不过,请查看Spacy 的关于依赖关系的页面。有很多事情需要考虑。您可以使用DisplaCy(此链接也是类似句子的示例,具有不同的依赖关系)。

我希望这至少可以帮助您指明正确的方向!