in utils/trees.py [0:0]
def is_unordered_exact_match(self, otree):
'''
This method is used to check if `otree`, a tree object, is an exact match
with the tree object calling this method.
:param otree: (TopSemanticTree/ExpressSemanticTree) Tree object to compare with.
:return: (bool) If the two trees have an unordered exact match or not.
'''
if not isinstance(otree, type(self)):
raise TypeError(f"Expected both trees to be of type {type(self)} "
f"but one of them is of type {type(otree)}")
# check if the roots of trees are same.
# If not, its not an exact match.
if self.root_symbol() != otree.root_symbol():
return False
# Check if the number of children of both trees are same.
# If not, its not an exact match.
if len(self.children()) != len(otree.children()):
return False
# Count the number of equal sub-trees, i.e. children
matched_subtrees_idx_1, matched_subtrees_idx_2 = set(), set()
for i,child_1 in enumerate(self.children()):
for j,child_2 in enumerate(otree.children()):
if (i not in matched_subtrees_idx_1) and \
(j not in matched_subtrees_idx_2):
if child_1.is_unordered_exact_match(child_2):
matched_subtrees_idx_1.add(i)
matched_subtrees_idx_2.add(j)
# If the no. of matched sub-trees are equal to the
# number of children itself, that means all childdren are a match
# and hence its an exact match, otherwise not.
return len(matched_subtrees_idx_1) == len(self.children())