【问题标题】:Finding span of each node in NLTK tree查找 NLTK 树中每个节点的跨度
【发布时间】:2017-06-06 09:20:21
【问题描述】:

我是 nltk 的新手,发现很难处理 nltk 树。给定来自 Penn 树库的 nltk 解析树,我希望能够从下到上递归地计算每个节点的跨度。叶节点的跨度为1。非终端节点的跨度是其子节点的跨度之和。有人可以告诉我怎么做吗?

谢谢。

【问题讨论】:

标签: python tree nltk text-parsing penn-treebank


【解决方案1】:

如果tnltk.Tree 中的任何树或子树,则其叶数由len(t.leaves()) 给出。

>>> t = Tree.fromstring('(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))')
>>> t[1,1]
Tree('NP', [Tree('D', ['the']), Tree('N', ['cat'])])
>>> len(t[1,1].leaves())
2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多