Skip to main content
 首页 » 编程设计

python之属性错误 : 'ParentedTree' object has no attribute 'label'

2025年05月04日6wuhuacong

我基本上是在处理已解析的树并尝试注释支配空类别的树节点(空节点注释)。

我定义了一个如下的递归函数,但我得到的错误是“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-"); 

请您参考如下方法:

Google 建议您使用 NLTK,我假设是这种情况。

A ParentedTree没有名为 .label() 的方法。所以当你这样写的时候:

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

Python 不知道该做什么。

A Tree ,另一方面,确实有一个 .label() 方法。您是否在某处使用了 ParentedTree 而不是 Tree?