def dogs_bark(): return { 'top': 'h0', 'index': 'e2', 'rels': [mrs.EP('_bark_v_1', 'h1', args={'ARG0': 'e2', 'ARG1': 'x4'}), mrs.EP('udef_q', 'h3', args={'ARG0': 'x4', 'RSTR': 'h5', 'BODY': 'h7'}), mrs.EP('_dog_n_1', 'h6', args={'ARG0': 'x4'})], 'hcons': [mrs.HCons.qeq('h0', 'h1'), mrs.HCons.qeq('h5', 'h6')], 'variables': { 'e2': {'TENSE': 'pres'}, 'x4': {'NUM': 'pl'}}}
def test__eq__(self): ep = mrs.EP('_dog_n_1', 'h3') assert ep == mrs.EP('_dog_n_1', 'h3') assert ep != mrs.EP('_dog_n_2', 'h3') assert ep != mrs.EP('_dog_n_1', 'h4') ep = mrs.EP('_chase_v_1', 'h1', {'ARG0': 'e2', 'ARG1': 'x4', 'ARG2': 'x6'}) assert ep == mrs.EP('_chase_v_1', 'h1', {'ARG0': 'e2', 'ARG1': 'x4', 'ARG2': 'x6'}) assert ep != mrs.EP('_chase_v_2', 'h1', {'ARG0': 'e2', 'ARG1': 'x4', 'ARG2': 'x6'}) assert ep != mrs.EP('_chase_v_1', 'h2', {'ARG0': 'e2', 'ARG1': 'x4', 'ARG2': 'x6'}) assert ep != mrs.EP('_chase_v_1', 'h2', {'ARG0': 'e2', 'ARG1': 'x6', 'ARG2': 'x4'}) assert ep != mrs.EP('_chase_v_1', 'h2')
def passive_arg(orig_sem, ep, arg_id): ''' return a MRS with ep passivised with arg_id as syntactic specifier ''' new_rels = orig_sem.rels[:] new_vars = copy_vars(orig_sem) parg = ep.args[arg_id] last_vid = get_max_vid(orig_sem) parg_label = ep.label parg_index = 'e' + str(last_vid + 1) parg_ep = mrs.EP(predicate='parg_d', label=parg_label, args={ 'ARG0': parg_index, 'ARG1': ep.args['ARG0'], 'ARG2': parg }) new_rels.append(parg_ep) new_vars[parg_label] = {} new_vars[parg_index] = { 'SF': 'prop', 'TENSE': 'untensed', 'MOOD': 'indicative', 'PROG': '-', 'PERF': '-' } new_sem = copy_sem(orig_sem, rels=new_rels, variables=new_vars) return new_sem
def copy_ep(orig_ep, predicate=None, label=None, args=None): ''' return a copy of orig_ep with specified changes ''' if not predicate: predicate = orig_ep.predicate if not label: label = orig_ep.label if not args: args = orig_ep.args.copy() return mrs.EP(predicate=predicate, label=label, args=args)
def negate_ep(orig_sem, ep): ''' return an MRS whose index EP is negated by EP:neg :param orig_sem: original MRS :type orig_sem: MRS :param ep: EP whose intrinsic variable matches sentential index :type ep: EP :rtype MRS ''' new_rels = orig_sem.rels[:] new_hcons = orig_sem.hcons[:] new_vars = copy_vars(orig_sem) last_vid = get_max_vid(orig_sem) neg_label = 'h' + str(last_vid + 1) neg_index = 'e' + str(last_vid + 2) neg_arg = 'h' + str(last_vid + 3) neg_ep = mrs.EP(predicate='neg', label=neg_label, args={ 'ARG0': neg_index, 'ARG1': neg_arg }) new_rels.append(neg_ep) for hc in new_hcons: if hc.lo == ep.label: hi_mid = mrs.HCons(hc.hi, 'qeq', neg_label) new_hcons.append(hi_mid) new_hcons.remove(hc) mid_lo = mrs.HCons(neg_arg, 'qeq', ep.label) new_hcons.append(mid_lo) new_vars[neg_index] = { 'SF': 'prop', 'TENSE': 'untensed', 'MOOD': 'indicative', 'PROG': '-', 'PERF': '-' } new_sem = copy_sem(orig_sem, rels=new_rels, hcons=new_hcons, variables=new_vars) return {'negation': new_sem}
def one_modality(orig_sem, ep, modality): ''' return one new MRS with added modality ''' last_vid = get_max_vid(orig_sem) modal_label = 'h' + str(last_vid + 1) modal_index = 'e' + str(last_vid + 2) modal_arg = 'h' + str(last_vid + 3) new_sem = replace_handle(orig_sem, ep.label, modal_label) new_index = new_sem.index new_rels = new_sem.rels[:] new_hcons = new_sem.hcons[:] new_vars = copy_vars(new_sem) if new_sem.index == ep.args['ARG0']: new_index = modal_index modal_ep = mrs.EP(predicate=modality, label=modal_label, args={ 'ARG0': modal_index, 'ARG1': modal_arg }) new_rels.append(modal_ep) modal_arg_hc = mrs.HCons(modal_arg, 'qeq', ep.label) new_hcons.append(modal_arg_hc) new_vars[modal_label] = {} new_vars[modal_arg] = {} new_vars[modal_index] = { 'SF': 'prop', 'TENSE': 'pres', 'MOOD': 'indicative', 'PROG': '-', 'PERF': '-' } new_vars[ep.args['ARG0']]['TENSE'] = 'untensed' new_sem = copy_sem(new_sem, index=new_index, rels=new_rels, hcons=new_hcons, variables=new_vars) return new_sem
def itcleft_arg(orig_sem, ep, arg_id, passive_ep=False): ''' return an MRS with arg extracted by it-cleft handle is the handle of original index EP ''' new_rels = orig_sem.rels[:] new_hcons = orig_sem.hcons[:] new_vars = copy_vars(orig_sem) last_vid = get_max_vid(orig_sem) cleft_label = 'h' + str(last_vid + 1) cleft_index = 'e' + str(last_vid + 2) cleft_ep = mrs.EP(predicate='_be_v_itcleft', label=cleft_label, args={ 'ARG0': cleft_index, 'ARG1': ep.args[arg_id], 'ARG2': ep.label }) new_rels.append(cleft_ep) new_vars[cleft_label] = {} new_vars[cleft_index] = { 'SF': 'prop', 'TENSE': 'pres', 'MOOD': 'indicative', 'PROG': '-', 'PERF': '-' } for i, hc in enumerate(new_hcons): if hc.lo == ep.label: new_hcons[i] = mrs.HCons(hc.hi, 'qeq', cleft_label) new_sem = copy_sem(orig_sem, index=cleft_index, rels=new_rels, hcons=new_hcons, variables=new_vars) if passive_ep: new_sem = passive(new_sem, ep, arg_id=arg_id)['passive: ' + arg_id] return new_sem
def swap_subj(orig_sem, index_ep): if index_ep.args.get('ARG2'): new_args = index_ep.args.copy() new_vars = copy_vars(orig_sem) new_rels = orig_sem.rels[:] # swap ARG1 and ARG2 new_args['ARG1'] = index_ep.args['ARG2'] new_args['ARG2'] = index_ep.args['ARG1'] new_ep = mrs.EP(predicate=index_ep.predicate, label=index_ep.label, args=new_args) # update number for agreement # new_pers = new_vars[index_ep.args['ARG2']]['PERS'] # new_num = new_vars[index_ep.args['ARG2']]['NUM'] # new_vars[index_ep.args['ARG0']]['PERS'] = new_pers # new_vars[index_ep.args['ARG0']]['NUM'] = new_num # update index_ep new_rels.remove(index_ep) new_rels.append(new_ep) new_sem = copy_sem(orig_sem, rels=new_rels, variables=new_vars) return {'swap subj/obj': new_sem}
def from_dmrs(d): """ Create an MRS by converting from DMRS *d*. Args: d: the input DMRS Returns: MRS Raises: MRSError when conversion fails. """ H = variable.HANDLE qeq = mrs.HCons.qeq vfac = variable.VariableFactory(starting_vid=0) top = vfac.new(H) if d.top is not None else None # do d.scopes() once to avoid potential errors if label generation # is ever non-deterministic _top, scopes = d.scopes() ns_args = d.arguments(types='xeipu') sc_args = d.scopal_arguments(scopes=scopes) id_to_lbl, id_to_iv = _dmrs_build_maps(d, scopes, vfac) # for index see https://github.com/delph-in/pydelphin/issues/214 index = None if not d.index else id_to_iv[d.index] hcons = [] if top is not None: hcons.append(qeq(top, _top)) icons = None # see https://github.com/delph-in/pydelphin/issues/220 rels = [] for node in d.nodes: id = node.id label = id_to_lbl[id] args = {mrs.INTRINSIC_ROLE: id_to_iv[id]} for role, tgt in ns_args[id]: args[role] = id_to_iv[tgt] for role, relation, tgt_label in sc_args[id]: if relation == scope.LHEQ: args[role] = tgt_label elif relation == scope.QEQ: hole = vfac.new(H) args[role] = hole hcons.append(qeq(hole, tgt_label)) else: raise mrs.MRSError('DMRS-to-MRS: invalid scope constraint') if node.carg is not None: args[mrs.CONSTANT_ROLE] = node.carg if d.is_quantifier(id) and mrs.BODY_ROLE not in args: args[mrs.BODY_ROLE] = vfac.new(H) rels.append( mrs.EP(node.predicate, label, args=args, lnk=node.lnk, surface=node.surface, base=node.base)) return mrs.MRS(top=top, index=index, rels=rels, hcons=hcons, icons=icons, variables=vfac.store, lnk=d.lnk, surface=d.surface, identifier=d.identifier)
def test__init__(self): with pytest.raises(TypeError): mrs.EP() with pytest.raises(TypeError): mrs.EP('_dog_n_1') mrs.EP('_dog_n_1', 'h3')