Exemple #1
0
def test_links():
    assert list(links(x1)) == [(0, 10000, None, 'H')]
    assert sorted(links(x2)) == [
        (0, 10000, None, 'H'), (10001, 10000, 'ARG1', 'EQ')
    ]
    assert sorted(links(x3)) == [
        (0, 10000, None, 'H'),
        (10000, 10001, 'ARG1', 'NEQ'),
        (10002, 10001, 'RSTR', 'H')
    ]
    assert sorted(links(x4)) == [
        (0, 10003, None, 'H'),
        (10000, 10002, 'RSTR', 'H'),
        (10002, 10001, 'ARG1', 'HEQ'),
        (10003, 10002, 'ARG1', 'NEQ')
    ]
    assert sorted(links(x5)) == [
        (0, 10006, None, 'H'),
        (10000, 10001, 'RSTR', 'H'),
        (10001, 10005, None, 'EQ'),
        (10002, 10004, 'RSTR', 'H'),
        (10003, 10001, 'ARG2', 'EQ'),
        (10003, 10004, 'ARG1', 'NEQ'),
        (10005, 10004, 'ARG1', 'NEQ'),
        (10006, 10001, 'ARG1', 'NEQ')
    ]
Exemple #2
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)
Exemple #3
0
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 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)
Exemple #5
0
 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)
Exemple #6
0
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
Exemple #7
0
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_ + ['}'])
Exemple #8
0
def select_links(xmrs, start=None, end=None, rargname=None, post=None):
    """
    Return the list of all [Links] that have the matching *start*,
    *end*, *rargname*, and/or *post* values. If none match, return
    an empty list.
    """
    linkmatch = lambda l: ((start is None or l.start == start) and
                           (end is None or l.end == end) and
                           (rargname is None or l.rargname == rargname) and
                           (post is None or l.post == post))
    return list(filter(linkmatch, links(xmrs)))
Exemple #9
0
def select_links(xmrs, start=None, end=None, rargname=None, post=None):
    """
    Return the list of all |Links| that have the matching *start*,
    *end*, *rargname*, and/or *post* values. If none match, return
    an empty list.
    """
    linkmatch = lambda l: (
        (start is None or l.start == start) and
        (end is None or l.end == end) and
        (rargname is None or l.rargname == rargname) and
        (post is None or l.post == post))
    return list(filter(linkmatch, links(xmrs)))
Exemple #10
0
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_ + ['}'])
Exemple #11
0
def test_links():
    assert list(links(x1)) == [(0, 10000, None, 'H')]
    assert sorted(links(x2)) == [(0, 10000, None, 'H'),
                                 (10001, 10000, 'ARG1', 'EQ')]
    assert sorted(links(x3)) == [(0, 10000, None, 'H'),
                                 (10000, 10001, 'ARG1', 'NEQ'),
                                 (10002, 10001, 'RSTR', 'H')]
    assert sorted(links(x4)) == [(0, 10003, None, 'H'),
                                 (10000, 10002, 'RSTR', 'H'),
                                 (10002, 10001, 'ARG1', 'HEQ'),
                                 (10003, 10002, 'ARG1', 'NEQ')]
    assert sorted(links(x5)) == [(0, 10006, None, 'H'),
                                 (10000, 10001, 'RSTR', 'H'),
                                 (10002, 10004, 'RSTR', 'H'),
                                 (10003, 10001, 'ARG2', 'EQ'),
                                 (10003, 10004, 'ARG1', 'NEQ'),
                                 (10005, 10001, BARE_EQ_ROLE, 'EQ'),
                                 (10005, 10004, 'ARG1', 'NEQ'),
                                 (10006, 10001, 'ARG1', 'NEQ')]
    assert sorted(links(x6)) == [(0, 10005, None, 'H'),
                                 (10000, 10004, 'RSTR', 'H'),
                                 (10001, 10004, 'ARG1', 'NEQ'),
                                 (10002, 10001, 'L-HNDL', 'HEQ'),
                                 (10002, 10001, 'L-INDEX', 'NEQ'),
                                 (10002, 10003, 'R-HNDL', 'HEQ'),
                                 (10002, 10003, 'R-INDEX', 'NEQ'),
                                 (10002, 10004, BARE_EQ_ROLE, 'EQ'),
                                 (10003, 10004, 'ARG1', 'NEQ'),
                                 (10005, 10004, 'ARG1', 'NEQ')]
Exemple #12
0
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
Exemple #13
0
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
Exemple #14
0
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
Exemple #15
0
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_ + ['}'])
Exemple #16
0
def select_links(xmrs, start=None, end=None, rargname=None, post=None):
    """
    Return the list of matching links for *xmrs*.

    :class:`~delphin.mrs.components.Link` objects for *xmrs* match if
    their `start` matches *start*, `end` matches *end*, `rargname`
    matches *rargname*, and `post` matches *post*. The *start*, *end*,
    *rargname*, and *post* filters are ignored if they are `None`.

    Args:
        xmrs (:class:`~delphin.mrs.xmrs.Xmrs`): semantic structure to
            query
        start (optional): link start nodeid to match
        end (optional): link end nodeid to match
        rargname (str, optional): role name to match
        post (str, optional): Link post-slash label to match
    Returns:
        list: matching links
    """
    linkmatch = lambda l: ((start is None or l.start == start) and
                           (end is None or l.end == end) and
                           (rargname is None or l.rargname == rargname) and
                           (post is None or l.post == post))
    return list(filter(linkmatch, links(xmrs)))
Exemple #17
0
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)