forked from taixzo/saera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeparser.py
224 lines (206 loc) · 7.13 KB
/
timeparser.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
#! /usr/bin/env python
from datetime import datetime, timedelta
from pyparsing import *
import calendar
# string conversion parse actions
def convertToTimedelta(toks):
unit = toks.timeunit.lower().rstrip("s")
td = {
'week' : timedelta(7),
'day' : timedelta(1),
'hour' : timedelta(0,0,0,0,0,1),
'minute' : timedelta(0,0,0,0,1),
'second' : timedelta(0,1),
}[unit]
if toks.qty:
td *= int(toks.qty)
if toks.dir:
td *= toks.dir
toks["timeOffset"] = td
def convertToDay(toks):
now = datetime.now()
if "wkdayRef" in toks:
todaynum = now.weekday()
daynames = [n.lower() for n in calendar.day_name]
nameddaynum = daynames.index(toks.wkdayRef.day.lower())
if toks.wkdayRef.dir > 0:
daydiff = (nameddaynum + 7 - todaynum) % 7
else:
daydiff = -((todaynum + 7 - nameddaynum) % 7)
toks["absTime"] = datetime(now.year, now.month, now.day)+timedelta(daydiff)
else:
name = toks.name.lower()
toks["absTime"] = {
"now" : now,
"today" : datetime(now.year, now.month, now.day),
"yesterday" : datetime(now.year, now.month, now.day)+timedelta(-1),
"tomorrow" : datetime(now.year, now.month, now.day)+timedelta(+1),
}[name]
def convertToAbsTime(toks):
now = datetime.now()
if "dayRef" in toks:
day = toks.dayRef.absTime
day = datetime(day.year, day.month, day.day)
else:
day = datetime(now.year, now.month, now.day)
if "timeOfDay" in toks:
try:
isstring = isinstance(toks.timeOfDay,basestring)
except NameError:
isstring = isinstance(toks.timeOfDay,str)
if isstring:
timeOfDay = {
"now" : timedelta(0, (now.hour*60+now.minute)*60+now.second, now.microsecond),
"noon" : timedelta(0,0,0,0,0,12),
"midnight" : timedelta(),
}[toks.timeOfDay]
else:
hhmmss = toks.timeparts
if hhmmss.miltime:
hh,mm = hhmmss.miltime
ss = 0
else:
hh,mm,ss = (hhmmss.HH % 12), hhmmss.MM, hhmmss.SS
if not mm: mm = 0
if not ss: ss = 0
if toks.timeOfDay.ampm == 'pm':
hh += 12
timeOfDay = timedelta(0, (hh*60+mm)*60+ss, 0)
else:
timeOfDay = timedelta(0, (now.hour*60+now.minute)*60+now.second, now.microsecond)
toks["absTime"] = day + timeOfDay
def calculateTime(toks):
if toks.absTime:
absTime = toks.absTime
else:
absTime = datetime.now()
if toks.timeOffset:
absTime += toks.timeOffset
toks["calculatedTime"] = absTime
# grammar definitions
CL = CaselessLiteral
today, tomorrow, yesterday, noon, midnight, now = map( CL,
"today tomorrow yesterday noon midnight now".split())
plural = lambda s : Combine(CL(s) + Optional(CL("s")))
week, day, hour, minute, second = map( plural,
"week day hour minute second".split())
am = CL("am")
pm = CL("pm")
COLON = Suppress(':')
# are these actually operators?
in_ = CL("in").setParseAction(replaceWith(1))
from_ = CL("from").setParseAction(replaceWith(1))
before = CL("before").setParseAction(replaceWith(-1))
after = CL("after").setParseAction(replaceWith(1))
till = CL("till").setParseAction(replaceWith(-1))
until = CL("until").setParseAction(replaceWith(-1))
past = CL("past").setParseAction(replaceWith(1))
ago = CL("ago").setParseAction(replaceWith(-1))
next_ = CL("next").setParseAction(replaceWith(1))
last_ = CL("last").setParseAction(replaceWith(-1))
couple = (Optional(CL("a")) + CL("couple") + Optional(CL("of"))).setParseAction(replaceWith(2))
one = CL("one").setParseAction(replaceWith(1))
two = CL("two").setParseAction(replaceWith(2))
three = CL("three").setParseAction(replaceWith(3))
four = CL("four").setParseAction(replaceWith(4))
five = CL("five").setParseAction(replaceWith(5))
six = CL("six").setParseAction(replaceWith(6))
seven = CL("seven").setParseAction(replaceWith(7))
eight = CL("eight").setParseAction(replaceWith(8))
nine = CL("nine").setParseAction(replaceWith(9))
ten = CL("ten").setParseAction(replaceWith(10))
eleven = CL("eleven").setParseAction(replaceWith(11))
twelve = CL("twelve").setParseAction(replaceWith(12))
a_qty = CL("a").setParseAction(replaceWith(1))
integer = Word(nums).setParseAction(lambda t:int(t[0]))
int4 = Group(Word(nums,exact=4).setParseAction(lambda t: [int(t[0][:2]),int(t[0][2:])] ))
qty = integer | couple | a_qty | one | two | three | four | five | six | seven | eight | nine | ten | eleven | twelve
dayName = oneOf( list(calendar.day_name) )
dayOffset = (qty("qty") + (week | day)("timeunit"))
dayFwdBack = (from_ + now.suppress() | ago)("dir")
weekdayRef = (Optional(next_ | last_,1)("dir") + dayName("day"))
dayRef = Optional( (dayOffset + (before | after | till | until | past | from_)("dir") ).setParseAction(convertToTimedelta) ) + \
((yesterday | today | tomorrow)("name")|
weekdayRef("wkdayRef")).setParseAction(convertToDay)
todayRef = (dayOffset + dayFwdBack).setParseAction(convertToTimedelta) | \
(in_("dir") + qty("qty") + day("timeunit")).setParseAction(convertToTimedelta)
dayTimeSpec = dayRef | todayRef
dayTimeSpec.setParseAction(calculateTime)
hourMinuteOrSecond = (hour | minute | second)
timespec = Group(int4("miltime") |
integer("HH") +
Optional(Optional(COLON) + integer("MM")) +
Optional(COLON + integer("SS")) + Optional((am | pm)("ampm"))
)
absTimeSpec = ((noon | midnight | now | timespec("timeparts"))("timeOfDay") +
Optional(dayRef)("dayRef"))
absTimeSpec.setParseAction(convertToAbsTime,calculateTime)
relTimeSpec = qty("qty") + hourMinuteOrSecond("timeunit") + \
(from_ | before | after | till | until | past)("dir") + \
absTimeSpec("absTime") | \
qty("qty") + hourMinuteOrSecond("timeunit") + ago("dir") | \
in_ + qty("qty") + hourMinuteOrSecond("timeunit")
relTimeSpec.setParseAction(convertToTimedelta,calculateTime)
nlTimeExpression = (absTimeSpec | dayTimeSpec | relTimeSpec)
def parse(t):
numbers = {'zero':0,'oh':0,'one':1,'two':2,'three':3,'four':4,'five':5,'six':6,'seven':7,'eight':8,'nine':9,'ten':10,'eleven':11,'twelve':12,'thirteen':13,'fourteen':14,'fifteen':15,'sixteen':16,
'seventeen':17,'eighteen':18,'nineteen':19,'twenty':20,'thirty':30,'forty':40,'fifty':50}
words = t.split()
for index in range(len(words)):
i = words[index]
if i in numbers:
if numbers[i]>19:
words[index] = str(numbers[i])
elif index>0 and words[index-1].isdigit() and int(words[index-1])>19 and int(words[index-1])%10==0:
words[index-1] = str(int(words[index-1]) + numbers[i])
words[index] = ''
else:
words[index] = str(numbers[i])
return nlTimeExpression.parseString(' '.join(words))
# test grammar
tests = """\
today
tomorrow
yesterday
in a couple of days
a couple of days from now
a couple of days from today
in a day
3 days ago
3 days from now
a day ago
now
10 minutes ago
10 minutes from now
in 10 minutes
in a minute
in a couple of minutes
20 seconds ago
in 30 seconds
20 seconds before noon
20 seconds before noon tomorrow
noon
midnight
noon tomorrow
6am tomorrow
0800 yesterday
12:15 AM today
3pm 2 days from today
a week from today
a week from now
3 weeks ago
noon next Sunday
noon Sunday
noon last Sunday
three days ago
twelve thirty""".splitlines()
if __name__=="__main__":
for t in tests:
print (t, "(relative to %s)" % datetime.now())
# res = nlTimeExpression.parseString(t)
res = parse(t)
if "calculatedTime" in res:
print (res.calculatedTime)
else:
print ("???")
print