Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_tree_without_id(self):
tree = TokenTree(token={"form": "hej", "deprel": "nmod"}, children=[])
with self.assertRaises(ParseException):
capture_print(tree.print_tree)
def test_missing_id(self):
tree = TokenTree(token={"form": "hej"}, children=[])
with self.assertRaises(ParseException):
tree.serialize()
def test_eq(self):
metadata = {"meta": "data"}
tokentree1 = TokenTree(token={"id": 1}, children=[TokenTree(token={"id": 2}, children=[])])
tokentree1.metadata = metadata
tokentree2 = TokenTree(token={"id": 1}, children=[])
self.assertNotEqual(tokentree1, tokentree2)
tokentree2.metadata = metadata
self.assertNotEqual(tokentree1, tokentree2)
tokentree2.children = [TokenTree(token={"id": 2}, children=[])]
self.assertEqual(tokentree1, tokentree2)
def test_tree_without_deprel(self):
tree = TokenTree(token={"id": 1, "form": "hej"}, children=[])
with self.assertRaises(ParseException):
capture_print(tree.print_tree)
def test_simple_tree(self):
tokenlist = TokenList([
Token([("id", 2), ("form", "dog"), ("head", 0)]),
Token([("id", 1), ("form", "a"), ("head", 2)]),
])
tree = TokenTree(
token=Token([("id", 2), ("form", "dog"), ("head", 0)]),
children=[TokenTree(
token=Token([("id", 1), ("form", "a"), ("head", 2)]),
children=[]
)]
)
self.assertTreeEqual(tokenlist.to_tree(), tree)
def test_metadata(self):
tree = TokenTree(token={"id": 1, "form": "hej"}, children=[])
metadata = {"meta": "data"}
tree.set_metadata(metadata)
self.assertEqual(tree.metadata, metadata)
tree = TokenTree(token={"id": 1, "form": "hej"}, children=[], metadata={"meta": "data"})
self.assertEqual(tree.metadata, metadata)
def test_removes_negative_nodes(self):
tokenlist = TokenList([
Token([("id", 2), ("form", "dog"), ("head", 0)]),
Token([("id", 1), ("form", "a"), ("head", 2)]),
Token([("id", 3), ("form", "😍"), ("head", -1)]),
])
tree = TokenTree(
token=Token([("id", 2), ("form", "dog"), ("head", 0)]),
children=[TokenTree(
token=Token([("id", 1), ("form", "a"), ("head", 2)]),
children=[]
)]
)
self.assertTreeEqual(tokenlist.to_tree(), tree)
def test_eq(self):
metadata = {"meta": "data"}
tokentree1 = TokenTree(token={"id": 1}, children=[TokenTree(token={"id": 2}, children=[])])
tokentree1.metadata = metadata
tokentree2 = TokenTree(token={"id": 1}, children=[])
self.assertNotEqual(tokentree1, tokentree2)
tokentree2.metadata = metadata
self.assertNotEqual(tokentree1, tokentree2)
tokentree2.children = [TokenTree(token={"id": 2}, children=[])]
self.assertEqual(tokentree1, tokentree2)
def _create_tree(head_to_token_mapping, id_=0):
return [
TokenTree(child, _create_tree(head_to_token_mapping, child["id"]))
for child in head_to_token_mapping[id_]
]