Repository navigation
fix(parser): keep short-line body pages out of PDF TOC removal - #20988
Conversation
RemoveTOCBoxes classifies a page as TOC from short boxes with chapter markers, guarded only by carriesProse (>2 boxes longer than 60 runes). A corpus of short lines (a legal code's articles wrap at ~35 runes, verse, lists) never produces such boxes, so prose stays 0 and a mixed page (TOC tail + body start) is read as a TOC page and dropped whole — losing body text such as an entire chapter plus its articles. Add a second body-evidence arm: marker-less, leader-less boxes longer than 20 runes. The leading-run scan keeps using the long-box signal alone so a cover page (short metadata lines, no body text) does not stop the scan before the TOC; every drop still passes the full guard.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
Harsh23Kashyap
left a comment
There was a problem hiding this comment.
LGTM at 34fdd02.
The body-text guard still runs before both outline-based and shape-based removal. Keeping the leading scan on long prose avoids stopping at a cover page, while the final guard protects the mixed TOC/body page. Leader-only boxes do not count as body evidence.
Ran CGO_ENABLED=0 go test ./internal/deepdoc/parser/pdf/layout -count=1 twice: PASS (0.809s, 0.769s). The five current CI checks passed. I did not independently rerun the real-PDF A/B comparison.
|
LGTM |
OCR detection misses dotted TOC leader columns, so the native-text recovery added in infiniflow#21399 emits them as their own 50-100 rune boxes. The remove_toc body guard (infiniflow#20988) reads any long, non-marker box as prose, so every page the recovery touched was classified as a body page and kept: remove_toc silently stopped removing the TOC of affected documents. Treat the recovered shapes as what they are: - a leader-only box is typographic filler and never prose; - a box ending in leader runs plus a page number is one TOC entry, so it confirms the page as a marker instead of contradicting it as body text; - a whole-box download-site ad line is not body evidence. Counting the ad lines of a piracy-supply PDF kept the last TOC page of such documents. Entry lines reuse tocEntryRunPattern through isTOCEntryLine, so the leader+number grammar stays in one place. The merged-block path still runs first, so a long box holding several entries keeps its existing detection.
OCR detection misses dotted TOC leader columns, so the native-text recovery added in infiniflow#21399 emits them as their own 50-100 rune boxes. The remove_toc body guard (infiniflow#20988) reads any long, non-marker box as prose, so every page the recovery touched was classified as a body page and kept: remove_toc silently stopped removing the TOC of affected documents. Treat the recovered shapes as what they are: - a leader-only box is typographic filler and never prose; - a box ending in leader runs plus a page number is one TOC entry, so it confirms the page as a marker instead of contradicting it as body text; - a whole-box download-site ad line is not body evidence. Counting the ad lines of a piracy-supply PDF kept the last TOC page of such documents. Entry lines reuse tocEntryRunPattern through isTOCEntryLine, so the leader+number grammar stays in one place. The merged-block path still runs first, so a long box holding several entries keeps its existing detection.
OCR detection misses dotted TOC leader columns, so the native-text recovery added in infiniflow#21399 emits them as their own 50-100 rune boxes. The remove_toc body guard (infiniflow#20988) reads any long, non-marker box as prose, so every page the recovery touched was classified as a body page and kept: remove_toc silently stopped removing the TOC of affected documents. Treat the recovered shapes as what they are: - a leader-only box is typographic filler and never prose; - a box ending in leader runs plus a page number is one TOC entry, so it confirms the page as a marker instead of contradicting it as body text; - a long box only counts as an entry when its leader run is real (tocMinLongEntryLeader), so prose ending in an ellipsis and a number keeps protecting its page; - long entry lines count toward the list threshold, so a bookmarkless TOC made of long entry boxes is still detected; - a whole-box download-site ad line is not body evidence. Counting the ad lines of a piracy-supply PDF kept the last TOC page of such documents. Entry lines reuse tocEntryRunPattern through isTOCEntryLine, so the leader+number grammar stays in one place. The merged-block path still runs first, so a long box holding several entries keeps its existing detection.
OCR detection misses dotted TOC leader columns, so the native-text recovery added in infiniflow#21399 emits them as their own 50-100 rune boxes. The remove_toc body guard (infiniflow#20988) reads any long, non-marker box as prose, so every page the recovery touched was classified as a body page and kept: remove_toc silently stopped removing the TOC of affected documents. Treat the recovered shapes as what they are: - a leader-only box is typographic filler and never prose; - a box ending in leader runs plus a page number is one TOC entry, so it confirms the page as a marker instead of contradicting it as body text; - a long box only counts as an entry when its leader run is real (tocMinLongEntryLeader), so prose ending in an ellipsis and a number keeps protecting its page; - long entry lines count toward the list threshold, so a bookmarkless TOC made of long entry boxes is still detected; - a bare URL line is not body evidence. It is the running ad of a piracy-supply PDF; prose that merely mentions a link still counts. The leader+number grammar accepts Roman-only page numbers and is reused by isTOCEntryLine. The merged-block path still runs first, so a long box holding several entries keeps its existing detection.
OCR detection misses dotted TOC leader columns, so the native-text recovery added in infiniflow#21399 emits them as their own 50-100 rune boxes. The remove_toc body guard (infiniflow#20988) reads any long, non-marker box as prose, so every page the recovery touched was classified as a body page and kept: remove_toc silently stopped removing the TOC of affected documents. Treat the recovered shapes as what they are: - a leader-only box is typographic filler and never prose; - a box ending in leader runs plus a page number is one TOC entry, so it confirms the page as a marker instead of contradicting it as body text; - a long box only counts as an entry when its leader run is real (tocMinLongEntryLeader), so prose ending in an ellipsis and a number keeps protecting its page; - long entry lines count toward the list threshold, so a bookmarkless TOC made of long entry boxes is still detected; - a bare URL that repeats on tocMinAdPages pages is a running ad and not body evidence. A one-off link (a reference, a links page) still counts, and so does prose that merely mentions a link. The leader+number grammar accepts Roman-only page numbers and is reused by isTOCEntryLine. The merged-block path still runs first, so a long box holding several entries keeps its existing detection.
OCR detection misses dotted TOC leader columns, so the native-text recovery added in infiniflow#21399 emits them as their own 50-100 rune boxes. The remove_toc body guard (infiniflow#20988) reads any long, non-marker box as prose, so every page the recovery touched was classified as a body page and kept: remove_toc silently stopped removing the TOC of affected documents. Treat the recovered shapes as what they are: - a leader-only box is typographic filler and never prose; - a box ending in leader runs plus a page number is one TOC entry, so it confirms the page as a marker instead of contradicting it as body text; - leader segments split by spaces count as one run, so OCR-split leaders still reach the threshold; - a long box only counts as an entry when its leader run is real (tocMinLongEntryLeader), and a short box needs tocMinShortEntryLeader, so prose ending in an ellipsis and a number keeps protecting its page; - long entry lines count toward the list threshold, so a bookmarkless TOC made of long entry boxes is still detected; - merged TOC blocks are confirmed entries, not prose, so several of them no longer guard their own page; - a bare URL that repeats on tocMinAdPages pages is a running ad and not body evidence. A one-off link (a reference, a links page) still counts, and so does prose that merely mentions a link. The leader+number grammar accepts Roman-only page numbers and is reused by isTOCEntryLine. The merged-block path still runs first, so a long box holding several entries keeps its existing detection.
OCR detection misses dotted TOC leader columns, so the native-text recovery added in infiniflow#21399 emits them as their own 50-100 rune boxes. The remove_toc body guard (infiniflow#20988) reads any long, non-marker box as prose, so every page the recovery touched was classified as a body page and kept: remove_toc silently stopped removing the TOC of affected documents. Treat the recovered shapes as what they are: - a leader-only box is typographic filler and never prose; - a box ending in leader runs plus a page number is one TOC entry, so it confirms the page as a marker instead of contradicting it as body text; - leader segments split by spaces count as one run, so OCR-split leaders still reach the threshold; - a long box only counts as an entry when its leader run is real (tocMinLongEntryLeader), and a short box needs tocMinShortEntryLeader, so prose ending in an ellipsis and a number keeps protecting its page; - long entry lines count toward the list threshold, so a bookmarkless TOC made of long entry boxes is still detected; - merged TOC blocks are confirmed entries, not prose, so several of them no longer guard their own page. The leader+number grammar accepts Roman-only page numbers and is reused by isTOCEntryLine. The merged-block path still runs first, so a long box holding several entries keeps its existing detection.
OCR detection misses dotted TOC leader columns, so the native-text recovery added in infiniflow#21399 emits them as their own 50-100 rune boxes. The remove_toc body guard (infiniflow#20988) reads any long, non-marker box as prose, so every page the recovery touched was classified as a body page and kept: remove_toc silently stopped removing the TOC of affected documents. Treat the recovered shapes as what they are: - a leader-only box is typographic filler and never prose; - a box ending in leader runs plus a page number is one TOC entry, so it confirms the page as a marker instead of contradicting it as body text; - leader segments split by spaces count as one run, so OCR-split leaders still reach the threshold; - a long box only counts as an entry when its leader run is real (tocMinLongEntryLeader), and a short box needs tocMinShortEntryLeader, so prose ending in an ellipsis and a number keeps protecting its page; - long entry lines count toward the list threshold, so a bookmarkless TOC made of long entry boxes is still detected; - merged TOC blocks are confirmed entries, not prose, so several of them no longer guard their own page. The leader+number grammar accepts Roman-only page numbers with a word boundary, so prose words are not mistaken for page numbers, and is reused by isTOCEntryLine. The merged-block path still runs first, so a long box holding several entries keeps its existing detection.
Summary
RemoveTOCBoxes(the PDFremove_tocpath) deletes whole pages it reads astable-of-contents pages, and its only content guard —
carriesProse()— countsboxes longer than 60 runes. A corpus of short lines never produces such boxes:
in
民法典every article line wraps at ~35 runes, soprosestayed 0 onevery page of the document (measured), and the guard never fired.
Consequence on a real file — page 7 of
民法典carries the TOC tail andthe body opening. It satisfied the TOC shape (markers=6, short boxes=24) and
passed the guard (prose=0), so the whole page was deleted: the
第一章 基本规定heading and articles第一条–第八条were silently lostfrom the parse, which then corrupted the downstream chunk hierarchy (chapter-1
articles appearing under the wrong heading).
Related Issue
Related to the box-level TOC detector introduced by #19779 (where the guard
lives) and adjacent to the CJK TOC detection issue #19744. Neither is closed by
this PR.
Changes
pageShapegains abodycounter: boxes that are neither chapter markersnor page numbers, longer than
tocBodyRunes(20), and not leader-only runs(
tocLeaderOnlyPattern— the dot column of a dotted TOC entry carries nobody text and must not count).
carriesProse()becomescarriesLongProse() || body > tocMaxBodyBoxes(2):a page carrying several such boxes is a body page that no signal may drop.
carriesLongProse()alone, so acover/copyright page (short metadata lines, no body text) does not stop the
scan before the TOC is reached; every drop still passes the full guard.
TestRemoveTOCBoxes_ShortLineBodyPageIsNotTOCreproducing the mixed TOC-tail + short-article page; verified to fail without
this change.
Test Plan
bash build.sh --test ./internal/deepdoc/parser/pdf/layout/— green,including the new test (red without the fix).
remove_toc=true, full-output content hash with vswithout the fix):
民法典_前15页.pdf: only the intended change — the wrongly dropped page isrestored (114 → 127 items, additions only;
第一章+第一条–第八条back);中华人民共和国法典_前15页.pdfand an English paper: byte-identicaloutput;
道德经(TOC + chapters): TOC pages still dropped, body kept; the only deltais the last pure-TOC page retained — the safe direction (a missed TOC page
costs noise chunks; a dropped content page loses text).
道德经_目录加前两章.pdf:前言/第1章/第2章/道可道all present.