Skip to content

Prefer fuzzy alignment over a shorter partial exact match - #591

Open
inamdarmihir wants to merge 1 commit into
google:mainfrom
inamdarmihir:fix/289-lesser-match-fuzzy-upgrade
Open

inamdarmihir wants to merge 1 commit into
google:mainfrom
inamdarmihir:fix/289-lesser-match-fuzzy-upgrade

Conversation

@inamdarmihir

Copy link
Copy Markdown
Contributor

Description

WordAligner.align_extractions accepts the first exact difflib block that starts at an extraction's first token and labels it MATCH_LESSER when it is shorter than the extraction. Fuzzy alignment only runs for extractions that are still unaligned, so it never runs for these. When the model paraphrases an extraction that shares only its opening tokens with the source (very common with CJK text), the resulting char_interval covers just those few tokens. In #289 a ~50 token extraction got an interval covering two characters.

This change tries LCS fuzzy alignment before settling for MATCH_LESSER, and uses the fuzzy span only when it matches strictly more tokens than the exact block (min_matches). Otherwise the existing MATCH_LESSER result is kept, so cases where the partial block is already the best answer are unchanged. Nothing changes when enable_fuzzy_alignment=False, accept_match_lesser=False, or with the legacy fuzzy algorithm.

Reproduction from the issue's data with UnicodeTokenizer: before, match_lesser over 每份; after, match_fuzzy over 每份汉堡约200克,含548大卡热量,其中蛋白质21.56克、脂肪18.12克、碳水化合物75.7克,能提供充足能量,但作为快餐食品,营养均衡方面有所欠缺.

Behavior note: tests/prompt_validation_test.py had a case (type-2 diabetes vs Type 2 diabetes.) pinning MATCH_LESSER; it now resolves to MATCH_FUZZY with the full span. It is still a non-exact alignment, so the validation issue count is unchanged.

Fixes #289

Bug fix

How Has This Been Tested?

Added shared_prefix_upgraded_to_fuzzy and shared_prefix_stays_lesser_without_fuzzy cases to AlignEntitiesTest.

$ pytest tests --deselect tests/create_provider_plugin_test.py
908 passed, 21 skipped
$ isort langextract tests && pyink langextract tests --config pyproject.toml

tests/create_provider_plugin_test.py has 3 failures on main in my environment (offline editable install / build isolation) unrelated to this change.

Checklist:

  • I have read and acknowledged Google's Open Source Code of conduct.
  • I have read the Contributing page, and I either signed the Google Individual CLA or am covered by my company's Corporate CLA.
  • I have discussed my proposed solution with code owners in the linked issue(s) and we have agreed upon the general approach.
  • I have made any needed documentation changes, or noted in the linked issue(s) that documentation elsewhere needs updating.
  • I have added tests, or I have ensured existing tests cover the changes
  • I have followed Google's Python Style Guide and ran pylint over the affected code.

@google-cla

google-cla Bot commented Oct 27, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@inamdarmihir
inamdarmihir force-pushed the fix/289-lesser-match-fuzzy-upgrade branch from dae7c03 to c9e1941 Compare October 27, 2026 07:13
When an extraction shared only its opening tokens with the source, the
exact phase accepted that short block as MATCH_LESSER and fuzzy
alignment never ran for it. With paraphrased extractions (common for
CJK text) this produced char_interval spans covering a couple of
tokens instead of the full passage.

Try LCS fuzzy alignment before settling for MATCH_LESSER and use it
only when it matches more tokens than the exact block.

Fixes google#289
@inamdarmihir
inamdarmihir force-pushed the fix/289-lesser-match-fuzzy-upgrade branch from c9e1941 to 08407ea Compare October 27, 2026 07:16
@github-actions

Copy link
Copy Markdown

⚠️ Branch Update Required

Your branch is 1 commits behind main. Please update your branch to ensure CI checks run with the latest code:

git fetch origin main
git merge origin/main
git push

Note: Enable "Allow edits by maintainers" to allow automatic updates.

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

⚠️ Branch Update Required

Your branch is 8 commits behind main. Please update your branch to ensure CI checks run with the latest code:

git fetch origin main
git merge origin/main
git push

Note: Enable "Allow edits by maintainers" to allow automatic updates.

@github-actions

Copy link
Copy Markdown

⚠️ Branch Update Required

Your branch is 13 commits behind main. Please update your branch to ensure CI checks run with the latest code:

git fetch origin main
git merge origin/main
git push

Note: Enable "Allow edits by maintainers" to allow automatic updates.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S Pull request with 50-150 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: When using version 1.1.1 to extracting Chinese text, char_interval is no longer empty, but it is not very accurate.

1 participant