def test_dumps(): assert penman.dumps([]) == '' assert penman.dumps([penman.Graph([('a', 'instance', None)])]) == '(a)' assert penman.dumps([ penman.Graph([('a', 'instance', None)]), penman.Graph([('b', 'instance', None)]), ]) == '(a)\n\n(b)'
def dumps(ds, properties=False, lnk=True, indent=False): """ Serialize DMRS objects to a PENMAN string. Args: ds: iterator of :class:`~delphin.mrs.dmrs.DMRS` objects to serialize properties: if `True`, encode variable properties lnk: if `False`, suppress surface alignments and strings indent: if `True`, adaptively indent; if `False` or `None`, don't indent; if a non-negative integer N, indent N spaces per level Returns: a PENMAN-serialization of the DMRS objects """ if indent is True: indent = -1 elif indent is False: indent = None to_graph = penman.Graph graphs = [ to_graph(to_triples(d, properties=properties, lnk=lnk)) for d in ds ] try: return penman.dumps(graphs, indent=indent) except penman.PenmanError as exc: raise PyDelphinException('could not decode with Penman') from exc
def test_dumps_triples(): assert penman.dumps([penman.Graph([('a', 'instance', None)])], triples=True) == 'instance(a, None)' assert penman.dumps([penman.Graph([('a', 'instance', 'aaa')])], triples=True) == 'instance(a, aaa)' assert penman.dumps( [penman.Graph([('a', 'instance', None), ('a', 'ARG', 'b')])], triples=True) == 'instance(a, None) ^\nARG(a, b)' class TestCodec(penman.PENMANCodec): TYPE_REL = 'test' TOP_VAR = 'TOP' TOP_REL = 'top' assert penman.dumps([penman.Graph([('a', 'ARG', 'b')])], triples=True, cls=TestCodec) == 'top(TOP, a) ^\nARG(a, b)'
def test_dumps_triples(): assert penman.dumps( [penman.Graph([('a', 'instance', None)])], triples=True ) == 'instance(a, None)' assert penman.dumps( [penman.Graph([('a', 'instance', 'aaa')])], triples=True ) == 'instance(a, aaa)' assert penman.dumps( [penman.Graph([('a', 'instance', None), ('a', 'ARG', 'b')])], triples=True ) == 'instance(a, None) ^\nARG(a, b)' gs = penman.dumps( [penman.Graph([(1, 'instance', 'alpha')])], triples=True ) == 'instance(1, alpha)' gs = penman.dumps( [penman.Graph([(1.1, 'instance', 'alpha')])], triples=True ) == 'instance(1.1, alpha)' gs = penman.dumps( [penman.Graph([('"a string"', 'instance', 'alpha')])], triples=True ) == 'instance("a string", alpha)' class TestCodec(penman.PENMANCodec): TYPE_REL = 'test' TOP_VAR = 'TOP' TOP_REL = 'top' assert penman.dumps( [penman.Graph([('a', 'ARG', 'b')])], triples=True, cls=TestCodec ) == 'top(TOP, a) ^\nARG(a, b)'
def main(args): with open(args.input, encoding='utf-8') as f, \ open(args.extra, encoding='utf-8') as e, \ open(args.output, mode='w', encoding='utf-8') as out: amr_pair: List[Tuple[Graph, Graph]] amr_pair = [] for sent_num, (cur_amr1, cur_amr2) in enumerate(generate_amr_lines(f, e), start=1): amr_pair.append((pp.decode(cur_amr1), pp.decode(cur_amr2))) amrs = [] for idx, (amr, extra) in enumerate(amr_pair): amr.metadata.update(extra.metadata) amrs.append(amr) out.write(pp.dumps(amrs))
def dumps(xs, model=None, properties=False, indent=True, **kwargs): """ Serialize Xmrs (or subclass) objects to PENMAN notation Args: xs: iterator of :class:`~delphin.mrs.xmrs.Xmrs` objects to serialize model: Xmrs subclass used to get triples properties: if `True`, encode variable properties indent: if `True`, adaptively indent; if `False` or `None`, don't indent; if a non-negative integer N, indent N spaces per level Returns: the PENMAN serialization of *xs* """ xs = list(xs) if not xs: return '' given_class = xs[0].__class__ # assume they are all the same if model is None: model = xs[0].__class__ if not hasattr(model, 'to_triples'): raise TypeError('{} class does not implement to_triples()'.format( model.__name__)) # convert MRS to DMRS if necessary; EDS cannot convert if given_class.__name__ in ('Mrs', 'Xmrs'): xs = [model.from_xmrs(x, **kwargs) for x in xs] elif given_class.__name__ == 'Eds' and model.__name__ != 'Eds': raise ValueError('Cannot convert EDS to non-EDS') codec = XMRSCodec() graphs = [ codec.triples_to_graph(model.to_triples(x, properties=properties)) for x in xs ] if 'pretty_print' in kwargs: indent = kwargs['pretty_print'] return penman.dumps(graphs, cls=XMRSCodec, indent=indent)
def dumps(xs, model=None, properties=False, indent=True, **kwargs): """ Serialize [Xmrs] (or subclass) objects to PENMAN notation Args: xs: an iterator of [Xmrs] objects to serialize model: the Xmrs subclass used to get triples properties: if True, encode variable properties indent: if True, adaptively indent; if False or None, don't indent; if a non-negative integer N, indent N spaces per level pretty_print: (deprecated) if set, it overrides indent Returns: the PENMAN serialization of *xs* """ xs = list(xs) if not xs: return '' if model is None: model = xs[0].__class__ if not hasattr(model, 'to_triples'): raise TypeError( '{} class does not implement to_triples()'.format(model.__name__) ) codec = XMRSCodec() graphs = [ codec.triples_to_graph( model.to_triples(model.from_xmrs(x), properties=properties) ) for x in xs ] if 'pretty_print' in kwargs: indent = kwargs['pretty_print'] return penman.dumps(graphs, cls=XMRSCodec, indent=indent)