Skip to main content
A
Docs

Classification

LLM-powered document type classification for Arabic and English documents.

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

TypeSub-typesKey Indicators
legalcontract, agreement, court_order, power_of_attorneyLegal terminology, party names, clauses, signatures
medicalprescription, lab_report, medical_certificate, discharge_summaryPatient info, diagnosis codes, medication names
financialinvoice, receipt, financial_statement, tax_returnAmounts, account numbers, transaction details
governmentofficial_letter, permit, application, circularMinistry headers, reference numbers, official stamps
identityemirates_id, passport, visa, driving_licenseID 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

FieldTypeDescription
document_typestringPrimary classification: legal, medical, financial, government, identity
confidencefloatClassification confidence (0.0-1.0)
sub_typestringSpecific document sub-type within the category
keywordsstring[]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 TypeTypical ConfidenceNotes
Identity0.90-0.99Highly structured, distinctive patterns
Government0.85-0.95Recognizable headers and reference numbers
Financial0.85-0.95Clear numeric patterns and terminology
Legal0.80-0.92Dependent on legal term density
Medical0.75-0.90Varied 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.