Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,9 +5,9 @@ import datetime
|
|
5 |
import copy
|
6 |
|
7 |
# Absolute Date [or pieces of it]
|
8 |
-
mdy_abs_1 = re.compile('[0-9]{1,2}[-\/.][0-9]{1,2}([-\/.][0-9]{4})?', re.IGNORECASE) #12-13-2023"
|
9 |
-
|
10 |
-
|
11 |
m_abs_1 = re.compile('(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|january|february|march|april|may|june|july|august|sept|september|october|november|december)', re.IGNORECASE) # Just month
|
12 |
d_abs_1 = re.compile('[0-9]{1,2}(th|st|nd|rd)*', re.IGNORECASE) # Just date
|
13 |
|
@@ -25,7 +25,6 @@ del_hours_rel_1 = re.compile('([0-9]{1,2}|(|a|the|one|two|three|few|four|five|si
|
|
25 |
|
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
|
@@ -40,6 +39,8 @@ def abs_date_parse(snip):
|
|
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:
|
@@ -101,12 +102,15 @@ 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 |
-
|
105 |
-
|
106 |
-
dow
|
107 |
-
|
108 |
-
dow
|
109 |
-
|
|
|
|
|
|
|
110 |
if "tomorrow" in snip:
|
111 |
return datetime.timedelta(days=1)
|
112 |
if "now" in snip or "today" in snip or "tonight" in snip:
|
@@ -114,23 +118,23 @@ def rel_date_parse(snip):
|
|
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):
|
@@ -155,13 +159,8 @@ def abs_time_parse(snip, inp):
|
|
155 |
if 'a' in snip and hours >= 12:
|
156 |
hours -= 12
|
157 |
if 'p' not in snip and 'a' not in snip:
|
158 |
-
|
159 |
-
|
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):
|
@@ -214,18 +213,21 @@ 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 |
-
|
|
|
227 |
else:
|
228 |
-
raise
|
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]
|
@@ -237,7 +239,7 @@ def abs_rel_date_resolver(inp, term_abs, term_rel, abs_dt, rel_td, adverbs, adv_
|
|
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("
|
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() + "'")
|
@@ -286,12 +288,12 @@ def time_parse(inp, debug=False):
|
|
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 |
-
|
290 |
-
|
291 |
if m_adverbs is not None: # Remember, this is used for addition
|
292 |
-
|
293 |
-
for (s,e) in
|
294 |
-
|
295 |
|
296 |
purged = inp
|
297 |
purged = inp.replace(abs_date_extraction, '')
|
@@ -321,7 +323,7 @@ def time_parse(inp, debug=False):
|
|
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(
|
325 |
|
326 |
d = datetime.datetime.now()
|
327 |
|
@@ -361,21 +363,38 @@ def time_parse(inp, debug=False):
|
|
361 |
if len(rel_time_deltas) == 1 and abs_date is None and len(rel_date_deltas) == 1 and len(abs_times) == 0:
|
362 |
datetime_request = d + rel_time_deltas[0] + rel_date_deltas[0]
|
363 |
return datetime_request
|
364 |
-
|
|
|
|
|
365 |
|
366 |
if len(rel_date_deltas) == 1 and abs_date is None:
|
367 |
datetime_request = d + rel_date_deltas[0]
|
368 |
if len(rel_date_deltas) == 2 and abs_date is None: # Two conflicting relative
|
369 |
-
datetime_request = d + rel_rel_date_resolver(inp, rel_date_extraction[0], rel_date_extraction[1], rel_date_deltas[0], rel_date_deltas[1],
|
370 |
if len(rel_date_deltas) == 1 and abs_date is not None: # Abs date conflicting with rel
|
371 |
-
datetime_request = abs_date + abs_rel_date_resolver(inp, abs_date_extraction, rel_date_extraction[0], abs_date, rel_date_deltas[0],
|
372 |
-
|
373 |
-
|
374 |
-
if len(rel_date_deltas) ==
|
375 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
376 |
|
377 |
# If no date, then assume today
|
378 |
if datetime_request is None:
|
|
|
379 |
datetime_request = datetime.datetime.now()
|
380 |
|
381 |
if len(abs_times) == 1 and len(rel_time_deltas) == 0:
|
@@ -383,11 +402,11 @@ def time_parse(inp, debug=False):
|
|
383 |
|
384 |
# Final check: if there total time is less, then just add a day?
|
385 |
if datetime_request < datetime.datetime.now():
|
|
|
386 |
datetime_request += datetime.timedelta(days=1)
|
387 |
return datetime_request
|
388 |
|
389 |
|
390 |
-
|
391 |
x = st.text_input("Request", value="")
|
392 |
st.write(x)
|
393 |
d = time_parse(x, debug=True)
|
|
|
5 |
import copy
|
6 |
|
7 |
# Absolute Date [or pieces of it]
|
8 |
+
mdy_abs_1 = re.compile('[^0-9][0-9]{1,2}[-\/.][0-9]{1,2}([-\/.][0-9]{4})?([^a-z]|$)', re.IGNORECASE) #12-13-2023, and no digits before"
|
9 |
+
mdy_abs_3 = re.compile('(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|january|february|march|april|may|june|july|august|sept|september|october|november|december)\.? ?[0-9]{1,2}(th|st|nd|rd)*,? ?([0-9]{4})?', re.IGNORECASE) # July 3
|
10 |
+
mdy_abs_2 = re.compile('[0-9]{1,2}(th|st|nd|rd)* ?(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|january|february|march|april|may|june|july|august|sept|september|october|november|december),? ?([0-9]{4})?', re.IGNORECASE)
|
11 |
m_abs_1 = re.compile('(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|january|february|march|april|may|june|july|august|sept|september|october|november|december)', re.IGNORECASE) # Just month
|
12 |
d_abs_1 = re.compile('[0-9]{1,2}(th|st|nd|rd)*', re.IGNORECASE) # Just date
|
13 |
|
|
|
25 |
|
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 |
def abs_date_parse(snip):
|
29 |
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}
|
30 |
month, date, year = None, None, None
|
|
|
39 |
for w in words:
|
40 |
if w in month_dict:
|
41 |
month = month_dict[w]
|
42 |
+
continue
|
43 |
+
w = w.replace('st', '').replace('nd', '').replace('rd', '').replace('th', '')
|
44 |
if w.isdigit() and int(w) > 31:
|
45 |
year = int(w)
|
46 |
if w.isdigit() and int(w) <= 31:
|
|
|
102 |
snip = snip.lower()
|
103 |
d = datetime.datetime.now()
|
104 |
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}
|
105 |
+
dow_list = list(dow_dict.keys())
|
106 |
+
for dow in dow_list:
|
107 |
+
if dow in snip:
|
108 |
+
current_dow = d.weekday()
|
109 |
+
dow = dow_dict[dow]
|
110 |
+
if dow < current_dow: # If they say same day, should that be next week? Or this week?. Need to fix eventyally
|
111 |
+
print("--------------THIS DAY-OF-WEEK HAS PASSED----ADDING +7 DAYS------------------")
|
112 |
+
dow += 7
|
113 |
+
return datetime.timedelta(days=dow-current_dow)
|
114 |
if "tomorrow" in snip:
|
115 |
return datetime.timedelta(days=1)
|
116 |
if "now" in snip or "today" in snip or "tonight" in snip:
|
|
|
118 |
if "day" in snip.split(' ') or "days" in snip.split(' '):
|
119 |
if "one" in snip.split(' ') or " day" and "days" not in snip.split(' '):
|
120 |
return datetime.timedelta(days=1)
|
121 |
+
if "two" in snip.split(' ') or "2" in snip.split(' '):
|
122 |
return datetime.timedelta(days=2)
|
123 |
+
if "three" in snip.split(' ') or "3" in snip.split(' '):
|
124 |
return datetime.timedelta(days=3)
|
125 |
+
if "four" in snip.split(' ') or "4" in snip.split(' '):
|
126 |
return datetime.timedelta(days=4)
|
127 |
+
if "five" in snip.split(' ') or "5" in snip.split(' '):
|
128 |
return datetime.timedelta(days=5)
|
129 |
+
if "six" in snip.split(' ') or "6" in snip.split(' '):
|
130 |
return datetime.timedelta(days=6)
|
131 |
+
if "seven" in snip.split(' ') or "7" in snip.split(' '):
|
132 |
return datetime.timedelta(days=7)
|
133 |
+
if "eight" in snip.split(' ') or "8" in snip.split(' '):
|
134 |
return datetime.timedelta(days=8)
|
135 |
+
if "nine" in snip.split(' ') or "9" in snip.split(' '):
|
136 |
return datetime.timedelta(days=9)
|
137 |
+
if "ten" in snip.split(' ') or "10" in snip.split(' '):
|
138 |
return datetime.timedelta(days=10)
|
139 |
|
140 |
def abs_time_parse(snip, inp):
|
|
|
159 |
if 'a' in snip and hours >= 12:
|
160 |
hours -= 12
|
161 |
if 'p' not in snip and 'a' not in snip:
|
162 |
+
print("\nNo 'AM' or 'PM' provided. Making assumptions.")
|
163 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
164 |
return datetime.time(hour=hours, minute=minutes)
|
165 |
|
166 |
def rel_time_parse(snip):
|
|
|
213 |
term_1_start = re.search(re.compile(term_1, re.IGNORECASE), inp).span()[0]
|
214 |
term_2_start = re.search(re.compile(term_2, re.IGNORECASE), inp).span()[0]
|
215 |
adv_ind, adv = get_relevant_adverb(inp, term_1, term_2, adverbs, adv_inds)
|
216 |
+
print(adv)
|
217 |
+
print(100000)
|
218 |
if adv in ["after", "from", "past"]:
|
219 |
+
return td_1 + td_2, adv_ind
|
220 |
if adv in ["before"]:
|
221 |
if term_1_start < term_2_start:
|
222 |
+
return td_2-td_1, adv_ind
|
223 |
else:
|
224 |
+
return td_1-td_2, adv_ind
|
225 |
if adv is None:
|
226 |
if td_1 == td_2: # Assume that they are the same
|
227 |
+
print("-------------------ASSUMING CONSISTENCY BETWEEN RELATIVES-------------------------")
|
228 |
+
return td_1, adv_ind
|
229 |
else:
|
230 |
+
raise Exception("There is a conflict with the datetimes provided. '" + term_1.upper() + "' != '" + term_2.upper() + "'")
|
231 |
|
232 |
def abs_rel_date_resolver(inp, term_abs, term_rel, abs_dt, rel_td, adverbs, adv_inds):
|
233 |
term_abs_start = re.search(re.compile(term_abs, re.IGNORECASE), inp).span()[0]
|
|
|
239 |
return -rel_td
|
240 |
if adv is None: # Check consistency
|
241 |
if abs_dt.month == (datetime.datetime.now()+rel_td).month and abs_dt.day == (datetime.datetime.now()+rel_td).day: # The same
|
242 |
+
print("-------------------------REDUNDANCY DETECTED--------------------------------------")
|
243 |
return datetime.timedelta(days=0, hours=0, minutes=0)
|
244 |
else:
|
245 |
raise Exception("There is a conflict with the datetimes provided. '" + str(abs_dt).upper() + "' != '" + term_rel.upper() + "'")
|
|
|
288 |
rel_date_extracted = True
|
289 |
rel_date_extraction.append(inp[match_del_days_rel_1.span()[0]:match_del_days_rel_1.span()[1]].strip())
|
290 |
|
291 |
+
adv_inds = []
|
292 |
+
advs = []
|
293 |
if m_adverbs is not None: # Remember, this is used for addition
|
294 |
+
adv_inds = ([(m.start(0), m.end(0)) for m in re.finditer(adverbs,inp)])
|
295 |
+
for (s,e) in adv_inds:
|
296 |
+
advs.append(inp[s:e].strip())
|
297 |
|
298 |
purged = inp
|
299 |
purged = inp.replace(abs_date_extraction, '')
|
|
|
323 |
print("Rel Date: \t\t" + str(rel_date_extraction))
|
324 |
print("Abs Time: \t\t" + str(abs_time_extraction))
|
325 |
print("Rel Time: \t\t" + str(rel_time_extraction))
|
326 |
+
print("Adverbs: \t\t" + str(advs))
|
327 |
|
328 |
d = datetime.datetime.now()
|
329 |
|
|
|
363 |
if len(rel_time_deltas) == 1 and abs_date is None and len(rel_date_deltas) == 1 and len(abs_times) == 0:
|
364 |
datetime_request = d + rel_time_deltas[0] + rel_date_deltas[0]
|
365 |
return datetime_request
|
366 |
+
|
367 |
+
if abs_date is not None and len(rel_date_deltas) == 0: # Regular abs date
|
368 |
+
datetime_request = abs_date
|
369 |
|
370 |
if len(rel_date_deltas) == 1 and abs_date is None:
|
371 |
datetime_request = d + rel_date_deltas[0]
|
372 |
if len(rel_date_deltas) == 2 and abs_date is None: # Two conflicting relative
|
373 |
+
datetime_request = d + rel_rel_date_resolver(inp, rel_date_extraction[0], rel_date_extraction[1], rel_date_deltas[0], rel_date_deltas[1], advs, adv_inds)[0]
|
374 |
if len(rel_date_deltas) == 1 and abs_date is not None: # Abs date conflicting with rel
|
375 |
+
datetime_request = abs_date + abs_rel_date_resolver(inp, abs_date_extraction, rel_date_extraction[0], abs_date, rel_date_deltas[0], advs, adv_inds)
|
376 |
+
|
377 |
+
###### EXPERIMENTAL###########
|
378 |
+
if len(rel_date_deltas) == 2 and abs_date is not None:
|
379 |
+
print("--------------------------------TRYING EXPERIMENTAL CODE (METHOD 1)---------------------------")
|
380 |
+
# Method 1: Resolve rel-rel, then resolve that with abs
|
381 |
+
# Method 2: Resolve rel-abs, then resolve that with additional rel? For now, I only do method 1
|
382 |
+
rel_td_resolved, adv_ind = rel_rel_date_resolver(inp, rel_date_extraction[0], rel_date_extraction[1], rel_date_deltas[0], rel_date_deltas[1], advs, adv_inds)
|
383 |
+
print(rel_td_resolved)
|
384 |
+
if adv_ind is None: # Assuming consistency between adverbs
|
385 |
+
datetime_request = abs_date + abs_rel_date_resolver(inp, abs_date_extraction, rel_date_extraction[0], abs_date, rel_td_resolved, advs, adv_inds)
|
386 |
+
if adv_ind is not None:
|
387 |
+
adv_inds.pop(adv_ind)
|
388 |
+
advs.pop(adv_ind)
|
389 |
+
datetime_request = abs_date + abs_rel_date_resolver(inp, abs_date_extraction, rel_date_extraction[0], abs_date, rel_td_resolved, advs, adv_inds)
|
390 |
+
|
391 |
+
# Additional Rules: (1) 3+ Rel-rel date conflicts
|
392 |
+
if len(rel_date_deltas) == 3:
|
393 |
+
raise Exception("Have not yet implemented Resolver for 3 rel-rel dates")
|
394 |
|
395 |
# If no date, then assume today
|
396 |
if datetime_request is None:
|
397 |
+
print("------------------Assuming datetime--------------")
|
398 |
datetime_request = datetime.datetime.now()
|
399 |
|
400 |
if len(abs_times) == 1 and len(rel_time_deltas) == 0:
|
|
|
402 |
|
403 |
# Final check: if there total time is less, then just add a day?
|
404 |
if datetime_request < datetime.datetime.now():
|
405 |
+
print("Requested datetime is less than current one. Adding a day")
|
406 |
datetime_request += datetime.timedelta(days=1)
|
407 |
return datetime_request
|
408 |
|
409 |
|
|
|
410 |
x = st.text_input("Request", value="")
|
411 |
st.write(x)
|
412 |
d = time_parse(x, debug=True)
|