def _decode_node(start, lexer): predicate = lexer.expect_type(SYMBOL).lower() lnk = Lnk(lexer.accept_type(LNK)) carg = lexer.accept_type(CARG) nodetype, properties = _decode_properties(start, lexer) edges = _decode_edges(start, lexer) return Node(start, predicate, nodetype, edges, properties, carg, lnk)
def from_triples(triples): """ Decode triples, as from :func:`to_triples`, into an EDS object. """ nids, nd = [], {} for src, rel, tgt in triples: rel = rel.lstrip(':') if src not in nd: nids.append(src) nd[src] = {'pred': None, 'type': None, 'edges': {}, 'props': {}, 'lnk': None, 'carg': None} if rel == 'instance': nd[src]['pred'] = tgt elif rel == 'lnk': nd[src]['lnk'] = Lnk(tgt.strip('"')) elif rel == 'carg': if (tgt[0], tgt[-1]) == ('"', '"'): tgt = tgt[1:-1] nd[src]['carg'] = tgt elif rel == 'type': nd[src]['type'] = tgt elif rel.islower(): nd[src]['props'][rel.upper()] = tgt else: nd[src]['edges'][rel] = tgt nodes = [Node(nid, nd[nid]['pred'], type=nd[nid]['type'], edges=nd[nid]['edges'], properties=nd[nid]['props'], carg=nd[nid]['carg'], lnk=nd[nid]['lnk']) for nid in nids] top = nids[0] if nids else None return EDS(top=top, nodes=nodes)
def test__bool__(self): assert not Lnk(None) assert not Lnk.charspan(-1, -1) assert Lnk.charspan(0, 0) assert Lnk.chartspan(0, 0) assert Lnk.tokens([]) assert Lnk.edge(0)
def _decode_lnk(lexer): lnk = lexer.accept_type(LNK) if lnk is not None: lnk = Lnk(lnk) return lnk
def test_raw_init(self): with pytest.raises(TypeError): Lnk() # don't allow just any Lnk type with pytest.raises(LnkError): Lnk('lnktype', (0, 1))
def test__eq__(self): assert Lnk(None) == Lnk(None) assert Lnk(None) != Lnk.charspan(0, 1) assert Lnk.charspan(0, 1) == Lnk.charspan(0, 1) assert Lnk.charspan(0, 1) != Lnk.charspan(0, 2) assert Lnk.charspan(0, 1) != Lnk.chartspan(0, 1)