def treetostr(self, node): l = Node.get_label(node) t = None if isinstance(l, tuple): t, l = l if t == 'O': return '%s(%s)' % (l, ', '.join( map(self.treetostr, Node.get_children(node)))) return l
def treesize(self, t): ''' Calculates the total size of the tree ''' size = 0 def ts(node): return 1 + sum(map(ts, Node.get_children(node))) for loc in t: for var, tree in list(t[loc].items()): lab = Node.get_label(tree) if lab == ('V', var): continue size += ts(tree) return size
def treesize(self, t): ''' Calculates the total size of the tree ''' size = 0 def ts(node): return 1 + sum(map(ts, Node.get_children(node))) for loc in t: for var, tree in t[loc].items(): lab = Node.get_label(tree) if lab == ('V', var): continue size += ts(tree) return size
# from simple_tree import Node from zss import Node, simple_distance def buildTree(state, node, chart): if state[4] == (): return else: for ptr in state[4]: child = Node(chart[ptr[0]][ptr[1]][0]) node.addkid(child) buildTree(chart[ptr[0]][ptr[1]], child, chart) rootNode1 = Node("Start", []) buildTree(root1, rootNode1, chart1) rootNode2 = Node("Start", []) buildTree(root2, rootNode2, chart2) print simple_distance(rootNode1, rootNode2) # for child in rootNode1.get_children(): # for chichild in child.get_children(): # print "parent is: ", chichild.get_label() # for chichichild in chichild.get_children(): # print chichichild.get_label() for child in Node.get_children(rootNode1): print Node.get_label(child)