0

我使用以下代码在 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”;如果不是这种情况,我想将.foo属性转换node_to_check为“原子”。

我在想这样的事情:

def find_node_to_check(self):
    for next in self.next:
        if next.name == 'S' or next.name == 'SBAR':
        is_present = check_children(next)
        if is_present == 'no':
            find_node_to_check(next)
        else:
            self.foo = 'atom'

def check_children(self):
    for next in self.next:
        # is this way of returning correct?
        if next.name == 'S' or next.name == 'SBAR':
            return 'no'
        else:
            check_sents(next)
            return 'yes'

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

4

1 回答 1

0

当尝试设计一个新颖的类时,知道你需要它做什么会告诉你如何构建它。存根在这里效果很好,例如:

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

于 2013-06-24T15:19:13.443 回答