在树中递归检查子节点的存在

时间:2013-06-24 14:36:59

标签: python tree nodes

我使用以下代码在python中创建了一个树对象:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re,sys,codecs

neg_markers_en=[u'not',u"napt",u'no',u'nobody',u'none',u'never']

class Node:
    def __init__(self,name=None,parent=None,sentence_number=0):
        self.name=name
        self.next=list()
        self.parent=parent
        self.depth=0
        self.n_of_neg=0
        self.subordinate=None
        self.foo=None

def print_node(self):
    print self.name,'contains',[(x.name,x.depth,x.foo) for x in self.next]
    for x in self.next:
        x.print_node()

def get_negation(self):


    for x in self.next:
        if x.n_of_neg!=0:
            print unicode(x.depth)+u' |||',
            try:
                x.look_for_parent_vp()
            except: print 'not in a VP',
            try:
                x.look_for_parent_sent()
            except: print '***'
        x.get_negation()


def look_for_parent_vp(self):

    if self.parent.name=='VP':
        self.parent.print_nont()
    else:
        self.parent.look_for_parent_vp()

def look_for_parent_sent(self):

    if self.parent.name=='S' or self.parent.name=='SBAR':
        #This is to send out to a text file, along with what it covers
        print '||| '+ self.parent.name,
        try:
            self.parent.check_subordinate()
            self.parent.print_nont()
            print '\n'
        except:
            print u'no sub |||',
            self.parent.print_nont()
            print '\n'
    elif self.parent=='None': print 'root |||'
    else:
        self.parent.look_for_parent_sent()


def print_nont(self):

    for x in self.next:
        if x.next==[]:
            print unicode(x.name),
        else: x.print_nont()


def mark_subordinate(self):

    for x in self.next:
        if x.name=='SBAR':
            x.subordinate='sub'
        else: x.subordinate='main'
        x.mark_subordinate()

def check_subordinate(self):

    if self.subordinate=='sub':
        print u'sub |||',
    else:
        self.parent.check_subordinate()


def create_tree(tree):

    #replace "n't" with 'napt' so to avoid errors in splitting
    tree=tree.replace("n't",'napt')
    lista=filter(lambda x: x!=' ',re.findall(r"\w+|\W",tree))

    start_node=Node(name='*NULL*')
    current_node=start_node

    for i in range(len(lista)-1):
        if lista[i]=='(':
            next_node=Node()
            next_node.parent=current_node
            next_node.depth=current_node.depth+1
            current_node.next.append(next_node)
            current_node=next_node
        elif lista[i]==')':
            current_node=current_node.parent
        else:
            if lista[i-1]=='(' or lista[i-1]==')':
                current_node.name=lista[i]
            else:
                next_node=Node()
                next_node.name=lista[i]
                next_node.parent=current_node
                #marks the depth of the node
                next_node.depth=current_node.depth+1
                if lista[i] in neg_markers_en:
                    current_node.n_of_neg+=1
                current_node.next.append(next_node)


return start_node

现在所有节点都被链接,以便父节点的子节点被附加到列表中,并且这些子节点中的每一个都通过实例父节点返回到它们的父节点。 我有以下问题: 对于名称为“S”或“SBAR”的每个节点(让我们称之为node_to_check),我必须查看其子节点的任何名称是“S”还是“SBAR”;如果这是 NOT ,我希望将.foo的{​​{1}}属性转换为'atom'。

我在考虑这样的事情:

node_to_check

我在问题中也包含了我迄今为止编写的代码。在create_tree(tree)函数中创建树结构;输入树是斯坦福分析器的括号中的符号。

1 个答案:

答案 0 :(得分:0)

当试图设计一个新的课程时,知道你需要它做什么来告诉你如何构建它。 Stubbing在这里运作良好,例如:

class Node:
   """A vertex of an n-adic tree"""

   def __init__(self, name):
       """since you used sentence, I assumed n-adic
          but that may be wrong and then you might want
          left and right children instead of a list or dictionary
          of children"""
       pass

   def append_children(self, children):
       """adds a sequence of child Nodes to self"""
       pass

   def create_child(self, name):
       """creates a new Named node and adds it as a child"""
       pass

   def delete_child(self, name):
       """deletes a named child from self or throws exception"""
       pass

等等。孩子需要订购吗?您是否需要删除节点(和后代)?您是否可以预先建立一个孩子列表,或者您是否必须一次完成一个孩子。你真的想存储节点是终端(这是多余的)还是你想让is_terminal()返回children is None