UNPKG

mindee

Version:

Mindee Client Library for Node.js

54 lines (53 loc) 1.86 kB
import { BaseField } from "./base.js"; import { Polygon, getBoundingBox } from "../../../geometry/index.js"; /** * A basic field with position and page information. */ export class Field extends BaseField { /** * @param {BaseFieldConstructor} constructor Constructor parameters. */ constructor({ prediction = {}, valueKey = "value", reconstructed = false, pageId, }) { super({ prediction, valueKey, reconstructed, pageId }); /** * Contains the relative vertices coordinates (points) of a polygon containing * the field in the document. */ this.polygon = new Polygon(); this.confidence = prediction["confidence"] ? prediction["confidence"] : 0.0; if (prediction["polygon"]) { this.polygon = prediction["polygon"]; } this.boundingBox = getBoundingBox(this.polygon); } /** @param array1 first Array of Fields @param array2 second Array of Fields @param attr Attribute to compare @returns true if all elements in array1 exist in array2 and vice-versa, false otherwise */ static compareArrays(array1, array2, attr = "value") { const list1 = array1.map((item) => item[attr]); const list2 = array2.map((item) => item[attr]); if (list1.length !== list2.length) return false; for (const item1 of list1) { if (!list2.includes(item1)) return false; } return true; } /** * @param {Field[]} array - Array of Fields * @returns {number} product of all the fields probability */ static arrayConfidence(array) { let total = 1.0; for (const field of array) { total *= field.confidence; if (isNaN(total)) return 0.0; } return total; } }