def politeRole(semR, env, opts): traceSyntR("politeRole", semR) instance = unquote(semR.get_instance()) if instance == "+": env.unshift(Q("Please")) elif instance == "-": env.unshift(Q("F...")) else: errorSyntR(":polite strange value:%s" % instance)
def comparedToRole(semR, env, opts): traceSyntR("comparedToRole", semR) global comp if comp == "co": env.push(PP(P("than"), makeSyntR(semR))) return if comp == "su": env.push(PP(P("in"), makeSyntR(semR))) return errorSyntR(" :compared-to without :degree:%s in %s" % (semR, semR.parent))
def haveRelRole91(concept,roles,env,opts): traceSyntR("haveRelRole91",concept) # syntR_A=makeSyntR(roles[":ARG0"]) if ":ARG0" in roles else None syntR_B=makeSyntR(roles[":ARG1"]) if ":ARG1" in roles else None if ":ARG2" in roles: relation=makeSyntR(roles[":ARG2"]) if isinstance(syntR_B,Pro) and isinstance(relation,NP): relation.elements[0]=makePoss(syntR_B.lemma) return addRoles(concept, roles, [":ARG1",":ARG2"], LexSem("relation","NP",[":ARG0"],lambda arg0:S(arg0,VP(V("be"),relation))), env, opts) if syntR_B!=None: dictInfo=LexSem("have-relation","S",[":ARG0",":ARG3",":ARG4"], lambda arg0,arg3,arg4:S(arg0,VP(V("be"),relation,syntR_B,arg3,arg4))) return addRoles(concept, roles, [":ARG1",":ARG2"], dictInfo, env, opts) else: return addRoles(concept, roles, [":ARG2"],LexSem("rel-role","NP",[],lambda:relation), env, opts) else: errorSyntR("haveRelRole91 with no :ARG2:\n%s"%concept) return Q("*rel*")
def instance2SyntR(semR): traceSyntR("instance2dsr", semR) instance = semR.instance if isinstance(instance, SemanticRep): myRole = semR.get_my_role() amrRef = instance refRole = amrRef.get_my_role() refConcept = amrRef.get_concept() # print("myRole:%s refRole:%s refConcept:%s"%(myRole,refRole,refConcept)) if isNoun(refConcept) or specialConcept.isSpecialConcept(refConcept): if myRole == ":ARG0" and refRole != None: pronoun = Pro("I") elif myRole == ":ARG1": # is object the same as the subject? parent = semR.get_parent() parentRoles = parent.get_roles() if ":ARG0" in parentRoles and ( parentRoles[":ARG0"] == amrRef or instance == parentRoles[":ARG0"].instance): pronoun = Pro("myself") else: pronoun = Pro("I") else: pronoun = Pro("me") return pronoun.pe(3).g(gender[refConcept] if refConcept in gender else "n") elif isPronoun(refConcept): pronoun = Pro("I") return addOptions(pronoun, pronounOptions[refConcept] ) if refConcept in pronounOptions else pronoun elif isVerb(refConcept): return VP(V(re.sub(r"-\d+$", "", refConcept)).t("b")) else: # clean the referenced concept and quote it return Q(generateConceptWord(refConcept)) elif is_number(instance): return NO(unquote(instance)) elif instance[0] == '"': return Q(unquote(instance)) elif instance in ['-', '+']: return instance else: errorSyntR(instance + " :undefined instance") return Q(instance)
def modeRole(semR, env, opts): traceSyntR("modeRole", semR) modeType = semR.get_instance() if modeType == "interrogative": opts.add("typ", {"int": "yon"}) elif modeType == "expressive": opts.add("a", "!") elif modeType == "imperative": opts.add("t", "ip") if ":ARG0" in env: del env[":ARG0"] if semR.parent != None: pRoles = semR.parent.roles if ":ARG0" in pRoles: if pRoles[":ARG0"].concept == "we": env.unshift(Q("let's")) if isPronoun(pRoles[":ARG0"].concept): del semR.parent.roles[":ARG0"] else: errorSyntR("unknown mode:%s" % modeType)
def haveQuant91(concept,roles,env,opts): traceSyntR("haveQuant91",concept) if ":ARG1" in roles: quant=makeSyntR(roles[":ARG1"]) else: errorSyntR("haveQuant91 without entity:%s"%concept) quant=SP(Q("*entity*")) if isinstance(quant,Terminal): quant=SP(quant) if ":ARG2" in roles: quant.add(makeSyntR(roles[":ARG2"]),0) else: quant.add(NP(D("the"),N("number"),P("of")),0) if ":ARG3" in roles: quant.add(VP(V("be"),makeSyntR(roles[":ARG3"]))) if ":ARG4" in roles: quant.add(PP(P("than"), makeSyntR(roles[":ARG4"]))) if ":ARG5" in roles: quant.add(PP(P("of"),makeSyntR(roles[":ARG5"]))) if ":ARG6" in roles: quant.add(PP(P("for"),makeSyntR(roles[":ARG6"]))) return addRoles(concept, roles, [":ARG1",":ARG2",":ARG3",":ARG4",":ARG5",":ARG6"], LexSem("qty","NP",[],lambda:quant), env, opts)
def ordRole(semR, env, opts): traceSyntR("ordRole", semR) if semR.get_concept() == "ordinal-entity": env.put(":A", makeSyntR(semR)) else: errorSyntR("ord without ordinal-entity:%s" % semR)
def haveDegree91(concept,roles,env,opts): traceSyntR("haveDegree91",concept) subject=None attribute=None comp="" quant=None if ":ARG1" in roles: arg1=roles[":ARG1"] # print("haveDegree1:",arg1.instanceIsRef()) if not arg1.instanceIsRef(): # do not follow link for subject subject=makeSyntR(arg1) if ":ARG2" in roles: concept=roles[":ARG2"].concept attribute=makeSyntR(roles[":ARG2"]) # HACK: remove spurious pronouns created by starrole if isinstance(attribute, AP) and len(attribute.elements)>1 and isinstance(attribute.elements[1],Pro): del attribute.elements[1] if len(attribute.elements)==1: attribute=attribute.elements[0] else: errorSyntR("have-degree-91 without :ARG2:%s"%concept) return Q("degree-91") if ":mod" in roles: if isinstance(attribute,Terminal): attribute=AP(attribute) attribute.add(makeSyntR(roles[":mod"])) if ":ARG3" in roles: rolesARG3=roles[":ARG3"] deg=rolesARG3.get_concept() moreRoles=rolesARG3.get_roles() if moreRoles!=None and len(moreRoles)>0: if ":quant" in moreRoles: quant=makeSyntR(moreRoles[":quant"]) else: attribute.add(makeSyntR(moreRoles[0][1])) if deg=="more": attribute=attribute.f("co") if quant!=None: attribute=NP(quant,attribute) quant=None comp="co" elif deg=="most": if isinstance(attribute,A): attribute=AP(D("the"),attribute.f("su")) else: attribute=AP(attribute,D("the"),Adv("most")) comp="su" elif deg in ["too","so","less"]: attribute=AdvP(Adv(deg),attribute) if deg=="less": comp="co" elif quant!=None: attribute=AP(attribute,quant) quant=None elif deg=="equal": attribute=AdvP(Adv("as"),attribute,Adv("as")) elif deg=="times": if quant!=None: quant=NP(quant,N("time")) attribute=attribute.f("co") comp="co" else: attribute=AdvP(attribute,Adv(deg)) if ":ARG4" in roles: syntR4=makeSyntR(roles[":ARG4"]) if comp=="co": attribute=PP(attribute,P("than"),syntR4) if quant!=None: attribute.add(quant,0) elif comp=="su": attribute=AP(attribute,Adv("in"),syntR4) else: attribute.add(syntR4) if ":ARG5" in roles: attribute.add(makeSyntR(roles[":ARG5"])) if ":ARG6" in roles: attribute=NP(attribute,makeSyntR(roles[":ARG6"])) # print("have-degree-91:subject:",subject) while ":li" in roles: semR=roles[":li"] del roles[":li"] ## analogous to roleProcessing.lirole semR_i=unquote(semR.get_instance()) if semR_i=="-1": attribute.add(Adv("lastly"),0) elif semR_i=="1": attribute.add(Adv("first"),0) elif is_number(semR_i): attribute.add(Q("("+str(semR_i)+")"),0) elif semR_i[0]=='"': attribute.add(Q("("+semR_i+")"),0) restRoles=addRoles(concept, roles, [":ARG1",":ARG2",":ARG3",":ARG4",":ARG5",":ARG6",":li",":mod",":polarity"], LexSem("degree","SP",[],lambda:SP()),env, opts) if len(restRoles.elements)>0: attribute=ensurePhrase(attribute).add(restRoles) if subject!=None: attribute=predicate(subject,attribute) if hasNegPolarity(roles): return attribute.typ({"neg":True}) else: if hasNegPolarity(roles): return AdvP(Adv("not"),attribute) return attribute