How to get all noun phrases in Spacy

后端 未结 3 1139
轮回少年
轮回少年 2021-02-14 17:06

I am new to Spacy and I would like to extract \"all\" the noun phrases from a sentence. I\'m wondering how I can do it. I have the following code:

i         


        
3条回答
  •  忘了有多久
    2021-02-14 17:33

    Please see commented code below to recursively combine the nouns. Code inspired by the Spacy Docs here

    import spacy
    
    nlp = spacy.load("en")
    
    doc = nlp("We try to explicitly describe the geometry of the edges of the images.")
    
    for np in doc.noun_chunks: # use np instead of np.text
        print(np)
    
    print()
    
    # code to recursively combine nouns
    # 'We' is actually a pronoun but included in your question
    # hence the token.pos_ == "PRON" part in the last if statement
    # suggest you extract PRON separately like the noun-chunks above
    
    index = 0
    nounIndices = []
    for token in doc:
        # print(token.text, token.pos_, token.dep_, token.head.text)
        if token.pos_ == 'NOUN':
            nounIndices.append(index)
        index = index + 1
    
    
    print(nounIndices)
    for idxValue in nounIndices:
        doc = nlp("We try to explicitly describe the geometry of the edges of the images.")
        span = doc[doc[idxValue].left_edge.i : doc[idxValue].right_edge.i+1]
        span.merge()
    
        for token in doc:
            if token.dep_ == 'dobj' or token.dep_ == 'pobj' or token.pos_ == "PRON":
                print(token.text)
    

提交回复
热议问题