Beispiel #1
0
    def test_to_dict(self):
        assert Mrs().to_dict() == {
            'relations': [], 'constraints': [], 'variables': {}
        }

        x = Mrs(rels=[EP(10, sp('"_rain_v_1_rel"'), 'h1', {'ARG0': 'e2'})])
        assert x.to_dict() == {
            'relations': [
                {'label': 'h1', 'predicate': '_rain_v_1',
                 'arguments': {'ARG0': 'e2'}}
            ],
            'constraints': [],
            'variables': {'h1': {'type': 'h'}, 'e2': {'type': 'e'}}
        }

        x = Mrs(
            top='h0',
            rels=[EP(10, sp('"_rain_v_1_rel"'), 'h1', {'ARG0': 'e2'})],
            hcons=[('h0', 'qeq', 'h1')],
            vars={'e2': {'SF': 'prop', 'TENSE': 'pres'}}
        )
        assert x.to_dict() == {
            'top': 'h0',
            'relations': [
                {'label': 'h1', 'predicate': '_rain_v_1',
                 'arguments': {'ARG0': 'e2'}}
            ],
            'constraints': [{'relation': 'qeq', 'high': 'h0', 'low': 'h1'}],
            'variables': {
                'h0': {'type': 'h'}, 'h1': {'type': 'h'},
                'e2': {'type': 'e',
                       'properties': {'SF': 'prop', 'TENSE': 'pres'}}
            }
        }
        assert x.to_dict(properties=False) == {
            'top': 'h0',
            'relations': [
                {'label': 'h1', 'predicate': '_rain_v_1',
                 'arguments': {'ARG0': 'e2'}}
            ],
            'constraints': [{'relation': 'qeq', 'high': 'h0', 'low': 'h1'}],
            'variables': {
                'h0': {'type': 'h'}, 'h1': {'type': 'h'},
                'e2': {'type': 'e'}
            }
        }
Beispiel #2
0
def parse_response(inp, ace_response, params):
    properties = True if params.get('properties') == 'json' else False
    tcpu, pedges = _get_parse_info(ace_response.get('NOTES', []))
    result_data = []
    for i, res in enumerate(ace_response.get('RESULTS', [])):
        mrs, udf = res['MRS'], res['DERIV']
        xmrs = simplemrs.loads_one(mrs)
        d = {'result-id': i}

        if params.get('derivation') == 'udf':
            d['derivation'] = udf
        elif params.get('derivation') == 'json':
            d['derivation'] = udf_to_dict(udf, params)

        if params.get('mrs') == 'simple':
            d['mrs'] = mrs
        elif params.get('mrs') == 'json':
            d['mrs'] = Mrs.to_dict(xmrs, properties=properties)
        elif params.get('mrs') == 'latex':
            abort(501, "The 'latex' format for MRS is not yet implemented.")

        if params.get('eds') == 'native':
            d['eds'] = eds.dumps(xmrs, single=True)
        elif params.get('eds') == 'json':
            d['eds'] = eds.Eds.from_xmrs(xmrs).to_dict(properties=properties)
        elif params.get('eds') == 'latex':
            abort(501, "The 'latex' format for EDS is not yet implemented.")

        if params.get('dmrs') == 'json':
            d['dmrs'] = Dmrs.to_dict(xmrs, properties=properties)
        elif params.get('dmrs') == 'latex':
            d['dmrs'] = latex.dmrs_tikz_dependency(xmrs)

        result_data.append(d)
    
    data = {
        'input': inp,
        'readings': len(ace_response.get('RESULTS', [])),
        'results': result_data
    }
    if tcpu is not None: data['tcpu'] = tcpu
    if pedges is not None: data['pedges'] = pedges

    return data
Beispiel #3
0
def _parse_repsonse(inp, ace_response, params):
    properties = True if params.get('properties') == 'json' else False

    tcpu = ace_response.get('tcpu')
    pedges = ace_response.get('pedges')
    readings = ace_response.get('readings')
    if readings is None:
        readings = len(ace_response.get('results', []))

    result_data = []
    for i, res in enumerate(ace_response.results()):
        mrs, udf = res['mrs'], res['derivation']
        xmrs = simplemrs.loads_one(mrs)
        d = {'result-id': i}

        if params.get('derivation') == 'udf':
            d['derivation'] = udf
        elif params.get('derivation') == 'json':
            d['derivation'] = _udf_to_dict(udf, params)

        if params.get('mrs') == 'simple':
            d['mrs'] = mrs
        elif params.get('mrs') == 'json':
            d['mrs'] = Mrs.to_dict(xmrs, properties=properties)
        elif params.get('mrs') == 'latex':
            abort(501, "The 'latex' format for MRS is not yet implemented.")

        if params.get('eds') == 'native':
            d['eds'] = eds.dumps(xmrs, single=True)
        elif params.get('eds') == 'json':
            d['eds'] = eds.Eds.from_xmrs(xmrs).to_dict(properties=properties)
        elif params.get('eds') in ('amr', 'penman'):
            d['eds'] = penman.dumps([xmrs], model=eds.Eds)
        elif params.get('eds') == 'latex':
            abort(501, "The 'latex' format for EDS is not yet implemented.")

        if params.get('dmrs') == 'json':
            d['dmrs'] = Dmrs.to_dict(xmrs, properties=properties)
        elif params.get('dmrs') == 'penman':
            d['dmrs'] = penman.dumps([xmrs], model=Dmrs)
        elif params.get('dmrs') == 'latex':
            d['dmrs'] = latex.dmrs_tikz_dependency(xmrs)

        result_data.append(d)

    data = {
        'input': inp,
        'readings': readings,
        'results': result_data
    }
    if tcpu is not None: data['tcpu'] = tcpu
    if pedges is not None: data['pedges'] = pedges
    if params.get('tokens'):
        t1 = ace_response.tokens('initial')
        t2 = ace_response.tokens('internal')
        if params['tokens'] == 'json':
            data['tokens'] = {
                'initial': t1.to_list(),
                'internal': t2.to_list()
            }
        elif params['tokens'] == 'yy':
            data['tokens'] = {
                'initial': str(t1),
                'internal': str(t2)
            }

    return data