Parsing 如何遍历NLTK树对象?

Parsing 如何遍历NLTK树对象?,parsing,tree,nlp,nltk,depth-first-search,Parsing,Tree,Nlp,Nltk,Depth First Search,给定一个括号内的解析,我可以将其转换为NLTK中的树对象,如下所示: >>> from nltk.tree import Tree >>> s = '(ROOT (S (NP (NNP Europe)) (VP (VBZ is) (PP (IN in) (NP (DT the) (JJ same) (NNS trends)))) (. .)))' >>> Tree.fromstring(s) Tree('ROOT', [Tree('S', [

给定一个括号内的解析,我可以将其转换为NLTK中的树对象,如下所示:

>>> from nltk.tree import Tree
>>> s = '(ROOT (S (NP (NNP Europe)) (VP (VBZ is) (PP (IN in) (NP (DT the) (JJ same) (NNS trends)))) (. .)))'
>>> Tree.fromstring(s)
Tree('ROOT', [Tree('S', [Tree('NP', [Tree('NNP', ['Europe'])]), Tree('VP', [Tree('VBZ', ['is']), Tree('PP', [Tree('IN', ['in']), Tree('NP', [Tree('DT', ['the']), Tree('JJ', ['same']), Tree('NNS', ['trends'])])])]), Tree('.', ['.'])])])
但当我尝试遍历它时,我只能访问最顶端的树:

>>> for i in Tree.fromstring(s):
...     print i
... 
(S
  (NP (NNP Europe))
  (VP (VBZ is) (PP (IN in) (NP (DT the) (JJ same) (NNS trends))))
  (. .))
>>> for i in Tree.fromstring(s):
...     print i, i.label()
... 
(S
  (NP (NNP Europe))
  (VP (VBZ is) (PP (IN in) (NP (DT the) (JJ same) (NNS trends))))
  (. .)) S
>>> 
我可以深入一层,如下所示:

>>> for i in Tree.fromstring(s):
...     print i.subtrees()
... 
<generator object subtrees at 0x7f1eb1571410>
>>> for i in Tree.fromstring(s):
...     for j in i.subtrees():
...             print j
... 
(S
  (NP (NNP Europe))
  (VP (VBZ is) (PP (IN in) (NP (DT the) (JJ same) (NNS trends))))
  (. .))
(NP (NNP Europe))
(NNP Europe)
(VP (VBZ is) (PP (IN in) (NP (DT the) (JJ same) (NNS trends))))
(VBZ is)
(PP (IN in) (NP (DT the) (JJ same) (NNS trends)))
(IN in)
(NP (DT the) (JJ same) (NNS trends))
(DT the)
(JJ same)
(NNS trends)
(. .)
树中i的
>>。fromstring:
...     打印i.子树()
... 
>>>对于树中的i.fromstring:
...     对于i.子树()中的j:
...             打印j
... 
(S)
(NP(NNP欧洲)
(副总裁(VBZ is)(PP(IN)(NP(DT)(JJ相同)(NNS趋势)))
(. .))
(NP(NNP欧洲)
(NNP欧洲)
(副总裁(VBZ is)(PP(IN)(NP(DT)(JJ相同)(NNS趋势)))
(VBZ是)
(PP(IN)(NP(DT)(JJ相同)(NNS趋势)))
(IN)
(NP(DT)(JJ相同)(NNS趋势))
(DT)
(JJ相同)
(NNS趋势)
(. .)
但是有没有一种方法可以在深度上遍历所有子树呢

如何在NLTK中遍历树?


如何遍历NLTK中的所有子树?

也许我忽略了一些事情,但这就是你想要的吗

import nltk
s = '(ROOT (S (NP (NNP Europe)) (VP (VBZ is) (PP (IN in) (NP (DT the) (JJ same) (NNS trends)))) (. .)))'
tree = nltk.tree.Tree.fromstring(s)
def traverse_tree(tree):
    # print("tree:", tree)
    for subtree in tree:
        if type(subtree) == nltk.tree.Tree:
            traverse_tree(subtree)
traverse_tree(tree)

它首先遍历树的深度。

NLTK为树的宽度优先遍历提供了一种方法: