Skip to content

Commit 48ec1b8

Browse files
committed
Clean up the English-language element code under dragonfly.language
- Fix errors in calendar.py and add docstrings. - Add a new TwelveHourTime element class. - Add license/copyright header to short_number.py. - Fix some other minor issues.
1 parent c70a04c commit 48ec1b8

File tree

3 files changed

+133
-18
lines changed

3 files changed

+133
-18
lines changed

dragonfly/language/en/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222
from .number import IntegerContent, DigitsContent
2323
from .short_number import ShortIntegerContent
2424
from .calendar import (AbsoluteDate, RelativeDate, Date,
25-
MilitaryTime, Time)
25+
TwelveHourTime, MilitaryTime, Time)

dragonfly/language/en/calendar.py

Lines changed: 103 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,33 +65,42 @@
6565
#---------------------------------------------------------------------------
6666

6767
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+
"""
6873

6974
def __init__(self, name):
7075
Choice.__init__(self, name=name, choices=month_names)
7176

7277

7378
class Day(Choice):
79+
"""
80+
Element allowing one of the days in a week to be recognized and
81+
used in an action.
7482
83+
"""
7584
def __init__(self, name):
7685
Choice.__init__(self, name=name, choices=day_names)
7786

7887

7988
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.
8094
95+
"""
8196
alts = [
8297
IntegerRef("year", 2000, 2100),
8398
Compound(
8499
spec="<century> <year>",
85-
extras=[Integer("century", 20, 21),
100+
extras=[Integer("century", 19, 21),
86101
IntegerRef("year", 10, 100)],
87102
value_func=lambda n, e: e["century"] * 100 + e["year"]
88103
),
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-
),
95104
]
96105

97106
def __init__(self, name):
@@ -101,6 +110,11 @@ def __init__(self, name):
101110
#---------------------------------------------------------------------------
102111

103112
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+
"""
104118

105119
spec = "(<day> <month> | <month> <day>) [<year>]"
106120
extras = [IntegerRef("day", 1, 32), Month("month"), Year("year")]
@@ -128,6 +142,11 @@ def value(self, node):
128142

129143

130144
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+
"""
131150

132151
class _DayOffset(Choice):
133152
def __init__(self):
@@ -144,7 +163,7 @@ def __init__(self):
144163
def value(self, node):
145164
value = Choice.value(self, node)
146165
n = node.get_child_by_name("n")
147-
print("November:", n)
166+
#print("November:", n)
148167
if n is not None:
149168
value = value * n.value()
150169
return date.today() + timedelta(days=value)
@@ -164,7 +183,7 @@ def value(self, node):
164183
value = Choice.value(self, node)
165184
day = node.get_child_by_name("day").value()
166185
now = date.today().weekday()
167-
print(value, day, now)
186+
#print(value, day, now)
168187
if value == "last day":
169188
if day < now: day_offset = -now + day
170189
else: day_offset = -7 - now + day
@@ -187,6 +206,11 @@ def __init__(self, name):
187206

188207

189208
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+
"""
190214

191215
alts = [
192216
AbsoluteDate(None),
@@ -199,13 +223,25 @@ def __init__(self, name):
199223

200224
#---------------------------------------------------------------------------
201225

202-
class MilitaryTime(Compound):
226+
class TwelveHourTime(Compound):
227+
"""
228+
Element allowing twelve-hour time to be recognized and used in an
229+
action.
203230
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>"
205236
extras = [
206-
Integer("hour", 0, 25),
237+
Integer("zero", 0, 1),
238+
Integer("hour", 1, 13),
207239
IntegerRef("min_1_10", 1, 10),
208240
IntegerRef("min_10_60", 10, 60),
241+
Choice("am_pm", {
242+
"AM | a.m.": "AM",
243+
"PM | p.m.": "PM",
244+
})
209245
]
210246

211247
def __init__(self, name):
@@ -214,6 +250,52 @@ def __init__(self, name):
214250

215251
def value(self, node):
216252
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
217299
if node.has_child_with_name("min_1_10"):
218300
minute = node.get_child_by_name("min_1_10").value()
219301
elif node.has_child_with_name("min_10_60"):
@@ -224,8 +306,17 @@ def value(self, node):
224306

225307

226308
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+
"""
227317

228318
alts = [
319+
TwelveHourTime(None),
229320
MilitaryTime(None),
230321
]
231322

dragonfly/language/en/short_number.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
1+
#
2+
# This file is part of Dragonfly.
3+
# (c) Copyright 2019, 2020 by Mike Roberts
4+
# Licensed under the LGPL.
5+
#
6+
# Dragonfly is free software: you can redistribute it and/or modify it
7+
# under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Dragonfly is distributed in the hope that it will be useful, but
12+
# WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
# Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public
17+
# License along with Dragonfly. If not, see
18+
# <http://www.gnu.org/licenses/>.
19+
#
120

2-
'''
21+
"""
322
ShortIntegerRef
423
============================================================================
524
6-
:class:`ShortIntegerRef` is a modified version of :class:`IntegerRef` which allows for greater flexibility in the way that numbers may be pronounced, allowing for words like "hundred" to be dropped. This may be particularly useful when navigating files by line or page number.
25+
:class:`ShortIntegerRef` is a modified version of :class:`IntegerRef` which
26+
allows for greater flexibility in the way that numbers may be pronounced,
27+
allowing for words like \"hundred\" to be dropped. This may be particularly
28+
useful when navigating files by line or page number.
729
830
Some examples of allowed pronunciations:
931
@@ -32,12 +54,14 @@
3254
four thousand 4000
3355
================================ ======
3456
35-
The class works in the same way as :class:`IntegerRef`, by adding the following as an extra.
57+
The class works in the same way as :class:`IntegerRef`, by adding the
58+
following as an extra:
3659
3760
.. code:: python
3861
39-
ShortIntegerRef("name", 0, 1000),
40-
'''
62+
ShortIntegerRef(\"name\", 0, 1000),
63+
64+
"""
4165

4266
from dragonfly.language.base.integer_internal import (CollectionIntBuilder,
4367
MagnitudeIntBuilder,

0 commit comments

Comments
 (0)