Exemple #1
0
    def test_to_dict(self):
        assert Dmrs().to_dict() == {'nodes': [], 'links': []}

        x = Dmrs(nodes=[Node(10, sp('"_rain_v_1_rel"'))])
        assert x.to_dict() == {
            'nodes': [{
                'nodeid': 10,
                'predicate': '_rain_v_1',
                'sortinfo': {
                    'cvarsort': UNKNOWNSORT
                }
            }],
            'links': []
        }

        x = Dmrs(nodes=[Node(10, sp('"_rain_v_1_rel"'), {'cvarsort': 'e'})],
                 links=[Link(0, 10, None, 'H')])
        assert x.to_dict() == {
            'nodes': [{
                'nodeid': 10,
                'predicate': '_rain_v_1',
                'sortinfo': {
                    'cvarsort': 'e'
                }
            }],
            'links': [{
                'from': 0,
                'to': 10,
                'rargname': None,
                'post': 'H'
            }]
        }
        assert x.to_dict(properties=False) == {
            'nodes': [{
                'nodeid': 10,
                'predicate': '_rain_v_1'
            }],
            'links': [{
                'from': 0,
                'to': 10,
                'rargname': None,
                'post': 'H'
            }]
        }
    def test_to_dict(self):
        assert Dmrs().to_dict() == {'nodes': [], 'links': []}

        x = Dmrs(nodes=[Node(10, sp('"_rain_v_1_rel"'))])
        assert x.to_dict() == {
            'nodes': [{'nodeid': 10, 'predicate': '_rain_v_1',
                       'sortinfo': {'cvarsort': UNKNOWNSORT}}],
            'links': []
        }

        x = Dmrs(
            nodes=[Node(10, sp('"_rain_v_1_rel"'), {'cvarsort': 'e'})],
            links=[Link(0, 10, None, 'H')]
        )
        assert x.to_dict() == {
            'nodes': [{'nodeid': 10, 'predicate': '_rain_v_1',
                       'sortinfo': {'cvarsort': 'e'}}],
            'links': [{'from': 0, 'to': 10, 'rargname': None, 'post': 'H'}]
        }
        assert x.to_dict(properties=False) == {
            'nodes': [{'nodeid': 10, 'predicate': '_rain_v_1'}],
            'links': [{'from': 0, 'to': 10, 'rargname': None, 'post': 'H'}]
        }
Exemple #3
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
Exemple #4
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