How to get all noun phrases in Spacy

后端 未结 3 1140
轮回少年
轮回少年 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:44

    For every noun chunk you can also get the subtree beneath it. Spacy provides two ways to access that:left_edge and right edge attributes and the subtree attribute, which returns a Token iterator rather than a span. Combining noun_chunks and their subtree lead to some duplication which can be removed later.

    Here is an example using the left_edge and right edge attributes

    {np.text
      for nc in doc.noun_chunks
      for np in [
        nc, 
        doc[
          nc.root.left_edge.i
          :nc.root.right_edge.i+1]]}                                                                                                                                                                                                                                                                                                                                                                                                                                                 
    
    ==>
    
    {'We',
     'the edges',
     'the edges of the images',
     'the geometry',
     'the geometry of the edges of the images',
     'the images'}
    

提交回复
热议问题