Skip to content

Fix repeated-mention alignment with monotonic occurrence DP - #485

Merged
aksg87 merged 1 commit into
mainfrom
fix/aligner-monotonic-dp
Jul 2, 2026
Merged

aksg87 merged 1 commit into
mainfrom
fix/aligner-monotonic-dp

Conversation

@aksg87

@aksg87 aksg87 commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Fix WordAligner so repeated mentions align to their successive occurrences instead of all collapsing to the first: the difflib exact phase greedily prefers the earliest source occurrence, and one greedy seed can also force preceding extractions into first-occurrence fuzzy matches.
  • Add a monotonic occurrence DP as the default exact phase: one occurrence per extraction, order-preserving and non-overlapping, maximizing total matched tokens so longer extractions keep priority in contested regions. Unplaced extractions fall through to the unchanged difflib lesser-match and fuzzy phases.
  • Add exact_alignment_algorithm ("dp" default; "difflib" restores legacy behavior) to align_extractions, resolver_params, and AlignmentPolicy, mirroring the fuzzy_alignment_algorithm pattern.
  • Behavior changes: repeated mentions relocate to their correct occurrences, some verbatim spans upgrade MATCH_FUZZY → MATCH_EXACT, and exact spans of different extractions may now overlap (duplicates of a single mention share its span).

Tests

  • 8 new tests in tests/resolver_test.py covering successive occurrences, greedy-seed lockout, the legacy escape hatch, chunk offsets, and invalid-value errors
  • 20k randomized differential trials against a brute-force reference (selections optimal, constraint-valid, deterministic); DP never yields fewer MATCH_EXACT results than legacy across 300 random corpora
  • .venv/bin/python -m pytest tests -ra -m "not live_api"
  • .venv/bin/python -m tox -e format
  • .venv/bin/python -m tox -e lint-src
  • .venv/bin/python -m tox -e lint-tests

@github-actions github-actions Bot added the size/M Pull request with 150-600 lines changed label Jul 2, 2026
WordAligner's exact phase relied on difflib's greedy longest-block
seeding, which prefers the earliest source occurrence. One early seed
could lock out exact matches for extractions ordered before it, and
repeated mentions fell through to fuzzy alignment, which pins every
duplicate to its first occurrence.

Add a DP exact phase that selects one occurrence per extraction,
order-preserving and non-overlapping, maximizing total matched tokens
so longer extractions keep priority in contested regions. Unmatched
extractions fall through to the existing difflib (lesser-match) and
fuzzy phases unchanged. Legacy behavior remains available via
exact_alignment_algorithm='difflib'.
@aksg87
aksg87 force-pushed the fix/aligner-monotonic-dp branch from 3ab951f to 3feccd0 Compare July 2, 2026 07:51
@aksg87
aksg87 merged commit 0dff547 into main Jul 2, 2026
19 checks passed
@aksg87
aksg87 deleted the fix/aligner-monotonic-dp branch July 2, 2026 07:59
@aksg87 aksg87 mentioned this pull request Sep 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M Pull request with 150-600 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant