如何遍历 NLTK 树对象? [英] How to Traverse an NLTK Tree object?

查看:38
本文介绍了如何遍历 NLTK 树对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个括号内的解析,我可以将它转换为 NLTK 中的 Tree 对象:

<预><代码>>>>从 nltk.tree 导入树>>>s = '(ROOT(S(NP(NNP欧洲))(VP(VBZ是)(PP(IN中)(NP(DT))(JJ同)(NNS趋势))))(..)))'>>>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('.', ['.'])])])

但是当我尝试遍历它时,我只能访问最顶层的树:

<预><代码>>>>对于 Tree.fromstring(s) 中的 i:...打印我...(S(NP (NNP 欧洲))(VP(VBZ)(PP(IN)(NP(DT)(JJ同)(NNS趋势))))(..))>>>对于 Tree.fromstring(s) 中的 i:... 打印 i, i.label()...(S(NP (NNP 欧洲))(VP(VBZ)(PP(IN)(NP(DT)(JJ同)(NNS趋势))))(. .)) S>>>

我可以按如下方式深入一层:

<预><代码>>>>对于 Tree.fromstring(s) 中的 i:... 打印 i.subtrees()...<0x7f1eb1571410处的生成器对象子树>>>>对于 Tree.fromstring(s) 中的 i:...对于 i.subtrees() 中的 j:... 打印 j...(S(NP (NNP 欧洲))(VP(VBZ)(PP(IN)(NP(DT)(JJ同)(NNS趋势))))(..))(NP (NNP 欧洲))(NNP 欧洲)(VP(VBZ)(PP(IN)(NP(DT)(JJ同)(NNS趋势))))(VBZ 是)(PP(IN in)(NP(DT)(JJ同)(NNS趋势)))(输入)(NP(DT)(JJ同)(NNS趋势))(DT了)(JJ同款)(神经网络趋势)(..)

但是有没有办法深度遍历所有子树?

在 NLTK 中应该如何遍历一棵树?

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

解决方案

也许我忽略了一些东西,但这就是你想要的吗?

import nltks = '(ROOT(S(NP(NNP欧洲))(VP(VBZ是)(PP(IN中)(NP(DT))(JJ同)(NNS趋势))))(..)))'树 = nltk.tree.Tree.fromstring(s)def traverse_tree(树):# 打印(树:",树)对于树中的子树:如果类型(子树)== nltk.tree.Tree:traverse_tree(子树)traverse_tree(树)

它以深度优先的方式遍历你的树.

Given a bracketed parse, I could convert it into a Tree object in NLTK as such:

>>> 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('.', ['.'])])])

But when I try to traverse it, I can only access the top most 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
>>> 

I could go one level deep as follows:

>>> 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)
(. .)

But is there a way to traverse all subtrees depth wise?

How should one traverse a tree in NLTK?

How to traverse all subtrees in NLTK?

解决方案

Maybe I'm overlooking things, but is this what you're after?

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)

It traverses your tree depth-first.

这篇关于如何遍历 NLTK 树对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆