Skip to content

fix(chunking): keep dose units wrapped onto their own line by PDF extraction - #334

Merged
BigSimmo merged 2 commits into
mainfrom
claude/chunking-unit-lines
Jul 7, 2026
Merged

fix(chunking): keep dose units wrapped onto their own line by PDF extraction#334
BigSimmo merged 2 commits into
mainfrom
claude/chunking-unit-lines

Conversation

@BigSimmo

Copy link
Copy Markdown
Owner

Summary

PyMuPDF extraction (page.get_text("text", sort=True), the exact call in worker/python/extract_pdf_assets.py) splits a dose in a narrow table cell across lines: "12.5\nmg". looksLikeMetadataNoise treats any line of <= 2 characters as extraction debris, so removePageNoise silently deleted the unit line, indexing a unitless dose (12.5) — a clinical-output correctness risk.

removePageNoise now rejoins a unit-only line (mg, mcg, µg, g, kg, mL, L, IU, U, mmol, %) to the preceding line when that line ends in a digit, before the debris filter runs. Conservative by design:

  • a lone unit token with no preceding dose number is still dropped as debris;
  • standalone page footers are never merged into;
  • longer unit tokens (mcg, mmol) also get rejoined, so "100\nmcg" indexes as 100 mcg instead of two lines.

Evidence

  • Live corpus could not be sampled (Supabase keys rotated 2026-07-06, no valid secret key on this machine).
  • Mechanism proven locally with the production extractor: a narrow-cell PDF built with PyMuPDF and extracted via get_text("text", sort=True) yields '12.5\nmg' / '600\nmcg'. Real guideline dose tables use exactly this narrow-column layout.
  • The three synthetic demo PDFs (wide layout) do not produce short lines, so demo mode was unaffected.

Tests

  • 4 regression tests added in tests/chunking.test.ts (rejoin decimal + integer doses, lone-unit still dropped, footer not merged). 31/31 pass.
  • npm run typecheck and prettier --check clean.
  • Contract comment in tests/property-chunking.test.ts (branch claude/audit-sweep-property-tests) updated to note the exception; its generators never emit unit-only lines so the property suite is unaffected.

Governance note

Touches ingestion chunking only; behavior change is additive (keeps/rejoins clinical content that was previously deleted). Existing indexed chunks are unaffected until a re-index; new ingestions pick this up immediately.

🤖 Generated with Claude Code

…raction
PyMuPDF get_text(sort=True) splits a dose in a narrow table cell across
lines ('12.5\nmg'); the <=2-char debris rule then deleted the unit line,
indexing a unitless dose. removePageNoise now rejoins a unit-only line
(mg, mcg, mL, IU, %, ...) to a preceding digit-ending line before
filtering. A lone unit with no preceding number is still dropped, and
page footers are never merged into.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@supabase

supabaseBot commented Jul 6, 2026

Copy link
Copy Markdown

This pull request has been ignored for the connected project sjrfecxgysukkwxsowpy because there are no changes detected in supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

@BigSimmo
BigSimmo marked this pull request as ready for review July 6, 2026 18:12
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@BigSimmo
BigSimmo enabled auto-merge July 6, 2026 18:12
Co-authored-by: BigSimmo <BigSimmo@users.noreply.github.com>
Sign up for freeto 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.

2 participants

@BigSimmo@cursoragent