Пример #1
0
    def test_from_dict(self):
        assert Mrs.from_dict({}) == Mrs()

        m1 = Mrs.from_dict({
            'relations': [{
                'label': 'h1',
                'predicate': '_rain_v_1',
                'arguments': {
                    'ARG0': 'e2'
                }
            }],
            'constraints': [],
            'variables': {
                'h1': {
                    'type': 'h'
                },
                'e2': {
                    'type': 'e'
                }
            }
        })
        m2 = Mrs(rels=[
            EP(FIRST_NODEID, sp('"_rain_v_1_rel"'), 'h1', {'ARG0': 'e2'})
        ])
        assert m1 == m2
Пример #2
0
def read_mrs(tokens, version=_default_version):
    """Decode a sequence of Simple-MRS tokens. Assume LTOP, INDEX, RELS,
       HCONS, and ICONS occur in that order."""
    # variables needs to be passed to any function that can call read_variable
    variables = defaultdict(list)
    # [ LTOP : handle INDEX : variable RELS : rels-list HCONS : hcons-list ]
    try:
        validate_token(tokens.popleft(), _left_bracket)
        ltop = index = surface = lnk = None
        # SimpleMRS extension for encoding surface string
        if tokens[0] == _left_angle:
            lnk = read_lnk(tokens)
        if tokens[0].startswith('"'): # and tokens[0].endswith('"'):
            surface = tokens.popleft()[1:-1] # get rid of first quotes
        if tokens[0] in (_ltop, _top):
            _, ltop = read_featval(tokens, variables=variables)
        if tokens[0] == _index:
            _, index = read_featval(tokens, feat=_index, variables=variables)
        rels = read_rels(tokens, variables=variables)
        hcons = read_hcons(tokens, variables=variables)
        icons = read_icons(tokens, variables=variables)
        validate_token(tokens.popleft(), _right_bracket)
        m = Mrs(top=ltop,
                index=index,
                rels=rels,
                hcons=hcons,
                icons=icons,
                lnk=lnk,
                surface=surface,
                vars=variables)
    except IndexError:
        unexpected_termination_error()
    return m
Пример #3
0
def _deserialize_mrs(elem):
    # <!ELEMENT mrs (label, var, (ep|hcons)*)>
    # <!ATTLIST mrs
    #           cfrom     CDATA #IMPLIED
    #           cto       CDATA #IMPLIED
    #           surface   CDATA #IMPLIED
    #           ident     CDATA #IMPLIED >
    elem = elem.find('.')  # in case elem is ElementTree rather than Element
    variables = defaultdict(list)
    # normalize_vars(elem) # try to make all vars have a sort
    top = elem.find('label')
    if top is not None:
        top = _decode_label(top)
    index = elem.find('var')
    if index is not None:
        index = _decode_var(index, variables=variables)
    return Mrs(top=top,
               index=index,
               rels=[_decode_ep(ep, variables) for ep in elem.iter('ep')],
               hcons=list(map(_decode_hcons, elem.iter('hcons'))),
               icons=list(map(_decode_icons, elem.iter('icons'))), # future
               lnk=_decode_lnk(elem.get('cfrom'), elem.get('cto')),
               surface=elem.get('surface'),
               identifier=elem.get('ident'),
               vars=variables)
Пример #4
0
def _read_mrs(tokens, version, errors):
    #return read_mrs(tokens)
    try:
        _read_literals(tokens, '[')
        top = idx = surface = lnk = None
        vars_ = {}
        if version >= 1.1:
            if tokens[0] == '<':
                lnk = _read_lnk(tokens)
            if tokens[0].startswith('"'):  # and tokens[0].endswith('"'):
                surface = tokens.popleft()[1:-1]  # get rid of first quotes
        if tokens[0].upper() in ('LTOP', 'TOP'):
            tokens.popleft()  # LTOP / TOP
            _read_literals(tokens, ':')
            top = tokens.popleft()
            vars_[top] = []
        if tokens[0].upper() == 'INDEX':
            tokens.popleft()  # INDEX
            _read_literals(tokens, ':')
            idx = tokens.popleft()
            vars_[idx] = _read_props(tokens)
        rels = _read_rels(tokens, vars_)
        hcons = _read_cons(tokens, 'HCONS', vars_)
        icons = _read_cons(tokens, 'ICONS', vars_)
        _read_literals(tokens, ']')
        # at this point, we could uniquify proplists in vars_, but most
        # likely it isn't necessary, and might harm things if we
        # leave potential dupes in there. let's see how it plays out.
        m = Mrs(top=top,
                index=idx,
                rels=rels,
                hcons=hcons,
                icons=icons,
                lnk=lnk,
                surface=surface,
                vars=vars_)
    except IndexError:
        _unexpected_termination_error()
    if errors != 'ignore':
        try:
            m.validate()
        except XmrsError as ex:
            if errors == 'warn':
                warn(str(ex), XmrsWarning)
            elif errors == 'strict':
                raise
    return m
Пример #5
0
 def test_empty(self):
     x = Mrs()
     # Mrs view
     assert len(eps(x)) == 0
     assert len(hcons(x)) == 0
     assert len(icons(x)) == 0
     # Xmrs members
     check_xmrs(x, None, None, None, 0, 0, 0, 0)
Пример #6
0
 def mrs(self):
     """
     Deserialize and return an Mrs object for simplemrs or
     JSON-formatted MRS data; otherwise return the original string.
     """
     mrs = self.get('mrs')
     if mrs is not None:
         if isinstance(mrs, dict):
             mrs = Mrs.from_dict(mrs)
         elif isinstance(mrs, stringtypes):
             mrs = simplemrs.loads_one(mrs)
     return mrs
Пример #7
0
 def mrs(self):
     """
     Deserialize and return an Mrs object for simplemrs or
     JSON-formatted MRS data; otherwise return the original string.
     """
     mrs = self.get('mrs')
     if mrs is not None:
         if isinstance(mrs, dict):
             mrs = Mrs.from_dict(mrs)
         elif isinstance(mrs, stringtypes):
             mrs = simplemrs.loads_one(mrs)
     return mrs
Пример #8
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'}
            }
        }
Пример #9
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
Пример #10
0
 def test_single_ep(self):
     # basic, one EP, no TOP
     x = Mrs(rels=[EP(10, sp('"_rain_v_1_rel"'), 'h1')])
     check_xmrs(x, None, None, None, 1, 0, 0, 1)
     # variables don't need to be created predictably, but it's nice
     # to get the expected values for simple cases
     assert x.label(10) == 'h1'
     assert x.ep(10).iv == None
     # now with ARG0
     x = Mrs(rels=[EP(10, sp('"_rain_v_1_rel"'), 'h1', {'ARG0': 'e2'})])
     check_xmrs(x, None, None, None, 1, 0, 0, 2)
     assert x.label(10) == 'h1'
     assert x.ep(10).iv == 'e2'
     # now with TOP
     x = Mrs(top='h0',
             rels=[EP(10, sp('"_rain_v_1_rel"'), 'h1', {'ARG0': 'e2'})],
             hcons=[('h0', 'qeq', 'h1')])
     check_xmrs(x, 'h0', None, None, 1, 1, 0, 3)
     assert x.label(10) == 'h1'
     assert x.ep(10).iv == 'e2'
Пример #11
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