43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
// The part of an OCR result this module reads, as a schema: what is kept as the OCR stage's cached
|
|
// artefact, so a file read once is not paid for twice. Words and polygons are not kept, and the
|
|
// SDK's own `OcrDocument` is assignable to the type this yields.
|
|
|
|
import { z } from "zod"
|
|
import { PAGE_CLASSES } from "../contract/index.js"
|
|
import type { ReadDocument } from "./ports.js"
|
|
|
|
const lineSchema = z.object({
|
|
text: z.string(),
|
|
bbox: z.tuple([z.number(), z.number(), z.number(), z.number()]),
|
|
confidence: z.number(),
|
|
order: z.number(),
|
|
repeated: z.boolean(),
|
|
})
|
|
|
|
const blockSchema = z.object({
|
|
id: z.string().min(1),
|
|
bbox: z.tuple([z.number(), z.number(), z.number(), z.number()]),
|
|
order: z.number(),
|
|
lines: z.array(z.number().int().min(0)),
|
|
})
|
|
|
|
const pageSchema = z.object({
|
|
index: z.number().int().min(0),
|
|
width: z.number(),
|
|
height: z.number(),
|
|
class: z.enum(PAGE_CLASSES),
|
|
source: z.enum(["ocr", "text_layer", "spreadsheet"]).optional(),
|
|
text: z.string(),
|
|
lines: z.array(lineSchema),
|
|
blocks: z.array(blockSchema),
|
|
})
|
|
|
|
export const readDocumentSchema: z.ZodType<ReadDocument> = z.object({
|
|
engine: z.object({ name: z.string(), version: z.string() }),
|
|
page_count: z.number().int().min(0),
|
|
pages: z.array(pageSchema),
|
|
})
|
|
|
|
/** What of a document is worth keeping: exactly the fields of `ReadDocument`, and no more. */
|
|
export const projectDocument = (document: ReadDocument): ReadDocument => readDocumentSchema.parse(document)
|