87 lines
4.1 KiB
Python
87 lines
4.1 KiB
Python
"""Calendar boundaries shared by dialogue understanding and resource planning."""
|
|
from datetime import date, datetime, timedelta
|
|
from zoneinfo import ZoneInfo
|
|
import re
|
|
from calendar import monthrange
|
|
|
|
|
|
_DAY = r"(\d{4})[-/年](\d{1,2})[-/月](\d{1,2})日?"
|
|
_CLOSED_DATES = re.compile(rf"(?<!\d){_DAY}(?:\s*(?:至|到|[~~—–])\s*{_DAY})?(?!\d)")
|
|
|
|
|
|
def explicit_date_bounds(text: str) -> tuple[str, str] | None:
|
|
"""Parse a complete date or a closed range, including Chinese date notation."""
|
|
match = _CLOSED_DATES.fullmatch(text.strip())
|
|
if not match:
|
|
return None
|
|
digits = match.groups()
|
|
try:
|
|
first = date(*map(int, digits[:3]))
|
|
last = date(*map(int, digits[3:])) if digits[3] else first
|
|
except ValueError:
|
|
return None
|
|
return (first.isoformat(), last.isoformat()) if first <= last else None
|
|
|
|
|
|
def single_explicit_date_span(text: str) -> str:
|
|
spans = list(_CLOSED_DATES.finditer(text))
|
|
return spans[0].group() if len(spans) == 1 else ""
|
|
|
|
|
|
def explicit_relative_period(message: str):
|
|
"""Canonical whole periods only; mixed ranges and specific weekdays stay explicit.
|
|
|
|
This parses time expressions independently of business intent or resource names.
|
|
"""
|
|
if re.search(r'\d{4}[-/年]|\d{1,2}月|第[一二三四1234]季度|[上下]半年|[前后][一二三四五六七八九十\d]+天|之前|以前|之后|以后|以来|截至|截止|至|到',message):
|
|
return None
|
|
patterns={
|
|
'this_year':r'今年|本年度|本年',
|
|
'last_year':r'去年|上年度',
|
|
'this_quarter':r'这个季度|本季度|这季度|本季(?!度)',
|
|
'last_quarter':r'上个季度|上季度|上季(?!度)',
|
|
'this_month':r'这个月|本月|这月',
|
|
'last_month':r'上个月|上月',
|
|
'last_week':r'(?:上周|上星期|上礼拜)(?![一二三四五六日天]|周[一二三四五六日天])',
|
|
'this_week':r'(?:本周|这周|本星期)(?![一二三四五六日天]|周[一二三四五六日天])',
|
|
'today':r'今天|今日', 'yesterday':r'昨天|昨日', 'tomorrow':r'明天|明日',
|
|
'recent_7_days':r'(?:最近|近)(?:七|7)天',
|
|
}
|
|
matches=[(key,m.group()) for key,pattern in patterns.items() for m in re.finditer(pattern,message)]
|
|
return matches[0] if len(matches)==1 else None
|
|
|
|
|
|
def calendar_context(today: date | str) -> dict:
|
|
if isinstance(today, str):
|
|
try:
|
|
today = date.fromisoformat(today)
|
|
except ValueError:
|
|
today = datetime.now(ZoneInfo("Asia/Shanghai")).date()
|
|
monday = today - timedelta(days=today.weekday())
|
|
quarter_month = (today.month - 1) // 3 * 3 + 1
|
|
quarter_start = date(today.year, quarter_month, 1)
|
|
quarter_end = date(today.year, quarter_month + 2, monthrange(today.year, quarter_month + 2)[1])
|
|
last_quarter_end = quarter_start - timedelta(days=1)
|
|
last_month_end = today.replace(day=1) - timedelta(days=1)
|
|
|
|
def period(first, last):
|
|
return {"date_from": first.isoformat(), "date_to": last.isoformat()}
|
|
|
|
return {
|
|
"today": today.isoformat(), "yesterday": (today - timedelta(days=1)).isoformat(),
|
|
"tomorrow": (today + timedelta(days=1)).isoformat(),
|
|
"recent_7_days": period(today - timedelta(days=6), today),
|
|
"this_week": period(monday, monday + timedelta(days=6)),
|
|
"this_week_to_date": period(monday, today),
|
|
"last_week": period(monday - timedelta(days=7), monday - timedelta(days=1)),
|
|
"this_month_to_date": period(today.replace(day=1), today),
|
|
"this_month": period(today.replace(day=1), today.replace(day=monthrange(today.year,today.month)[1])),
|
|
"last_month": period(last_month_end.replace(day=1), last_month_end),
|
|
"this_quarter": period(quarter_start, quarter_end),
|
|
"last_quarter": period(date(last_quarter_end.year, (last_quarter_end.month-1)//3*3+1, 1), last_quarter_end),
|
|
"this_quarter_to_date": period(quarter_start, today),
|
|
"this_year": period(today.replace(month=1,day=1), today.replace(month=12,day=31)),
|
|
"this_year_to_date": period(today.replace(month=1, day=1), today),
|
|
"last_year": period(date(today.year - 1, 1, 1), date(today.year - 1, 12, 31)),
|
|
}
|