def jsrTemp(val, lang): if val == 0: return N("zero") if lang == "en" else N("zéro") if val < 0: return AdvP(A("minus") if lang == "en" else Adv("moins"), NO(abs(val))) if val <= 5: return AP(A("plus"), NO(val)) if lang == "en" else AdvP( Adv("plus"), NO(val)) return NO(val)
def havePolarity91(concept,roles,env,opts): if ":ARG2" in roles: pol=unquote(roles[":ARG2"].get_instance()) if pol=='-': env.push(AdvP(Adv("not"))) elif pol =='+': env.push(AdvP(Adv("yes"))) return addRoles(concept, roles, [":ARG2"], LexSem("polarity","SP",[],lambda:SP()), env, opts) return None
def makeAdv(lemma): adv=adverbs[lemma] if isinstance(adv,AdvP): return LexSem(lemma,"Adv",[":op1"],lambda op1:copy.deepcopy(adv).add(accPron(op1))) if isinstance(adv,Adv): return LexSem(lemma,"Adv",[":op1"],lambda op1:AdvP(copy.deepcopy(adv),accPron(op1))) if isinstance(adv,str): return LexSem(lemma,"Adv",[":op1"],lambda op1:Adv(adv) if op1==None else AdvP(Adv(adv),accPron(op1))) print("### strange adverb:"+str(adv)) return adverbs[lemma]
def apply(self, env=None, opts=None): if env == None: env = Env() if opts == None: opts = Options() ## process args from dictInfo building the list of arguments or None argV = [env.get(arg) if arg in env else None for arg in self.args] syntR = self.lambda_(*argV) if all([arg == None for arg in argV]) and len(env) == 0: return opts.apply(syntR) if isinstance(syntR, Terminal): if isinstance(syntR, A): if ":ARG1" in env: syntR = S(env.get(":ARG1"), VP(V("be"), syntR)) else: syntR = AP(syntR) elif isinstance(syntR, (Pro, Q, NO)): syntR = SP(syntR) elif isinstance(syntR, Adv): syntR = AdvP(syntR) elif isinstance(syntR, P): syntR = PP(syntR) else: print("** apply: strange syntR:%s:%s" % (type(syntR), syntR)) syntR = SP(syntR) ## add unprocessed args if len(env) > 0: syntR.add(env.get(":start"), 0) syntR.add(env.get(".*")) return opts.apply(syntR)
def pathRole(semR, env, opts): traceSyntR("pathRole", semR) concept = semR.get_concept() if concept == "past" and ":op1" in semR.roles: env.push(AdvP(Adv("past"), makeSyntR(semR.roles[":op1"]))) else: syntR = makeSyntR(semR) if isinstance(syntR, Pro): syntR.lemma = "me" else: if isinstance(syntR, (Adv, P, PP)): env.push(syntR) else: env.push(PP(P("via"), syntR))
P("en"), N("matinée"))) }, "b": { "en": lambda t, u: S( Adv("low"), u.a(","), P("with"), temp_trend("en", "rise", t, PP(P("by"), N("morning")))), "fr": lambda t, u: S( N("minimum"), u.a(","), temp_trend("fr", "hausse", t, PP(P("en"), N("matinée")))) }, "c": { "en": lambda t, p: temp_trend("en", "rise", t, p).add( AdvP(Adv("then"), A("steady"))), "fr": lambda t, p: temp_trend("fr", "hausse", t, p).add( PP(P("pour"), Adv("ensuite"), V("demeurer").t("b"), A("stable"))) }, "d": { "en": lambda t, p: temp_trend("en", "rise", t, p).add( AdvP(Adv("then"), V("rise").t("pr"), Adv("slowly"))), "fr": lambda t, p: temp_trend("fr", "hausse", t, p).add( AdvP(Adv("puis"), NP(N("hausse"), A("graduel")))) }, "e": {
conjunctions["sum-of"]=op16("and") conjunctions["ratio-of"]=op12("ratio-of",NP(N("ratio"),P("of")),"to") conjunctions["difference-of"]=op16("minus") conjunctions["quotient-of"]=op16("divided by") conjunctions["power-of"]=op16("to the power of") conjunctions["root-of"]= LexSem("root-of","P",[":op1",":op2"], lambda op1,op2:NP(NO(op2[0].lemma).dOpt({"ord":True}),N("root"),PP(P("of"),op1))) conjunctions["logarithm-of"]= LexSem("log-of","P",[":op1",":op2"], lambda op1,op2:NP(N("logarithm"),PP(P("of"),op1,N("base").a(":"),op2))) def isConjunction(lemma):return lemma in conjunctions def makeCP(lemma): return conjunctions[lemma] ### Adverbs adverbs["et-cetera"]=AdvP(Adv("etc")) adverbs["at-least"]=AdvP(P("at"),Adv("least")) adverbs["next-to"]=AdvP(Adv("next"),P("to")) adverbs["kind-of"]=AdvP(Q("kind of")) adverbs["away"]=AdvP(Adv("away"),P("from")) delCat(adverbs,"between") delCat(adverbs,"near") delCat(adverbs,"this") delCat(adverbs,"that") delCat(adverbs,"after") # keep only prep delCat(adverbs,"this") def isAdverb(lemma):return lemma in adverbs def makeAdv(lemma): adv=adverbs[lemma] if isinstance(adv,AdvP):
def quantRole(semR, env, opts): def processSimpleQuant(concept): simpleQuants = { # concept: string to add to the start, boolean to indicate if plural "most": ["most of", True], "more": ["more", True], "numerous": ["numerous", True], "many": ["many", True], "multiple": ["multiple", True], "few": ["few", True], "some": ["some", False], "such": ["a sack of", False], "last": ["last", False], "one": ["one", False], "entire": ["entire", False], "lot": ["a lot", False], "sack": ["a sack of", False] } if concept in simpleQuants: if concept == "lot": env.push(Q(simpleQuants[concept][0])) else: env.unshift(Q(simpleQuants[concept][0])) if simpleQuants[concept][1]: opts.add("n", "p") return True return False # fuzzy operators from https://www.isi.edu/~ulf/amr/lib/popup/quantities.html#non-exact-quantities # role :op1 # about, above, almost, approximately, around, at-least, at-most, below, close-to, couple, few, less-than, # lot, many, more-than, multiple, nearly, no-more-than, over, roughly, several, some, under, up-to # number is dealt separately def fuzzyQuant1(concept): fuzzyQuants = [ 'about', 'above', 'almost', 'approximately', 'around', 'at-least', 'at-most', 'below', 'close-to', 'couple', 'few', 'less-than', 'lot', 'many', 'more-than', 'nearly', 'no-more-than', 'over', 'roughly', 'several', 'some', 'under', 'up-to' ] if concept == "multiple": env.unshift(Q("multiple")) env.push(Q("of")) if ":op1" in semR.roles: env.push(makeSyntR(semR.roles[":op1"])) return True if concept in fuzzyQuants and ":op1" in semR.roles: env.put( ":D", AP(Q(generateConceptWord(concept)), makeSyntR(semR.roles[":op1"]))) opts.add("n", "p") return True return False def fuzzyQuant2(concept): roles = semR.get_roles() if ":op1" in roles and ":op2" in roles: env.put( ":D", PP(P("between"), makeSyntR(roles[":op1"]), C("and"), makeSyntR(roles[":op2"]))) return True return False traceSyntR("quantRole", semR) concept = semR.get_concept() roles = semR.roles if len(roles) == 0 and processSimpleQuant(concept): return if fuzzyQuant1(concept): return if fuzzyQuant2(concept): return if isinstance(concept, str) and re.match(r'.+-quantity$', concept): env.unshift( PP(specialConcept.quantity(concept, roles, Env(), Options()), P("of"))) return if concept == "amr-unknown": env.unshift(AdvP(Adv("how"), Adv("much"))) opts.add("a", "?") else: syntR = makeSyntR(semR, False) if isinstance(syntR, NO): env.put(":D", syntR) else: env.unshift(PP(syntR, P("of")))
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