|
| 1 | +""" |
| 2 | +This block searches for relative clauses modifying a determiner ('el que, el cual...'). |
| 3 | +It is written for Spanish but a similar block should work for other Romance |
| 4 | +languages. |
| 5 | +""" |
| 6 | +from udapi.core.block import Block |
| 7 | +import logging |
| 8 | +import re |
| 9 | + |
| 10 | +class ElQue(Block): |
| 11 | + |
| 12 | + def __init__(self, fix=False, **kwargs): |
| 13 | + """ |
| 14 | + Default: Print the annotation patterns but do not fix anything. |
| 15 | + fix=1: Do not print the patterns but fix them. |
| 16 | + """ |
| 17 | + super().__init__(**kwargs) |
| 18 | + self.fix = fix |
| 19 | + |
| 20 | + def process_node(self, node): |
| 21 | + # We take 'que' as the central node of the construction. |
| 22 | + if re.match(r'^(que|cual)$', node.lemma) and node.upos == 'PRON' and node.parent.ord > node.ord: |
| 23 | + # We will refer to the parent of 'que' as a verb, although it can be |
| 24 | + # a non-verbal predicate, too. |
| 25 | + que = node |
| 26 | + verb = node.parent |
| 27 | + # Check the lemma of the determiner. The form may vary for gender and number. |
| 28 | + if que.prev_node and que.prev_node.lemma == 'el': |
| 29 | + el = que.prev_node |
| 30 | + adp = None |
| 31 | + if el.prev_node and el.prev_node.upos == 'ADP': |
| 32 | + adp = el.prev_node |
| 33 | + if adp.udeprel == 'fixed': |
| 34 | + adp = adp.parent |
| 35 | + if self.fix: |
| 36 | + self.fix_pattern(adp, el, que, verb) |
| 37 | + else: |
| 38 | + self.print_pattern(adp, el, que, verb) |
| 39 | + |
| 40 | + def print_pattern(self, adp, el, que, verb): |
| 41 | + stanford = [] |
| 42 | + if adp: |
| 43 | + if adp.parent == el: |
| 44 | + parentstr = 'el' |
| 45 | + elif adp.parent == que: |
| 46 | + parentstr = 'que' |
| 47 | + elif adp.parent == verb: |
| 48 | + parentstr = 'VERB' |
| 49 | + else: |
| 50 | + parentstr = 'OTHER' |
| 51 | + stanford.append(adp.deprel + '(' + parentstr + ', ADP)') |
| 52 | + if el.parent == adp: |
| 53 | + parentstr = 'ADP' |
| 54 | + elif el.parent == que: |
| 55 | + parentstr = 'que' |
| 56 | + elif el.parent == verb: |
| 57 | + parentstr = 'VERB' |
| 58 | + else: |
| 59 | + parentstr = 'OTHER' |
| 60 | + stanford.append(el.deprel + '(' + parentstr + ', el)') |
| 61 | + # We found the verb as the parent of 'que', so we do not need to check the parent of 'que' now. |
| 62 | + stanford.append(que.deprel + '(VERB, que)') |
| 63 | + if verb.parent == adp: |
| 64 | + parentstr = 'ADP' |
| 65 | + elif verb.parent == el: |
| 66 | + parentstr = 'el' |
| 67 | + else: |
| 68 | + parentstr = 'OTHER' |
| 69 | + stanford.append(verb.deprel + '(' + parentstr + ', VERB)') |
| 70 | + print('; '.join(stanford)) |
| 71 | + |
| 72 | + def fix_pattern(self, adp, el, que, verb): |
| 73 | + if adp: |
| 74 | + if adp.parent == que or adp.parent == verb: |
| 75 | + attach(adp, el, 'case') |
| 76 | + if el.parent == que: |
| 77 | + ###!!! Just a temporary change. In the end it will be attached elsewhere. |
| 78 | + attach(el, verb) |
| 79 | + el.parent = verb |
| 80 | + if len(el.deps) == 1: |
| 81 | + el.deps[0]['parent'] = verb |
| 82 | + if verb.parent != adp and verb.parent != el and verb.parent != que: |
| 83 | + eldeprel = None |
| 84 | + if re.match(r'^[nc]subj$', verb.udeprel): |
| 85 | + eldeprel = 'nsubj' |
| 86 | + elif re.match(r'^ccomp$', verb.udeprel): |
| 87 | + eldeprel = 'obj' |
| 88 | + elif re.match(r'^advcl$', verb.udeprel): |
| 89 | + eldeprel = 'obl' |
| 90 | + elif re.match(r'^acl$', verb.udeprel): |
| 91 | + eldeprel = 'nmod' |
| 92 | + elif re.match(r'^(xcomp|conj|appos|root)$', verb.udeprel): |
| 93 | + eldeprel = verb.deprel |
| 94 | + if eldeprel: |
| 95 | + attach(el, verb.parent, eldeprel) |
| 96 | + attach(verb, el, 'acl:relcl') |
| 97 | + # If anything before 'el' depends on the verb ('cc', 'mark', 'punct' etc.), |
| 98 | + # re-attach it to 'el'. |
| 99 | + for c in verb.children: |
| 100 | + if c.ord < el.ord and re.match(r'^(cc|mark|case|punct)$', c.udeprel): |
| 101 | + attach(c, el) |
| 102 | + |
| 103 | +def attach(node, parent, deprel=None): |
| 104 | + """ |
| 105 | + Attach a node to a new parent with a new deprel in the basic tree. In |
| 106 | + addition, if there are enhanced dependencies and there is just one incoming |
| 107 | + enhanced relation (this is the case in AnCora), this relation will be |
| 108 | + modified accordingly. |
| 109 | + """ |
| 110 | + node.parent = parent |
| 111 | + if deprel: |
| 112 | + node.deprel = deprel |
| 113 | + if len(node.deps) == 1: |
| 114 | + node.deps[0]['parent'] = parent |
| 115 | + if deprel: |
| 116 | + node.deps[0]['deprel'] = deprel |
0 commit comments