Tiancheng: fix but in extract_date_parts_en

This commit is contained in:
承玙 2024-07-03 21:12:10 +08:00
parent 50d7331eb2
commit 4ec4903a37

View file

@ -77,11 +77,13 @@ class DatetimeHandler(object):
"weekday": -1
}
# Patterns to extract the parts of the date/time
# Patterns to extract the parts of the date/time
patterns = {
"year": r"\b(\d{4})\b",
"month": r"\b(January|February|March|April|May|June|July|August|September|October|November|December)\b",
"day": r"\b(\d{1,2})\b",
"day_month_year": r"\b(?P<month>January|February|March|April|May|June|July|August|September|October|November|December) (?P<day>\d{1,2}),? (?P<year>\d{4})\b",
"day_month": r"\b(?P<month>January|February|March|April|May|June|July|August|September|October|November|December) (?P<day>\d{1,2})\b",
"hour_12": r"\b(\d{1,2})\s*(AM|PM|am|pm)\b",
"hour_24": r"\b(\d{1,2}):(\d{2}):(\d{2})\b"
}
@ -97,20 +99,30 @@ class DatetimeHandler(object):
"Friday": 5, "Saturday": 6, "Sunday": 7
}
day_month_year_match = re.search(patterns["day_month_year"], input_string)
if day_month_year_match:
date_info["year"] = int(day_month_year_match.group("year"))
date_info["month"] = month_mapping[day_month_year_match.group("month")]
date_info["day"] = int(day_month_year_match.group("day"))
# Extract month and day without year
elif date_info["year"] == -1:
day_month_match = re.search(patterns["day_month"], input_string)
if day_month_match:
date_info["month"] = month_mapping[day_month_match.group("month")]
date_info["day"] = int(day_month_match.group("day"))
# Extract year
year_match = re.search(patterns["year"], input_string)
if year_match:
date_info["year"] = int(year_match.group(0))
if date_info["year"] == -1:
year_match = re.search(patterns["year"], input_string)
if year_match:
date_info["year"] = int(year_match.group(0))
# Extract month
month_match = re.search(patterns["month"], input_string)
if month_match:
date_info["month"] = month_mapping[month_match.group(0)]
# Extract day
day_match = re.findall(patterns["day"], input_string)
if day_match:
date_info["day"] = int(day_match[0])
if date_info["month"] == -1:
month_match = re.search(patterns["month"], input_string)
if month_match:
date_info["month"] = month_mapping[month_match.group(0)]
# Extract 12-hour format time
hour_12_match = re.search(patterns["hour_12"], input_string)
@ -122,12 +134,12 @@ class DatetimeHandler(object):
hour = 0
date_info["hour"] = hour
# Extract 24-hour format time
hour_24_match = re.search(patterns["hour_24"], input_string)
if hour_24_match:
date_info["hour"] = int(hour_24_match.group(1))
date_info["minute"] = int(hour_24_match.group(2))
date_info["second"] = int(hour_24_match.group(3))
# # Extract 24-hour format time
# hour_24_match = re.search(patterns["hour_24"], input_string)
# if hour_24_match:
# date_info["hour"] = int(hour_24_match.group(1))
# date_info["minute"] = int(hour_24_match.group(2))
# date_info["second"] = int(hour_24_match.group(3))
# Extract weekday
for week_day, value in weekday_mapping.items():
@ -135,9 +147,6 @@ class DatetimeHandler(object):
date_info["weekday"] = value
break
# Remove unused keys
date_info = {k: v for k, v in date_info.items()}
return date_info
@classmethod