Repository navigation
fix: drop PDF page markers and stop chunks from splitting words - #540
Merged
Merged
Conversation
pdf-parse 2.x inserts "-- N of M --" between pages by default; pass an empty pageJoiner so pages are joined by a blank line only. Oversized paragraphs now split at the last whitespace within the limit, overlap prefixes start at a word boundary (keeping CJK text, which has no spaces), and the streaming chunker for content over 1 MB snaps window starts and ends to whitespace and only treats a period followed by whitespace as a sentence end.
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
|
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.



Problem
Chunks from indexed PDFs had two defects that hurt search:
-- 12 of 92 --, sometimes twice in a row....the Enterprise Message API rand the next startingesorts to, or an overlap startingction within an application.Causes
getText()defaultspageJoinerto'\n-- page_number of total_number --'(src/core/parsers/pdf.ts).splitAtParagraphshard-split an oversized paragraph everymaxSizecharacters. PDF text has few blank lines, so most PDF chunks went through this path.addChunkOverlaptookprev.slice(-overlapChars)at a raw offset.windowEnd - overlap, an arbitrary character, andfindWindowBoundaryaccepted any.(including3.10.0) or cut atend.Fix
getText({ pageJoiner: "" }). Pages are still joined by a blank line, so words do not fuse across pages.splitAtParagraphssplits at the last whitespace within the limit. It hard-splits only a single token longer than the limit.addChunkOverlapdrops a leading partial word from the overlap. Han, Hiragana and Katakana are exempt, so CJK text keeps its overlap.windowEnd, which the previous window already covered.findWindowBoundarytakes a.only when whitespace follows it, then a newline, then whitespace, then the end of the text, within 200 characters.Effect on other documents
Any document with a paragraph larger than
maxChunkSizegets slightly different chunk boundaries. Nothing depends on chunk boundaries: document dedup and change detection hash the whole content. Already-indexed documents keep their chunks until re-indexed. A PDF's content hash changes because the markers are gone, so it re-indexes once when next seen.Tests
New tests in
tests/unit/indexing.test.tsandtests/unit/parsers.test.ts(a two-page PDF built in the test). Each fails without its fix, except two guards that pin unchanged behaviour: the long-token hard split and pure CJK overlap.Verification
npm run format:check,lint(0 errors),typecheck,npm test: 2154 tests pass.chunkContentandchunkContentStreaming(10,000 random texts each, with spaces, tabs, CRLF, NBSP, CJK, dotted tokens and over-long words). No empty chunks, no lost or reordered words, no oversized chunks, no non-terminating runs, and no mid-word cut where whitespace was available.splitIntoChunks: 0 fragments and 0 lost tokens, against 3,103 fragments and 1,506 lost tokens onmain.Not verified
test:coverage,buildanddocs:checkwere not run on this branch.