-1

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

谢谢你。

4

1 回答 1

0

如果t是 中的任何树或子树nltk.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
于 2017-06-06T10:15:10.050 回答