--- name: document-structure-analysis description: Map Nordea PDF layout into zones (header, account info, table, footer) and compute a layout signature hash for template matching. Use after text conversion to classify document regions and detect template version. metadata: category: document_processing priority: CRITICAL config_id: document_structure_analysis --- # Document Structure Analysis ## Purpose Zone classification + template fingerprinting. Enables template_library matching (PerfectPDF Skill B) and anchors table extraction coordinates. ## Zones (Nordea Tiliote, A4 595x842) | Zone | Y range | Contents | |------|---------|----------| | header | 15-126 | Nordea logo, bank info | | account | 40-110 | Account holder, address, IBAN | | period | ~130-160 | "Kausi: DD.MM.YYYY - DD.MM.YYYY" | | table | 170-800 | Transaction rows | | footer | 800-820 | "Sivu X/Y", disclaimers | ## Workflow 1. Load spans from `pdf-text-conversion` 2. Assign each span to a zone by bbox Y 3. Extract anchors: IBAN regex, "Kausi:" period line, "Sivu X/Y" page marker 4. Compute layout signature: SHA1 of (page_count, block_count per page, font set, zone boundaries) 5. Match signature against `templates` table -> template_id + version ## Code ```python import hashlib, re IBAN_RE = re.compile(r"FI\d{2}\s?\d{4}\s?\d{4}\s?\d{4}\s?\d{2}") PERIOD_RE = re.compile(r"Kausi:\s*(\d{2}\.\d{2}\.\d{4})\s*-\s*(\d{2}\.\d{2}\.\d{4})") def layout_signature(pages: list[dict]) -> str: parts = [str(len(pages))] for p in pages: fonts = sorted({s["font"] for s in p["spans"]}) parts.append(f"{len(p['spans'])}:{'|'.join(fonts)}") return hashlib.sha1(";;".join(parts).encode()).hexdigest() def find_anchors(full_text: str) -> dict: return { "iban": (m.group(0) if (m := IBAN_RE.search(full_text)) else None), "period": PERIOD_RE.findall(full_text), } ``` ## Integration - Feeds: `pdf-table-extraction` (zone coords), template matching - Uses: `pdf-text-conversion` output ## Errors & Edge Cases - Unknown signature -> create new template candidate, flag for human review - Extra pages (attachments) -> exclude from signature, process separately