cools commited on
Commit
8688e3a
1 Parent(s): 0e53375

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +356 -1
app.py CHANGED
@@ -26,5 +26,360 @@ del_hours_rel_1 = re.compile('([0-9]{1,2}|(|a|the|one|two|three|few|four|five|si
26
  adverbs = re.compile('(from|after|before|next|end|this|past) ', re.IGNORECASE) # Adverbs used to resolve if multiple patterns are "hit". This leads to the "addition" problem (described on notion).
27
 
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- x = st.text_input("Request", value="")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  adverbs = re.compile('(from|after|before|next|end|this|past) ', re.IGNORECASE) # Adverbs used to resolve if multiple patterns are "hit". This leads to the "addition" problem (described on notion).
27
 
28
 
29
+ def abs_date_parse(snip):
30
+ month_dict = {'jan': 1, 'january': 1, 'feb': 2, 'february': 2, 'mar': 3, 'march': 3, 'apr': 4, 'april': 4, 'may': 5, 'jun': 6, 'june': 6, 'jul': 7, 'july': 7, 'aug': 8, 'august': 8, 'sep': 9, 'sept': 9, 'september': 9, 'oct': 10, 'october': 10, 'nov': 11, 'november': 11, 'dec': 12, 'december': 12}
31
+ month, date, year = None, None, None
32
+ snip = snip.replace(',', '').lower()
33
+ m = re.search('[a-zA-Z]{3}', snip)
34
+
35
+ d = datetime.datetime.now()
36
+ current_month, current_date, current_year = d.month, d.date, d.year
37
+
38
+ if m is not None:
39
+ words = snip.split(' ')
40
+ for w in words:
41
+ if w in month_dict:
42
+ month = month_dict[w]
43
+ if w.isdigit() and int(w) > 31:
44
+ year = int(w)
45
+ if w.isdigit() and int(w) <= 31:
46
+ date = int(w)
47
+ if month is None:
48
+ raise Exception("There should have been a month, but could not identify")
49
+ if date is None:
50
+ raise Exception("There should have been a date, but could not identify")
51
+ if year is None: # May need to check around year-changes if this gets funky
52
+ year = d.year
53
+ if datetime.datetime(year, month, date) < datetime.datetime.now():
54
+ year = d.year + 1
55
+ else:
56
+ if '-' in snip:
57
+ tokens = snip.split('-')
58
+ if '/' in snip:
59
+ tokens = snip.split('/')
60
+ if '.' in snip:
61
+ tokens = snip.split('.')
62
+ remainder = copy.deepcopy(tokens)
63
+ for (i,t) in enumerate(tokens):
64
+ if int(t) > 31 and year is None:
65
+ year = int(t)
66
+ remainder.remove(t)
67
+ if int(t) > 12 and int(t) <= 31 and date is None:
68
+ date = int(t)
69
+ remainder.remove(t)
70
+
71
+ if len(remainder) == 1: # Just fit it where it belongs
72
+ if month is None:
73
+ month = int(remainder[0])
74
+ if date is None:
75
+ date = int(remainder[0])
76
+ if len(remainder) == 2: # Probably lack of clarity on month and date
77
+ print("There are multiple options for what this could mean. Going to select shortest one.")
78
+ test_date_1 = datetime.datetime(d.year, int(remainder[0]), int(remainder[1]))
79
+ test_date_2 = datetime.datetime(d.year, int(remainder[1]), int(remainder[0]))
80
+ d1, d2 = (test_date_1-d).total_seconds(), (test_date_2-d).total_seconds()
81
+ year = d.year
82
+ if d1 < 0 and d2 < 0:
83
+ test_date_1 = datetime.datetime(d.year+1, int(remainder[0]), int(remainder[1]))
84
+ test_date_2 = datetime.datetime(d.year+1, int(remainder[1]), int(remainder[0]))
85
+ d1, d2 = (test_date_1-d).total_seconds(), (test_date_2-d).total_seconds()
86
+ year = d.year+1
87
+ if d1 > 0 and (d2 < 0 or d1 < d2):
88
+ month, date = remainder[0], remainder[1]
89
+ if d2 > 0 and (d1 < 0 or d2 < d1):
90
+ month, date = remainder[1], remainder[0]
91
+ if len(remainder) == 3:
92
+ raise Exception("Something OOFED")
93
+
94
+ if year is None: # May need to check around year-changes if this gets funky
95
+ year = d.year
96
+ if datetime.datetime(year, month, date) < datetime.datetime.now():
97
+ year = d.year + 1
98
+ return datetime.datetime(int(year), int(month), int(date))
99
 
100
+ def rel_date_parse(snip):
101
+ snip = snip.lower()
102
+ d = datetime.datetime.now()
103
+ dow_dict = {'monday':0, 'mon':0, 'tue':1, 'tues':1, 'tuesday':1, 'wed':2, 'weds':2, 'wednesday':2, 'thu':3, 'thurs':3, 'fri':4, 'friday':4, 'sat':5, 'saturday':5, 'sun':6, 'sunday':6}
104
+ if snip in dow_dict:
105
+ current_dow = d.weekday()
106
+ dow = dow_dict[snip]
107
+ if dow <= current_dow: # If they say same day, should that be next week? Or this week?
108
+ dow += 7
109
+ return datetime.timedelta(days=dow-current_dow)
110
+ if "tomorrow" in snip:
111
+ return datetime.timedelta(days=1)
112
+ if "now" in snip or "today" in snip or "tonight" in snip:
113
+ return datetime.timedelta(days=0)
114
+ if "day" in snip.split(' ') or "days" in snip.split(' '):
115
+ if "one" in snip.split(' ') or " day" and "days" not in snip.split(' '):
116
+ return datetime.timedelta(days=1)
117
+ if "two" in snip.split(' '):
118
+ return datetime.timedelta(days=2)
119
+ if "three" in snip.split(' '):
120
+ return datetime.timedelta(days=3)
121
+ if "four" in snip.split(' '):
122
+ return datetime.timedelta(days=4)
123
+ if "five" in snip.split(' '):
124
+ return datetime.timedelta(days=5)
125
+ if "six" in snip.split(' '):
126
+ return datetime.timedelta(days=6)
127
+ if "seven" in snip.split(' '):
128
+ return datetime.timedelta(days=7)
129
+ if "eight" in snip.split(' '):
130
+ return datetime.timedelta(days=8)
131
+ if "nine" in snip.split(' '):
132
+ return datetime.timedelta(days=9)
133
+ if "ten" in snip.split(' '):
134
+ return datetime.timedelta(days=10)
135
+
136
+ def abs_time_parse(snip, inp):
137
+ snip = snip.lower()
138
+ if ':' in snip:
139
+ hours = int(snip.split(':')[0].strip())
140
+ minutes = int(snip.split(':')[1][0:2].strip())
141
+ elif 'noon' in snip:
142
+ hours, minutes = 12, 0
143
+ elif 'midnight' in snip:
144
+ hours, minutes = 0, 0
145
+ else:
146
+ digits = [c for c in snip if c.isdigit()]
147
+ if len(digits) >= 3:
148
+ hours = int("".join(digits[:-2]))
149
+ minutes = int("".join(digits[-2:]))
150
+ if len(digits) <= 2:
151
+ hours = int("".join(digits))
152
+ minutes = 0
153
+ if 'p' in snip and hours < 12:
154
+ hours += 12
155
+ if 'a' in snip and hours >= 12:
156
+ hours -= 12
157
+ if 'p' not in snip and 'a' not in snip:
158
+ # print("\nNo 'AM' or 'PM' provided. Making assumptions.")
159
+ # if ("evening" in inp or "night" in inp or "afternoon" in inp) and hours < 12:
160
+ # hours += 12
161
+ # elif "morning" in inp and hours > 12:
162
+ # morning -= 12
163
+ # else:
164
+ pass
165
+ return datetime.time(hour=hours, minute=minutes)
166
+
167
+ def rel_time_parse(snip):
168
+ snip = snip.lower()
169
+ digits = [c for c in snip if c.isdigit()]
170
+ val = 0
171
+ if len(digits) > 0:
172
+ val = int(" ".join(digits))
173
+ else:
174
+ if ("hour" in snip and "hours" not in snip) or ("minute" in snip and "minutes" not in snip):
175
+ val = 1
176
+ if "two" in snip:
177
+ val = 2
178
+ if "three" in snip or "few" in snip:
179
+ val = 3
180
+ if "four" in snip:
181
+ val = 4
182
+ if "five" in snip:
183
+ val = 5
184
+ if "six" in snip:
185
+ val = 6
186
+ if "seven" in snip:
187
+ val = 7
188
+ if "eight" in snip:
189
+ val = 8
190
+ if "nine" in snip:
191
+ val = 9
192
+ if "ten" in snip:
193
+ val = 10
194
+ if "hour" in snip and "minute" not in snip:
195
+ return datetime.timedelta(hours=val)
196
+ if "minute" in snip and "hour" not in snip:
197
+ return datetime.timedelta(minutes=val)
198
+ if "hour" in snip and "minute" in snip:
199
+ raise Exception("Does not support a relative time input with both (i) hours and (ii) minutes")
200
+
201
+ def get_relevant_adverb(inp, term_1, term_2, adverbs, adv_inds):
202
+ m_1 = re.search(re.compile(term_1, re.IGNORECASE), inp)
203
+ m_2 = re.search(re.compile(term_2,re.IGNORECASE), inp)
204
+ if m_1.span()[1] < m_2.span()[0]:
205
+ start, end = m_1.span()[1], m_2.span()[0]
206
+ if m_2.span()[1] < m_1.span()[0]:
207
+ start, end = m_2.span()[1], m_1.span()[0]
208
+ for (i, (s, e)) in enumerate(adv_inds):
209
+ if s >= start and e <= end:
210
+ return i, adverbs[i]
211
+ return None, None
212
+
213
+ def rel_rel_date_resolver(inp, term_1, term_2, td_1, td_2, adverbs, adv_inds):
214
+ term_1_start = re.search(re.compile(term_1, re.IGNORECASE), inp).span()[0]
215
+ term_2_start = re.search(re.compile(term_2, re.IGNORECASE), inp).span()[0]
216
+ adv_ind, adv = get_relevant_adverb(inp, term_1, term_2, adverbs, adv_inds)
217
+ if adv in ["after", "from", "past"]:
218
+ return td_1 + td_2
219
+ if adv in ["before"]:
220
+ if term_1_start < term_2_start:
221
+ return td_2-td_1
222
+ else:
223
+ return td_1-td_2
224
+ if adv is None:
225
+ if td_1 == td_2: # Assume that they are the same
226
+ return td_1
227
+ else:
228
+ raise Exception("There is a conflict with the datetimes provided. '" + term_1.upper() + "' != '" + term_2.upper() + "'")
229
+
230
+ def abs_rel_date_resolver(inp, term_abs, term_rel, abs_dt, rel_td, adverbs, adv_inds):
231
+ term_abs_start = re.search(re.compile(term_abs, re.IGNORECASE), inp).span()[0]
232
+ term_rel_start = re.search(re.compile(term_rel, re.IGNORECASE), inp).span()[0]
233
+ adv_ind, adv = get_relevant_adverb(inp, term_abs, term_rel, adverbs, adv_inds)
234
+ if adv in ["after", "from", "past"]:
235
+ return rel_td
236
+ if adv in ["before"]:
237
+ return -rel_td
238
+ if adv is None: # Check consistency
239
+ if abs_dt.month == (datetime.datetime.now()+rel_td).month and abs_dt.day == (datetime.datetime.now()+rel_td).day: # The same
240
+ print("----------------------REDUNDANCY DETECTED-----------------------")
241
+ return datetime.timedelta(days=0, hours=0, minutes=0)
242
+ else:
243
+ raise Exception("There is a conflict with the datetimes provided. '" + str(abs_dt).upper() + "' != '" + term_rel.upper() + "'")
244
+
245
+
246
+ def time_parse(inp, debug=False):
247
+ match_mdy_abs_1 = re.search(mdy_abs_1, inp)
248
+ match_mdy_abs_2 = re.search(mdy_abs_2, inp)
249
+ match_mdy_abs_3 = re.search(mdy_abs_3, inp)
250
+ match_m_abs_1 = re.search(m_abs_1, inp)
251
+ match_d_abs_1 = re.search(d_abs_1, inp)
252
+
253
+ match_mdy_rel_1 = re.search(mdy_rel_1, inp)
254
+ match_mdy_rel_2 = re.search(mdy_rel_2, inp)
255
+ match_del_days_rel_1 = re.search(del_days_rel_1, inp) # Should be re.finditer in case people go crazy
256
+
257
+ m_adverbs = re.search(adverbs, inp)
258
+
259
+ abs_date_extracted = False
260
+ rel_date_extracted = False
261
+ abs_date_extraction = ""
262
+ rel_date_extraction = []
263
+ adv = []
264
+
265
+
266
+ if match_mdy_abs_1 is not None: # Unclear, do nearest neighbor
267
+ abs_date_extraction = inp[match_mdy_abs_1.span()[0]:match_mdy_abs_1.span()[1]].strip()
268
+ abs_date_extracted = True
269
+ if match_mdy_abs_2 is not None and not abs_date_extracted: # Month first
270
+ abs_date_extraction = inp[match_mdy_abs_2.span()[0]:match_mdy_abs_2.span()[1]].strip()
271
+ abs_date_extracted = True
272
+ if match_mdy_abs_3 is not None and not abs_date_extracted: # Date first
273
+ abs_date_extraction = inp[match_mdy_abs_3.span()[0]:match_mdy_abs_3.span()[1]].strip()
274
+ abs_date_extracted = True
275
+ if match_m_abs_1 is not None and match_d_abs_1 is not None and not abs_date_extracted: # Fix this to be in order? Or make this a new regex?
276
+ abs_date_extraction = inp[match_m_abs_1.span()[0]:match_m_abs_1.span()[1]].strip()
277
+ abs_date_extraction += inp[match_d_abs_1.span()[0]:match_d_abs_1.span()[1]].strip()
278
+
279
+ if match_mdy_rel_1 is not None:
280
+ rel_date_extracted = True
281
+ rel_date_extraction.append(inp[match_mdy_rel_1.span()[0]:match_mdy_rel_1.span()[1]].strip())
282
+ if match_mdy_rel_2 is not None: # Technically, should do re.finditer in case it appears multiple times
283
+ rel_date_extracted = True
284
+ rel_date_extraction.append(inp[match_mdy_rel_2.span()[0]:match_mdy_rel_2.span()[1]].strip())
285
+ if match_del_days_rel_1 is not None: # Technically, should do re.finditer in case it appears multiple times
286
+ rel_date_extracted = True
287
+ rel_date_extraction.append(inp[match_del_days_rel_1.span()[0]:match_del_days_rel_1.span()[1]].strip())
288
+
289
+ adv_ind = []
290
+ adv = []
291
+ if m_adverbs is not None: # Remember, this is used for addition
292
+ adv_ind = ([(m.start(0), m.end(0)) for m in re.finditer(adverbs,inp)])
293
+ for (s,e) in adv_ind:
294
+ adv.append(inp[s:e].strip())
295
+
296
+ purged = inp
297
+ purged = inp.replace(abs_date_extraction, '')
298
+ for rde in rel_date_extraction:
299
+ purged = purged.replace(rde, '')
300
+
301
+ abs_time_extracted = False
302
+ rel_time_extracted = False
303
+ abs_time_extraction = []
304
+ rel_time_extraction = []
305
+
306
+ match_hhmm_abs_1 = re.search(hhmm_abs_1, purged) # Remember, we should technically only operate on non-months
307
+ match_hhmm_abs_2 = re.search(hhmm_abs_2, purged)
308
+ match_del_hours_rel_1 = re.search(del_hours_rel_1, purged)
309
+
310
+ if match_hhmm_abs_1 is not None:
311
+ abs_time_extracted = True
312
+ abs_time_extraction.append(purged[match_hhmm_abs_1.span()[0]:match_hhmm_abs_1.span()[1]].strip())
313
+ if match_hhmm_abs_2 is not None:
314
+ abs_time_extracted = True
315
+ abs_time_extraction.append(purged[match_hhmm_abs_2.span()[0]:match_hhmm_abs_2.span()[1]].strip())
316
+ if match_del_hours_rel_1 is not None:
317
+ rel_time_extraction.append(purged[match_del_hours_rel_1.span()[0]:match_del_hours_rel_1.span()[1]].strip())
318
+
319
+ if debug:
320
+ print("Abs Date: \t\t" + abs_date_extraction)
321
+ print("Rel Date: \t\t" + str(rel_date_extraction))
322
+ print("Abs Time: \t\t" + str(abs_time_extraction))
323
+ print("Rel Time: \t\t" + str(rel_time_extraction))
324
+ print("Adverbs: \t\t" + str(adv))
325
+
326
+ d = datetime.datetime.now()
327
+
328
+ abs_date = None
329
+ rel_date_deltas = []
330
+ abs_times = []
331
+ rel_time_deltas = []
332
+
333
+
334
+ if abs_date_extraction != "":
335
+ abs_date = abs_date_parse(abs_date_extraction)
336
+ for rde in rel_date_extraction:
337
+ rel_date_deltas.append(rel_date_parse(rde))
338
+ for ate in abs_time_extraction:
339
+ abs_times.append(abs_time_parse(ate, inp))
340
+ for rte in rel_time_extraction:
341
+ rel_time_deltas.append(rel_time_parse(rte))
342
+
343
+ if debug:
344
+ print('\n\n')
345
+ print('Current Date: \t\t' + str(d))
346
+ print('Abs Date: \t\t' + str(abs_date))
347
+ print('Rel Date Deltas: \t' + str(rel_date_deltas))
348
+ print('Abs Time: \t\t' + str(abs_times))
349
+ print('Rel Time Deltas: \t' + str(rel_time_deltas))
350
+
351
+
352
+ if len(abs_times) == 0 and len(rel_time_deltas) == 0:
353
+ raise Exception("Could not identify a time. Be sure to use 'AM/PM' if you specify an absolute time")
354
+
355
+ # Relative Time Deltas Resolved Everythign Else
356
+ if len(rel_time_deltas) == 1 and abs_date is None and len(rel_date_deltas) == 0 and len(abs_times) == 0: # Straight up "in X hours"
357
+ datetime_request = d + rel_time_deltas[0]
358
+ return datetime_request
359
+ if len(rel_time_deltas) == 1 and abs_date is None and len(rel_date_deltas) == 1 and len(abs_times) == 0:
360
+ datetime_request = d + rel_time_deltas[0] + rel_date_deltas[0]
361
+ return datetime_request
362
+
363
+
364
+ if len(rel_date_deltas) == 1 and abs_date is None:
365
+ datetime_request = d + rel_date_deltas[0]
366
+ if len(rel_date_deltas) == 2 and abs_date is None: # Two conflicting relative
367
+ datetime_request = d + rel_rel_date_resolver(inp, rel_date_extraction[0], rel_date_extraction[1], rel_date_deltas[0], rel_date_deltas[1], adv, adv_ind)
368
+ if len(rel_date_deltas) == 1 and abs_date is not None: # Abs date conflicting with rel
369
+ datetime_request = abs_date + abs_rel_date_resolver(inp, abs_date_extraction, rel_date_extraction[0], abs_date, rel_date_deltas[0], adv, adv_ind)
370
+
371
+ # Additional Rules: (1) 3+ Rel-rel date conflicts, (2) 2 rel-rel date + abs date conflicts
372
+ if len(rel_date_deltas) == 3 or (len(rel_date_deltas) == 2 and abs_date is not None):
373
+ raise Exception("Have not yet implemented Resolver for 3 rel-rel dates or 2 rel-rel dates + 1 abs date")
374
+
375
+ if len(abs_times) == 1 and len(rel_time_deltas) == 0:
376
+ datetime_request = datetime_request.replace(hour=abs_times[0].hour, minute=abs_times[0].minute)
377
+
378
+ # Final check: if there total time is less, then just add a day?
379
+ return datetime_request
380
+
381
+
382
+ x = st.text_input("Request", value="")
383
+ inp = "I need a ride tomorrow march 26 to del mar at 6p"
384
+ d = time_parse(inp, debug=True)
385
+ print('Request Time: \t\t' + d.strftime("%m/%d @ %I:%M %p"))