You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@alexander0042 Hope you're enjoying your holidays! I updated my weekly summaries code to reduce the number of ands it can create in the text. It still can create a lot of ands which there isn't much we can do about that.
I took a look at archives of the Dark Sky website and what they did seems pretty basic? I found one example of them saying rain throughout the week when not every day was forecasting rain and one where they used through but one day didn't forecast rain.
This code can now generate summaries like: Mixed precipitation today through Sunday and on Tuesday through Thursday, with high temperatures peaking at 6°C on Monday. which wasn't possible in the old code. I also fixed a few bugs but will likely need to be tested more as I've tested a few scenarios to see if it worked (It generates this for my location: Mixed precipitation on Sunday through Thursday, with high temperatures peaking at 6°C on Monday.
I also didn't realise that Dark Sky showed the different intensity levels in the weekly text so I added that in. I haven't changed the precipitation calculation locally so it'd need to be updated to match the changes you made. You can use either max intensity or average intensity in the calculations it doesn't really matter. It also calculates it based on the number of days which surpass the precipitation threshold. (So one day with 2.5 mm/h intensity will use Light rain instead of Drizzle)
Weekly Summary
fromcollectionsimportCounterfromitertoolsimportgroupbyfromoperatorimportitemgetterfromjavascriptimportrequiredefMost_Common(lst):
""" Finds the most common icon to use as the weekly icon Parameters: - lst (arr): An array of weekly icons Returns: - str: The most common icon in the lst. """data=Counter(lst)
returndata.most_common(1)[0][0]
defcalculate_text(precipIntensity, prepIntensityUnit, precipType):
""" Calculates the textual precipitation text for the week. Parameters: - avgIntensity (float): The average precipitation intensity for the week - intensityUnit (int): The conversion factor for the precipitation intensity - precipType (str): The type of precipitation for the week Returns: - cSummary (str): The textual summary of conditions (very-light-rain, medium-sleet, etc.). """ifprecipType=="rain":
ifprecipIntensity< (0.4*prepIntensityUnit):
cSummary="very-light-rain"elifprecipIntensity>= (0.4*prepIntensityUnit) andprecipIntensity< (
2.5*prepIntensityUnit
):
cSummary="light-rain"elifprecipIntensity>= (2.5*prepIntensityUnit) andprecipIntensity< (
10*prepIntensityUnit
):
cSummary="medium-rain"else:
cSummary="heavy-rain"elifprecipType=="snow":
ifprecipIntensity< (0.13*prepIntensityUnit):
cSummary="very-light-snow"elifprecipIntensity>= (0.13*prepIntensityUnit) andprecipIntensity< (
0.83*prepIntensityUnit
):
cSummary="light-snow"elifprecipIntensity>= (0.83*prepIntensityUnit) andprecipIntensity< (
3.33*prepIntensityUnit
):
cSummary="medium-snow"else:
cSummary="heavy-snow"elifprecipType=="sleet":
ifprecipIntensity< (0.4*prepIntensityUnit):
cSummary="very-light-sleet"elifprecipIntensity>= (0.4*prepIntensityUnit) andprecipIntensity< (
2.5*prepIntensityUnit
):
cSummary="light-sleet"elifprecipIntensity>= (2.5*prepIntensityUnit) andprecipIntensity< (
10*prepIntensityUnit
):
cSummary="medium-sleet"else:
cSummary="heavy-sleet"else:
# Because soemtimes there's precipitation not no type use a generic precipitation summaryifprecipIntensity< (0.4*prepIntensityUnit):
cSummary="very-light-precipitation"elifprecipIntensity>= (0.4*prepIntensityUnit) andprecipIntensity< (
2.5*prepIntensityUnit
):
cSummary="light-precipitation"elifprecipIntensity>= (2.5*prepIntensityUnit) andprecipIntensity< (
10*prepIntensityUnit
):
cSummary="medium-precipitation"else:
cSummary="heavy-precipitation"returncSummarydefcalculate_summary_text(precipitation, avgIntensity, intensityUnit):
""" Calculates the precipitation summary if there are between 1 and 8 days of precipitation Parameters: - precipitation (arr): An array of arrays that contain the days with precipitation if there are any. The inner array contains: The index in the week array, the day of the week and the precipitation type - avgIntensity (float): The average precipitation intensity for the week - intensityUnit (int): The conversion factor for the precipitation intensity Returns: - precipSummary (arr): A summary of the precipitation for the week. - wIcon (str): The textual summary of conditions (Drizzle, Sleet, etc.). - wWeekend (bool): If the summary includes over-weekend or not - cIcon (str): The icon representing the conditions for the week. """wIcon=NonewText=NonewWeekend=FalsedayIndexes= []
days= []
cIcon=None# Loop through each index in the precipitation arrayfordayinprecipitation:
# Create an array of day indexesdayIndexes.append(day[0])
# If an icon does not exist then set it. Otherwise if already set, check if the icons are the same and if not use the mixed-precipitation textifnotwIcon:
wIcon=day[2]
elifwIcon!=day[2] andwIcon!="mixed-precipitation":
wIcon="mixed-precipitation"# Create a list of consecutive days so we can use the through text instead of multiple andsfork, gingroupby(enumerate(dayIndexes), lambdaix: ix[0] -ix[1]):
days.append(list(map(itemgetter(1), g)))
# If the icon is not mixed precipitation change it to translations formatifwIcon!="mixed-precipitation":
cIcon=wIconwIcon=calculate_text(avgIntensity, intensityUnit, wIcon)
else:
cIcon="sleet"iflen(precipitation) ==2:
ifprecipitation[0][1] =="saturday"andprecipitation[1][1] =="sunday":
# If the precipitation occurs on the weekend then use the over weekend textwText="over-weekend"wWeekend=Trueelse:
# Join the days together with an andwText= [
"and",
precipitation[0][1],
precipitation[len(precipitation) -1][1],
]
elif (
precipitation[len(precipitation) -1][0] -precipitation[0][0]
==len(precipitation) -1
):
wText= [
"through",
precipitation[0][1],
precipitation[len(precipitation) -1][1],
]
else:
# Calcuate the start/end of the second day arrayarrTwoStart=len(days[0])
arrTwoEnd=len(days[0]) +len(days[1])
arr0=""arr1=""arr2=""arr3=""iflen(days[0]) >2:
# If there are more than two indexes in the array use the through textarr0= ["through", precipitation[0][1], precipitation[arrTwoStart-1][1]]
eliflen(days[0]) ==2:
# Join the days together with an andarr0= ["and", precipitation[0][1], precipitation[arrTwoStart-1][1]]
else:
# Otherwise just return the dayarr0=precipitation[0][1]
iflen(days[1]) >2:
# If there are more than two indexes in the array use the through textarr1= [
"through",
precipitation[arrTwoStart][1],
precipitation[arrTwoEnd-1][1],
]
eliflen(days[1]) ==2:
# Join the days together with an andarr1= [
"and",
precipitation[arrTwoStart][1],
precipitation[arrTwoEnd-1][1],
]
else:
# Otherwise just return the dayarr1=precipitation[arrTwoStart][1]
# If there are more than two day indexes then calculate the text for the third indexiflen(days) >=3:
# Calcuate the start/end of the third day arrayarrThreeStart=len(days[0]) +len(days[1])
arrThreeEnd=len(days[0]) +len(days[1]) +len(days[2])
iflen(days[2]) >2:
# If there are more than two indexes in the array use the through textarr2= [
"through",
precipitation[arrThreeStart][1],
precipitation[arrThreeEnd-1][1],
]
eliflen(days[2]) ==2:
# Join the days together with an andarr2= [
"and",
precipitation[arrThreeStart][1],
precipitation[arrThreeEnd-1][1],
]
else:
# Otherwise just return the dayarr2=precipitation[arrThreeStart][1]
# If there are four day indexes then calculate the text for the fourth indexiflen(days) ==4:
# Calcuate the start/end of the fourth day arrayarrFourStart=len(days[0]) +len(days[1]) +len(days[2])
arrFourEnd=len(days[0]) +len(days[1]) +len(days[2]) +len(days[3])
iflen(days[3]) >2:
# If there are more than two indexes in the array use the through textarr3= [
"through",
precipitation[arrFourStart][1],
precipitation[arrFourEnd-1][1],
]
eliflen(days[3]) ==2:
# Join the days together with an andarr3= [
"and",
precipitation[arrFourStart][1],
precipitation[arrFourEnd-1][1],
]
else:
# Otherwise just return the dayarr3=precipitation[arrFourStart][1]
# Join the day indexes together with an andiflen(days) ==2:
wText= ["and", arr0, arr1]
iflen(days) ==3:
wText= ["and", arr0, ["and", arr1, arr2]]
iflen(days) ==4:
wText= ["and", arr0, ["and", arr1, ["and", arr2, arr3]]]
returnwIcon, wText, wWeekend, cIcondefcalculate_precip_summary(
precipitation, precipitationDays, icons, avgIntensity, intensityUnit
):
""" Calculates the weekly precipitation summary. Parameters: - precipitation (bool): If precipitation is occuring during the week or not - precipitationDays (arr): An array of arrays that contain the days with precipitation if there are any. The inner array contains: The index in the week array, the day of the week and the precipitation type - icons (arr): An array of the daily icons - avgIntensity (float): The average precipitation intensity for the week - intensityUnit (int): The conversion factor for the precipitation intensity Returns: - precipSummary (arr): A summary of the precipitation for the week. - cIcon (str): An string representing the conditions for the week. """cIcon=Noneifnotprecipitation:
# If there has been no precipitation use the no precipitation text.precipSummary= ["for-week", "no-precipitation"]
# If there has been any fog forecast during the week show that icon, then the wind icon otherwise use the most common icon during the weekif"fog"inicons:
cIcon="fog"elif"wind"inicons:
cIcon="wind"else:
cIcon=Most_Common(icons)
eliflen(precipitationDays) ==1:
# If one day has any precipitation then set the icon to the precipitation type and use the medium precipitation text in the precipitation summarycIcon=precipitationDays[0][2]
precipSummary= [
"during",
calculate_text(avgIntensity, intensityUnit, precipitationDays[0][2]),
precipitationDays[0][1],
]
elif1<len(precipitationDays) <8:
# If between 1 and 8 days have precipitation call the function to calculate the summary text using the precipitation array.wIcon, wSummary, wWeekend, cIcon=calculate_summary_text(
precipitationDays, avgIntensity, intensityUnit
)
# Check if the summary has the over-weekend textifnotwWeekend:
precipSummary= ["during", wIcon, wSummary]
else:
precipSummary= [wSummary, wIcon]
eliflen(precipitationDays) ==8:
if (
precipitationDays[0][2]
==precipitationDays[1][2]
==precipitationDays[2][2]
==precipitationDays[3][2]
==precipitationDays[4][2]
==precipitationDays[5][2]
==precipitationDays[6][2]
==precipitationDays[7][2]
):
# If all days have precipitation then if they all have the same type then use that iconcIcon=precipitationDays[0][2]
# Since precipitation is occuring everyday use the for week text instead of throughprecipSummary= ["for-week", "medium-"+precipitationDays[0][2]]
else:
# If the types are not the same then set the icon to sleet and use the mixed precipitation text (as is how Dark Sky did it)cIcon="sleet"precipSummary= ["for-week", "mixed-precipitation"]
returnprecipSummary, cIcondefcalculate_temp_summary(highTemp, lowTemp):
""" Calculates the temperature summary for the week Parameters: - highTemp (arr): An array that contains the index in the week array, the high temperture of the week and the temperature units. - lowTemp (arr): An array that contains the index in the week array, the low temperture of the week and the temperature units. Returns: - arr: A summary of the temperature for the week. """# Change C to celsius otherwise change it to fahrenheitifhighTemp[3] =="C":
highTemp[3] ="celsius"else:
highTemp[3] ="fahrenheit"iflowTemp[3] =="C":
lowTemp[3] ="celsius"else:
lowTemp[3] ="fahrenheit"# If the temperature is increasing everyday or if the lowest temperatue is at the start of the week and the highest temperature is at the end of the week use the rising textifweekArr[0][5] <weekArr[1][5] <weekArr[2][5] <weekArr[3][5] <weekArr[4][
5
] <weekArr[5][5] <weekArr[6][5] <weekArr[7][5] or (
highTemp[0] >=6andlowTemp[0] <=1
):
# Set the temperature summaryreturn [
"temperatures-rising",
[highTemp[3], round(highTemp[2], 0)],
highTemp[1],
]
# If the temperature is decreasing everyday or if the lowest temperatue is at the end of the week and the highest temperature is at the start of the week use the rising textelifweekArr[0][5] >weekArr[1][5] >weekArr[2][5] >weekArr[3][5] >weekArr[4][
5
] >weekArr[5][5] >weekArr[6][5] >weekArr[7][5] or (
highTemp[0] <=1andlowTemp[0] >=6
):
return ["temperatures-falling", [lowTemp[3], round(lowTemp[2], 0)], lowTemp[1]]
# If the lowest temperatue is in the middle of the week and the highest temperature is at the start or end of the week use the valleying textelif (highTemp[0] <=1orhighTemp[0] >=6) and0<lowTemp[0] <7:
return [
"temperatures-valleying",
[lowTemp[3], round(lowTemp[2], 0)],
lowTemp[1],
]
else:
# Otherwise use the peaking textreturn [
"temperatures-peaking",
[highTemp[3], round(highTemp[2], 0)],
highTemp[1],
]
defcalculate_weeky_text(weekArr):
""" Calculates the weekly summary given an array of weekdays Parameters: - weekArr (arr): An array of the weekdays Returns: - cText (arr): The precipitation and temperature summary for the week. - cIcon (str): The icon representing the conditions for the week. """# Variables to use in calculating the weekly summarycIcon=NonecText=Noneprecipitation=FalseprecipitationDays= []
precipSummary=""highTemp= []
lowTemp= []
icons= []
tempSummary=""avgIntensity=0intensityUnit=0# Loop through the week arrayforidx, dayinenumerate(weekArr):
# Add the daily icon to the list of weekly iconsicons.append(day[6])
# Check if the day has enough precipitation to reach the threshold and record the index in the array, the day it occured on and the typeifday[1] =="snow"and (day[2] *day[3]) >= (1.0*day[3]):
# Sets that there has been precipitation during the weekprecipitation=TrueprecipitationDays.append([idx, day[0], day[1]])
avgIntensity+=day[7] *day[3]
intensityUnit=day[3]
elifday[1] =="rain"and (day[2] *day[3]) >= (0.01*day[3]):
# Sets that there has been precipitation during the weekprecipitation=TrueprecipitationDays.append([idx, day[0], day[1]])
avgIntensity+=day[7] *day[3]
intensityUnit=day[3]
elifday[1] =="sleet"and (day[2] *day[3]) >= (0.01*day[3]):
# Sets that there has been precipitation during the weekprecipitation=TrueprecipitationDays.append([idx, day[0], day[1]])
avgIntensity+=day[7] *day[3]
intensityUnit=day[3]
elifday[1] =="none"and (day[2] *day[3]) >= (0.01*day[3]):
# Sets that there has been precipitation during the weekprecipitation=TrueprecipitationDays.append([idx, day[0], "precipitation"])
avgIntensity+=day[7] *day[3]
intensityUnit=day[3]
# Determine the highest temperature of the week and record the index in the array, the day it occured on, the temperature and the temperature unitsifnothighTemp:
highTemp= [idx, day[0], day[4], day[5]]
elifday[4] >highTemp[2]:
highTemp= [idx, day[0], day[4], day[5]]
# Determine the lowest temperature of the week and record the index in the array, the day it occured on, the temperature and the temperature unitsifnotlowTemp:
lowTemp= [idx, day[0], day[4], day[5]]
elifday[4] <lowTemp[2]:
lowTemp= [idx, day[0], day[4], day[5]]
iflen(precipitationDays) >0:
avgIntensity=avgIntensity/len(precipitationDays)
precipSummary, cIcon=calculate_precip_summary(
precipitation, precipitationDays, icons, avgIntensity, intensityUnit
)
tempSummary=calculate_temp_summary(highTemp, lowTemp)
# Combine the two texts together using withcText= ["sentence", ["with", precipSummary, tempSummary]]
# If we somehow have a generic precipitation icon we use rain insteadifcIcon=="precipitation":
cIcon="rain"returncText, cIconTranslations=require("./node_modules/translations/index.js")
lang="en"translation=Translations[lang]
iftranslationisNone:
translation=Translations["en"]
weekArr= []
# Day of the week, precipitation type, precipitation accumulation, accumulation unit, temperature, temperature unitweekArr.append(["today", "rain", 0.8128, 1, 2.91, "C", "rain", 1.7781])
weekArr.append(["tomorrow", "rain", 0.2286, 1, 5.85, "C", "cloudy", 0.5081])
weekArr.append(["tuesday", "rain", 0.0804, 1, 3.66, "C", "rain", 0.26])
weekArr.append(["wednesday", "rain", 2.9419, 1, 1.73, "C", "rain", 0.5878])
weekArr.append(["thursday", "snow", 1.2672, 1, -2.02, "C", "partly-cloudy-day", 0.1693])
weekArr.append(["friday", "snow", 0, 1, -3.88, "C", "partly-cloudy-day", 0])
weekArr.append(["saturday", "snow", 0, 1, -7.84, "C", "partly-cloudy-day", 0])
weekArr.append(["next-sunday", "snow", 0, 1, -9.37, "C", "partly-cloudy-day", 0])
summary_text, cIcon=calculate_weeky_text(weekArr)
cText=translation.translate(summary_text)
print(cText)
print(cIcon)
When I was testing using my location I found that using the thresholds in PirateText.py file that Tuesday was not included even though its showing the rain icon on live. I would check dev but I'm assuming the JavaScript bridge crashed the server.
I kinda just guessed on how to setup the temperature summaries as I'm not sure how Dark Sky set them up. Feel free to change them if you have better ideas on how they should work.
@alexander0042 Hope you're enjoying your holidays! I updated my weekly summaries code to reduce the number of ands it can create in the text. It still can create a lot of ands which there isn't much we can do about that.
I took a look at archives of the Dark Sky website and what they did seems pretty basic? I found one example of them saying rain throughout the week when not every day was forecasting rain and one where they used through but one day didn't forecast rain.
This code can now generate summaries like: Mixed precipitation today through Sunday and on Tuesday through Thursday, with high temperatures peaking at 6°C on Monday. which wasn't possible in the old code. I also fixed a few bugs but will likely need to be tested more as I've tested a few scenarios to see if it worked (It generates this for my location: Mixed precipitation on Sunday through Thursday, with high temperatures peaking at 6°C on Monday.
I also didn't realise that Dark Sky showed the different intensity levels in the weekly text so I added that in. I haven't changed the precipitation calculation locally so it'd need to be updated to match the changes you made. You can use either max intensity or average intensity in the calculations it doesn't really matter. It also calculates it based on the number of days which surpass the precipitation threshold. (So one day with 2.5 mm/h intensity will use Light rain instead of Drizzle)
Weekly Summary
When I was testing using my location I found that using the thresholds in PirateText.py file that Tuesday was not included even though its showing the rain icon on live. I would check dev but I'm assuming the JavaScript bridge crashed the server.
I kinda just guessed on how to setup the temperature summaries as I'm not sure how Dark Sky set them up. Feel free to change them if you have better ideas on how they should work.
Originally posted by @cloneofghosts in #38 (comment)
The text was updated successfully, but these errors were encountered: