Skip to content

Add Page.search_for_fuzzy() for edit-distance text search - #5293

Open
veget-able wants to merge 2 commits into
mainfrom
feature/fuzzy-text-search
Open

veget-able wants to merge 2 commits into
mainfrom
feature/fuzzy-text-search

Conversation

@veget-able

Copy link
Copy Markdown
Collaborator

Adds Page.search_for_fuzzy(needle, *, max_edits, clip=None, textpage=None) to find text with small spelling differences. It returns one dictionary per occurrence, containing the extracted text, Levenshtein distance and grouped quads for highlighting. No additional dependency is required.

for match in page.search_for_fuzzy("revenue increasd today", max_edits=1):
    print(match["text"], match["distance"])
    page.add_highlight_annot(match["quads"])

The matcher normalizes Unicode case, whitespace and line-end hyphens, while preserving source character boundaries. Exact query pieces narrow the candidate regions before a bounded edit-distance scan. The API documentation describes overlap selection, extraction order, TextPage reuse and the limits of interpreting edit distance.

An internal RAWDICT mode retains native character quads when quad corrections are disabled, including the setting used by PyMuPDF4LLM. This keeps highlighting accurate without changing global settings. The usual RAWDICT output remains unchanged.

Local measurements with 100-character queries and max_edits=2, including extraction and quad construction, were 3.8–5.4 times the native search_for(quads=True) time with corrections enabled, and 5.8–6.6 times with corrections disabled. These cover 1k, 5k and 20k synthetic pages and the first page of github_sample.pdf; repeated text can be substantially slower. The documented usage is exact search followed by a fuzzy fallback.

Validation on Windows, Python 3.13.2 and MuPDF 1.28.6:

  • Built and installed the wheel, rebuilding the Python/C++ bindings against the released MuPDF SDK.
  • 115 search, text extraction, clipping, quad and annotation tests passed. The 59 fuzzy tests also passed with quad corrections initially disabled.
  • Repository-wide tests/test_pylint.py passed, including the new module.
  • Sphinx 8.1.3 generated HTML with the same 126 warnings as upstream 75ea5228; no new warnings.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant