25

我已经使用以下方法分块了一个句子:

grammar = '''                                                                                                              
    NP:                                                                                                                    
       {<DT>*(<NN.*>|<JJ.*>)*<NN.*>}                                                                                       
     NVN:                                                                                                                  
       {<NP><VB.*><NP>}                                                                                                    
    '''
chunker = nltk.chunk.RegexpParser(grammar)
tree = chunker.parse(tagged)
print tree

结果如下所示:

(S
  (NVN
    (NP The_Pigs/NNS)
    are/VBP
    (NP a/DT Bristol-based/JJ punk/NN rock/NN band/NN))
  that/WDT
  formed/VBN
  in/IN
  1977/CD
  ./.)

但现在我一直在试图弄清楚如何导航。我希望能够找到 NVN 子树,并访问左侧的名词短语(“The_Pigs”)、动词(“are”)和右侧的名词短语(“a Bristol-based punk rock band”) . 我怎么做?

4

4 回答 4

19

尝试:

ROOT = 'ROOT'
tree = ...
def getNodes(parent):
    for node in parent:
        if type(node) is nltk.Tree:
            if node.label() == ROOT:
                print "======== Sentence ========="
                print "Sentence:", " ".join(node.leaves())
            else:
                print "Label:", node.label()
                print "Leaves:", node.leaves()

            getNodes(node)
        else:
            print "Word:", node

getNodes(tree)
于 2014-03-08T14:10:29.687 回答
9

当然,您可以编写自己的深度优先搜索……但有一种更简单(更好)的方法。如果您希望每个子树都以 NVM 为根,请使用 Tree 的子树方法并定义过滤器参数。

>>> print t
(S
    (NVN
        (NP The_Pigs/NNS)
        are/VBP
        (NP a/DT Bristol-based/JJ punk/NN rock/NN band/NN))
    that/WDT
    formed/VBN
    in/IN
    1977/CD
    ./.)
>>> for i in t.subtrees(filter=lambda x: x.node == 'NVN'):
...     print i
... 
(NVN
    (NP The_Pigs/NNS)
    are/VBP
    (NP a/DT Bristol-based/JJ punk/NN rock/NN band/NN))
于 2013-03-04T23:12:35.173 回答
7

这是一个代码示例,用于生成带有标签“NP”的所有子树

def filt(x):
    return x.label()=='NP'

for subtree in t.subtrees(filter =  filt): # Generate all subtrees
    print subtree

对于兄弟姐妹,您可能想看看方法ParentedTree.left_siblings()

有关更多详细信息,这里有一些有用的链接。

http://www.nltk.org/howto/tree.html #一些基本用法和示例 http://nbviewer.ipython.org/github/gmonce/nltk_parsing/blob/master/1.%20NLTK%20Syntax%20Trees。 ipynb #a notebook playwith这些方法

http://www.nltk.org/_modules/nltk/tree.html #all api with source

于 2014-12-11T03:25:28.150 回答
5

尝试这个:

for a in tree:
        if type(a) is nltk.Tree:
            if a.node == 'NVN': # This climbs into your NVN tree
                for b in a:
                    if type(b) is nltk.Tree and b.node == 'NP':
                        print b.leaves() # This outputs your "NP"
                    else:
                        print b # This outputs your "VB.*"

它输出这个:

[('The_Pigs','NNS')]

('是','VBP')

[('a', 'DT'), ('Bristol-based', 'JJ'), ('punk', 'NN'), ('rock', 'NN'), ('band', 'NN' ')]

于 2013-02-13T22:26:23.293 回答