UNPKG

mindee

Version:

Mindee Client Library for Node.js

83 lines (82 loc) 3.38 kB
import { CropperExtra, FullTextOcrExtra, Extras } from "./extras/index.js"; import { OrientationField } from "./orientation.js"; /** * Page prediction wrapper class. Holds the results of a parsed document's page. * Holds a `Prediction` that's either a document-level Prediction, or inherits from one. * @typeParam T an extension of an `Prediction`. Mandatory in order to properly create a page-level prediction. */ export class Page { /** * @param predictionType constructor signature for an inference. * @param rawPrediction raw http response. * @param pageId the page's index (identifier). * @param orientation the page's orientation. */ constructor(predictionType, rawPrediction, pageId, orientation) { if (pageId !== undefined && orientation !== undefined) { this.orientation = new OrientationField({ prediction: orientation, pageId: pageId, }); } this.id = pageId; this.prediction = new predictionType(rawPrediction["prediction"], pageId); if (rawPrediction["extras"] && Object.keys(rawPrediction["extras"].length > 0)) { const extras = {}; Object.entries(rawPrediction["extras"]).forEach(([extraKey, extraValue]) => { if (extraValue) { switch (extraKey) { case "cropper": extras["cropper"] = new CropperExtra(extraValue); break; case "full_text_ocr": extras["fullTextOcr"] = new FullTextOcrExtra(extraValue); break; } } }); this.extras = new Extras(extras); } if (!this.extras || !("fullTextOcr" in this.extras) || this.extras["fullTextOcr"].toString().length === 0) { this.injectFullTextOcr(rawPrediction); } } /** * Default string representation. */ toString() { const title = `Page ${this.id}`; return `${title} ${"-".repeat(title.length)} ${this.prediction.toString()} `; } injectFullTextOcr(rawPrediction) { if (!("extras" in rawPrediction) || !("full_text_ocr" in rawPrediction["extras"])) { return; } if (!rawPrediction["extras"] || rawPrediction["extras"] === null || !rawPrediction["extras"]["full_text_ocr"] || rawPrediction["extras"]["full_text_ocr"] === null || !rawPrediction["extras"]["full_text_ocr"]["content"] || rawPrediction["extras"]["full_text_ocr"]["content"] === null) { return; } const fullTextOcr = rawPrediction["extras"]["full_text_ocr"]["content"]; const artificialTextObj = { // eslint-disable-next-line @typescript-eslint/naming-convention "full_text_ocr": { "content": fullTextOcr.length > 0 ? fullTextOcr : "", }, }; if (!this.extras) { // eslint-disable-next-line @typescript-eslint/naming-convention this.extras = new Extras({ "full_text_ocr": new FullTextOcrExtra(artificialTextObj) }); } else { this.extras["fullTextOcr"] = new FullTextOcrExtra(artificialTextObj); } } }