Towards Data Science
Read post

Building Document Structure with Loop Engineering: Recovering a PDF’s Outline from Body Typography for RAG

A detailed walkthrough of recovering a PDF's table of contents from body typography when no native outline or printed contents page exists, as part of an enterprise RAG pipeline. Six deterministic per-line signals (font size ratio, boldness, numeric prefix, short length, left alignment, blank line above) score heading candidates from an enriched line_df/span_df. A bounded LLM validation loop then filters false positives. The approach handles three scenarios: no TOC at all, partial native outlines needing deeper levels, and composite concatenated documents. Evaluated against six real PDFs, the deterministic pass achieves 72–100% recall on decimal-numbered documents but poor precision (7–38%), which the LLM validation lifts dramatically (to 72–100%) while preserving recall on most fixtures. The article also introduces a two-layer enrichment concept where typography-derived hierarchy (level) is complemented by business-domain tags per paragraph for more precise RAG retrieval.

    #rag
Yesterday•25m read time•From towardsdatascience.com
Post cover image
Table of contents
1. Where this article sits and what it does2. Four cases, one cascade3. How the cascade reconstructs a TOC4. Testing against native outlines: how well does it work5. Beyond a single hierarchical TOC: multi-tag enriched structure6. Conclusion7. Further reading and sources
9 Impressions
Towards Data Science's image
Towards Data Science

Towards Data Science is a community-powered publication that showcases work in data science, machine...

1.2K Followers

•

7.3K Upvotes

Would you recommend this post?

Copy link
WhatsApp
Facebook
X
New Squad
  • © 2026 Daily Dev Ltd.
  • Guidelines
  • Explore
  • Tags
  • Sources
  • Squads
  • Leaderboard