UNPKG

mindee

Version:

Mindee Client Library for Node.js

67 lines (66 loc) 2.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CustomV1Document = void 0; const common_1 = require("../../parsing/common"); const custom_1 = require("../../parsing/custom"); /** * Document data for Custom builds. */ class CustomV1Document { constructor(rawPrediction, pageId) { /** Map of fields for a Custom build. */ this.fields = new Map(); /** Map of classification fields for a Custom build. */ this.classifications = new Map(); Object.entries(rawPrediction).forEach(([fieldName, fieldValue]) => { this.setField(fieldName, fieldValue, pageId); }); } /** * Sorts and sets fields between classification fields and regular fields. * Note: Currently, two types of fields possible in a custom API response: * fields having a list of values, and classification fields. * @param fieldName name of the field. * @param fieldValue value of the field. * @param pageId page the field was found on. */ setField(fieldName, fieldValue, pageId) { if (fieldValue && fieldValue["values"] !== undefined) { // Only value lists have the 'values' attribute. this.fields.set(fieldName, new custom_1.ListField({ prediction: fieldValue, pageId: pageId, })); } else if (fieldValue && fieldValue["value"] !== undefined) { // Only classifications have the 'value' attribute. this.classifications.set(fieldName, new custom_1.ClassificationField({ prediction: fieldValue })); } else { throw new Error(`Unknown API field type for field ${fieldName} : ${fieldValue}`); } } /** * Order column fields into line items. * @param anchorNames list of possible anchor fields. * @param fieldNames list of all column fields. * @param heightTolerance height tolerance to apply to lines. */ columnsToLineItems(anchorNames, fieldNames, heightTolerance = 0.01) { return (0, custom_1.getLineItems)(anchorNames, fieldNames, this.fields, heightTolerance); } /** * Default string representation. */ toString() { let outStr = ""; this.classifications.forEach((fieldData, name) => { outStr += `:${name}: ${fieldData}\n`; }); this.fields.forEach((fieldData, name) => { outStr += `:${name}: ${fieldData}\n`; }); return (0, common_1.cleanOutString)(outStr).trimEnd(); } } exports.CustomV1Document = CustomV1Document;