import docx def create_path_b_doc(): doc = docx.Document() # Title doc.add_heading('Path B Implementation Details: Generalized Scanned Document OCR', 0) # Introduction doc.add_heading('1. Overview', level=1) doc.add_paragraph('Path B is the OCR pipeline designated for scanned or low-quality digital documents that lack an explicit extraction template. It uses a hybrid approach combining Table Transformer (TATR), PaddleOCR, and a SmolDocling fallback to ensure accurate data extraction without relying on predetermined bounding boxes.') # Architecture doc.add_heading('2. Pipeline Architecture', level=1) doc.add_paragraph('The extraction runs on a per-page basis and follows a three-step process:') # Step 1: Table Extraction doc.add_heading('Step 2.1: Table Extraction and Structuring', level=2) p1 = doc.add_paragraph('Initially, the ') p1.add_run('Table Transformer (TATR)').bold = True p1.add_run(' model evaluates the page to detect and structure tabular data. If TATR fails to identify any tables (often the case with borderless layouts), an ') p1.add_run('img2table fallback').bold = True p1.add_run(' is invoked to catch remaining borderless tables. This ensures structured grids are extracted first.') # Step 2: Masked PaddleOCR doc.add_heading('Step 2.2: Masked Free-Text OCR', level=2) p2 = doc.add_paragraph('Once tables are identified, their specific regions on the page image are ') p2.add_run('whited-out (masked)').bold = True p2.add_run('. Then, ') p2.add_run('PaddleOCR').bold = True p2.add_run(' scans the remaining unmasked areas. Masking the tables prevents the text within them from being re-extracted as free text, thereby avoiding data duplication.') # Step 3: SmolDocling Fallback doc.add_heading('Step 2.3: Low-Confidence Fallback (SmolDocling)', level=2) p3 = doc.add_paragraph('The system evaluates the confidence scores of the text blocks extracted by PaddleOCR. If more than ') p3.add_run('30% of the blocks score below a 0.75 confidence threshold').bold = True p3.add_run(', the pipeline triggers ') p3.add_run('SmolDocling').bold = True p3.add_run(' (provided it is enabled via the CT_ENABLE_SMOLDOCLING environment variable) to re-evaluate the page and merge the results. This handles highly degraded scans.') # Data Schema doc.add_heading('3. Output Schema', level=1) doc.add_paragraph('The output matches the schema of Path A. It returns a PageExtractionResult containing:') doc.add_paragraph('? fields: A list of TraceabilityField objects containing free text.', style='List Bullet') doc.add_paragraph('? tables: A list of lists of TraceabilityField objects, representing individual table cells.', style='List Bullet') # Pre-processing dependency doc.add_heading('4. Pre-processing', level=1) doc.add_paragraph('Before Path B begins, documents undergo deskewing, binarization, and perspective correction (handled by the preprocessing module) to maximize the accuracy of the underlying PaddleOCR and TATR models.') # Save doc.save('Path_B_Implementation.docx') print("Document successfully created: Path_B_Implementation.docx") if __name__ == "__main__": create_path_b_doc()