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 scaleRole(semR, env, opts): traceSyntR("scaleRole", semR) semR_c = semR.get_concept() if (semR_c == "celsius"): env.push(PP(makeSyntR(semR), N("degree"), Q("Celsius"))) else: env.push(PP(P("on"), D("the"), makeSyntR(semR), N("scale")))
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 title_block(wInfo, lang): issueDate = wInfo.get_issue_date() noSecond = {"second": False} if lang == "en": s1 = S( NP(N("forecast").n("p")), VP( V("issue").t("pp"), PP( P("by"), Q("jsRealB"), DT(issueDate).dOpt(noSecond), P("for"), CP( C("and"), N("today"), DT(issueDate + datetime.timedelta(days=1)).dOpt( {"rtime": issueDate}))))) s2 = S( NP(D("the"), D("next"), V("schedule").t("pp"), N("forecast").n("p")), VP( V("be").t("f"), V("issue").t("pp"), DT(wInfo.get_next_issue_date()).dOpt(noSecond))) else: s1 = S( NP(N("prévision").n("p")), VP( V("émettre").t("pp"), PP( P("par"), Q("jsRealB"), DT(issueDate).dOpt(noSecond), P("pour"), CP( C("et"), Adv("aujourd'hui"), DT(issueDate + datetime.timedelta(days=1)).dOpt( {"rtime": issueDate}))))) s2 = S( NP(D("le"), A("prochain").pos("pre"), N("prévision").n("p")), VP( V("être").t("f"), V("émettre").t("pp"), DT(wInfo.get_next_issue_date()).dOpt(noSecond))) return "\n".join([realize(s1, lang, False), realize(s2, lang, False)])
def jsrDayPeriod(hour,lang): isTomorrow=hour>23 hour=hour%24 for (s,e,jsrExp) in dayPeriods: if hour in range(s,e): exp=jsrExp[lang]() if isTomorrow: return exp.add(N("tomorrow" if lang=="en" else "demain"),0) elif s!=18: return exp.add(D("this" if lang=="en" else "ce"),0)
def temp_trend(lang, trend, goalTemp, when): if lang == "en": return S( N("temperature"), VP(V(trend).t("pr"), PP(P("to"), jsrTemp(goalTemp, lang)), when)) else: return S( NP(N("température").n("p"), PP(P("à"), NP(D("le"), N(trend)))), PP(P("pour"), V("atteindre").t("b"), jsrTemp(goalTemp, lang), when))
def makePoss(s): if isinstance(s,str): if s in pronounOptions: return addOptions(D("my"),pronounOptions[s]) ## IT MUST return a Terminal not a LexSem else: print("makePoss: unknown s:"+str(s)) elif isinstance(s,Pro): s.terminal="D" s.lemma="my" return s
def frequencyRole(semR, env, opts): traceSyntR("frequencyRole", semR) concept = semR.get_concept() instance = semR.get_instance() if concept == None and is_number(instance): if instance == 1: env.push(Adv("once")) elif instance == 2: env.push(D("twice")) else: env.push(NP(NO(instance), N("time"))) else: env.push(makeSyntR(semR))
def jsrHour(h,lang): if h in range(0,6): return PP(P("during"),NP(D("the"),N("night"))) if lang=="en" else \ PP(P("durant"),NP(D("le"),N("nuit"))) if h in range(6,11): return PP(P("in"),NP(D("the"),N("morning"))) if lang=="en" else \ NP(D("le"),N("matin")) if h in range(11,14): return PP(P("around"),N("noon")) if lang=="en" else \ PP(P("vers"),N("midi")) if h in range(14,18): return PP(P("in"),NP(D("the"),N("afternoon"))) if lang=="en" else \ PP(P("durant"),NP(D("le"),N("après-midi"))) if h in range(18,24): return PP(P("in"),NP(D("the"),N("evening"))) if lang=="en" else \ PP(P("dans"),NP(D("le"),N("soirée")))
def quantity(concept,roles,env,opts): traceSyntR("quantity",concept) if ":quant" in roles: qty=makeSyntR(roles[":quant"]) if ":unit" in roles: unit=makeSyntR(roles[":unit"]) if isinstance(unit,Pro): # get the referenced unit... unit=makeSyntR(roles[":unit"].instance) if isinstance(unit,NP) and qty!=None and isinstance(qty,NO): unit.elements[0]=qty qty=unit elif isinstance(unit,Phrase): qty=unit.add(qty) else: qty=SP(unit,qty) elif len(roles)>1 and isinstance(qty,Terminal): qty=NP(qty) if ":scale" in roles: qty.add(PP(P("on"),makeSyntR(roles[":scale"]),N("scale"))) if ":*:ARG1" in roles: qty.add([D("the"),N("quantity"),Pro("that")]) env.push(qty) return addRoles(concept,roles,[":quant",":unit",":scale"],LexSem("qty","SP",[],lambda:SP()),env,opts)
def simpleModNoun(concept, neg): ## check for "simple" cases if concept in [ "all", "many", "both", "no", "too", "any", "other", "some", "one", "kind-yy", "then", "such" ]: env.put(":D", Q("kind of" if concept == "kind-yy" else concept)) if neg: env.unshift(Q("not")) if concept in ["all", "many", "both"]: opts.add("n", "p") elif concept in determiners: env.put(":D", D(concept)) elif is_number(concept): env.push(NO(concept)) elif isNoun(concept): ## prefix the :mod for nouns newNoun = N(nouns[concept].lemma) # keep only noun from NP if ":A" in env: env.insertAfter(":A", ":A", newNoun) else: env.put(":A", newNoun) if neg: env.insertBefore(":A", ":A", Q("non")) elif isAdjective(concept): ## postfix the :mod for adjectives newAdj = A(adjectives[concept].lemma) if ":A" in env: env.insertAfter(":A", ":A", newAdj) else: env.put(":A", newAdj) if neg: env.insertBefore(":A", ":A", Q("non")) elif isAdverb(concept): env.put(":D", Adv(concept)) elif isVerb(concept): env.push(V(re.sub(r"-\d+$", '', concept)).t("pr")) elif semR.roles.areEmpty(): ## equivalent to processSimpleModOther env.push(Q(generateConceptWord(concept))) else: return False return True
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)
from jsRealBclass import N,A,Adv,V,D,P,C,DT,NO,Q, NP,AP,AdvP,VP,S,PP,CP from Realization.common import realize, jsrDayPeriod sky_condition_terminology = { ## types of sky conditions "c1":{"en":(AP(A("sunny")),AP(A("clear"))), "fr":(AP(A("ensoleillé")),AP(A("dégagé")))}, "c2":{"en":(AP(Adv("mainly"),A("sunny")),NP(Q("a"),D("few"),N("cloud").n("p"))), "fr":(AP(Adv("généralement"),A("ensoleillé")),NP(D("quelque"),N("nuage").n("p")))}, "c3":{"en":(NP(D("a"),N("mix"),PP(P("of"),CP(C("and"),N("sun"),N("cloud").n("p")))), AP(Adv("partly"),A("cloudy"))), "fr":(NP(N("alternance"),CP(C("et"),PP(P("de"),N("soleil")),PP(P("de"),N("nuage").n("p")))), AP(Adv("partiellement"),A("couvert")))}, "c4":{"en":(AP(Adv("mainly"),A("cloudy")),), "fr":(AP(Adv("généralement"),A("nuageux")),)}, "c5":{"en":(AP(A("cloudy")),), "fr":(AP(A("nuageux")),)}, "c6":{"en":(AP(A("overcast")),), "fr":(AP(A("couvert")),)}, "c7":{"en":(NP(V("increase").t("pr"),N("cloudiness")),), "fr":(NP(N("ennuagement")),)}, "c8":{"en":(NP(N("clearing")),), "fr":(NP(N("dégagement")),)}, } def sky_condition(mc,period,lang): previous_conditions=[] jsrExprs=[] def addNoRepeat(c,dn,period=None): # avoid generating same sentence twice if c not in previous_conditions: if len(sky_condition_terminology[c][lang])==1:dn=0
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": { "en": lambda t, p: temp_trend("en", "rise", t, p).add( AdvP(Adv("then"), V("fall").t("pr"))), "fr": lambda t, p: temp_trend("fr", "hausse", t, p).add( PP(P("pour"), Adv("ensuite"), V("être").t("b"), PP(P("à"), NP(D("le"), N("baisse"))))) }, }, "day": { "a": { "en": lambda t, _: temp_trend("en", "fall", t, PP( P("by"), N("afternoon"))), "fr": lambda t, _: temp_trend("fr", "baisse", t, PP(P("en"), N("après-midi"))) }, "b": { "en": lambda t, u: S( Adv("high"), u.a(","), P("with"),
# coding=utf-8 from jsRealBclass import N,A,Pro,D,Adv,V,C,P,DT,NO,Q, NP,AP,AdvP,VP,CP,PP,S,SP, jsRealB, Terminal, Phrase,Constituent from amrDicoGen import pp,optD, nouns,adjectives,verbs,adverbs,prepositions, verbalizations ,morphVerbalizations from lexicalSemantics import LexSem, nounInfo,adjInfo,pp,optD import utils import copy,re ## function to build a noun syntR dn = lambda det,arg: NP(D(det),arg) if arg!=None else None cn = lambda conj,arg:S(C(conj),arg) if arg!=None else None def addOptions(syntR,opts): for (key,val) in opts.items(): getattr(syntR, key)(val) return syntR def delCat(dct,w): if w in dct:del dct[w] ### Pronouns pronounOptions = { "I":{"pe":1}, "i":{"pe":1}, "me":{"pe":1}, "you":{"pe":2}, "he":{"pe":3,"g":"m"}, "she":{"pe":3,"g":"f"}, "it":{"pe":3,"g":"n"}, "we":{"pe":1,"n":"p"}, "they":{"pe":3,"n":"p"},
from jsRealBclass import jsRealB, N, A, Adv, V, D, P, C, DT, NO, Q, NP, AP, AdvP, VP, S, PP, CP from Realization.common import realize, jsrDayPeriod, jsrHour, get_max_term, get_min_term, get_term_at ### Pubpro sec 2.3.4 ## vents : start end direction modif? speed value exception? # e | nil | n | ne | nw | w | ely | nly | nely | nwly | wly | sly| sely | swly | sly | sely | sw | vrbl jsrWindDirection = { "e": { "en": Adv("east"), "fr": NP(D("le"), N("est")), "deg": 90 }, "n": { "en": Adv("north"), "fr": NP(D("le"), N("nord")), "deg": 0 }, "ne": { "en": Adv("northeast"), "fr": NP(D("le"), N("nord-est")), "deg": 45 }, "nw": { "en": Adv("northwest"), "fr": NP(D("le"), N("nord-ouest")), "deg": 315 }, "w": { "en": Adv("west"), "fr": NP(D("le"), N("ouest")), "deg": 290
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 len(env) > 0: ## add unprocessed args syntR.add(env.get(":start"), 0) syntR.add(env.get(".*")) return opts.apply(syntR) pp = lambda prep, arg: PP(P(prep), arg) if arg != None else None optD = lambda det: det if det != None else D("the") verbs['give-01'] = LexSem( "V", "give", [":ARG0", ":ARG1", ":ARG2"], lambda arg0, arg1, arg2: S(arg0, VP(V("give"), arg1, pp("to", arg2)))) nouns['envelope'] = LexSem("envelope", "N", [":D", ":A"], lambda d, a: NP(optD(d), a, N("envelope"))) nouns['boy'] = LexSem("boy", "N", [":D", ":A"], lambda d, a: NP(optD(d), a, N("boy"))) nouns['girl'] = LexSem("girl", "N", [":D", ":A"], lambda d, a: NP(optD(d), a, N("girl"))) adjectives["little"] = LexSem("little", "A", [], lambda: A("little")) adjectives["nice"] = LexSem("nice", "A", [], lambda: A("nice"))
def pcpn(type,action,tense,moment,quantity=None,unit=None): return S(type, VP(V(action).t(tense), CP(PP(P("in"),NP(D("the"),N(moment))), None if quantity==None else NP(N("amount"),NP(NO(quantity),unit)))))
isTomorrow=hour>23 hour=hour%24 for (s,e,jsrExp) in dayPeriods: if hour in range(s,e): exp=jsrExp[lang]() if isTomorrow: return exp.add(N("tomorrow" if lang=="en" else "demain"),0) elif s!=18: return exp.add(D("this" if lang=="en" else "ce"),0) dayOnly={"day":True,"year": False, "month": False, "date": False,"hour":False,"minute":False, "second": False,"det":False} periodNames = { "today":{"en":lambda _:N("today"), "fr":lambda _:Adv("aujourd'hui")}, "tonight":{"en":lambda _:N("tonight"), "fr":lambda _:CP(C("et"),NP(D("ce"),N("soir")),NP(D("ce"),N("nuit")))}, "tomorrow":{"en":lambda d:DT(d).dOpt(dayOnly), "fr":lambda d:DT(d).dOpt(dayOnly)}, "tomorrow_night":{"en":lambda d:NP(DT(d).dOpt(dayOnly),N("night")), "fr":lambda d:CP(C("et"),NP(DT(d).dOpt(dayOnly),N("soir")),NP(N("nuit")))} } ### time generation def jsrHour(h,lang): if h in range(0,6): return PP(P("during"),NP(D("the"),N("night"))) if lang=="en" else \ PP(P("durant"),NP(D("le"),N("nuit"))) if h in range(6,11): return PP(P("in"),NP(D("the"),N("morning"))) if lang=="en" else \ NP(D("le"),N("matin"))
def relative(concept, syntR): pronoun = Pro("who") if concept in amrDico.gender else D("that") if isinstance(syntR, S): return syntR.add(pronoun, 0) return predicate(pronoun, syntR)
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