【问题标题】:Print sentence tree without using nltk.tree不使用 nltk.tree 打印句子树
【发布时间】:2021-11-17 12:15:07
【问题描述】:

我有一棵这样的句子树:

[{'ROOT': ['S']}, {'S': ['VPS']}, {'VPS': ['N', 'VP']}, {'N': ['#']}, {'VP': ['PP', 'MV']}, {'PP': ['PREP_EZ', 'N']}, {'PREP_EZ': ['#']}, {'N': ['#']}, {'MV': ['N', 'V']}, {'N': ['#']}, {'V': ['#']}]

如何在不使用 nltk.tree 的情况下打印其对应的树? (由于某些原因我不能使用 nltk 库)

我希望是这样的: The desired output

【问题讨论】:

  • 你能提供“对应树”的样子吗?
  • @Manu 我已经编辑了我的问题
  • 是视觉问题(如何构建图像本身)或如何迭代列表
  • 在输出中打印这样的内容就足够了。 @Manu

标签: python sentence parse-tree


【解决方案1】:

好的,让我们看看这是否可以帮助您:

class Node:

    def __init__(self, value, children=[]):
        self.value = value
        self.children = children

    def __str__(self, level=0):
        str = "    " * level + "  |--" + self.value + "\n"
        for child in self.children:
            str += child.__str__(level + 1)
        return str


tree = Node('ROOT', children=[
    Node('S', children=[
        Node('VPS', children=[
            Node('N'),
            Node('VP', children=[
                Node('PP', children=[
                    Node('PREP_EZ')
                ]),
                Node('MV', children=[
                    Node('N'),
                    Node('V')
                ])
            ])
        ])
    ])
])
    
>>> print(tree)
  |--'ROOT'
      |--'S'
          |--'VPS'
              |--'N'
              |--'VP'
                  |--'PP'
                      |--'PREP_EZ'
                  |--'MV'
                      |--'N'
                      |--'V'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 2022-01-25
    • 2012-07-20
    • 1970-01-01
    相关资源
    最近更新 更多