def possRole(semR, env, opts): traceSyntR("possRole", semR) if semR.get_concept() == "amr-unknown": env.push(PP(P("of"), Pro("whom"))) opts.add("a", "?") else: syntR = makeSyntR(semR) if isinstance(syntR, D): env.put(":D", syntR) elif isinstance(syntR, Pro): concept = semR.concept if concept != None: env.put(":D", makePoss(concept)) elif isinstance(semR.instance, SemanticRep.SemanticRep): ref = semR.instance.get_concept() poss = D("my") if isNoun(ref): g = nouns[ref]["g"] env.put(":D", poss.g(g if g != None else "n")) elif isPronoun(ref): poss.props = pronounOptions[ref] env.put( ":D", poss.g(pronounOptions[ref]["g"] if "g" in pronounOptions[ref] else "m")) else: env.put(":D", poss) else: env.put(":D", makePoss(syntR.lemma)) else: env.push(PP(P("of"), syntR))
def person(concept,roles,env,opts): traceSyntR("person",concept) ## with a single :named role ignore person if ":named" in roles and len(roles)==1: return makeSyntR(roles[":named"]) ## deal with frequent patterns associated with a person #### (p/person :ARG0-of (v / verb)) == ((p/person :*:ARG0 (v / verb :ARG0 p))) #### ==> find verbalization starARG0=roles[":*:ARG0"] if starARG0!=None: starARG0_ARG0=starARG0.roles[":ARG0"] if ":ARG0" in starARG0.roles else None if starARG0_ARG0!=None: if len(starARG0_ARG0.roles)==1: verb=starARG0_ARG0.get_concept() nom=getNominalization(verb) if nom!=None: return addRoles(concept, roles, [":*:ARG0"], nounInfo(nom), env, opts) if re.match(r"have-(org|rel)-role-91",starARG0.get_concept()) and len(starARG0.roles)==2: ##### shortcut : https://www.isi.edu/~ulf/amr/lib/amr-dict.html#shortcuts ### (p2 / person :ARG0-of (h / have-org-role-91 :ARG2 (m / mayor))) == ### [person,\p2,[':*:ARG0',['have-org-role-91',h,[':ARG0',p2], [':ARG2',[mayor,m]]] ==> ### mayor !!! starARG0_ARG2=starARG0.roles[":ARG2"] if starARG0_ARG2!=None: nom=makeSyntR(starARG0_ARG2) if isinstance(nom,(NP,N)): return addRoles(concept, roles, [":*:ARG0"], LexSem("person","NP",[],lambda:nom), env, opts) if re.match(r"have-(org|rel)-role-91",starARG0.get_concept()) and len(starARG0.roles)==3: ##### shortcut : https://www.isi.edu/~ulf/amr/lib/amr-dict.html#shortcuts ### (p / person ### :ARG0-of (h / have-rel-role-91 :ARG1 (i / i):ARG2 (g / mother))) == ### (p person [:*:ARG0 (h2 have-rel-role-91 [:ARG1 ^h ↑h2, ### :ARG2 (m mother [] ↑h2), ### :ARG0 ^p ↑h2] ↑p) ^_]) ### (his/her) mother !!! starARG0_ARG1=starARG0.roles[":ARG1"] starARG0_ARG2=starARG0.roles[":ARG2"] person=None if starARG0_ARG1!=None and starARG0_ARG2!=None and isNoun(starARG0_ARG2.get_concept()): conceptSyntR=makeSyntR(starARG0_ARG2) if starARG0_ARG1.instanceIsRef(): env.put(":D",makePoss(SemR2SyntR.instance2SyntR(starARG0_ARG1))) person=addRoles(concept, starARG0_ARG2.roles,[":ARG1",":ARG2"], LexSem("person","NP",[],lambda:conceptSyntR), env, opts) elif starARG0_ARG1.roles.areEmpty(): starARG0_ARG1concept=starARG0_ARG1.get_concept() if isNoun(starARG0_ARG1concept): env.put(":D",makeSyntR(starARG0_ARG1)) person=addRoles(concept,starARG0_ARG2.roles,[":ARG1",":ARG2",":ARG0"], LexSem("person","NP",[],lambda:conceptSyntR),env,opts) elif isPronoun(starARG0_ARG1concept): conceptSyntR.elements[0]=makePoss(starARG0_ARG1concept) person=addRoles(concept,starARG0_ARG2.roles,[":ARG1",":ARG2",":ARG0"], LexSem("person","NP",[],lambda:conceptSyntR),env,opts) if person!=None: return addRoles(concept, roles, [":*:ARG0"],LexSem("person","NP",[],lambda:person), env, opts) return namedEntity(concept,roles,env,opts)
def domainRole(semR, env, opts): traceSyntR("domainRole", semR) concept = semR.get_concept() if concept in determiners: env.put(":D", D(concept)) elif isPronoun(concept): env.unshift(SP(Pro(concept), V("be"))) else: env.push(VP(V("be"), makeSyntR(semR)))
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)