tree - 如何导航 nltk.tree.Tree?

标签 tree nltk

我使用以下语句对句子进行了分块:

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”) .我怎么做?

最佳答案

尝试:

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)

关于tree - 如何导航 nltk.tree.Tree?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14841997/

相关文章:

nlp - nltk 词干分析器 : string index out of range

python - 如何让 *logger 和 *ology 这样的词词干/词形还原为相同的词根?

python - 如何分析sklearn中tfidf矩阵的值?

PostgreSQL 简单键与复合键

java - 为什么在我的 BST 中序遍历中显示的是指针而不是字符串?

c++ - 在 TTree(cern 根)中写入/读取字符串

python - 应用LDA后如何自动标记主题

python - 波斯语 NLTK

python - 如何逐层打印二叉树

d3.js - 将 d3js 树折叠到指定深度