Description
I have repeatedly encountered the following situation: A small change is needed for a batch of nodes, such as changing the DEPREL (or both the HEAD and the DEPREL) for a node that satisfies a condition. Typically I do it directly from the commandline, using udapy -s util.Eval node='if CONDITION: node.parent = X; node.deprel = Y'
. Now if the treebank contains enhanced dependencies, the same change should (typically) be also made in the enhanced graph. I usually forget to do it properly but when I want to do it, it is not as simple as the change in the basic tree. I wrote the code below in some of my blocks where I had to do it but I don't like having to repeat the code in several blocks again and again, and can hardly use it in util.Eval from commandline:
def reattach(self, node, parent, deprel):
"""
Changes the incoming dependency relation to a node. Makes sure that the
same change is done in the basic tree and in the enhanced graph.
"""
if node.deps:
# If the enhanced graph contains the current basic relation, remove it.
# Note that the enhanced deprel may contain subtypes that are not in the basic deprel.
orig_n_deps = len(node.deps)
node.deps = [x for x in node.deps if x['parent'] != node.parent or re.sub(r':.*', '', x['deprel']) != node.udeprel]
# Add the new basic relation to the enhanced graph only if the original one was there.
if len(node.deps) < orig_n_deps:
node.deps.append({'parent': parent, 'deprel': deprel})
node.parent = parent
node.deprel = deprel
Would it make sense to have something similar in the core code, as a method of Node?