给定一个括号内的解析,我可以将其转换为 NLTK 中的 Tree 对象,如下所示:
>>> 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)
(. .)
但是有没有一种方法可以深度遍历所有子树呢?
在 NLTK 中应该如何遍历一棵树?
如何遍历NLTK中的所有子树?