65
65
#---------------------------------------------------------------------------
66
66
67
67
class Month (Choice ):
68
+ """
69
+ Element allowing one of the twelve months of the year to be
70
+ recognized and used in an action.
71
+
72
+ """
68
73
69
74
def __init__ (self , name ):
70
75
Choice .__init__ (self , name = name , choices = month_names )
71
76
72
77
73
78
class Day (Choice ):
79
+ """
80
+ Element allowing one of the days in a week to be recognized and
81
+ used in an action.
74
82
83
+ """
75
84
def __init__ (self , name ):
76
85
Choice .__init__ (self , name = name , choices = day_names )
77
86
78
87
79
88
class Year (Alternative ):
89
+ """
90
+ Element allowing a specific calendar year to be recognized and used
91
+ in an action.
92
+
93
+ The years 1910 to 2099 may be recognized.
80
94
95
+ """
81
96
alts = [
82
97
IntegerRef ("year" , 2000 , 2100 ),
83
98
Compound (
84
99
spec = "<century> <year>" ,
85
- extras = [Integer ("century" , 20 , 21 ),
100
+ extras = [Integer ("century" , 19 , 21 ),
86
101
IntegerRef ("year" , 10 , 100 )],
87
102
value_func = lambda n , e : e ["century" ] * 100 + e ["year" ]
88
103
),
89
- Compound (
90
- spec = "<century> <year>" ,
91
- extras = [Integer ("century" , 19 , 20 ),
92
- IntegerRef ("year" , 1 , 100 )],
93
- value_func = lambda n , e : e ["century" ] * 100 + e ["year" ]
94
- ),
95
104
]
96
105
97
106
def __init__ (self , name ):
@@ -101,6 +110,11 @@ def __init__(self, name):
101
110
#---------------------------------------------------------------------------
102
111
103
112
class AbsoluteDate (Compound ):
113
+ """
114
+ Element allowing a date in the current year or in a specific year
115
+ to be recognized and used in an action.
116
+
117
+ """
104
118
105
119
spec = "(<day> <month> | <month> <day>) [<year>]"
106
120
extras = [IntegerRef ("day" , 1 , 32 ), Month ("month" ), Year ("year" )]
@@ -128,6 +142,11 @@ def value(self, node):
128
142
129
143
130
144
class RelativeDate (Alternative ):
145
+ """
146
+ Element allowing a date relative to the current date to be
147
+ recognized and used in an action.
148
+
149
+ """
131
150
132
151
class _DayOffset (Choice ):
133
152
def __init__ (self ):
@@ -144,7 +163,7 @@ def __init__(self):
144
163
def value (self , node ):
145
164
value = Choice .value (self , node )
146
165
n = node .get_child_by_name ("n" )
147
- print ("November:" , n )
166
+ # print("November:", n)
148
167
if n is not None :
149
168
value = value * n .value ()
150
169
return date .today () + timedelta (days = value )
@@ -164,7 +183,7 @@ def value(self, node):
164
183
value = Choice .value (self , node )
165
184
day = node .get_child_by_name ("day" ).value ()
166
185
now = date .today ().weekday ()
167
- print (value , day , now )
186
+ # print(value, day, now)
168
187
if value == "last day" :
169
188
if day < now : day_offset = - now + day
170
189
else : day_offset = - 7 - now + day
@@ -187,6 +206,11 @@ def __init__(self, name):
187
206
188
207
189
208
class Date (Alternative ):
209
+ """
210
+ Element allowing either an absolute date or a relative date to be
211
+ recognized and used in an action.
212
+
213
+ """
190
214
191
215
alts = [
192
216
AbsoluteDate (None ),
@@ -199,13 +223,25 @@ def __init__(self, name):
199
223
200
224
#---------------------------------------------------------------------------
201
225
202
- class MilitaryTime (Compound ):
226
+ class TwelveHourTime (Compound ):
227
+ """
228
+ Element allowing twelve-hour time to be recognized and used in an
229
+ action.
203
230
204
- spec = "<hour> (hundred | (oh | zero) <min_1_10> | <min_10_60>)"
231
+ Examples: nine AM, one oh five PM, three thirty PM.
232
+
233
+ """
234
+
235
+ spec = "<hour> [(<zero> <min_1_10> | <min_10_60>)] <am_pm>"
205
236
extras = [
206
- Integer ("hour" , 0 , 25 ),
237
+ Integer ("zero" , 0 , 1 ),
238
+ Integer ("hour" , 1 , 13 ),
207
239
IntegerRef ("min_1_10" , 1 , 10 ),
208
240
IntegerRef ("min_10_60" , 10 , 60 ),
241
+ Choice ("am_pm" , {
242
+ "AM | a.m." : "AM" ,
243
+ "PM | p.m." : "PM" ,
244
+ })
209
245
]
210
246
211
247
def __init__ (self , name ):
@@ -214,6 +250,52 @@ def __init__(self, name):
214
250
215
251
def value (self , node ):
216
252
hour = node .get_child_by_name ("hour" ).value ()
253
+ am_pm = node .get_child_by_name ("am_pm" ).value ()
254
+ if hour < 12 :
255
+ if am_pm == "PM" :
256
+ hour += 12
257
+ elif am_pm == "AM" :
258
+ hour = 0
259
+ if node .has_child_with_name ("min_1_10" ):
260
+ minute = node .get_child_by_name ("min_1_10" ).value ()
261
+ elif node .has_child_with_name ("min_10_60" ):
262
+ minute = node .get_child_by_name ("min_10_60" ).value ()
263
+ else :
264
+ minute = 0
265
+ return time (hour , minute )
266
+
267
+
268
+ class MilitaryTime (Compound ):
269
+ """
270
+ Element allowing military time to be recognized and used in an
271
+ action.
272
+
273
+ Examples: zero hundred, oh eight hundred, seventeen hundred hours,
274
+ ten oh five, seventeen thirty.
275
+
276
+ """
277
+
278
+ spec = ("(<zero_oh> | <zero_oh> <hour_0_10> | <hour_10_24>)"
279
+ " (hundred | <zero_oh> <min_1_10> | <min_10_60>) [hours]" )
280
+ extras = [
281
+ Integer ("zero_oh" , 0 , 1 ),
282
+ Integer ("hour_0_10" , 1 , 10 ),
283
+ Integer ("hour_10_24" , 10 , 24 ),
284
+ IntegerRef ("min_1_10" , 1 , 10 ),
285
+ IntegerRef ("min_10_60" , 10 , 60 ),
286
+ ]
287
+
288
+ def __init__ (self , name ):
289
+ Compound .__init__ (self , name = name , spec = self .spec ,
290
+ extras = self .extras )
291
+
292
+ def value (self , node ):
293
+ if node .has_child_with_name ("hour_0_10" ):
294
+ hour = node .get_child_by_name ("hour_0_10" ).value ()
295
+ elif node .has_child_with_name ("hour_10_24" ):
296
+ hour = node .get_child_by_name ("hour_10_24" ).value ()
297
+ else :
298
+ hour = 0
217
299
if node .has_child_with_name ("min_1_10" ):
218
300
minute = node .get_child_by_name ("min_1_10" ).value ()
219
301
elif node .has_child_with_name ("min_10_60" ):
@@ -224,8 +306,17 @@ def value(self, node):
224
306
225
307
226
308
class Time (Alternative ):
309
+ """
310
+ Element allowing the time of day to be recognized and used in an
311
+ action.
312
+
313
+ This allows speaking the twelve-hour time or twenty-four hour
314
+ military time.
315
+
316
+ """
227
317
228
318
alts = [
319
+ TwelveHourTime (None ),
229
320
MilitaryTime (None ),
230
321
]
231
322
0 commit comments