Skip to content
Snippets Groups Projects
Commit 8e09e336 authored by Mateusz Klimaszewski's avatar Mateusz Klimaszewski Committed by Mateusz Klimaszewski
Browse files

Add restoring collapsed edges (gapping).

parent 826e57a7
Branches
Tags
2 merge requests!9Enhanced dependency parsing develop to master,!8Enhanced dependency parsing
......@@ -207,6 +207,8 @@ class SemanticMultitaskPredictor(predictor.Predictor):
tree_tokens=tree_tokens,
root_idx=self.vocab.get_token_index("root", "deprel_labels"),
vocab_index=self.vocab.get_index_to_token_vocabulary("deprel_labels"))
empty_tokens = graph.restore_collapse_edges(tree_tokens)
tree.tokens.extend(empty_tokens)
return tree, predictions["sentence_embedding"]
......
......@@ -85,3 +85,30 @@ def _dfs(graph, start, end):
if next_state in path:
continue
fringe.append((next_state, path + [next_state]))
def restore_collapse_edges(tree_tokens):
empty_tokens = []
for token in tree_tokens:
deps = token["deps"].split("|")
for i, d in enumerate(deps):
if ">" in d:
# {head}:{empty_node_relation}>{current_node_relation}
# should map to
# For new, empty node:
# {head}:{empty_node_relation}
# For current node:
# {new_empty_node_id}:{current_node_relation}
# TODO consider where to put new_empty_node_id (currently at the end)
head, relation = d.split(':', 1)
ehead = f"{len(tree_tokens)}.{len(empty_tokens) + 1}"
empty_node_relation, current_node_relation = relation.split(">", 1)
deps[i] = f"{ehead}:{current_node_relation}"
empty_tokens.append(
{
"id": ehead,
"deps": f"{head}:{empty_node_relation}"
}
)
token["deps"] = "|".join(deps)
return empty_tokens
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment