遍历以下树预序的正确方法是什么?
给定以下解析树:
in:
from nltk.parse import CoreNLPParser
from nltk.treeprettyprinter import TreePrettyPrinter
parser = CoreNLPParser(url='http://localhost:9000')
next(parser.raw_parse('What is the airspeed of an unladen swallow ?')).pretty_print()
out:
ROOT
|
SBARQ
__________________________|____________________________
| SQ |
| ___|_________________ |
| | NP |
| | _____________|______________________ |
| | | PP S |
| | | ____|___ | |
WHNP | NP | NP VP |
| | ___|_____ | ___|_____ | |
WP VBZ DT NN IN DT JJ VB .
| | | | | | | | |
What is the airspeed of an unladen swallow ?
在有或没有终端节点的情况下预先订购的正确方法是什么?
到目前为止,我的主要问题是我不明白如何通过树迭代。当我这样做时:
for e in parse_tree:
print(e)
我得到:
(ROOT
(SBARQ
(WHNP (WP What))
(SQ
(VBZ is)
(NP
(NP (DT the) (NN airspeed))
(PP (IN of) (NP (DT an) (JJ unladen)))
(S (VP (VB swallow)))))
(. ?)))
换句话说,我无法访问树的每个分支。穿越这种结构的正确方法是什么?
Given the following parse tree:
In:
from nltk.parse import CoreNLPParser
from nltk.treeprettyprinter import TreePrettyPrinter
parser = CoreNLPParser(url='http://localhost:9000')
next(parser.raw_parse('What is the airspeed of an unladen swallow ?')).pretty_print()
Out:
ROOT
|
SBARQ
__________________________|____________________________
| SQ |
| ___|_________________ |
| | NP |
| | _____________|______________________ |
| | | PP S |
| | | ____|___ | |
WHNP | NP | NP VP |
| | ___|_____ | ___|_____ | |
WP VBZ DT NN IN DT JJ VB .
| | | | | | | | |
What is the airspeed of an unladen swallow ?
What is the correct way of traversing it in pre-order with and without the terminal nodes?
So far my main issue is that I dont understand how to iterate through the tree. When I do :
for e in parse_tree:
print(e)
I get:
(ROOT
(SBARQ
(WHNP (WP What))
(SQ
(VBZ is)
(NP
(NP (DT the) (NN airspeed))
(PP (IN of) (NP (DT an) (JJ unladen)))
(S (VP (VB swallow)))))
(. ?)))
In other words, I cant access to each branch of the tree. What is the correct way of traversing this structure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的描述中'Unladen燕子的空速是什么?'。我认为如果我正确,您一直想要叶子节点。您应该应用DFS(预订),该DFS将提供树的输出叶节点。
- : [打印叶节点]
From your description 'What is the airspeed of an unladen swallow ?'. I think you wanted leaf node all the time if i am correct.! you should apply DFS(preorder) which will give output leaf nodes of the tree.
Code for-:[To print leaf node]