fix(normalizer): treat leading date qualifiers (nach/vor/…) as APPROX
_preprocess now sets approx=True when a leading marker is stripped; add _match_year_only so bare years (e.g. "nach 1900" -> "1900") resolve to 1900-01-01/YEAR before being upgraded to APPROX. Strengthen test_parse_approx_marker_upgrades_precision and add test_parse_leading_qualifier_is_approx (11 tests, all pass). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -72,7 +72,7 @@ _LEADING_MARKERS = re.compile(
|
||||
|
||||
|
||||
def _preprocess(raw: str):
|
||||
"""Return (cleaned_string, approx_flag)."""
|
||||
"""Return (cleaned_string, approx_flag). Any uncertainty/qualifier marker -> approx."""
|
||||
s = (raw or "").strip()
|
||||
if not s:
|
||||
return "", False
|
||||
@@ -82,8 +82,10 @@ def _preprocess(raw: str):
|
||||
s = re.sub(r"\(\s*\?\s*\)", " ", s) # remove "(?)"
|
||||
s = s.replace("?", " ")
|
||||
s = re.sub(r",.*$", "", s) # drop trailing editorial note (", 2. Brief")
|
||||
s = _LEADING_MARKERS.sub("", s)
|
||||
s = re.sub(r"\s+", " ", s).strip(" .,")
|
||||
stripped = _LEADING_MARKERS.sub("", s)
|
||||
if stripped != s: # a leading qualifier (um/ca/nach/vor/anfang/…) signals approximation
|
||||
approx = True
|
||||
s = re.sub(r"\s+", " ", stripped).strip(" .,")
|
||||
return s, approx
|
||||
|
||||
|
||||
@@ -114,8 +116,17 @@ def _match_numeric(s):
|
||||
return None
|
||||
|
||||
|
||||
_YEAR_ONLY_RE = re.compile(r"\d{4}")
|
||||
|
||||
|
||||
def _match_year_only(s):
|
||||
if _YEAR_ONLY_RE.fullmatch(s):
|
||||
return datetime.date(int(s), 1, 1).isoformat(), Precision.YEAR
|
||||
return None
|
||||
|
||||
|
||||
# Matchers are tried in order. Later tasks append to this list.
|
||||
_MATCHERS = [_match_iso, _match_numeric]
|
||||
_MATCHERS = [_match_iso, _match_numeric, _match_year_only]
|
||||
|
||||
|
||||
def parse_date(raw: str, date_overrides: dict | None = None) -> ParsedDate:
|
||||
|
||||
Reference in New Issue
Block a user