infoxtractor/src/ix/provenance/__init__.py
Dirk Riemann 1e340c82fa
All checks were successful
tests / test (pull_request) Successful in 1m10s
tests / test (push) Successful in 1m11s
feat(provenance): mapper + verifier for ReliabilityStep (spec §9.4, §6)
Lands the two remaining provenance-subsystem pieces:

mapper.py — map_segment_refs_to_provenance:
- For each LLM SegmentCitation, pick seg-ids per source_type
  (`value` vs `value_and_context`), cap at max_sources_per_field,
  resolve each via SegmentIndex, track invalid references.
- Resolve field values by dot-path (`result.items[0].name` supported —
  `[N]` bracket notation is normalised to `.N` before traversal).
- Skip fields that resolve to zero valid sources (spec §9.4).
- Write quality_metrics with fields_with_provenance / total_fields /
  coverage_rate / invalid_references.

verify.py — verify_field + apply_reliability_flags:
- Dispatches per Pydantic field type: date → parse-both-sides compare;
  int/float/Decimal → normalize + whole-snippet / numeric-token scan;
  IBAN (detected via `iban` in field name) → upper+strip compare;
  Literal / None → flags stay None; else string substring.
- _unwrap_optional handles BOTH typing.Union AND types.UnionType so
  `Decimal | None` (PEP 604, what get_type_hints emits on 3.12+) resolves
  correctly — caught by the integration-style test_writes_flags_and_counters.
- Number comparator scans numeric tokens in the snippet so labels
  ("Closing balance CHF 1'234.56") don't mask the match.
- apply_reliability_flags mutates the passed ProvenanceData in place and
  writes verified_fields / text_agreement_fields to quality_metrics.

Tests cover each comparator, Literal/None skip, short-value skip (strings
and numerics), Decimal via optional union, and end-to-end flag+counter
writing against a Pydantic use-case schema that mirrors bank_statement_header.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 11:01:19 +02:00

41 lines
1.2 KiB
Python

"""Provenance subsystem — normalisers, mapper, verifier.
Three pieces compose the reliability check:
* :mod:`ix.provenance.normalize` — pure text/number/date/IBAN normalisers
used to compare OCR snippets to extracted values.
* :mod:`ix.provenance.mapper` — resolves LLM-emitted segment IDs to
:class:`~ix.contracts.provenance.FieldProvenance` entries.
* :mod:`ix.provenance.verify` — per-field-type dispatcher that writes the
``provenance_verified`` / ``text_agreement`` flags.
Only :mod:`normalize` is exported from the package at this step; the mapper
and verifier land in task 1.8.
"""
from __future__ import annotations
from ix.provenance.mapper import (
map_segment_refs_to_provenance,
resolve_nested_path,
)
from ix.provenance.normalize import (
normalize_date,
normalize_iban,
normalize_number,
normalize_string,
should_skip_text_agreement,
)
from ix.provenance.verify import apply_reliability_flags, verify_field
__all__ = [
"apply_reliability_flags",
"map_segment_refs_to_provenance",
"normalize_date",
"normalize_iban",
"normalize_number",
"normalize_string",
"resolve_nested_path",
"should_skip_text_agreement",
"verify_field",
]