AttributeError: 'ParentedTree'对象没有'label'属性

2 投票
2 回答
1944 浏览
提问于 2025-04-30 07:48

我正在处理解析树,并试图给树的节点加上注释,特别是那些支配空类别的节点(空节点注释)。

我定义了一个递归函数,但我遇到的错误是“AttributeError: 'ParentedTree'对象没有'label'这个属性”。

def annotateTraceNodes(node):
numChildren = len(node);
numNone=0;

    for child in node:
        if isinstance(child,Tree):
            annotateTraceNodes(child);
            if(numChildren==0 or child.label().endswith("-NONE-")):
            numNone+=1;            
    if(numChildren==numNone):
        print "setting the label";
        node.set_label(node.label()+"-NONE-");
暂无标签

2 个回答

0

这可能也是个版本问题。

我用3.3版本的时候是可以的。

pip install nltk==3.3
1

谷歌建议你在使用NLTK,我假设你确实在用这个。

一个ParentedTree没有叫做.label()的方法。所以当你写这样的代码时:

child.label().endswith("-NONE-")

Python就不知道该怎么处理了。

Tree是有.label()这个方法的。你是不是在某个地方用错了,使用了ParentedTree而不是Tree呢?

撰写回答