def _encode_dmrs(m, strict=False): _strict = strict attributes = OrderedDict([('cfrom', str(m.cfrom)), ('cto', str(m.cto))]) if m.surface is not None: attributes['surface'] = m.surface if m.identifier is not None: attributes['ident'] = m.identifier if not _strict and m.index is not None: # index corresponds to a variable, so link it to a nodeid index_nodeid = None try: index_nodeid = m.nodeid(m.index) except KeyError: pass if index_nodeid is not None: attributes['index'] = str(index_nodeid) e = xml.Element('dmrs', attrib=attributes) for node in nodes(m): e.append(dmrx._encode_node(node)) for link in links(m): e.append(dmrx._encode_link(link)) return e
def test_empty(self): x = Dmrs() # Dmrs view assert len(nodes(x)) == 0 assert len(links(x)) == 0 # Xmrs members check_xmrs(x, None, None, None, 0, 0, 0, 0)
def dmrs_tikz_dependency(xs): def link_label(link): return '{}/{}'.format(link.rargname or '', link.post) def label_side(link): if ((link.post == H_POST and link.rargname == RSTR_ROLE) or link.post == EQ_POST): return 'edge below' return 'edge above' if isinstance(xs, Xmrs): xs = [xs] slots = defaultdict(set) # from/(+-)to; + for above, - for below lines = [ '\\documentclass{standalone}', '\\usepackage{tikz-dependency}', '\\begin{document}' ] for x in xs: lines.append( '\\begin{dependency}[' 'text only label,' 'label style={above,font=\\footnotesize},' 'edge unit distance=2ex' ']' ) ns = nodes(x) ls = links(x) predlist = [latex_escape(n.pred.short_form()) for n in ns] lines.extend([ ' \\begin{deptext}[column sep=10pt]', ' {} \\\\'.format(' \\& '.join(predlist)), ' \\end{deptext}' ]) nodeidx = {n.nodeid: i+1 for i, n in enumerate(ns)} for link in links(x): if link.start == 0: lines.append( ' \\deproot[edge unit distance=2ex]{{{}}}{{{}}}'.format( nodeidx[link.end], 'TOP' # latex_escape('/' + link.post) ) ) else: side = label_side(link) opts = [side] slot = link.end * -1 if side.endswith('below') else link.end if slot in slots[link.start]: opts.append('edge unit distance=4ex') slots[link.start].add(slot) lines.append(' \\depedge[{}]{{{}}}{{{}}}{{{}}}'.format( ','.join(opts), nodeidx[link.start], nodeidx[link.end], latex_escape(link_label(link)) )) lines.append('\\end{dependency}') lines.append('\\end{document}') return '\n'.join(lines)
def encode_dmrs(m, indent=2): if indent is not None: delim = '\n' space = ' ' * indent else: delim = '' space = ' ' nodes_ = [ _node.format(indent=space, nodeid=n.nodeid, pred=n.pred.string, lnk='' if n.lnk is None else str(n.lnk), sortinfo=('' if not n.sortinfo else _sortinfo.format( cvarsort=n.cvarsort, properties=' '.join('{}={}'.format(k, v) for k, v in n.sortinfo.items() if k != CVARSORT), ))) for n in nodes(m) ] links_ = [ _link.format(indent=space, start=l.start, pre=l.rargname or '', post=l.post, arrow='->' if l.rargname or l.post != EQ_POST else '--', end=l.end) for l in links(m) ] return delim.join(['dmrs {'] + nodes_ + links_ + ['}'])
def select_nodes(xmrs, nodeid=None, pred=None): """ Return the list of all |Nodes| that have the matching *nodeid* and/or *pred* values. If none match, return an empty list. """ nodematch = lambda n: ((nodeid is None or n.nodeid == nodeid) and (pred is None or n.pred == pred)) return list(filter(nodematch, nodes(xmrs)))
def select_nodes(xmrs, nodeid=None, pred=None): """ Return the list of all [Nodes] that have the matching *nodeid* and/or *pred* values. If none match, return an empty list. """ nodematch = lambda n: ((nodeid is None or n.nodeid == nodeid) and (pred is None or n.pred == pred)) return list(filter(nodematch, nodes(xmrs)))
def dmrs_tikz_dependency(xs): """ Return a LaTeX document with each Xmrs in *xs* rendered as DMRSs. DMRSs use the tikz-dependency package for visualization. """ def link_label(link): return '{}/{}'.format(link.rargname or '', link.post) def label_side(link): if ((link.post == H_POST and link.rargname == RSTR_ROLE) or link.post == EQ_POST): return 'edge below' return 'edge above' if isinstance(xs, Xmrs): xs = [xs] slots = defaultdict(set) # from/(+-)to; + for above, - for below lines = [ '\\documentclass{standalone}', '\\usepackage{tikz-dependency}', '\\begin{document}' ] for x in xs: lines.append('\\begin{dependency}[' 'text only label,' 'label style={above,font=\\footnotesize},' 'edge unit distance=2ex' ']') ns = nodes(x) ls = links(x) predlist = [_latex_escape(n.pred.short_form()) for n in ns] lines.extend([ ' \\begin{deptext}[column sep=10pt]', ' {} \\\\'.format(' \\& '.join(predlist)), ' \\end{deptext}' ]) nodeidx = {n.nodeid: i + 1 for i, n in enumerate(ns)} for link in links(x): if link.start == 0: lines.append( ' \\deproot[edge unit distance=2ex]{{{}}}{{{}}}'.format( nodeidx[link.end], 'TOP' # _latex_escape('/' + link.post) )) else: side = label_side(link) opts = [side] slot = link.end * -1 if side.endswith('below') else link.end if slot in slots[link.start]: opts.append('edge unit distance=4ex') slots[link.start].add(slot) lines.append(' \\depedge[{}]{{{}}}{{{}}}{{{}}}'.format( ','.join(opts), nodeidx[link.start], nodeidx[link.end], _latex_escape(link_label(link)))) lines.append('\\end{dependency}') lines.append('\\end{document}') return '\n'.join(lines)
def _encode_dmrs(m, properties, indent=2): if indent is not None: delim = '\n' space = ' ' * indent else: delim = '' space = ' ' attrs = [] nodes_ = [ _node.format( indent=space, nodeid=n.nodeid, pred=n.pred.string, lnk='' if n.lnk is None else str(n.lnk), carg='' if n.carg is None else '("{}")'.format(n.carg), sortinfo=('' if not (properties and n.sortinfo) else _sortinfo.format( cvarsort=n.cvarsort, properties=' '.join('{}={}'.format(k, v) for k, v in n.sortinfo.items() if k != CVARSORT), ))) for n in nodes(m) ] links_ = [] for l in links(m): if l.start == 0: attrs.append('top={}'.format(l.end)) else: links_.append( _link.format( indent=space, start=l.start, pre=l.rargname or '', post=l.post, arrow='->' if l.rargname or l.post != EQ_POST else '--', end=l.end)) if m.index is not None: idx = m.nodeid(m.index) if idx is not None: attrs.append('index={}'.format(idx)) if m.lnk is not None: attrs.append('lnk={}'.format(str(m.lnk))) if m.surface is not None: attrs.append('surface="{}"'.format(m.surface)) if attrs: attrs = ['{}[{}]'.format(space, ' '.join(attrs))] return delim.join(['dmrs {'] + attrs + nodes_ + links_ + ['}'])
def _encode_dmrs(m, strict=False): _strict = strict attributes = OrderedDict([('cfrom', str(m.cfrom)), ('cto', str(m.cto))]) if m.surface is not None: attributes['surface'] = m.surface if m.identifier is not None: attributes['ident'] = m.identifier # if not _strict and m.index is not None: # # index corresponds to a variable, so link it to a nodeid # index_nodeid = m.get_nodeid(m.index) # if index_nodeid is not None: # attributes['index'] = str(index_nodeid) e = etree.Element('dmrs', attrib=attributes) for node in nodes(m): e.append(_encode_node(node)) for link in links(m): e.append(_encode_link(link)) return e
def encode_dmrs(m, strict=False): _strict = strict attributes = OrderedDict([('cfrom', str(m.cfrom)), ('cto', str(m.cto))]) if m.surface is not None: attributes['surface'] = m.surface if m.identifier is not None: attributes['ident'] = m.identifier # if not _strict and m.index is not None: # # index corresponds to a variable, so link it to a nodeid # index_nodeid = m.get_nodeid(m.index) # if index_nodeid is not None: # attributes['index'] = str(index_nodeid) e = etree.Element('dmrs', attrib=attributes) for node in nodes(m): e.append(encode_node(node)) for link in links(m): e.append(encode_link(link)) return e
def select_nodes(xmrs, nodeid=None, pred=None): """ Return the list of matching nodes in *xmrs*. DMRS :class:`nodes <delphin.mrs.components.node>` for *xmrs* match if their `nodeid` matches *nodeid* and `pred` matches *pred*. The *nodeid* and *pred* filters are ignored if they are `None`. Args: xmrs (:class:`~delphin.mrs.xmrs.Xmrs`): semantic structure to query nodeid (optional): DMRS nodeid to match pred (str, :class:`~delphin.mrs.components.Pred`, optional): predicate to match Returns: list: matching nodes """ nodematch = lambda n: ((nodeid is None or n.nodeid == nodeid) and (pred is None or n.pred == pred)) return list(filter(nodematch, nodes(xmrs)))
def _encode_dmrs(m, properties): attributes = OrderedDict([('cfrom', str(m.cfrom)), ('cto', str(m.cto))]) # if m.top is not None: ... currently handled by links() if m.index is not None: idx = m.nodeid(m.index) if idx is not None: attributes['index'] = str(idx) if m.xarg is not None: xarg = m.nodeid(m.xarg) if xarg is not None: attributes['xarg'] = str(xarg) if m.surface is not None: attributes['surface'] = m.surface if m.identifier is not None: attributes['ident'] = m.identifier e = etree.Element('dmrs', attrib=attributes) for node in nodes(m): e.append(_encode_node(node, properties)) for link in links(m): e.append(_encode_link(link)) return e
def _encode_dmrs(m, indent=2): if indent is not None: delim = '\n' space = ' ' * indent else: delim = '' space = ' ' nodes_ = [ _node.format( indent=space, nodeid=n.nodeid, pred=n.pred.string, lnk='' if n.lnk is None else str(n.lnk), sortinfo=( '' if not n.sortinfo else _sortinfo.format( cvarsort=n.cvarsort, properties=' '.join('{}={}'.format(k, v) for k, v in n.sortinfo.items() if k != CVARSORT), ) ) ) for n in nodes(m) ] links_ = [ _link.format( indent=space, start=l.start, pre=l.rargname or '', post=l.post, arrow='->' if l.rargname or l.post != EQ_POST else '--', end=l.end ) for l in links(m) ] return delim.join(['dmrs {'] + nodes_ + links_ + ['}'])
def dmrs_tikz_dependency(xs, **kwargs): """ Return a LaTeX document with each Xmrs in *xs* rendered as DMRSs. DMRSs use the `tikz-dependency` package for visualization. """ def link_label(link): return '{}/{}'.format(link.rargname or '', link.post) def label_edge(link): if link.post == H_POST and link.rargname == RSTR_ROLE: return 'rstr' elif link.post == EQ_POST: return 'eq' else: return 'arg' if isinstance(xs, Xmrs): xs = [xs] lines = """\\documentclass{standalone} \\usepackage{tikz-dependency} \\usepackage{relsize} %%% %%% style for dmrs graph %%% \\depstyle{dmrs}{edge unit distance=1.5ex, label style={above, scale=.9, opacity=0, text opacity=1}, baseline={([yshift=-0.7\\baselineskip]current bounding box.north)}} %%% set text opacity=0 to hide text, opacity = 0 to hide box \\depstyle{root}{edge unit distance=3ex, label style={opacity=1}} \\depstyle{arg}{edge above} \\depstyle{rstr}{edge below, dotted, label style={text opacity=1}} \\depstyle{eq}{edge below, label style={text opacity=1}} \\depstyle{icons}{edge below, dashed} \\providecommand{\\named}{} \\renewcommand{\\named}{named} %%% styles for predicates and roles (from mrs.sty) \\providecommand{\\spred}{} \\renewcommand{\\spred}[1]{\\mbox{\\textsf{#1}}} \\providecommand{\\srl}{} \\renewcommand{\\srl}[1]{\\mbox{\\textsf{\\smaller #1}}} %%% \\begin{document}""".split("\n") for ix, x in enumerate(xs): lines.append("%%%\n%%% {}\n%%%".format(ix + 1)) lines.append("\\begin{dependency}[dmrs]") ns = nodes(x) ### predicates lines.append(" \\begin{deptext}[column sep=10pt]") for i, n in enumerate(ns): sep = "\\&" if (i < len(ns) - 1) else "\\\\" pred = _latex_escape(n.pred.short_form()) pred = "\\named{}" if pred == 'named' else pred if n.carg is not None: print(n.carg.strip('"')) pred += "\\smaller ({})".format(n.carg.strip('"')) lines.append(" \\spred{{{}}} {} % node {}".format( pred, sep, i + 1)) lines.append(" \\end{deptext}") nodeidx = {n.nodeid: i + 1 for i, n in enumerate(ns)} ### links for link in links(x): if link.start == 0: lines.append(' \\deproot[root]{{{}}}{{{}}}'.format( nodeidx[link.end], '\\srl{TOP}' # _latex_escape('/' + link.post) )) else: lines.append( ' \\depedge[{}]{{{}}}{{{}}}{{\\srl{{{}}}}}'.format( label_edge(link), nodeidx[link.start], nodeidx[link.end], _latex_escape(link_label(link)))) ### placeholder for icons lines.append('% \\depedge[icons]{f}{t}{FOCUS}') lines.append('\\end{dependency}\n') lines.append('\\end{document}') return '\n'.join(lines)