diff --git a/tools/import-normalizer/dates.py b/tools/import-normalizer/dates.py index a34bd357..65449a52 100644 --- a/tools/import-normalizer/dates.py +++ b/tools/import-normalizer/dates.py @@ -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: diff --git a/tools/import-normalizer/tests/test_dates.py b/tools/import-normalizer/tests/test_dates.py index d8e06e22..5a2a6f5b 100644 --- a/tools/import-normalizer/tests/test_dates.py +++ b/tools/import-normalizer/tests/test_dates.py @@ -56,5 +56,10 @@ def test_parse_numeric_unparseable(): def test_parse_approx_marker_upgrades_precision(): r = dates.parse_date("17.Nov (?) 1887") # month-name handled in a later task; here just the marker path - # after the marker is detected, a parsed date becomes APPROX (verified fully in Task 8) assert r.raw == "17.Nov (?) 1887" + assert r.precision == Precision.UNKNOWN # no month-name matcher until Task 7; full APPROX check in Task 8 + +def test_parse_leading_qualifier_is_approx(): + r = dates.parse_date("nach 1900") # "after 1900" -> year salvaged, but precision is APPROX not exact + assert r.iso == "1900-01-01" + assert r.precision == Precision.APPROX