-
Notifications
You must be signed in to change notification settings - Fork 61
/
dialog.py
435 lines (380 loc) · 16.3 KB
/
dialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# Copyright 2021, Mycroft AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Abstraction of dialog building for the weather skill.
There are A LOT of dialog files in this skill. All the permutations of timeframe,
weather condition and location add up fast. To help with discoverability, a naming
convention was applied to the dialog files:
<timeframe>-<weather info>-<qualifier>-<locale>.dialog
Example:
daily-temperature-high-local.dialog
* Timeframe: the date or time applicable to the forecast. This skill supports
current, hourly and daily weather.
* Weather info: a description of what type of weather the dialog refers to.
Examples include "temperature", "weather" and "sunrise".
* Qualifier: further qualifies what type of weather is being reported. For
example, temperature can be qualified by "high" or "low".
* Locale: indicates if the dialog is for local weather or weather in a remote
location.
The skill class will use the "name" and "data" attributes to pass to the TTS process.
"""
from typing import List, Tuple
from mycroft.util.format import join_list, nice_number, nice_time
from mycroft.util.time import now_local
from .config import WeatherConfig
from .intent import WeatherIntent
from .util import get_speakable_day_of_week, get_time_period
from .weather import (
CURRENT,
CurrentWeather,
DAILY,
DailyWeather,
HOURLY,
HourlyWeather,
)
# TODO: MISSING DIALOGS
# - current.clear.alternative.local
# - current.clouds.alternative.local
# - daily.snow.alternative.local
# - all hourly.<condition>.alternative.local/location
# - all hourly.<condition>.not.expected.local/location
class WeatherDialog:
"""Abstract base class for the weather dialog builders."""
def __init__(self, intent_data: WeatherIntent, config: WeatherConfig):
self.intent_data = intent_data
self.config = config
self.name = None
self.data = None
def _add_location(self):
"""Add location information to the dialog."""
if self.intent_data.location is None:
self.name += "-local"
else:
self.name += "-location"
if self.config.country == self.intent_data.geolocation["country"]:
spoken_location = ", ".join(
[
self.intent_data.geolocation["city"],
self.intent_data.geolocation["region"],
]
)
else:
spoken_location = ", ".join(
[
self.intent_data.geolocation["city"],
self.intent_data.geolocation["country"],
]
)
self.data.update(location=spoken_location)
class CurrentDialog(WeatherDialog):
"""Weather dialog builder for current weather."""
def __init__(
self, intent_data: WeatherIntent, config: WeatherConfig, weather: CurrentWeather
):
super().__init__(intent_data, config)
self.weather = weather
self.name = CURRENT
def build_weather_dialog(self):
"""Build the components necessary to speak current weather."""
self.name += "-weather"
self.data = dict(
condition=self.weather.condition.description,
temperature=self.weather.temperature,
temperature_unit=self.config.temperature_unit,
)
self._add_location()
def build_high_low_temperature_dialog(self):
"""Build the components necessary to speak high and low temperature."""
self.name += "-temperature-high-low"
self.data = dict(
high_temperature=self.weather.high_temperature,
low_temperature=self.weather.low_temperature,
)
def build_temperature_dialog(self, temperature_type: str):
"""Build the components necessary to speak the current temperature.
:param temperature_type: indicates if temperature is current, high or low
"""
self.name += "-temperature"
if temperature_type == "high":
self.name += "-high"
self.data = dict(temperature=self.weather.high_temperature)
elif temperature_type == "low":
self.name += "-low"
self.data = dict(temperature=self.weather.low_temperature)
else:
self.data = dict(temperature=self.weather.temperature)
self.data.update(
temperature_unit=self.intent_data.unit or self.config.temperature_unit
)
self._add_location()
def build_condition_dialog(self, intent_match: bool):
"""Select the relevant dialog file for condition based reports.
A condition can for example be "snow" or "rain".
:param intent_match: true if intent matches a vocabulary for the condition
"""
self.data = dict(condition=self.weather.condition.description.lower())
if intent_match:
self.name += "-condition-expected"
else:
self.name += "-condition-not-expected".format(
self.weather.condition.category.lower()
)
self._add_location()
def build_sunrise_dialog(self):
"""Build the components necessary to speak the sunrise time."""
if self.intent_data.location is None:
now = now_local()
else:
now = now_local(tz=self.intent_data.geolocation["timezone"])
if now < self.weather.sunrise:
self.name += "-sunrise-future"
else:
self.name += "-sunrise-past"
self.data = dict(time=nice_time(self.weather.sunrise))
self._add_location()
def build_sunset_dialog(self):
"""Build the components necessary to speak the sunset time."""
if self.intent_data.location is None:
now = now_local()
else:
now = now_local(tz=self.intent_data.geolocation["timezone"])
if now < self.weather.sunset:
self.name += ".sunset.future"
else:
self.name = ".sunset.past"
self.data = dict(time=nice_time(self.weather.sunset))
self._add_location()
def build_wind_dialog(self):
"""Build the components necessary to speak the wind conditions."""
wind_strength = self.weather.determine_wind_strength(self.config.speed_unit)
self.data = dict(
speed=nice_number(self.weather.wind_speed),
speed_unit=self.config.speed_unit,
direction=self.weather.wind_direction,
)
self.name += "-wind-" + wind_strength
self._add_location()
def build_humidity_dialog(self):
"""Build the components necessary to speak the percentage humidity."""
self.data = dict(percent=self.weather.humidity)
self.name += "-humidity"
self._add_location()
class HourlyDialog(WeatherDialog):
"""Weather dialog builder for hourly weather."""
def __init__(
self, intent_data: WeatherIntent, config: WeatherConfig, weather: HourlyWeather
):
super().__init__(intent_data, config)
self.weather = weather
self.name = HOURLY
def build_weather_dialog(self):
"""Build the components necessary to speak the forecast for a hour."""
self.name += "-weather"
self.data = dict(
condition=self.weather.condition.description,
time=self.weather.date_time.strftime("%H:00"),
temperature=self.weather.temperature,
)
self._add_location()
def build_temperature_dialog(self, _):
"""Build the components necessary to speak the hourly temperature."""
self.name += "-temperature"
self.data = dict(
temperature=self.weather.temperature,
time=get_time_period(self.weather.date_time),
temperature_unit=self.intent_data.unit or self.config.temperature_unit,
)
self._add_location()
def build_condition_dialog(self, intent_match: bool):
"""Select the relevant dialog file for condition based reports.
A condition can for example be "snow" or "rain".
:param intent_match: true if intent matches a vocabulary for the condition
"""
self.data = dict(
condition=self.weather.condition.description.lower(),
time=nice_time(self.weather.date_time),
)
if intent_match:
self.name += "-condition-expected"
else:
self.name += "-condition-not-expected".format(
self.weather.condition.category.lower()
)
self._add_location()
def build_wind_dialog(self):
"""Build the components necessary to speak the wind conditions."""
wind_strength = self.weather.determine_wind_strength(self.config.speed_unit)
self.data = dict(
speed=nice_number(self.weather.wind_speed),
speed_unit=self.config.speed_unit,
direction=self.weather.wind_direction,
time=nice_time(self.weather.date_time),
)
self.name += "-wind-" + wind_strength
self._add_location()
def build_next_precipitation_dialog(self):
"""Build the components necessary to speak the next chance of rain."""
if self.weather is None:
self.name += "-precipitation-next-none"
self.data = dict()
else:
self.name += "-precipitation-next"
self.data = dict(
percent=self.weather.chance_of_precipitation,
precipitation="rain",
day=self.weather.date_time.strftime("%A"),
time=get_time_period(self.weather.date_time),
)
self._add_location()
class DailyDialog(WeatherDialog):
"""Weather dialog builder for daily weather."""
def __init__(
self, intent_data: WeatherIntent, config: WeatherConfig, weather: DailyWeather
):
super().__init__(intent_data, config)
self.weather = weather
self.name = DAILY
def build_weather_dialog(self):
"""Build the components necessary to speak the forecast for a day."""
self.name += "-weather"
self.data = dict(
condition=self.weather.condition.description,
day=get_speakable_day_of_week(self.weather.date_time),
high_temperature=self.weather.temperature.high,
low_temperature=self.weather.temperature.low,
)
if self.weather.date_time.date() == self.intent_data.location_datetime.date():
self.data.update(day="Today")
self._add_location()
def build_temperature_dialog(self, temperature_type: str):
"""Build the components necessary to speak the daily temperature.
:param temperature_type: indicates if temperature is day, high or low
"""
self.name += "-temperature"
if temperature_type == "high":
self.name += "-high"
self.data = dict(temperature=self.weather.temperature.high)
elif temperature_type == "low":
self.name += "-low"
self.data = dict(temperature=self.weather.temperature.low)
else:
self.data = dict(temperature=self.weather.temperature.day)
self.data.update(
day=get_speakable_day_of_week(self.weather.date_time),
temperature_unit=self.intent_data.unit or self.config.temperature_unit,
)
self._add_location()
def build_condition_dialog(self, intent_match: bool):
"""Select the relevant dialog file for condition based reports.
A condition can for example be "snow" or "rain".
:param intent_match: true if intent matches a vocabulary for the condition
"""
self.data = dict(
condition=self.weather.condition.description.lower(),
day=get_speakable_day_of_week(self.weather.date_time),
)
if intent_match:
self.name += "-condition-expected"
else:
self.name += "-condition-not-expected".format(
self.weather.condition.category.lower()
)
self._add_location()
def build_sunrise_dialog(self):
"""Build the components necessary to speak the sunrise time."""
self.name += "-sunrise"
self.data = dict(time=nice_time(self.weather.sunrise))
self.data.update(day=get_speakable_day_of_week(self.weather.date_time))
self._add_location()
def build_sunset_dialog(self):
"""Build the components necessary to speak the sunset time."""
self.name += "-sunset"
self.data = dict(time=nice_time(self.weather.sunset))
self.data.update(day=get_speakable_day_of_week(self.weather.date_time))
self._add_location()
def build_wind_dialog(self):
"""Build the components necessary to speak the wind conditions."""
wind_strength = self.weather.determine_wind_strength(self.config.speed_unit)
self.data = dict(
day=self.weather.date_time.strftime("%A"),
speed=nice_number(self.weather.wind_speed),
speed_unit=self.config.speed_unit,
direction=self.weather.wind_direction,
)
self.name += "-wind-" + wind_strength
self._add_location()
def build_humidity_dialog(self):
"""Build the components necessary to speak the percentage humidity."""
self.data = dict(
percent=self.weather.humidity, day=self.weather.date_time.strftime("%A")
)
self.name += "-humidity"
self._add_location()
def build_next_precipitation_dialog(self):
"""Build the components necessary to speak the next chance of rain."""
if self.weather is None:
self.name += "-precipitation-next-none"
self.data = dict()
else:
self.name += "-precipitation-next"
self.data = dict(
percent=self.weather.chance_of_precipitation,
precipitation="rain",
day=self.weather.date_time.strftime("%A"),
)
self._add_location()
class WeeklyDialog(WeatherDialog):
"""Weather dialog builder for weekly weather."""
def __init__(
self,
intent_data: WeatherIntent,
config: WeatherConfig,
forecast: List[DailyWeather],
):
super().__init__(intent_data, config)
self.forecast = forecast
self.name = "weekly"
def build_temperature_dialog(self):
"""Build the components necessary to temperature ranges for a week."""
low_temperatures = [daily.temperature.low for daily in self.forecast]
high_temperatures = [daily.temperature.high for daily in self.forecast]
self.name += "-temperature"
self.data = dict(
low_min=min(low_temperatures),
low_max=max(low_temperatures),
high_min=min(high_temperatures),
high_max=max(high_temperatures),
)
def build_condition_dialog(self, condition: str):
"""Build the components necessary to speak the days of week for a condition."""
self.name += "-condition"
self.data = dict(condition=condition)
days_with_condition = []
for daily in self.forecast:
if daily.condition.category == condition:
day = get_speakable_day_of_week(daily.date_time)
days_with_condition.append(day)
self.data.update(days=join_list(days_with_condition, "and"))
def get_dialog_for_timeframe(timeframe: str, dialog_ags: Tuple):
"""Use the intent data to determine which dialog builder to use.
:param timeframe: current, hourly, daily
:param dialog_ags: Arguments to pass to the dialog builder
:return: The correct dialog builder for the timeframe
"""
if timeframe == DAILY:
dialog = DailyDialog(*dialog_ags)
elif timeframe == HOURLY:
dialog = HourlyDialog(*dialog_ags)
else:
dialog = CurrentDialog(*dialog_ags)
return dialog