In commercial law and public judiciary administration, data does not arrive as clean markdown or formatted JSON. It arrives as thousands of multi-gigabyte scanned PDF packets: crooked photocopies, faded carbon copies from 1984, faint handwritten judicial endorsements, and dense double-column trial transcripts.
When building the document intelligence engine for QanoonMate, we had to process over 10 million pages of judicial case law, statutory codes, and litigation filings.
Why Standard OCR Pipelines Collapse on Legal Files
Standard document parsing libraries (such as naive pdfplumber or generic cloud OCR) create severe downstream retrieval errors:
- Column Collision: Reading across a two-column legal brief interleaves lines, turning coherent legal arguments into nonsensical word salads.
- Header/Footer Contamination: Repeating case caption banners and page numbers pollute vector embeddings with irrelevant token noise.
- Table & Schedule Mutilation: Creditor settlement matrices and damages calculations lose row-column coordinate integrity.
The Hierarchical Ingestion Architecture
Raw Scanned PDF File
│
▼
[Document Pre-Processing & Deskewing] (OpenCV Morphological Clean)
│
▼
[Vision-Layout Transformer] (Detects columns, headers, stamps, tables)
│
▼
[Hierarchical Chunking Engine]
- Parent Chunks (Full Legal Section / Argument Block - 2000 tokens)
- Child Chunks (Atomic Clauses / Holdings - 300 tokens)
│
▼
[Hybrid Search Ingestion]
- Dense Vector Embeddings (Domain-Adapted Legal Embedder)
- Sparse BM25 Keywords (Statute numbers, Case Citation Keys)
Preserving Legal Citation Semantics
A critical insight from our domain research: legal citations are explicit knowledge graph edges.
When an opinion states "See Marbury v. Madison, 5 U.S. 137 (1803) at 177", a purely semantic vector model treats this as an abstract collection of tokens. A legal practitioner, however, requires exact citation lookup.
We implement automated regex and NER citation extractors that tag chunks with structured metadata:
export interface LegalDocumentChunk {
id: string;
documentId: string;
matterId: string;
sectionTitle: string;
pageNumber: number;
text: string;
citationsFound: {
caseName: string;
reporterVolume: number;
reporterName: string;
firstPage: number;
year: number;
}[];
embedding: number[];
}
By combining dense semantic vectors with explicit citation graph edges in a hybrid index, QanoonMate returns relevant precedent in under 40 milliseconds with zero citation drift.


