Created
August 11, 2021 09:42
-
-
Save hantek/ab66abe2459f8a019fd757472e9db87e to your computer and use it in GitHub Desktop.
distance to tree with different branching biases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#balanced tree construction | |
def build_tree(depth, sen): | |
assert len(depth) == len(sen) | |
assert len(depth) >= 0 | |
if len(depth) == 1: | |
parse_tree = sen[0] | |
else: | |
idx_max = numpy.argmax(depth[1:]) + 1 | |
parse_tree = [] | |
if len(sen[:idx_max]) > 0: | |
tree0 = build_tree(depth[:idx_max], sen[:idx_max]) | |
parse_tree.append(tree0) | |
if len(sen[idx_max:]) > 0: | |
tree1 = build_tree(depth[idx_max:], sen[idx_max:]) | |
parse_tree.append(tree1) | |
return parse_tree | |
# with right branching bias | |
def build_tree(depth, sen): | |
assert len(depth) == len(sen) | |
if len(depth) == 1: | |
parse_tree = sen[0] | |
else: | |
idx_max = numpy.argmax(depth) | |
parse_tree = [] | |
tree_head = sen[idx_max] | |
if len(sen[:idx_max]) > 0: | |
tree0 = build_tree(depth[:idx_max], sen[:idx_max]) | |
parse_tree.append(tree0) | |
if len(sen[idx_max + 1:]) > 0: | |
tree1 = build_tree(depth[idx_max + 1:], sen[idx_max + 1:]) | |
tree_head = [tree_head, tree1] | |
if parse_tree == []: | |
parse_tree = tree_head | |
else: | |
parse_tree.append(tree_head) | |
return parse_tree | |
# with left branching bias | |
def build_tree(depth, sen): | |
assert len(depth) == len(sen) | |
if len(depth) == 1: | |
parse_tree = sen[0] | |
else: | |
idx_max = numpy.argmax(depth) | |
parse_tree = [] | |
tree_head = sen[idx_max] | |
if len(sen[:idx_max]) > 0: | |
tree0 = build_tree(depth[:idx_max], sen[:idx_max]) | |
tree_head = [tree0, tree_head] | |
parse_tree.append(tree_head) | |
if len(sen[idx_max + 1:]) > 0: | |
tree1 = build_tree(depth[idx_max + 1:], sen[idx_max + 1:]) | |
parse_tree.append(tree1) | |
if parse_tree == []: | |
parse_tree = tree_head | |
return parse_tree | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment