|
| 1 | +"""Block that takes PARSEME-like annotation of multiword expressions from MISC, |
| 2 | + looks for dependent possessive pronouns and reports how they are treated.""" |
| 3 | +from udapi.core.block import Block |
| 4 | +import logging |
| 5 | +import re |
| 6 | + |
| 7 | +class Possessives(Block): |
| 8 | + |
| 9 | + def collect_mwes(self, root): |
| 10 | + """ |
| 11 | + Collects annotations of multiword expressions from MISC of the nodes. |
| 12 | + The expected annotation is in the style of Parseme (see |
| 13 | + https://gitlab.com/parseme/corpora/-/wikis/home#annotation and download |
| 14 | + the data from http://hdl.handle.net/11372/LRT-5124), except that there |
| 15 | + are only ten columns and the annotation from the eleventh column is |
| 16 | + copied to the tenth (MISC) as the attribute Mwe (e.g., Mwe=1:LVC.cause). |
| 17 | + """ |
| 18 | + nodes = root.descendants |
| 19 | + mwes = {} # for each mwe id, its type and list of node ids |
| 20 | + mwes_by_nodes = {} # for each node id, a list of mwe ids |
| 21 | + for n in nodes: |
| 22 | + mwes_by_nodes[n.ord] = [] |
| 23 | + miscmwe = n.misc['Mwe'] |
| 24 | + if miscmwe: |
| 25 | + # A node may belong to multiple multiword expressions. |
| 26 | + miscmwes = miscmwe.split(';') |
| 27 | + for m in miscmwes: |
| 28 | + # Either it is NUMBER:TYPE, or just NUMBER. |
| 29 | + # Number identifies this MWE among all MWEs in the sentence. |
| 30 | + # Type is a main uppercase string (VID, LVC etc.), optionally |
| 31 | + # followed by a subtype ('LVC.cause'). |
| 32 | + # See https://gitlab.com/parseme/corpora/-/wikis/home |
| 33 | + match = re.match(r"^([0-9]+)(?::([A-Za-z\.]+))?$", m) |
| 34 | + if match: |
| 35 | + number = match.group(1) |
| 36 | + type = match.group(2) |
| 37 | + if not number in mwes: |
| 38 | + mwes[number] = {'nodes': [], 'type': ''} |
| 39 | + if type: |
| 40 | + mwes[number]['type'] = type |
| 41 | + mwes[number]['nodes'].append(n.ord) |
| 42 | + mwes_by_nodes[n.ord].append(number) |
| 43 | + else: |
| 44 | + logging.warning("Cannot parse Mwe=%s" % m) |
| 45 | + return (mwes, mwes_by_nodes) |
| 46 | + |
| 47 | + def process_tree(self, root): |
| 48 | + """ |
| 49 | + Collects annotations of multiword expressions from MISC of the nodes. |
| 50 | + Then surveys the possessive pronouns. |
| 51 | + """ |
| 52 | + (mwes, mwes_by_nodes) = self.collect_mwes(root) |
| 53 | + nodes = root.descendants |
| 54 | + for m in mwes: |
| 55 | + mwenodes = [x for x in nodes if m in mwes_by_nodes[x.ord]] |
| 56 | + mweheads = [x for x in mwenodes if not x.parent in mwenodes] |
| 57 | + mwedescendantset = set() |
| 58 | + for x in mweheads: |
| 59 | + mwedescendantset = mwedescendantset.union(set(x.descendants)) |
| 60 | + mwedescendants = list(sorted(mwedescendantset)) |
| 61 | + # Is there a possessive pronoun? |
| 62 | + possprons = [x for x in mwedescendants if x.upos == 'PRON' and x.feats['Poss'] == 'Yes'] |
| 63 | + inpp = [x for x in possprons if m in mwes_by_nodes[x.ord]] |
| 64 | + outpp = [x for x in possprons if not m in mwes_by_nodes[x.ord]] |
| 65 | + observation = '' |
| 66 | + if inpp and outpp: |
| 67 | + observation = 'both' |
| 68 | + elif inpp: |
| 69 | + observation = 'in' |
| 70 | + elif outpp: |
| 71 | + observation = 'out' |
| 72 | + if observation: |
| 73 | + expression = ' '.join([x.form if m in mwes_by_nodes[x.ord] else '('+x.form+')' for x in mwedescendants]) |
| 74 | + print(observation + ': ' + expression) |
0 commit comments