|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from udapi.core.block import Block |
| 4 | + |
| 5 | + |
| 6 | +def eparent(node): |
| 7 | + """ |
| 8 | + Return an effective parent for the given node. |
| 9 | +
|
| 10 | + The rule for the effective parent - when the current node A has a deprel 'conj' to its parent B, |
| 11 | + return B.parent, otherwise return A.parent. |
| 12 | +
|
| 13 | + :param node: An input node. |
| 14 | + :return: An effective parent. |
| 15 | + :rtype: udapi.core.node.Node |
| 16 | +
|
| 17 | + """ |
| 18 | + if node.deprel == 'conj': |
| 19 | + return node.parent.parent |
| 20 | + |
| 21 | + return node.parent |
| 22 | + |
| 23 | + |
| 24 | +def echildren(node): |
| 25 | + """ |
| 26 | + Return a list with node's effective children. |
| 27 | +
|
| 28 | + :param node: An input node. |
| 29 | + :return: A list with node's effective children. |
| 30 | + :rtype: list |
| 31 | +
|
| 32 | + """ |
| 33 | + target_deprels = ['subj', 'subjpass', 'dobj', 'iobj', 'compl'] |
| 34 | + node_parent = eparent(node) |
| 35 | + echildren = node.children |
| 36 | + |
| 37 | + for candidate_child in node_parent.children: |
| 38 | + # Check if a candidate node C has the target deprel. |
| 39 | + if candidate_child.deprel not in target_deprels: |
| 40 | + continue |
| 41 | + |
| 42 | + # Check if such deprel is not in the current node children already. |
| 43 | + no_such_deprel = True |
| 44 | + for current_child in node.children: |
| 45 | + if current_child.deprel == candidate_child.deprel: |
| 46 | + no_such_deprel = False |
| 47 | + break |
| 48 | + |
| 49 | + # If there is no such deprel, we can add a new secondary dependence. |
| 50 | + if no_such_deprel: |
| 51 | + echildren.append(candidate_child) |
| 52 | + |
| 53 | + |
| 54 | +def enhance_deps(node, new_dependence): |
| 55 | + """ |
| 56 | + Add a new dependence to the node.deps, but firstly check |
| 57 | + if there is no such dependence already. |
| 58 | +
|
| 59 | + :param node: A node to be enhanced. |
| 60 | + :param new_dependence: A new dependence to be add into node.deps. |
| 61 | +
|
| 62 | + """ |
| 63 | + for existing_dependence in node.deps: |
| 64 | + if existing_dependence['parent'] == new_dependence['parent'] and \ |
| 65 | + existing_dependence['deprel'] == new_dependence['deprel']: |
| 66 | + return |
| 67 | + |
| 68 | + node.deps.append(new_dependence) |
| 69 | + |
| 70 | + |
| 71 | +class EnhanceDeps(Block): |
| 72 | + """ |
| 73 | + Identify new relations between nodes in the dependency tree (an analogy of effective parents/children from PML). |
| 74 | + Add these new relations into secondary dependencies slot. |
| 75 | +
|
| 76 | + """ |
| 77 | + |
| 78 | + def __init__(self, args=None): |
| 79 | + """ |
| 80 | + Initialization. |
| 81 | +
|
| 82 | + :param args: A dict of optional parameters. |
| 83 | +
|
| 84 | + """ |
| 85 | + super(Block, self).__init__() |
| 86 | + |
| 87 | + if args is None: |
| 88 | + args = {} |
| 89 | + |
| 90 | + def process_node(self, node): |
| 91 | + """ |
| 92 | + Enhance secondary dependencies by application of the following rules: |
| 93 | + 1. when the current node A has a deprel 'conj' to its parent B, create a new secondary dependence |
| 94 | + (B.parent, B.deprel) to A |
| 95 | + 2. when the current node A has a deprel 'conj' to its parent B, look at B.children C |
| 96 | + when C.deprel is in {subj, subjpass, iobj, dobj, compl} and there is no A.children D |
| 97 | + such that C.deprel == D.deprel, add a new secondary dependence (A, C.deprel) to C |
| 98 | +
|
| 99 | + :param node: A node to be process. |
| 100 | +
|
| 101 | + """ |
| 102 | + # Both rules require node.deprel to be 'conj'. |
| 103 | + if node.deprel != 'conj': |
| 104 | + return |
| 105 | + |
| 106 | + # Node's parent should not be root. |
| 107 | + if node.parent.is_root(): |
| 108 | + return |
| 109 | + |
| 110 | + # Apply rule (1) |
| 111 | + enhance_deps(node, {'parent': node.parent.parent, 'deprel': node.parent.deprel}) |
| 112 | + |
| 113 | + # Apply rule (2) |
| 114 | + target_deprels = ['subj', 'subjpass', 'dobj', 'iobj', 'compl'] |
| 115 | + for candidate_child in node.parent.children: |
| 116 | + # Check if a candidate node C has the target deprel. |
| 117 | + if candidate_child.deprel not in target_deprels: |
| 118 | + continue |
| 119 | + |
| 120 | + # Check if such deprel is not in the current node children already. |
| 121 | + no_such_deprel = True |
| 122 | + for current_child in node.children: |
| 123 | + if current_child.deprel == candidate_child.deprel: |
| 124 | + no_such_deprel = False |
| 125 | + break |
| 126 | + |
| 127 | + # If there is no such deprel, we can add a new secondary dependence. |
| 128 | + if no_such_deprel: |
| 129 | + enhance_deps(candidate_child, {'parent': node, 'deprel': candidate_child.deprel}) |
0 commit comments