How Classification Works
After OCR extracts text from a document, the classifier determines what type of document it is. This step is critical for routing documents to the correct processing pipeline and applying type-specific extraction rules.
The classifier sends the extracted text to an LLM (default: llama-3.3-70b-versatile via Groq) with a structured prompt that asks for the document type, sub-type, confidence score, and relevant keywords.
Document Categories
| Type | Sub-types | Key Indicators |
|---|---|---|
| legal | contract, agreement, court_order, power_of_attorney | Legal terminology, party names, clauses, signatures |
| medical | prescription, lab_report, medical_certificate, discharge_summary | Patient info, diagnosis codes, medication names |
| financial | invoice, receipt, financial_statement, tax_return | Amounts, account numbers, transaction details |
| government | official_letter, permit, application, circular | Ministry headers, reference numbers, official stamps |
| identity | emirates_id, passport, visa, driving_license | ID numbers, photo placeholders, biometric markers |
Running Classification
Classification requires a previously uploaded and OCR-processed document:
curl -X POST "http://localhost:8008/api/v1/docs/classify?document_id=DOC_ID"
Response:
{
"document_type": "identity",
"confidence": 0.95,
"sub_type": "emirates_id",
"keywords": ["Emirates ID", "الهوية", "784", "United Arab Emirates"]
}
Response Fields
| Field | Type | Description |
|---|---|---|
document_type | string | Primary classification: legal, medical, financial, government, identity |
confidence | float | Classification confidence (0.0-1.0) |
sub_type | string | Specific document sub-type within the category |
keywords | string[] | Key terms that influenced the classification |
Classification Logic
The LLM classifier evaluates multiple signals in the extracted text:
Structural Signals
- Document headers and footers (ministry logos, letterheads)
- Reference number patterns (e.g., "Ref: MOI-2024-001")
- Table structures and form fields
- Signature blocks and stamp areas
Lexical Signals
- Domain-specific vocabulary (legal: "hereinafter", "pursuant to"; medical: "diagnosis", "dosage")
- Arabic institutional terms (وزارة, هيئة, مؤسسة)
- ID number patterns (Emirates ID: 784-XXXX-XXXXXXX-X)
Language Signals
- Primary language of the document
- Mixed-language sections common in identity documents
- Formal MSA vs. colloquial Arabic
Arabic-First Classification
The classifier is tuned for GCC document conventions. Arabic-language government documents from UAE, Saudi, Qatar, and Oman are classified with higher confidence than generic international documents, because the training prompts include GCC-specific patterns and institutional naming conventions.
Accuracy Considerations
Classification confidence varies by document type:
| Document Type | Typical Confidence | Notes |
|---|---|---|
| Identity | 0.90-0.99 | Highly structured, distinctive patterns |
| Government | 0.85-0.95 | Recognizable headers and reference numbers |
| Financial | 0.85-0.95 | Clear numeric patterns and terminology |
| Legal | 0.80-0.92 | Dependent on legal term density |
| Medical | 0.75-0.90 | Varied formats, handwriting challenges |
For documents with confidence below 0.80, manual review is recommended before proceeding to extraction.
Integration with Extraction
Classification determines which extraction template is applied. An identity document triggers extraction of name, ID number, expiry date, and nationality fields. A financial document triggers extraction of amounts, accounts, and transaction dates.
This pipeline ensures that the extraction step has context about what fields to expect, improving both accuracy and completeness.