mindee
Version:
Mindee Client Library for Node.js
88 lines (87 loc) • 3.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Page = void 0;
const extras_1 = require("./extras");
const extras_2 = require("./extras/extras");
const orientation_1 = require("./orientation");
/**
* 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.
*/
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 orientation_1.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 extras_1.CropperExtra(extraValue);
break;
case "full_text_ocr":
extras["fullTextOcr"] = new extras_1.FullTextOcrExtra(extraValue);
break;
}
}
});
this.extras = new extras_2.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_2.Extras({ "full_text_ocr": new extras_1.FullTextOcrExtra(artificialTextObj) });
}
else {
this.extras["fullTextOcr"] = new extras_1.FullTextOcrExtra(artificialTextObj);
}
}
}
exports.Page = Page;