Skip to content

Commit

Permalink
Recursively comparing subtrees.
Browse files Browse the repository at this point in the history
  • Loading branch information
shoeffner committed Dec 5, 2018
1 parent 9d81d7e commit 2decc3b
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/ccg2xml/test_xml2ccg.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ def tree_sort_key(elem):
return elem.tag + str(sorted('{}={}'.format(*a) for a in elem.attrib.items()))


def recursive_tree_comparison(left, right):
if left.tag != right.tag:
return False
elif left.attrib != right.attrib:
return False
elif left.find('*') is not None and right.find('*') is None:
return False
elif left.find('*') is None:
return True
else:
return all(recursive_tree_comparison(l, r) for l, r in zip(iter(left), iter(right)))


def compare_grammar_tree(left, right):
"""Compares two XML tree structures.
Expand Down Expand Up @@ -169,7 +182,7 @@ def compare_grammar_tree(left, right):
except StopIteration:
return False, (l, first_r)

if l.tag == r.tag and l.attrib == r.attrib:
if recursive_tree_comparison(l, r):
break

for comp in [compare_type_elements, compare_file_tags]:
Expand Down

0 comments on commit 2decc3b

Please sign in to comment.