|
| 1 | +"""Links is a class for storing a set of links with the same source node.""" |
| 2 | +import collections.abc |
| 3 | +import logging |
| 4 | +import re |
| 5 | + |
| 6 | +Link = collections.namedtuple('Link', 'node relation') |
| 7 | + |
| 8 | +class Links(list): |
| 9 | + """Links class serves as a `list` with additional methods. |
| 10 | +
|
| 11 | + >>> enhdeps = EnhDeps('4:nsubj|11:nsubj') |
| 12 | + >>> for enhdep in enhdeps: |
| 13 | + >>> str(enhdep) |
| 14 | + '4:nsubj' |
| 15 | + '11:nsubj' |
| 16 | + >>> enhdeps[0].parent = node_with_ord5 |
| 17 | + >>> enhdeps[0].deprel = 'obj' |
| 18 | + >>> str(enhdeps) |
| 19 | + '5:obj|11:nsubj' |
| 20 | +
|
| 21 | + This class provides access to both |
| 22 | + * a structured (list of named tuples) representation and |
| 23 | + * a string (serialized) representation of the enhanced depndencies. |
| 24 | +
|
| 25 | + Implementation details: |
| 26 | + Unlike `DualDict` |
| 27 | + * the structured internal storage is list, not dict |
| 28 | + * the string representation is always computed on the fly, it is not stored. |
| 29 | + """ |
| 30 | + |
| 31 | + def __init__(self, src_node, string=None): |
| 32 | + self.src_node = src_node |
| 33 | + items = [] |
| 34 | + if string is not None: |
| 35 | + all_nodes = src_node.root.descendants(add_self=1) |
| 36 | + for edge_str in string.split('|'): |
| 37 | + try: |
| 38 | + trg_node_id, relation = edge_str.split(':') |
| 39 | + except ValueError as exception: |
| 40 | + logging.error("<%s> contains <%s> which does not contain one ':' symbol.", |
| 41 | + string, edge_str) |
| 42 | + raise exception |
| 43 | + # TODO allow `trg_node_id`s like 5.1, /zone#1, bundle/zone#1, bundle#1 |
| 44 | + trg_node = all_nodes[int(trg_node_id)] |
| 45 | + link = Link(node=trg_node, relation=relation) |
| 46 | + items.append(link) |
| 47 | + super().__init__(self, items) |
| 48 | + |
| 49 | + def __str__(self): |
| 50 | + serialized = [] |
| 51 | + for link in self: |
| 52 | + # TODO allow `trg_node_id`s like /zone#1, bundle/zone#1, bundle#1 |
| 53 | + serialized.append('%s:%s' % (link.node.ord, link.relation)) |
| 54 | + return '|'.join(serialized) if serialized else '_' |
| 55 | + |
| 56 | + def set_links(self, value): |
| 57 | + """Set the edges from a list of tuples or string. |
| 58 | +
|
| 59 | + If the `value` is None or an empty string, it is converted to storing empty list of edges. |
| 60 | + If the `value` is a string, it is parsed as in `__init__`. |
| 61 | + If the `value` is a list of `Edge` namedtuples its copy is stored. |
| 62 | + Other types of `value` raise an `ValueError` exception. |
| 63 | + """ |
| 64 | + if value is None: |
| 65 | + self.clear() |
| 66 | + elif isinstance(value, str): |
| 67 | + self.clear() |
| 68 | + self.__init__(value) |
| 69 | + elif isinstance(value, collections.abc.Sequence): |
| 70 | + self.clear() |
| 71 | + super().__init__(value) |
| 72 | + else: |
| 73 | + raise ValueError("Unsupported value type " + str(value)) |
| 74 | + |
| 75 | + def __call__(self, following_only=False, preceding_only=False, relations=None): |
| 76 | + """Return a subset of links contained in this list as specified by the args. |
| 77 | +
|
| 78 | + TODO: document args |
| 79 | + """ |
| 80 | + if not following_only and not preceding_only and relations is None: |
| 81 | + return self |
| 82 | + links = list(self) |
| 83 | + if preceding_only: |
| 84 | + links = [l for l in links if l.node.precedes(self.src_node)] |
| 85 | + if following_only: |
| 86 | + links = [l for l in links if self.src_node.precedes(l.node)] |
| 87 | + if relations: |
| 88 | + links = [l for l in links if re.match(relations, l.relation)] |
| 89 | + return Links(self.src_node, links) |
| 90 | + |
| 91 | + @property |
| 92 | + def nodes(self): |
| 93 | + """Return a list of the target nodes (without relations).""" |
| 94 | + return [link.node for link in self] |
| 95 | + |
| 96 | + # TODO make sure backlinks are created and updated |
| 97 | + def TODO__setitem__(self, index, new_value): |
| 98 | + old_value = self[index] |
| 99 | + old_value.node._enh_children = [l for l in old_value.node._enh_children if l != old_value] |
| 100 | + if new_value.node._enh_children is None: |
| 101 | + new_value.node._enh_children = Links(new_value.node, None) |
| 102 | + super().__setitem__(self, index, new_value) |
0 commit comments