NLTK: How do I traverse a noun phrase to return list of strings?

前端 未结 1 1854
面向向阳花
面向向阳花 2021-01-05 08:38

In NLTK, how do I traverse a parsed sentence to return a list of noun phrase strings?

I have two goals:
(1) Create the list of Noun Phrases instead of printing t

相关标签:
1条回答
  • 2021-01-05 09:04
    def extract_np(psent):
      for subtree in psent.subtrees():
        if subtree.label() == 'NP':
          yield ' '.join(word for word, tag in subtree.leaves())
    
    
    cp = nltk.RegexpParser(grammar)
    parsed_sent = cp.parse(tagged_sent)
    for npstr in extract_np(parsed_sent):
        print (npstr)
    
    0 讨论(0)
提交回复
热议问题