Deep Document Understanding (deepdoc)
RAGFlow's neural document parsing pipeline β from scanned PDFs to structured chunks.
Simple text extraction fails on complex documents. deepdoc combines layout recognition, OCR, and table understanding to extract structured information while preserving semantic relationships.
Architecture at a Glance
deepdoc orchestrates three layers: file-format specific parsers (PDF, DOCX, Excel, HTML, etc.), neural vision models (layout recognition, OCR, table detection), and semantic chunking that respects document structure.
Sub-Modules
Parser Module
File-format specific extraction β PDF, DOCX, Excel, HTML, Markdown, JSON, Text, EPUB, PowerPoint.
- pdf_parser.py β Master PDF handler (85KB)
- docx_parser.py β DOCX via python-docx
- excel_parser.py β XLSX cell extraction
- html_parser.py β HTML tree traversal
- Alternative engines β Docling, MinerU, PaddleOCR, TCADp, OpenDataLoader
Vision Module
Neural recognizers for layout analysis and OCR β DLA-based layout detection, ONNX inference, table cell recognition, and multi-language OCR.
- layout_recognizer.py β 12-class layout detection
- ocr.py β Text detection & recognition
- table_structure_recognizer.py β Cell grid decomposition
- recognizer.py β ONNX base class
- postprocess.py β NMS & filtering
Narrated Walkthrough: Scanned PDF to Chunks
Let's trace how deepdoc processes a real-world document β a scanned 10-page financial report with mixed layouts, tables, and figures.
- Input: scanned_report.pdf (10 pages, 300 DPI, JPEG-compressed pages)
- Load PDF: pdf_parser.py opens file via pdfplumber
- Attempt text extraction per page
- Pages 1-3: success (OCR'd layer embedded)
- Pages 4-10: no text found (pure image scans)
- Render & Zoom: Pages 4-10 rendered at 3x zoom (900 DPI equivalent)
- Improves OCR accuracy
- Helps layout recognizer detect small elements
- Layout Recognition: LayoutRecognizer processes each page image
- ONNX DLA model detects 12 classes: Title, Text, Table, Figure, Caption, Header, Footer, etc.
- Outputs bboxes: {x, y, w, h, class_label, confidence}
- Example: page 4 β [Title@top, Text@left_col, Figure@right, Table@bottom, Caption@figure]
- Region Assignment: Text from steps 2 & 3 assigned to detected bboxes
- Text-class bboxes: assign extracted text
- Figure-class bboxes: crop image, save separately, extract caption from adjacent Text bbox
- Table-class bboxes: mark for specialized handling
- Table Structure Recognition: For detected Table bboxes
- Crop image region
- Run table_structure_recognizer β detects cell grid (rows, cols, merged cells)
- Reconstruct as Markdown table or HTML
- OCR Fallback: Pages with low text confidence
- Run CRAFT text detection on image
- Run PaddleOCR on detected regions
- Merge results, preserving spatial order
- Page Assembly: Assemble all regions into semantic blocks
- Example page 4 result: [{type: "title", text: "Q3 2025 Report"}, {type: "text", text: "..."}, {type: "table", html: "
..."}, {type: "figure", text: "...", image_path: "fig_1.png"}]
- Bbox-Aware Chunking: Respect semantic boundaries when chunking
- Keep tables whole (or split by rows if oversized)
- Keep figures with captions
- Split long text blocks by heading hierarchy, not token count
- Output: List of chunks, each with:
- Content (text, HTML, or image reference)
- Type (heading, body, table, figure)
- Page number & bbox coordinates (enables spatial search)
- Confidence scores (OCR, layout, table detection)
- Metadata (figure captions, table row/col indices)
Design Philosophy
What is this? β deepdoc's core insight: layout matters. A document is not a flat sequence of charactersβit's a 2D spatial arrangement. Respecting this arrangement during parsing preserves semantic relationships that flat text extraction loses.Why multiple parsers? β Different documents demand different strategies. A scanned resume needs OCR + text layout. A born-digital business report needs table structure. Chinese documents need language-specific OCR. deepdoc offers pluggable engines so you choose the right strategy for your corpus.Why ONNX + PaddlePaddle? β ONNX is framework-agnostic (deploy anywhere, no TensorFlow/PyTorch dependency). PaddlePaddle models were trained on massive document datasets (stronger than general vision models). Combined: portable, accurate, lightweight.Key Concepts
Concept Explanation BBox (Bounding Box) Rectangular region {x, y, width, height} on a page. Every detected layout element (text block, table, figure) has a bbox for spatial awareness. Layout Class One of 12 semantic labels: Text, Title, Figure, Table, Figure caption, Table caption, Header, Footer, Reference, Equation. Assigned by LayoutRecognizer. OCR Confidence 0-1 score from PaddleOCR per character. High confidence = reliable text; low = degraded image or unusual font. Table Structure Grid of cells with row/col indices and merge information. Enables round-trip to Markdown/HTML without data loss. Fallback Chain On failure: try faster method first, fall back to slower but more robust method. E.g., text extraction β OCR. Semantic Chunking Respects document structure (headings, table boundaries, figure-caption pairs) when splitting text, unlike token-based chunking. Common Patterns
Pattern 1: Born-Digital PDF with Tables
Fast path: pdfplumber text extraction β layout recognition β table detection β HTML reconstruction. No OCR needed.
Pattern 2: Scanned Document (100% image)
Slow path: render page β layout recognition β OCR for text regions β table detection β assembly. Fallback OCR compensates for missing text layer.
Pattern 3: Multi-Column Layout
Layout recognizer detects column-ordered text blocks. Spatial reassembly preserves reading order before chunking.
Pattern 4: Figure + Caption
Figure bbox detected, adjacent Text bbox with caption extracted, both linked in output metadata for RAG context.
Summary
deepdoc transforms document parsing from "extract all text, hope for the best" to "understand layout, extract intelligently, preserve structure." By combining layout recognition, language-specific OCR, table analysis, and semantic chunking, deepdoc enables RAG systems to work with real-world documentsβscanned, digital, mixed-format, anything.
- Bbox-Aware Chunking: Respect semantic boundaries when chunking
- Example page 4 result: [{type: "title", text: "Q3 2025 Report"}, {type: "text", text: "..."}, {type: "table", html: "