def make_zssNode_from_tuple(t): """Convert my tuple representation into the Node object representation used in zss.""" n = Node(t[0]) for s in t[1:]: n.addkid(make_zssNode_from_tuple(s)) return n
def addnode(t, i): if i >= 0: n = ZSNode(names[i]) else: n = ZSNode('c') tn = t[-i - 1] for k in [tn.left, tn.right]: n.addkid(addnode(t, k)) return n
def randtree(depth=2, alpha='abcdefghijklmnopqrstuvwxyz', repeat=2, width=2): labels = [''.join(x) for x in itertools.product(alpha, repeat=repeat)] shuffle(labels) labels = (x for x in labels) root = Node("root") p = [root] c = list() for x in xrange(depth - 1): for y in p: for z in xrange(randint(1, 1 + width)): n = Node(labels.next()) y.addkid(n) c.append(n) p = c c = list() return root
def tree4(): return (Node("f").addkid( Node("d").addkid(Node("q")).addkid(Node("c").addkid( Node("b")))).addkid(Node("e")))
def tree3(): return (Node("a").addkid( Node("d").addkid(Node("f")).addkid(Node("c").addkid( Node("b")))).addkid(Node("e")))
def T(tree): root = Node(tree.get_key()) for c in tree.get_children(): root.addkid(T(c)) return root
def _dict2zt(d): n = ZSNode(d['lab']) for kn in d['kids']: n.addkid(_dict2zt(kn)) return n
def test_incorrect_behavior_regression(): A = (Node("a").addkid(Node("b").addkid(Node("x")).addkid(Node("y")))) B = (Node("a").addkid(Node("x")).addkid(Node("b").addkid(Node("y")))) dist = compare.distance(A, B) print dist assert dist == 2
def test_simplelabelchange(): A = (Node("f").addkid( Node("a").addkid(Node("h")).addkid(Node("c").addkid( Node("l")))).addkid(Node("e"))) B = (Node("f").addkid( Node("a").addkid(Node("d")).addkid(Node("r").addkid( Node("b")))).addkid(Node("e"))) dist = compare.distance(A, B) print dist assert dist == 3
def test_paper_tree(): A = (Node("f").addkid( Node("d").addkid(Node("a")).addkid(Node("c").addkid( Node("b")))).addkid(Node("e"))) B = (Node("f").addkid( Node("c").addkid(Node("d").addkid(Node("a")).addkid( Node("b")))).addkid(Node("e"))) #print A #print #print B dist = compare.distance(A, B) assert dist == 2
def test_empty_tree_distance(): assert compare.distance(Node(''), Node('')) == 0 assert compare.distance(Node('a'), Node('')) == 1 assert compare.distance(Node(''), Node('b')) == 1