llmvalidate 0.7.1__tar.gz → 0.8.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {llmvalidate-0.7.1/src/llmvalidate.egg-info → llmvalidate-0.8.0}/PKG-INFO +1 -1
- {llmvalidate-0.7.1 → llmvalidate-0.8.0}/pyproject.toml +1 -1
- {llmvalidate-0.7.1 → llmvalidate-0.8.0}/src/llmvalidate/__init__.py +6 -1
- llmvalidate-0.8.0/src/llmvalidate/sortable_date.py +468 -0
- {llmvalidate-0.7.1 → llmvalidate-0.8.0/src/llmvalidate.egg-info}/PKG-INFO +1 -1
- {llmvalidate-0.7.1 → llmvalidate-0.8.0}/src/llmvalidate.egg-info/SOURCES.txt +1 -0
- {llmvalidate-0.7.1 → llmvalidate-0.8.0}/LICENSE +0 -0
- {llmvalidate-0.7.1 → llmvalidate-0.8.0}/readme.md +0 -0
- {llmvalidate-0.7.1 → llmvalidate-0.8.0}/setup.cfg +0 -0
- {llmvalidate-0.7.1 → llmvalidate-0.8.0}/src/llmvalidate/structured.py +0 -0
- {llmvalidate-0.7.1 → llmvalidate-0.8.0}/src/llmvalidate/utils.py +0 -0
- {llmvalidate-0.7.1 → llmvalidate-0.8.0}/src/llmvalidate/validation.py +0 -0
- {llmvalidate-0.7.1 → llmvalidate-0.8.0}/src/llmvalidate.egg-info/dependency_links.txt +0 -0
- {llmvalidate-0.7.1 → llmvalidate-0.8.0}/src/llmvalidate.egg-info/requires.txt +0 -0
- {llmvalidate-0.7.1 → llmvalidate-0.8.0}/src/llmvalidate.egg-info/top_level.txt +0 -0
|
@@ -1,19 +1,24 @@
|
|
|
1
1
|
# Managed by python-semantic-release — do not edit by hand; the release workflow rewrites
|
|
2
2
|
# this line. Set to 0.7.0 once by hand to clear the drift from ONC-12379, during which the
|
|
3
3
|
# release never reached this file and it stayed at 0.0.0 across every published version.
|
|
4
|
-
__version__ = "0.
|
|
4
|
+
__version__ = "0.8.0"
|
|
5
5
|
|
|
6
6
|
# `structured` is pydantic-only and cheap, so it is imported eagerly: consumers that
|
|
7
7
|
# only need the extraction contract (StructuredResult / StructuredField) get it without
|
|
8
8
|
# pulling in the pandas/numpy scoring stack.
|
|
9
9
|
from .structured import StructuredResult, StructuredGroup, StructuredField
|
|
10
10
|
|
|
11
|
+
# `sortable_date` is std-lib only (`re`, `datetime`), so it is likewise eager: normalising a
|
|
12
|
+
# date string must not cost the caller a pandas import. Keep it dependency-free.
|
|
13
|
+
from .sortable_date import to_sortable_date
|
|
14
|
+
|
|
11
15
|
__all__ = [
|
|
12
16
|
"validate",
|
|
13
17
|
"bootstrap_CI",
|
|
14
18
|
"StructuredResult",
|
|
15
19
|
"StructuredGroup",
|
|
16
20
|
"StructuredField",
|
|
21
|
+
"to_sortable_date",
|
|
17
22
|
]
|
|
18
23
|
|
|
19
24
|
|
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
"""
|
|
2
|
+
========================================================================================================================
|
|
3
|
+
`to_sortable_date` – a one-stop normaliser for the *messy* date/time stamps you meet in free-text documents
|
|
4
|
+
========================================================================================================================
|
|
5
|
+
|
|
6
|
+
Goal
|
|
7
|
+
----
|
|
8
|
+
Turn a fragment of text that *might* contain a date (and maybe a time) into one, **lexically-sortable** string.
|
|
9
|
+
|
|
10
|
+
Why?
|
|
11
|
+
• Sorting these strings with a plain collator will also sort them chronologically.
|
|
12
|
+
• A single canonical representation makes indexing / deduplication far simpler.
|
|
13
|
+
• You can still see if information was missing (month, day, year, time granularity) because we never
|
|
14
|
+
“invent” values – we only include what the source really told us.
|
|
15
|
+
|
|
16
|
+
Output shape
|
|
17
|
+
------------
|
|
18
|
+
The function returns the *left-most* valid date it can find in one of the following shapes
|
|
19
|
+
(components to the right disappear when absent):
|
|
20
|
+
|
|
21
|
+
YYYY
|
|
22
|
+
YYYY-MM
|
|
23
|
+
YYYY-MM-DD
|
|
24
|
+
YYYY-MM-DD HH
|
|
25
|
+
YYYY-MM-DD HH:MM
|
|
26
|
+
YYYY-MM-DD HH:MM:SS
|
|
27
|
+
????-MM
|
|
28
|
+
????-MM-DD
|
|
29
|
+
????-MM-DD HH… (unknown year but explicit day/time)
|
|
30
|
+
|
|
31
|
+
Key rules
|
|
32
|
+
---------
|
|
33
|
+
1. **No gaps** – we never output seconds without minutes, or minutes without hours, etc.
|
|
34
|
+
2. **Unknown year** – if the text clearly has *month + day* but no usable year, we insert `"????"`.
|
|
35
|
+
3. **Time is optional** – we only add it when a full calendar date (day present) was captured.
|
|
36
|
+
4. **Bare hour is ignored** – if the text just says “23” (with no `:`, no AM/PM and no `hours`
|
|
37
|
+
cue) we assume it is not an intentional time and we drop it.
|
|
38
|
+
*Explicit* hours like “11 PM”, “23 h”, or “around 07 hours” **are** kept.
|
|
39
|
+
5. **Timezone is ignored** – we strip `Z`, `UTC`, `+0800`, etc. but *never* shift the clock.
|
|
40
|
+
6. **Earliest wins** – if the string contains several dates, the first valid one (left-most) is returned.
|
|
41
|
+
7. **Invalid explicit dates** (e.g. `2021-02-29`) make the whole parse fail and return `None`.
|
|
42
|
+
|
|
43
|
+
Detection strategy
|
|
44
|
+
------------------
|
|
45
|
+
The parser moves from *most specific* → *least specific* patterns, gathering every match together
|
|
46
|
+
with its starting offset; the first chronologically valid candidate wins:
|
|
47
|
+
|
|
48
|
+
1. ISO week dates: `YYYY-Www-d`
|
|
49
|
+
2. Compact `YYYYMMDD`
|
|
50
|
+
3. Full day-precision Y-M-D (numeric and textual variants, DM/YM ambiguity handled by `dayFirst`)
|
|
51
|
+
4. Year-Month (two components)
|
|
52
|
+
5. Unknown-year Month-Day
|
|
53
|
+
6. Unknown-year Month
|
|
54
|
+
7. Quarter notation (`Q1 2025`)
|
|
55
|
+
8. Year-only
|
|
56
|
+
|
|
57
|
+
Once the earliest *calendar* date is chosen, the same slice of text is examined for a time stamp:
|
|
58
|
+
|
|
59
|
+
* `HH:MM:SS(.fff)? (AM|PM)?`
|
|
60
|
+
* `HH:MM (AM|PM)?`
|
|
61
|
+
* `HH (AM|PM)`
|
|
62
|
+
* `HH (h|hr|hrs|hour|hours)` or “around HH hours”
|
|
63
|
+
|
|
64
|
+
Seconds and sub-seconds are trimmed to `SS`; subseconds are discarded.
|
|
65
|
+
AM/PM is converted to 24-hour clock.
|
|
66
|
+
If only `HH` is present **and it isn’t explicit** (e.g. `23` alone) we drop the time entirely.
|
|
67
|
+
|
|
68
|
+
Dependencies
|
|
69
|
+
------------
|
|
70
|
+
Only the Python std-lib (`re`, `datetime`) – no `dateutil` needed.
|
|
71
|
+
|
|
72
|
+
Non-text input
|
|
73
|
+
--------------
|
|
74
|
+
`raw` is typed `Any` because callers pass values straight from documents and dataframes.
|
|
75
|
+
Anything that is not a `str` returns `None` – including `float("nan")`, which is *truthy* and
|
|
76
|
+
would otherwise reach `.strip()` and raise.
|
|
77
|
+
|
|
78
|
+
A two-digit number is only read as a year when nothing time-like follows it: `“Feb 20, 12”` is
|
|
79
|
+
February 2012, while `“Feb 20 13:45”` is `????-02-20 13:45` – the 13 is the hour, and the year
|
|
80
|
+
stays unknown rather than being invented (ONC-12551).
|
|
81
|
+
"""
|
|
82
|
+
|
|
83
|
+
import re
|
|
84
|
+
from datetime import date
|
|
85
|
+
from typing import Any
|
|
86
|
+
|
|
87
|
+
# ---------------------------------------------------------------------
|
|
88
|
+
# Static tables & regexes (unchanged, plus one new for ordinals)
|
|
89
|
+
# ---------------------------------------------------------------------
|
|
90
|
+
_MONTHS = {k: v for names, v in [
|
|
91
|
+
(['january', 'jan', 'jan.'], 1),
|
|
92
|
+
(['february', 'feb', 'feb.'], 2),
|
|
93
|
+
(['march', 'mar', 'mar.'], 3),
|
|
94
|
+
(['april', 'apr', 'apr.'], 4),
|
|
95
|
+
(['may'], 5),
|
|
96
|
+
(['june', 'jun', 'jun.'], 6),
|
|
97
|
+
(['july', 'jul', 'jul.'], 7),
|
|
98
|
+
(['august', 'aug', 'aug.'], 8),
|
|
99
|
+
(['september', 'sep', 'sep.', 'sept', 'sept.'], 9),
|
|
100
|
+
(['october', 'oct', 'oct.'], 10),
|
|
101
|
+
(['november', 'nov', 'nov.'], 11),
|
|
102
|
+
(['december', 'dec', 'dec.'], 12),
|
|
103
|
+
] for k in names}
|
|
104
|
+
|
|
105
|
+
_ORDINAL_RE = re.compile(r'\b(\d{1,2})(st|nd|rd|th)\b', re.IGNORECASE)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
# ---------------------------------------------------------------------
|
|
109
|
+
# Utilities (existing + minor tweaks)
|
|
110
|
+
# ---------------------------------------------------------------------
|
|
111
|
+
def _month_from_name(tok):
|
|
112
|
+
return _MONTHS.get(tok.lower())
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def _normalize_two_digit_year(ystr):
|
|
116
|
+
y = int(ystr)
|
|
117
|
+
return 2000 + y if y < 100 else y
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _valid_ym(y, m):
|
|
121
|
+
return 1000 <= y <= 9999 and 1 <= m <= 12
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _valid_ymd(y, m, d):
|
|
125
|
+
try:
|
|
126
|
+
date(y, m, d)
|
|
127
|
+
return True
|
|
128
|
+
except Exception:
|
|
129
|
+
return False
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _valid_unknown_ymd(m, d):
|
|
133
|
+
if not (1 <= m <= 12 and 1 <= d <= 31):
|
|
134
|
+
return False
|
|
135
|
+
if m in (4, 6, 9, 11) and d > 30:
|
|
136
|
+
return False
|
|
137
|
+
if m == 2 and d > 29:
|
|
138
|
+
return False
|
|
139
|
+
return True
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def _earliest(matches):
|
|
143
|
+
"""Return (start, string, end) for the earliest non‑None match list."""
|
|
144
|
+
return min((x for x in matches if x is not None), key=lambda t: t[0]) if matches else None
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
# ---------------------------------------------------------------------
|
|
148
|
+
# New helper – time extraction
|
|
149
|
+
# ---------------------------------------------------------------------
|
|
150
|
+
_TIME_RE = re.compile(r"""
|
|
151
|
+
^[\sT\-,.(]* # leading junk, spaces, 'T', punctuation
|
|
152
|
+
(?:(?:at|around)\s+)? # optional 'at' / 'around'
|
|
153
|
+
(?P<h>\d{1,2}) # hour
|
|
154
|
+
(?:
|
|
155
|
+
:(?P<m>\d{1,2}) # minutes
|
|
156
|
+
(?:
|
|
157
|
+
:(?P<s>\d{1,2}) # seconds
|
|
158
|
+
(?:\.\d+)? # + fractional
|
|
159
|
+
)?
|
|
160
|
+
)?
|
|
161
|
+
\s*
|
|
162
|
+
(?P<ampm>[AaPp][Mm])? # AM/PM (not any two of A/P/M — 'PP' is not a time)
|
|
163
|
+
(?:\s*(?P<unit>h|hrs?|hours?)\b)? # explicit hour cue: 23 h / 07 hours
|
|
164
|
+
(?:\s*(?:Z|UTC|\(UTC\)|[+-]\d{2}:?\d{2}))? # trailing tz we ignore
|
|
165
|
+
""", re.VERBOSE)
|
|
166
|
+
|
|
167
|
+
# What makes a bare two-digit number a *time* rather than a year: an immediately following
|
|
168
|
+
# `:MM`, an AM/PM qualifier, or an hour unit. The year patterns consult this so that
|
|
169
|
+
# "Feb 20 13:45" is not read as February 2013.
|
|
170
|
+
_TIME_CONTEXT_RE = re.compile(r"""
|
|
171
|
+
^(?: :\d{1,2} # 13:45
|
|
172
|
+
| \s*[APap][Mm]\b # 11 PM
|
|
173
|
+
| \s*(?:h|hrs?|hours?)\b # 23 h / 07 hours
|
|
174
|
+
)
|
|
175
|
+
""", re.VERBOSE)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def _year_is_actually_time(ystr, text, end):
|
|
179
|
+
"""True when a *two-digit* year candidate is really the hour of a following time."""
|
|
180
|
+
return len(ystr) == 2 and _TIME_CONTEXT_RE.match(text[end:]) is not None
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def _extract_time(substring):
|
|
184
|
+
"""
|
|
185
|
+
Parse a time immediately following the date fragment.
|
|
186
|
+
Returns a normalised string (HH, HH:MM or HH:MM:SS) or None.
|
|
187
|
+
"""
|
|
188
|
+
m = _TIME_RE.match(substring)
|
|
189
|
+
if not m:
|
|
190
|
+
return None
|
|
191
|
+
|
|
192
|
+
h = int(m.group('h'))
|
|
193
|
+
mnt = m.group('m')
|
|
194
|
+
sec = m.group('s')
|
|
195
|
+
ampm = m.group('ampm')
|
|
196
|
+
unit = m.group('unit')
|
|
197
|
+
|
|
198
|
+
# Bare hour is ignored: only AM/PM or an explicit unit ("23 h", "around 07 hours")
|
|
199
|
+
# marks a lone number as an intentional time.
|
|
200
|
+
if not ampm and not unit and mnt is None:
|
|
201
|
+
return None
|
|
202
|
+
|
|
203
|
+
# Impossible clock values are not a time: emitting them would break the one guarantee
|
|
204
|
+
# this function makes, that the result is a valid lexically-sortable stamp. Before the
|
|
205
|
+
# check, "29:75:90" came back verbatim and "23:30 PM" became "35:30".
|
|
206
|
+
if (mnt is not None and int(mnt) > 59) or (sec is not None and int(sec) > 59):
|
|
207
|
+
return None
|
|
208
|
+
if ampm and not 1 <= h <= 12:
|
|
209
|
+
return None
|
|
210
|
+
|
|
211
|
+
# 12‑hour conversion
|
|
212
|
+
if ampm:
|
|
213
|
+
if ampm.lower() == 'pm' and h != 12:
|
|
214
|
+
h += 12
|
|
215
|
+
if ampm.lower() == 'am' and h == 12:
|
|
216
|
+
h = 0
|
|
217
|
+
|
|
218
|
+
if not 0 <= h <= 23:
|
|
219
|
+
return None
|
|
220
|
+
|
|
221
|
+
# Midnight '00:00' with no AM/PM => ignore. The hour test matters: without it *every*
|
|
222
|
+
# whole-hour stamp was dropped, so "13:00" lost its time.
|
|
223
|
+
if not ampm and not unit and h == 0 and mnt == '00' and sec is None:
|
|
224
|
+
return None
|
|
225
|
+
|
|
226
|
+
if mnt is None:
|
|
227
|
+
return f"{h:02d}"
|
|
228
|
+
if sec is None:
|
|
229
|
+
return f"{h:02d}:{int(mnt):02d}"
|
|
230
|
+
return f"{h:02d}:{int(mnt):02d}:{int(sec):02d}"
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
# ---------------------------------------------------------------------
|
|
234
|
+
# Main routine
|
|
235
|
+
# ---------------------------------------------------------------------
|
|
236
|
+
def to_sortable_date(raw: Any, dayFirst: bool = True) -> str | None:
|
|
237
|
+
"""
|
|
238
|
+
Convert *raw* to a sortable date (optionally with time) as described
|
|
239
|
+
in the doc‑string of the original implementation.
|
|
240
|
+
"""
|
|
241
|
+
# `raw` is Any because callers pass straight from documents/dataframes. Anything that is
|
|
242
|
+
# not text has no date in it — and note float("nan") is *truthy*, so a NaN cell would
|
|
243
|
+
# otherwise reach .strip() and raise.
|
|
244
|
+
if not isinstance(raw, str) or not raw.strip():
|
|
245
|
+
return None
|
|
246
|
+
|
|
247
|
+
s = re.sub(r'\s+', ' ', raw.strip())
|
|
248
|
+
s_clean = _ORDINAL_RE.sub(r'\1', s)
|
|
249
|
+
|
|
250
|
+
# Holders for candidates at different precisions
|
|
251
|
+
ymd, ym, uymd, um = [], [], [], []
|
|
252
|
+
found_invalid_ymd = found_invalid_ym = False
|
|
253
|
+
|
|
254
|
+
# --- 1. ISO week date -------------------------------------------------
|
|
255
|
+
m = re.search(r'\b(\d{4})-W(\d{2})-(\d)\b', s_clean)
|
|
256
|
+
if m:
|
|
257
|
+
try:
|
|
258
|
+
d = date.fromisocalendar(int(m[1]), int(m[2]), int(m[3]))
|
|
259
|
+
date_str = f"{d.year:04d}-{d.month:02d}-{d.day:02d}"
|
|
260
|
+
end_idx = m.end()
|
|
261
|
+
time = _extract_time(s_clean[end_idx:])
|
|
262
|
+
return f"{date_str} {time}" if time else date_str
|
|
263
|
+
except Exception:
|
|
264
|
+
return None
|
|
265
|
+
|
|
266
|
+
# --- 2. Compact YYYYMMDD (e.g. 20210405) ------------------------------
|
|
267
|
+
for m in re.finditer(r'\b(1[5-9]\d{2}|20\d{2}|21\d{2})(\d{2})(\d{2})\b', s_clean):
|
|
268
|
+
y, mm, dd = int(m[1]), int(m[2]), int(m[3])
|
|
269
|
+
if _valid_ymd(y, mm, dd):
|
|
270
|
+
ymd.append((m.start(), f"{y:04d}-{mm:02d}-{dd:02d}", m.end()))
|
|
271
|
+
else:
|
|
272
|
+
found_invalid_ymd = True
|
|
273
|
+
|
|
274
|
+
# --- 3. Full Y‑M‑D in various flavours --------------------------------
|
|
275
|
+
# Numeric YYYY‑MM‑DD / YYYY/MM/DD / YYYY.MM.DD
|
|
276
|
+
# The boundary is a lookahead, not a consumed character: a date ending in prose
|
|
277
|
+
# punctuation ("…on 2021-07-04.") must still match at day precision, and an invalid
|
|
278
|
+
# one ("2021-02-29,") must fail the parse rather than silently degrade to 2021-02.
|
|
279
|
+
for pat in (r'\b(\d{4})-(\d{1,2})-(\d{1,2})(?=T|\s|[.,;:)\]}]|$)',
|
|
280
|
+
r'\b(\d{4})/(\d{1,2})/(\d{1,2})\b',
|
|
281
|
+
r'\b(\d{4})\.(\d{1,2})\.(\d{1,2})\b'):
|
|
282
|
+
for m in re.finditer(pat, s_clean):
|
|
283
|
+
y, mm, dd = int(m[1]), int(m[2]), int(m[3])
|
|
284
|
+
if _valid_ymd(y, mm, dd):
|
|
285
|
+
ymd.append((m.start(), f"{y:04d}-{mm:02d}-{dd:02d}", m.end()))
|
|
286
|
+
else:
|
|
287
|
+
found_invalid_ymd = True
|
|
288
|
+
|
|
289
|
+
# Ambiguous slash dates (D/M/Y or M/D/Y)
|
|
290
|
+
for m in re.finditer(r'\b(\d{1,2})[-/.](\d{1,2})[-/.](\d{2,4})\b', s_clean):
|
|
291
|
+
# A 2-digit "year" that is really an hour belongs to the time, not the date.
|
|
292
|
+
if _year_is_actually_time(m[3], s_clean, m.end()):
|
|
293
|
+
continue
|
|
294
|
+
p1, p2, py = int(m[1]), int(m[2]), m[3]
|
|
295
|
+
y = _normalize_two_digit_year(py.replace("'", ""))
|
|
296
|
+
d, mm = (p1, p2) if dayFirst else (p2, p1)
|
|
297
|
+
if _valid_ymd(y, mm, d):
|
|
298
|
+
ymd.append((m.start(), f"{y:04d}-{mm:02d}-{d:02d}", m.end()))
|
|
299
|
+
else:
|
|
300
|
+
found_invalid_ymd = True
|
|
301
|
+
|
|
302
|
+
# Monthname Day Year (“Feb 20, 2012”)
|
|
303
|
+
for m in re.finditer(r"\b([A-Za-z]{3,9}\.?)[ ]+(\d{1,2})(?:,)?[ ]+(?:'\s*)?(\d{2,4})\b", s_clean):
|
|
304
|
+
mon = _month_from_name(m[1])
|
|
305
|
+
if mon:
|
|
306
|
+
# A 2-digit "year" that is really an hour belongs to the time, not the date.
|
|
307
|
+
if _year_is_actually_time(m[3], s_clean, m.end()):
|
|
308
|
+
continue
|
|
309
|
+
d = int(m[2])
|
|
310
|
+
y = _normalize_two_digit_year(m[3].replace("'", ""))
|
|
311
|
+
if _valid_ymd(y, mon, d):
|
|
312
|
+
ymd.append((m.start(), f"{y:04d}-{mon:02d}-{d:02d}", m.end()))
|
|
313
|
+
else:
|
|
314
|
+
found_invalid_ymd = True
|
|
315
|
+
|
|
316
|
+
# Day Month Year (“5 Jan 2020”)
|
|
317
|
+
for m in re.finditer(r"\b(\d{1,2})\s+(?:of\s+)?([A-Za-z]{3,9}\.?),?\s+(?:'\s*)?(\d{2,4})\b", s_clean, re.IGNORECASE):
|
|
318
|
+
mon = _month_from_name(m[2])
|
|
319
|
+
if mon:
|
|
320
|
+
# A 2-digit "year" that is really an hour belongs to the time, not the date.
|
|
321
|
+
if _year_is_actually_time(m[3], s_clean, m.end()):
|
|
322
|
+
continue
|
|
323
|
+
d = int(m[1])
|
|
324
|
+
y = _normalize_two_digit_year(m[3].replace("'", ""))
|
|
325
|
+
if _valid_ymd(y, mon, d):
|
|
326
|
+
ymd.append((m.start(), f"{y:04d}-{mon:02d}-{d:02d}", m.end()))
|
|
327
|
+
else:
|
|
328
|
+
found_invalid_ymd = True
|
|
329
|
+
|
|
330
|
+
# Month‑Day‑Year (“Oct‑31‑2021”)
|
|
331
|
+
for m in re.finditer(r'\b([A-Za-z]{3,9}\.?)[-/](\d{1,2})[-/](\d{2,4})\b', s_clean):
|
|
332
|
+
mon = _month_from_name(m[1])
|
|
333
|
+
if mon:
|
|
334
|
+
# A 2-digit "year" that is really an hour belongs to the time, not the date.
|
|
335
|
+
if _year_is_actually_time(m[3], s_clean, m.end()):
|
|
336
|
+
continue
|
|
337
|
+
d = int(m[2])
|
|
338
|
+
y = _normalize_two_digit_year(m[3].replace("'", ""))
|
|
339
|
+
if _valid_ymd(y, mon, d):
|
|
340
|
+
ymd.append((m.start(), f"{y:04d}-{mon:02d}-{d:02d}", m.end()))
|
|
341
|
+
else:
|
|
342
|
+
found_invalid_ymd = True
|
|
343
|
+
|
|
344
|
+
# Day‑Month‑Year (“31‑Oct‑2021”)
|
|
345
|
+
for m in re.finditer(r'\b(\d{1,2})[-/]([A-Za-z]{3,9}\.?)[-/](\d{2,4})\b', s_clean):
|
|
346
|
+
mon = _month_from_name(m[2])
|
|
347
|
+
if mon:
|
|
348
|
+
# A 2-digit "year" that is really an hour belongs to the time, not the date.
|
|
349
|
+
if _year_is_actually_time(m[3], s_clean, m.end()):
|
|
350
|
+
continue
|
|
351
|
+
d = int(m[1])
|
|
352
|
+
y = _normalize_two_digit_year(m[3].replace("'", ""))
|
|
353
|
+
if _valid_ymd(y, mon, d):
|
|
354
|
+
ymd.append((m.start(), f"{y:04d}-{mon:02d}-{d:02d}", m.end()))
|
|
355
|
+
else:
|
|
356
|
+
found_invalid_ymd = True
|
|
357
|
+
|
|
358
|
+
if found_invalid_ymd:
|
|
359
|
+
return None
|
|
360
|
+
if ymd:
|
|
361
|
+
start, date_str, end_idx = _earliest(ymd)
|
|
362
|
+
time = _extract_time(s_clean[end_idx:])
|
|
363
|
+
return f"{date_str} {time}" if time else date_str
|
|
364
|
+
|
|
365
|
+
# --- 4. Year‑Month ----------------------------------------------------
|
|
366
|
+
# Numeric YYYY‑MM / YYYY.MM / YYYY/MM
|
|
367
|
+
for pat in (r'\b(\d{4})-(\d{1,2})\b', r'\b(\d{4})\.(\d{1,2})\b', r'\b(\d{4})/(\d{1,2})\b'):
|
|
368
|
+
for m in re.finditer(pat, s_clean):
|
|
369
|
+
y, mm = int(m[1]), int(m[2])
|
|
370
|
+
if _valid_ym(y, mm):
|
|
371
|
+
ym.append((m.start(), f"{y:04d}-{mm:02d}", m.end()))
|
|
372
|
+
else:
|
|
373
|
+
found_invalid_ym = True
|
|
374
|
+
|
|
375
|
+
# Month/Year (“05/2021”)
|
|
376
|
+
for m in re.finditer(r'\b(\d{1,2})/(\d{4})\b', s_clean):
|
|
377
|
+
mm, y = int(m[1]), int(m[2])
|
|
378
|
+
if _valid_ym(y, mm):
|
|
379
|
+
ym.append((m.start(), f"{y:04d}-{mm:02d}", m.end()))
|
|
380
|
+
else:
|
|
381
|
+
found_invalid_ym = True
|
|
382
|
+
|
|
383
|
+
# Monthname Year (“March 2021”)
|
|
384
|
+
for m in re.finditer(r'\b([A-Za-z]{3,9}\.?)[ ]+(\d{4})\b', s_clean):
|
|
385
|
+
mon = _month_from_name(m[1])
|
|
386
|
+
if mon:
|
|
387
|
+
y = int(m[2])
|
|
388
|
+
if _valid_ym(y, mon):
|
|
389
|
+
ym.append((m.start(), f"{y:04d}-{mon:02d}", m.end()))
|
|
390
|
+
else:
|
|
391
|
+
found_invalid_ym = True
|
|
392
|
+
|
|
393
|
+
# Monthname + 4-digit year with dash or slash (e.g., Jan-2024, Jan/2024)
|
|
394
|
+
for m in re.finditer(r'\b([A-Za-z]{3,9}\.?)\s*[-/]\s*(\d{4})\b', s_clean):
|
|
395
|
+
mon = _month_from_name(m[1])
|
|
396
|
+
if not mon:
|
|
397
|
+
continue
|
|
398
|
+
y = int(m[2])
|
|
399
|
+
if _valid_ym(y, mon):
|
|
400
|
+
ym.append((m.start(), f"{y:04d}-{mon:02d}", m.end()))
|
|
401
|
+
else:
|
|
402
|
+
found_invalid_ym = True
|
|
403
|
+
|
|
404
|
+
# Monthname + 2-digit year with apostrophe, dash, or slash (e.g. Jan'24, Jan '24, Jan/24, Jan-24)
|
|
405
|
+
for m in re.finditer(r"\b([A-Za-z]{3,9}\.?)\s*(?:[\'’]|[-/])\s*(\d{2})\b", s_clean):
|
|
406
|
+
mon = _month_from_name(m[1])
|
|
407
|
+
if not mon:
|
|
408
|
+
continue
|
|
409
|
+
y = _normalize_two_digit_year(m[2])
|
|
410
|
+
if _valid_ym(y, mon):
|
|
411
|
+
ym.append((m.start(), f"{y:04d}-{mon:02d}", m.end()))
|
|
412
|
+
else:
|
|
413
|
+
found_invalid_ym = True
|
|
414
|
+
|
|
415
|
+
if found_invalid_ym:
|
|
416
|
+
return None
|
|
417
|
+
if ym:
|
|
418
|
+
return _earliest(ym)[1] # never attach time to YM
|
|
419
|
+
|
|
420
|
+
# --- 5. Unknown‑year Month‑Day ---------------------------------------
|
|
421
|
+
# Month name + day (space, dash, or slash)
|
|
422
|
+
for m in re.finditer(r'\b([A-Za-z]{3,9}\.?)\s*[-/ ]\s*(\d{1,2})\b', s_clean):
|
|
423
|
+
mon = _month_from_name(m[1])
|
|
424
|
+
if mon:
|
|
425
|
+
d = int(m[2])
|
|
426
|
+
if _valid_unknown_ymd(mon, d):
|
|
427
|
+
uymd.append((m.start(), f"????-{mon:02d}-{d:02d}", m.end()))
|
|
428
|
+
|
|
429
|
+
# Day + Month name (space "of", or dash/slash)
|
|
430
|
+
for m in re.finditer(r'\b(\d{1,2})\s+(?:of\s+)?([A-Za-z]{3,9}\.?)\b', s_clean, re.IGNORECASE):
|
|
431
|
+
mon = _month_from_name(m[2])
|
|
432
|
+
if mon:
|
|
433
|
+
d = int(m[1])
|
|
434
|
+
if _valid_unknown_ymd(mon, d):
|
|
435
|
+
uymd.append((m.start(), f"????-{mon:02d}-{d:02d}", m.end()))
|
|
436
|
+
|
|
437
|
+
for m in re.finditer(r'\b(\d{1,2})\s*[-/ ]\s*([A-Za-z]{3,9}\.?)\b', s_clean):
|
|
438
|
+
mon = _month_from_name(m[2])
|
|
439
|
+
if mon:
|
|
440
|
+
d = int(m[1])
|
|
441
|
+
if _valid_unknown_ymd(mon, d):
|
|
442
|
+
uymd.append((m.start(), f"????-{mon:02d}-{d:02d}", m.end()))
|
|
443
|
+
|
|
444
|
+
if uymd:
|
|
445
|
+
start, date_str, end_idx = _earliest(uymd)
|
|
446
|
+
time = _extract_time(s_clean[end_idx:])
|
|
447
|
+
return f"{date_str} {time}" if time else date_str
|
|
448
|
+
|
|
449
|
+
# --- 6. Unknown‑year Month‑only --------------------------------------
|
|
450
|
+
um = [(m.start(), f"????-{_month_from_name(m[1]):02d}", m.end())
|
|
451
|
+
for m in re.finditer(r'\b([A-Za-z]{3,9}\.?)+\b', s_clean)
|
|
452
|
+
if _month_from_name(m[1])]
|
|
453
|
+
if um:
|
|
454
|
+
return _earliest(um)[1]
|
|
455
|
+
|
|
456
|
+
# --- 7. Quarter collapse --------------------------------------------
|
|
457
|
+
m = re.search(r'\bQ([1-4])\s+(\d{4})\b', s_clean, re.IGNORECASE)
|
|
458
|
+
if m:
|
|
459
|
+
y = int(m[2])
|
|
460
|
+
if 1000 <= y <= 9999:
|
|
461
|
+
return f"{y:04d}"
|
|
462
|
+
|
|
463
|
+
# --- 8. Year‑only -----------------------------------------------------
|
|
464
|
+
m = re.search(r'\b(1[5-9]\d{2}|20\d{2}|21\d{2})\b', s_clean)
|
|
465
|
+
if m:
|
|
466
|
+
return f"{int(m[1]):04d}"
|
|
467
|
+
|
|
468
|
+
return None
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|