End-to-End Document Extraction
Core-Field Auto Extraction for Customs Declarations
Extracts KCS import-declaration fields from unstructured documents such as invoices and B/Ls, then outputs usable JSON/EDI.
One-line summary: Automated the path from input documents to text acquisition, candidate IR, scoring, SHACL validation, normalization, and EOC output across UI/CLI/API.
Role: end-to-end design/build · schema and quality assurance · documentation/dashboard
How It Works
Pre-inspection → preprocessing/OCR → routing → candidate extraction → scoring/SHACL → post-processing/normalization → EOC merge
- Pre-InspectCheck input state and required items
- Preprocess/OCRAcquire text and add OCR fallback
- RoutingDecide document type and processing path
- Candidate ExtractionFind field candidates from IR
- ScoringRun score and SHACL quality checks
- NormalizePost-process and merge into EOC
- OutputReview results through UI, CLI, or API
Evidence: Quality & Validation
Text Quality
- OCR preference based on
printable_ratio, pages, and file size - Quality inference through text statistics
SHACL Validation
- Conformance bonuses and violation penalties
- QC metrics and scoring
Normalization Guards
- Two-decimal amounts and YYYY-MM-DD dates
- Header-like string and category correction
Note: Metrics explicitly compare sample and operating-environment differences. “100% normalization” means accuracy under schema casting.
Data Contract
Supports a built-in DEFAULT_SCHEMA plus external report_format.json overrides, with loose casting through cast_contract_to_schema.
{
"vendor": "string",
"invoice_no": "string",
"date": "YYYY-MM-DD",
"amount_total": "number(2-digit decimals)",
"currency": "string",
"items": [
{ "desc": "string", "qty": "number", "unit_price": "number", "line_total": "number" }
],
"meta": {
"used_ocr": "bool",
"printable_ratio": "float",
"shacl": { "score": "float", "violations": "int" }
}
}Conceptual sample structure; actual fields follow the project schema.
API Sketch
POST /analyze
curl -X POST http://localhost:8080/analyze \
-H "Content-Type: multipart/form-data" \
-F "files=@sample.pdf"Input: files[]. Output: per-document candidates, ir, and pipeline artifacts.
POST /render
curl -X POST http://localhost:8080/render \
-H "Content-Type: application/json" \
-d '{"doc_id":"...", "select":"best"}'Applies post-processing and normalization to the selected result.
Module Summary
cli / poc_runner
- Pipeline orchestration and final EOC merge
run_pipeline,run,_merge_docs_to_eoc
pre_inspect
- MIME/page/readability/size checks and
prefer_ocrdecision - Outputs
inputs[]metadata
pre_flight
- Text acquisition with PDF extraction and OCR fallback
- Outputs
docs[]text/statistics
routing
- Classifies PDF and image paths
- Passes through
docs
multi_engine
- Builds IR and regex-based candidates
- Outputs
candidatesandir
scoring
- Combines weights, sanity checks, SHACL score, and best selection
- Outputs
scoreandqc{shacl}
post_process
- Schema casting, amount/date normalization, and cross-field correction
- Produces final
finalpayload
Utilities
- pdf_text: text statistics and post-processing
- ocr: page OCR
- compare: normalized diff
Dashboard / API
- FastAPI:
GET /,POST /analyze,/render - Includes Uvicorn execution scripts
Folder/File Structure Check
- cli/poc_runner.py required and present.
- core/compare.py, core/contract.py required and present at root.
- engine/image_preprocess.py required but not present.
- tests/dashboard_ui.py required; runtime uses root dashboard_ui.py module path.