Source code for automated_document_parser.intelligence.analyzer

"""Public document intelligence facade."""

from __future__ import annotations

from collections.abc import Mapping
from typing import Any

from ..models import ParsedDocument
from .classification import classify_document
from .entities import extract_entities
from .extraction import RuleBasedSchemaExtractor, SchemaExtractionBackend
from .models import DocumentClassification, ExtractedEntity, StructuredExtraction


[docs] class DocumentIntelligence: """Classify documents and extract entities or schema-shaped data. The default backend is deterministic and dependency-free. A custom backend can implement ``SchemaExtractionBackend`` to integrate an LLM or domain model while preserving the same evidence-aware result contract. """
[docs] def __init__(self, schema_extractor: SchemaExtractionBackend | None = None) -> None: self.schema_extractor = schema_extractor or RuleBasedSchemaExtractor()
[docs] def classify( self, document: ParsedDocument, *, top_k: int = 3 ) -> DocumentClassification: """Return ranked document type hypotheses.""" return classify_document(document, top_k=top_k)
[docs] def extract_entities(self, document: ParsedDocument) -> tuple[ExtractedEntity, ...]: """Extract common entities with exact source evidence.""" return extract_entities(document)
[docs] def extract( self, document: ParsedDocument, schema: Mapping[str, Any] ) -> StructuredExtraction: """Extract schema fields and retain evidence for every populated value.""" result = self.schema_extractor.extract(document, schema) if not isinstance(result, StructuredExtraction): raise TypeError( "schema extraction backends must return a StructuredExtraction instance" ) return result