Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def rec(node):
attrib = node.attrib
if 'terminal' not in attrib:
cat = Category.parse(attrib['category'])
children = [rec(spans[child]) for child in attrib['child'].split(' ')]
if len(children) == 1:
return Tree.make_unary(cat, children[0], lang)
else:
assert len(children) == 2
left, right = children
combinator = guess_combinator_by_triplet(
binary_rules, cat, left.cat, right.cat)
combinator = combinator or UNKNOWN_COMBINATOR
return Tree.make_binary(cat, left, right, combinator, lang)
else:
cat = Category.parse(attrib['category'])
word = try_get_surface(tokens[attrib['terminal']])
return Tree.make_terminal(word, cat, lang)
position += 1
else:
assert isinstance(stack[-1], Tree)
children = []
while isinstance(stack[-1], Tree):
tree = stack.pop()
children.append(tree)
category = stack.pop()
if len(children) == 1:
tree = Tree.make_unary(category, children[0], lang)
elif len(children) == 2:
right, left = children
combinator = guess_combinator_by_triplet(
binary_rules, category, left.cat, right.cat)
combinator = combinator or UNKNOWN_COMBINATOR
tree = Tree.make_binary(category, left, right, combinator, lang)
else:
assert False
stack.append(tree)