fix(pdf): merge paragraph breaks spuriously inserted mid-sentence - #2388
Open
Pujitha Paladugu (pujitha24) wants to merge 6 commits into
Open
fix(pdf): merge paragraph breaks spuriously inserted mid-sentence#2388Pujitha Paladugu (pujitha24) wants to merge 6 commits into
Pujitha Paladugu (pujitha24) wants to merge 6 commits into
Conversation
Motivation:
PDF text extraction occasionally inserts a spurious blank-line paragraph
break in the middle of a sentence. A genuine paragraph, heading, or list
item essentially never starts with a lowercase letter, so this can be
detected and corrected.
Approach:
Add `_merge_wrapped_paragraph_breaks`, a text-level post-processing pass
(alongside the existing `_merge_partial_numbering_lines` in the same
file) applied to the final markdown output. It splits text on blank
lines and rejoins a block with the previous one (using a single space)
when the previous block doesn't end in sentence-terminal punctuation
(`.!?:;"'"'`) and the next block starts with a lowercase letter
(optionally after a single opening parenthesis, to catch wrapped
parentheticals). Short lettered/numbered list markers ("a)", "(i)",
"1.", with or without a following space) are excluded via
`_LIST_MARKER_PATTERN` so list items are not glued together.
Validation:
Added `packages/markitdown/tests/test_pdf_wrapped_paragraph_breaks.py`
with 9 unit tests covering: merging a lowercase continuation, merging a
lowercase parenthetical continuation, leaving a real paragraph break
alone, leaving an uppercase-starting next block alone, a no-op single
paragraph, not merging lettered list items ("a)"/"b)"), not merging
parenthetical list items ("(i)"/"(ii)"), not merging a standalone
numeric paragraph into a following paragraph, and not merging list
markers that have no space after the marker (e.g. "a)text").
Report: microsoft#2370
Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com>
Assisted-by: claude-sonnet-5 (via Claude Code)
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation:
PDF text extraction occasionally inserts a spurious blank-line paragraph
break in the middle of a sentence. A genuine paragraph, heading, or list
item essentially never starts with a lowercase letter, so this can be
detected and corrected.
Approach:
Add
_merge_wrapped_paragraph_breaks, a text-level post-processing pass(alongside the existing
_merge_partial_numbering_linesin the samefile) applied to the final markdown output. It splits text on blank
lines and rejoins a block with the previous one (using a single space)
when the previous block doesn't end in sentence-terminal punctuation
(
.!?:;"'"') and the next block starts with a lowercase letter(optionally after a single opening parenthesis, to catch wrapped
parentheticals). Short lettered/numbered list markers ("a)", "(i)",
"1.", with or without a following space) are excluded via
_LIST_MARKER_PATTERNso list items are not glued together.Validation:
Added
packages/markitdown/tests/test_pdf_wrapped_paragraph_breaks.pywith 9 unit tests covering: merging a lowercase continuation, merging a
lowercase parenthetical continuation, leaving a real paragraph break
alone, leaving an uppercase-starting next block alone, a no-op single
paragraph, not merging lettered list items ("a)"/"b)"), not merging
parenthetical list items ("(i)"/"(ii)"), not merging a standalone
numeric paragraph into a following paragraph, and not merging list
markers that have no space after the marker (e.g. "a)text").
Report: #2370
Signed-off-by: Pujitha Paladugu 10557236+pujitha24@users.noreply.github.com
Assisted-by: claude-sonnet-5 (via Claude Code)