def paperExample(): from jsRealBclass import jsRealB, N,A,Adv,V,D,P,C,DT,NO,Q, NP,AP,AdvP,VP,S,SP,PP,CP ## example function used in the paper 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))))) print(realize(pcpn(N("flurry").n("p"),"begin","p","morning",2,N("foot")),"en")) print(realize(pcpn(N("rain"),"begin","p","evening",1,N("inch")),"en")) print(realize(pcpn(N("snow"),"stop","pr","evening"),"en")) print(realize(pcpn(NP(V("freeze").t("pr"),N("drizzle")),"start","f","morning"),"en"))
def temperature(wInfo, period, lang): temperature_terms = wInfo.get_temperature(period) if temperature_terms == None: return None maxTemp = get_max_term(temperature_terms, 0).infos[0] minTemp = get_min_term(temperature_terms, 0).infos[0] dn = "night" if period in ["tonight", "tomorrow_night"] else "day" tempVals = wInfo.get_temperature_values(period) periodName = periodNames[period][lang](wInfo.get_issue_date()) # checking for an abnormal temperature trend, either # positive change of least 3°C during the night # negative change of last 3°C during the day (t1,t2,i1)=(maxTemp,minTemp,tempVals.index(minTemp)) if dn=="night" else\ (minTemp,maxTemp,tempVals.index(maxTemp)) # print("dn=",dn,"t1=",t1,"t2=",t2) if t1 >= t2 + 3: # abnormal change time if i1 <= 1: return realize(jsrAbnormal[dn]["a"][lang](t1, periodName), lang, False) else: if i1 < 6: # abnormality occurs during the first 6 hours of the period rest = tempVals[i1:] if all([abs(t - t1) <= 2 for t in rest]): # c) remains +/- 2 for the rest of the period return realize(jsrAbnormal[dn]["c"][lang](t1, periodName), lang, False) elif any([t - t1 > 2 for t in rest]): # d) rises more than 2 for the rest return realize(jsrAbnormal[dn]["d"][lang](t1, periodName), lang, False) elif any([t1 - t > 2 for t in rest]): # e) falls more than 2 for the rest (this should never happen!!!) return realize(jsrAbnormal[dn]["e"][lang](t1, periodName), lang, False) else: # b) low temperature after the beginning (but no special case) return realize( jsrAbnormal[dn]["b"][lang](t1, jsrTemp(t2, lang)), lang, False) # normal case res = [] if lang == "en": # output maximum temperature res.append(realize(S(Adv("high"), jsrTemp(maxTemp, "en")), "en", False)) else: res.append( realize(S(N("maximum"), jsrTemp(maxTemp, "fr")), "fr", False)) if minTemp < maxTemp - 2: # output minimum if it differs significantly from the maximum if lang == "en": res.append( realize(S(Adv("low"), jsrTemp(minTemp, "en")), "en", False)) else: res.append( realize(S(N("minimum"), jsrTemp(minTemp, "fr")), "fr", False)) return " ".join(res)
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 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 forecast_text(wInfo,lang): paragraphs=[] for period in wInfo.get_periods(): if lang=="en" : wInfo.show_data(period) # clearSavedJsrIO() paragraphs.append( textwrap.fill( realize(periodNames[period][lang](wInfo.get_issue_date()+timedelta(days=1)).cap(True),lang,False) +" : "+forecast_period(wInfo, period, lang) ,width=70,subsequent_indent=" ") ) # for (jsrInput,realization) in getSavedJsrIO(): # print(jsrInput) # print(realization) # print(" ---") return "\n".join(paragraphs)
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 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 jsrExpr=sky_condition_terminology[c][lang][dn] if period!=None:jsrExpr.add(period) jsrExprs.append(jsrExpr) previous_conditions.append(c) sc_terms=mc.get_sky_cover(period) if sc_terms==None: return None for sc_term in sc_terms: valStart=sc_term.infos[0] valEnd =sc_term.infos[1] dayNight = 0 if period in ["today","tomorrow"] else 1 if valStart==valEnd: if valStart in [0,1]: addNoRepeat("c1",dayNight) if valStart in [2,3]: addNoRepeat("c2",dayNight) if valStart in [4,5,6]: addNoRepeat("c3",dayNight) if valStart in [7,8]: addNoRepeat("c4",dayNight) if valStart in [9]: addNoRepeat("c5",dayNight) if valStart in [10]: addNoRepeat("c6",dayNight) elif valStart in [0,1,2,3] and valEnd in [7,8,9,10]: addNoRepeat("c7",dayNight,jsrDayPeriod(sc_term.start,lang)) elif (valStart in [7,8,9,10] and valEnd in [0,1,2,3]) or \ (valStart in [5,6] and valEnd in [0,1]): addNoRepeat("c8",dayNight,jsrDayPeriod(sc_term.start,lang)) return " ".join(realize(jsrExpr,lang) for jsrExpr in jsrExprs)
def showEnFr(jsrExprEN, jsrExprFR): print( f" %s\n %s\n" % (realize(jsrExprEN, "en", False), realize(jsrExprFR, "fr", False)))
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)
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) if __name__ == '__main__': ## exercise all precipitationTypes expressions for pt in precipitationTypes: print("%-20s : %-20s"%(realize(precipitationTypes[pt]["en"],"en",False), realize(precipitationTypes[pt]["fr"],"fr",False)))
valEnd =sc_term.infos[1] dayNight = 0 if period in ["today","tomorrow"] else 1 if valStart==valEnd: if valStart in [0,1]: addNoRepeat("c1",dayNight) if valStart in [2,3]: addNoRepeat("c2",dayNight) if valStart in [4,5,6]: addNoRepeat("c3",dayNight) if valStart in [7,8]: addNoRepeat("c4",dayNight) if valStart in [9]: addNoRepeat("c5",dayNight) if valStart in [10]: addNoRepeat("c6",dayNight) elif valStart in [0,1,2,3] and valEnd in [7,8,9,10]: addNoRepeat("c7",dayNight,jsrDayPeriod(sc_term.start,lang)) elif (valStart in [7,8,9,10] and valEnd in [0,1,2,3]) or \ (valStart in [5,6] and valEnd in [0,1]): addNoRepeat("c8",dayNight,jsrDayPeriod(sc_term.start,lang)) return " ".join(realize(jsrExpr,lang) for jsrExpr in jsrExprs) if __name__ == '__main__': ## exercise all sky_condition_terminology expressions for c in sky_condition_terminology: for lang in ["en","fr"]: jsrExprs=sky_condition_terminology[c][lang] for jsrExpr in jsrExprs: print(realize(jsrExpr,lang))