tjl8 commited on
Commit
fdd7166
·
verified ·
1 Parent(s): 5eeac4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -13
app.py CHANGED
@@ -363,28 +363,58 @@ def extract_month_year(q):
363
  yr = int(ym.group()) if ym else None
364
  return mon, yr
365
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
366
  def extract_date_range(query):
367
  """
368
  Extracts a start and end month-year from a question like 'from Jan 2024 to May 2025'
 
369
  """
370
  month_map = {
371
  "january": 1, "february": 2, "march": 3, "april": 4, "may": 5, "june": 6,
372
  "july": 7, "august": 8, "september": 9, "october": 10, "november": 11, "december": 12
373
  }
374
- # pattern = r"(?i)from\s+([a-zA-Z]+)\s+(\d{4})\s+(to|until)\s+([a-zA-Z]+)\s+(\d{4})"
375
- pattern = [
376
- r"(?i)(from|between)?\s*([a-zA-Z]+)\s+(\d{4})\s*(to|through|and|-)\s*([a-zA-Z]+)\s+(\d{4})",
377
  ]
378
- match = re.search(pattern, query)
379
- if match:
380
- start_month_str, start_year = match.group(1).lower(), int(match.group(2))
381
- end_month_str, end_year = match.group(4).lower(), int(match.group(5))
382
- start_month = month_map.get(start_month_str)
383
- end_month = month_map.get(end_month_str)
384
- if start_month and end_month:
385
- start_date = datetime(start_year, start_month, 1)
386
- end_date = datetime(end_year, end_month, 28)
387
- return start_date, end_date
 
 
 
 
 
388
  return None, None
389
 
390
  def extract_topic_match(query, df):
 
363
  yr = int(ym.group()) if ym else None
364
  return mon, yr
365
 
366
+ # def extract_date_range(query):
367
+ # """
368
+ # Extracts a start and end month-year from a question like 'from Jan 2024 to May 2025'
369
+ # """
370
+ # month_map = {
371
+ # "january": 1, "february": 2, "march": 3, "april": 4, "may": 5, "june": 6,
372
+ # "july": 7, "august": 8, "september": 9, "october": 10, "november": 11, "december": 12
373
+ # }
374
+ # # pattern = r"(?i)from\s+([a-zA-Z]+)\s+(\d{4})\s+(to|until)\s+([a-zA-Z]+)\s+(\d{4})"
375
+ # pattern = [
376
+ # r"(?i)(from|between)?\s*([a-zA-Z]+)\s+(\d{4})\s*(to|through|and|-)\s*([a-zA-Z]+)\s+(\d{4})",
377
+ # ]
378
+ # match = re.search(pattern, query)
379
+ # if match:
380
+ # start_month_str, start_year = match.group(1).lower(), int(match.group(2))
381
+ # end_month_str, end_year = match.group(4).lower(), int(match.group(5))
382
+ # start_month = month_map.get(start_month_str)
383
+ # end_month = month_map.get(end_month_str)
384
+ # if start_month and end_month:
385
+ # start_date = datetime(start_year, start_month, 1)
386
+ # end_date = datetime(end_year, end_month, 28)
387
+ # return start_date, end_date
388
+ # return None, None
389
+
390
  def extract_date_range(query):
391
  """
392
  Extracts a start and end month-year from a question like 'from Jan 2024 to May 2025'
393
+ Supports patterns like 'from', 'between', 'to', 'through', 'and', '-'.
394
  """
395
  month_map = {
396
  "january": 1, "february": 2, "march": 3, "april": 4, "may": 5, "june": 6,
397
  "july": 7, "august": 8, "september": 9, "october": 10, "november": 11, "december": 12
398
  }
399
+
400
+ patterns = [
401
+ r"(?i)(?:from|between)?\s*([a-zA-Z]+)\s+(\d{4})\s*(?:to|through|and|-)\s*([a-zA-Z]+)\s+(\d{4})",
402
  ]
403
+
404
+ for pattern in patterns:
405
+ match = re.search(pattern, query)
406
+ if match:
407
+ start_month_str, start_year = match.group(1).lower(), int(match.group(2))
408
+ end_month_str, end_year = match.group(3).lower(), int(match.group(4))
409
+
410
+ start_month = month_map.get(start_month_str)
411
+ end_month = month_map.get(end_month_str)
412
+
413
+ if start_month and end_month:
414
+ start_date = datetime(start_year, start_month, 1)
415
+ end_date = datetime(end_year, end_month, 28) # safe default
416
+ return start_date, end_date
417
+
418
  return None, None
419
 
420
  def extract_topic_match(query, df):