遍历以下树预序的正确方法是什么?

发布于 2025-01-18 01:35:08 字数 1632 浏览 0 评论 0原文

给定以下解析树:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

許願樹丅啲祈禱 2025-01-25 01:35:08

从您的描述中'Unladen燕子的空速是什么?'。我认为如果我正确,您一直想要叶子节点。您应该应用DFS(预订),该DFS将提供树的输出叶节点。

- : [打印叶节点]

leafnodes=[]

def leafnode(node):
    if not node:
        return 
    if not node.left and not node.right:
        leafnodes.append(node.val)
    leafnode(node.left)
    leafnode(node.right)

leafnode(root)

print(leafnodes)

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]

leafnodes=[]

def leafnode(node):
    if not node:
        return 
    if not node.left and not node.right:
        leafnodes.append(node.val)
    leafnode(node.left)
    leafnode(node.right)

leafnode(root)

print(leafnodes)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文