Example #1
0
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)
Example #2
0
def accumulate(golds, tests, ignore_missing_gold, ignore_missing_test):
    """
    Sum the matches for all *golds* and *tests*.
    """
    info = logger.isEnabledFor(logging.INFO)
    totals = Match(Count(0, 0, 0), Count(0, 0, 0), Count(0, 0, 0),
                   Count(0, 0, 0), Count(0, 0, 0))

    for i, (gold, test) in enumerate(zip_longest(golds, tests), 1):
        logger.info('pair %d', i)

        if gold is None and test is None:
            logger.info('no gold or test representation; skipping')
            continue
        elif gold is None:
            if ignore_missing_gold:
                logger.info('no gold representation; skipping')
                continue
            else:
                logger.debug('missing gold representation')
                gold = EDS()
        elif test is None:
            if ignore_missing_test:
                logger.info('no test representation; skipping')
                continue
            else:
                logger.debug('missing test representation')
                test = EDS()

        result = match(gold, test)

        if info:
            logger.info('             gold\ttest\tboth\tPrec.\tRec.\tF-Score')
            fmt = '%11s: %4d\t%4d\t%4d\t%5.3f\t%5.3f\t%5.3f'
            logger.info(fmt, 'Names', *result.name, *_prf(*result.name))
            logger.info(fmt, 'Arguments', *result.argument,
                        *_prf(*result.argument))
            logger.info(fmt, 'Properties', *result.property,
                        *_prf(*result.property))
            logger.info(fmt, 'Constants', *result.constant,
                        *_prf(*result.constant))
            logger.info(fmt, 'Tops', *result.top, *_prf(*result.top))

        totals = totals.add(result)

    return totals
Example #3
0
def test_encode(dogs_bark_from_mrs):
    d = EDS(**dogs_bark_from_mrs)
    assert edsnative.encode(d) == (
        '{e2: e2:_bark_v_1{e}[ARG1 x4] _1:udef_q[BV x4] x4:_dog_n_1{x}[]}')
    assert edsnative.encode(d, indent=True) == ('{e2:\n'
                                                ' e2:_bark_v_1{e}[ARG1 x4]\n'
                                                ' _1:udef_q[BV x4]\n'
                                                ' x4:_dog_n_1{x}[]\n'
                                                '}')
Example #4
0
def _decode_eds(lexer):
    _, top, _ = lexer.expect_type(LBRACE, SYMBOL, COLON)
    lexer.accept_type(GRAPHSTATUS)
    nodes = []
    while lexer.peek()[0] != RBRACE:
        lexer.accept_type(NODESTATUS)
        start, _ = lexer.expect_type(SYMBOL, COLON)
        nodes.append(_decode_node(start, lexer))
    lexer.expect_type(RBRACE)
    return EDS(top=top, nodes=nodes)
Example #5
0
def test_basic_EDS(dogs_bark):
    d = EDS(**dogs_bark)
    assert d.top == '_1'
    assert len(d.nodes) == 3

    assert d.nodes[0].predicate == '_bark_v_1'
    assert d.nodes[1].predicate == 'udef_q'
    assert d.nodes[2].predicate == '_dog_n_1'

    assert d.nodes[0].edges == {'ARG1': '_3'}
    assert d.nodes[1].edges == {'BV': '_3'}
    assert d.nodes[2].edges == {}

    assert len(d.edges) == 2
    assert d.edges[0] == ('_1', 'ARG1', '_3')
    assert d.edges[1] == ('_2', 'BV', '_3')
Example #6
0
def from_dict(d):
    """
    Decode a dictionary, as from :func:`to_dict`, into an EDS object.
    """
    top = d.get('top')
    nodes = []
    for nodeid, node in d.get('nodes', {}).items():
        props = node.get('properties', None)
        nodetype = node.get('type')
        lnk = None
        if 'lnk' in node:
            lnk = Lnk.charspan(node['lnk']['from'], node['lnk']['to'])
        nodes.append(
            Node(id=nodeid,
                 predicate=node['label'],
                 type=nodetype,
                 edges=node.get('edges', {}),
                 properties=props,
                 carg=node.get('carg'),
                 lnk=lnk))
    nodes.sort(key=lambda n: (n.cfrom, -n.cto))
    return EDS(top, nodes=nodes)
Example #7
0
def test_empty_EDS():
    d = EDS()
    assert d.top is None
    assert d.nodes == []