in src/pixparse/utils/json_utils.py [0:0]
def update_cost(node1: Node, node2: Node):
"""
Update cost for tree edit distance.
If both are leaf node, calculate string edit distance between two labels (special token '<leaf>' will be ignored).
If one of them is leaf node, cost is length of string in leaf node + 1.
If neither are leaf node, cost is 0 if label1 is same with label2 othewise 1
"""
label1 = node1.label
label2 = node2.label
label1_leaf = "<leaf>" in label1
label2_leaf = "<leaf>" in label2
if label1_leaf == True and label2_leaf == True:
return edit_distance(label1.replace("<leaf>", ""), label2.replace("<leaf>", ""))
elif label1_leaf == False and label2_leaf == True:
return 1 + len(label2.replace("<leaf>", ""))
elif label1_leaf == True and label2_leaf == False:
return 1 + len(label1.replace("<leaf>", ""))
else:
return int(label1 != label2)