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 precipitation(wInfo,period,lang): jsrExprs=[] prob_terms=wInfo.get_precipitation_probabilities(period) type_terms=wInfo.get_precipitation_type(period) accum_terms=wInfo.get_precipitation_accumulation(period) for prob_term in prob_terms: prob_val=round(prob_term.infos[0]/10)*10 type_term=get_term_at(type_terms,prob_term.start) if type_term!=None and prob_val>=30: # interesting precipitation if prob_val <= 70 and prob_val!=50: # show probability if lang=="en": prob=NP(NO(prob_val),Q("percent"),N("chance").n("s"),P("of")) else: prob=NP(NO(prob_val),Q("pour cent"),P("de"),N("probabilité").n("s"),P("de")) timePeriod=None else: # probability >= 80% prob=None # indicate beginning or ending start=prob_term.start end=prob_term.end if wInfo.is_in_period(start,period): timePeriod=VP(V("begin" if lang=="en" else "débuter").t("pr"),jsrHour(start%24,lang)) elif wInfo.is_in_period(end,period): timePeriod=VP(V("end" if lang=="en" else "finir").t("pr"),jsrHour(end%24,lang)) else: timePeriod=None jsrExpr=NP(prob,precipitationTypes[type_term.infos[0]][lang],timePeriod) amount_term=get_term_at(accum_terms,prob_term.start) if amount_term!=None: # check for significant amount pcpnType=amount_term.infos[0] amount=amount_term.infos[1] jsrAmount=None if pcpnType=="rain" and amount>=25: jsrAmount=NP(NO(round(amount)),Q("mm")) elif pcpnType=="snow" and amount>=2: jsrAmount=NP(NO(round(amount)),Q("cm")) if jsrAmount!=None: if lang=="en": jsrAmount.add(N("amount"),0) else: jsrAmount.add(N("accumulation"),0).add(P("de"),1) jsrExpr=SP(jsrExpr.a(","),jsrAmount) jsrExprs.append(jsrExpr) return " ".join(realize(jsrExpr,lang) for jsrExpr in jsrExprs)
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 uv_index(wInfo,period,lang): if period in ["tonight","tomorrow_night"]: # no UV index during the night return None uvi_terms=wInfo.get_uv_index(period) if uvi_terms==None:return None uvVal=uvi_terms[0].infos[0] # consider only the first uvi_term if uvVal<1: return None # too low uvVal=round(uvVal) if uvVal==0:return None for high,expr in uv_ranges: if uvVal<=high: return realize(NP(Q("UV index" if lang=="en" else "indice UV"), NO(uvVal),C("or" if lang=="en" else "ou"),expr[lang]), lang) return None
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 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
lambda op1,op2,op3,op4,op5,op6:CP(C(conj),op1,op2,op3,op4,op5,op6)) def op12(lemma,np,prep): return LexSem(lemma,"P",[":op1",":op2"], lambda op1,op2:SP(np,op1,pp(prep,op2))) conjunctions={} conjunctions["or"]=op16("or") conjunctions["and"]=op16("and") conjunctions["either"]=op16("either") conjunctions["product-of"]=op16("times") 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")
def wind(wInfo, period, lang): wind_terms = wInfo.get_wind(period) if wind_terms == None: return None lastSpeed = None lastDir = None jsrExprs = [] for wind_term in wind_terms: wSpeed = wind_term.infos[2] wDir = wind_term.infos[0] jsrExpr = S() # current expression if wSpeed >= 15 and wDir in jsrWindDirection: if lastSpeed != None and abs( wSpeed - lastSpeed) >= 20: # significant speed change lastSpeed = wSpeed if lang == "en": jsrExpr.add( VP(V("increase").t("pr"), PP(P("to"), NO(wSpeed)))) else: jsrExpr.add( VP(V("augmenter").t("pr"), PP(P("à"), NO(wSpeed)))) elif lastDir != None and dir_diff( wDir, lastDir): # significant direction change if lang == "en": jsrExpr.add( VP(V("become").t("pr"), jsrWindDirection[wDir][lang])) else: jsrExpr.add( VP( V("devenir").t("pr"), PP(P("de"), jsrWindDirection[wDir][lang]))) lastDir = wDir else: # realize wind and direction lastSpeed = wSpeed lastDir = wDir if lang == "en": jsrExpr.add( NP(N("wind"), jsrWindDirection[wDir][lang], NO(wSpeed), Q("km/h"))) else: jsrExpr.add( NP( N("vent").n("p"), PP(P("de"), jsrWindDirection[wDir][lang]), PP(P("de"), NO(wSpeed), Q("km/h")))) if len(wind_term.infos) > 3: # add gusting information gust = wind_term.infos[3] if gust.infos[0] == 'gust': if lang == "en": jsrExpr.add( VP( V("gust").t("pr"), PP(P("to"), NO(gust.infos[1])))) else: jsrExpr.add( PP( P("avec"), NP( N("rafale").n("p"), P("à"), NO(gust.infos[1])))) else: # add time information jsrExpr.add(jsrHour(wind_term.start, lang)) jsrExprs.append(jsrExpr) # add current expression to the list return " ".join(realize(jsrExpr, lang, False) for jsrExpr in jsrExprs)
(5, {"en":A("moderate"), "fr":A("modéré")}), (7, {"en":A("high"), "fr":A("élevé")}), (10, {"en":AP(Adv("very"), A("high")), "fr":AP(Adv("très"),A("élevé"))}), (1000,{"en":A("extreme"), "fr":A("extrême")})] def uv_index(wInfo,period,lang): if period in ["tonight","tomorrow_night"]: # no UV index during the night return None uvi_terms=wInfo.get_uv_index(period) if uvi_terms==None:return None uvVal=uvi_terms[0].infos[0] # consider only the first uvi_term if uvVal<1: return None # too low uvVal=round(uvVal) if uvVal==0:return None for high,expr in uv_ranges: if uvVal<=high: return realize(NP(Q("UV index" if lang=="en" else "indice UV"), NO(uvVal),C("or" if lang=="en" else "ou"),expr[lang]), lang) return None if __name__ == '__main__': def showEnFr(jsrExprEN,jsrExprFR): print(f" %s\n %s\n"%(realize(jsrExprEN,"en",False),realize(jsrExprFR,"fr",False))) ## exercise all kinds of uv_ranges for i in range(0,len(uv_ranges)): (val,jsrEnFr)=uv_ranges[i] showEnFr(NP(Q("UV"),N("index"),NO(val),C("or"),jsrEnFr["en"]), NP(N("indice"),Q("UV"),NO(val),C("ou"),jsrEnFr["fr"]))
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)))))