mindee 1.4.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/CHANGELOG.md +33 -19
  2. package/README.md +43 -127
  3. package/bin/mindee +3 -0
  4. package/dist/package.json +70 -0
  5. package/dist/src/api/endpoint.d.ts +29 -0
  6. package/dist/src/api/endpoint.js +148 -0
  7. package/dist/src/api/index.d.ts +2 -0
  8. package/dist/src/api/index.js +18 -0
  9. package/dist/src/api/response.d.ts +52 -0
  10. package/dist/src/api/response.js +107 -0
  11. package/dist/src/cli.d.ts +1 -0
  12. package/dist/src/cli.js +117 -0
  13. package/dist/src/client.d.ts +48 -0
  14. package/dist/src/client.js +134 -0
  15. package/dist/src/constants.d.ts +13 -0
  16. package/dist/src/constants.js +54 -0
  17. package/dist/src/documents/custom.d.ts +14 -0
  18. package/dist/src/documents/custom.js +48 -0
  19. package/dist/src/documents/document.d.ts +38 -0
  20. package/dist/src/documents/document.js +60 -0
  21. package/dist/src/documents/documentConfig.d.ts +44 -0
  22. package/dist/src/documents/documentConfig.js +121 -0
  23. package/dist/src/documents/financialDocument.d.ts +32 -0
  24. package/dist/src/documents/financialDocument.js +130 -0
  25. package/dist/src/documents/index.d.ts +12 -0
  26. package/dist/src/documents/index.js +20 -0
  27. package/dist/src/documents/invoice.d.ts +37 -0
  28. package/dist/src/documents/invoice.js +303 -0
  29. package/dist/src/documents/passport.d.ts +36 -0
  30. package/dist/src/documents/passport.js +218 -0
  31. package/dist/src/documents/receipt.d.ts +24 -0
  32. package/dist/src/documents/receipt.js +157 -0
  33. package/dist/src/errors/handler.d.ts +7 -0
  34. package/dist/src/errors/handler.js +18 -0
  35. package/dist/src/fields/amount.d.ts +13 -0
  36. package/dist/src/fields/amount.js +34 -0
  37. package/dist/src/fields/companyRegistration.d.ts +6 -0
  38. package/dist/src/fields/companyRegistration.js +11 -0
  39. package/dist/src/fields/customDocs.d.ts +42 -0
  40. package/dist/src/fields/customDocs.js +61 -0
  41. package/dist/src/fields/date.d.ts +12 -0
  42. package/dist/src/fields/date.js +25 -0
  43. package/dist/src/fields/field.d.ts +74 -0
  44. package/dist/src/fields/field.js +106 -0
  45. package/dist/src/fields/fullText.d.ts +24 -0
  46. package/dist/src/fields/fullText.js +89 -0
  47. package/dist/src/fields/index.d.ts +11 -0
  48. package/dist/src/fields/index.js +27 -0
  49. package/dist/src/fields/locale.d.ts +13 -0
  50. package/dist/src/fields/locale.js +40 -0
  51. package/dist/src/fields/orientation.d.ts +10 -0
  52. package/dist/src/fields/orientation.js +22 -0
  53. package/dist/src/fields/paymentDetails.d.ts +31 -0
  54. package/dist/src/fields/paymentDetails.js +58 -0
  55. package/dist/src/fields/tax.d.ts +30 -0
  56. package/dist/src/fields/tax.js +51 -0
  57. package/dist/src/geometry.d.ts +59 -0
  58. package/dist/src/geometry.js +113 -0
  59. package/dist/src/index.d.ts +2 -0
  60. package/dist/src/index.js +12 -0
  61. package/dist/src/inputs.d.ts +69 -0
  62. package/dist/src/inputs.js +223 -0
  63. package/dist/src/logger.d.ts +20 -0
  64. package/dist/src/logger.js +35 -0
  65. package/package.json +38 -28
  66. package/mindee/api/financialDocument.js +0 -47
  67. package/mindee/api/index.js +0 -5
  68. package/mindee/api/invoice.js +0 -53
  69. package/mindee/api/object.js +0 -82
  70. package/mindee/api/receipt.js +0 -39
  71. package/mindee/api/request.js +0 -72
  72. package/mindee/api/response.js +0 -80
  73. package/mindee/documents/document.js +0 -69
  74. package/mindee/documents/fields/amount.js +0 -25
  75. package/mindee/documents/fields/date.js +0 -29
  76. package/mindee/documents/fields/field.js +0 -86
  77. package/mindee/documents/fields/index.js +0 -7
  78. package/mindee/documents/fields/locale.js +0 -30
  79. package/mindee/documents/fields/orientation.js +0 -24
  80. package/mindee/documents/fields/paymentDetails.js +0 -52
  81. package/mindee/documents/fields/tax.js +0 -47
  82. package/mindee/documents/financialDocument.js +0 -269
  83. package/mindee/documents/index.js +0 -5
  84. package/mindee/documents/invoice.js +0 -456
  85. package/mindee/documents/receipt.js +0 -287
  86. package/mindee/errors/handler.js +0 -16
  87. package/mindee/index.js +0 -39
  88. package/mindee/inputs.js +0 -181
  89. package/mindee/logger.js +0 -34
@@ -0,0 +1,157 @@
1
+ "use strict";
2
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
5
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
+ };
7
+ var _Receipt_instances, _Receipt_initFromApiPrediction, _Receipt_checklist, _Receipt_taxesMatchTotal, _Receipt_reconstruct, _Receipt_reconstructTotalExclFromTCCAndTaxes, _Receipt_reconstructTotalTax;
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.Receipt = void 0;
10
+ const document_1 = require("./document");
11
+ const fields_1 = require("../fields");
12
+ class Receipt extends document_1.Document {
13
+ /**
14
+ * @param {Object} apiPrediction - Json parsed prediction from HTTP response
15
+ * @param {Input} inputFile - input file given to parse the document
16
+ * @param {number} pageId - Page ID for multi-page document
17
+ * @param {FullText} fullText - full OCR extracted text
18
+ */
19
+ constructor({ apiPrediction, inputFile = undefined, fullText = undefined, pageId = undefined, }) {
20
+ super(inputFile, pageId, fullText);
21
+ _Receipt_instances.add(this);
22
+ this.taxes = [];
23
+ this.locale = new fields_1.Locale({
24
+ prediction: apiPrediction.locale,
25
+ pageId: pageId,
26
+ });
27
+ this.totalTax = new fields_1.Amount({
28
+ prediction: { value: undefined, confidence: 0 },
29
+ valueKey: "value",
30
+ pageId: pageId,
31
+ });
32
+ this.totalExcl = new fields_1.Amount({
33
+ prediction: { value: undefined, confidence: 0 },
34
+ valueKey: "value",
35
+ pageId: pageId,
36
+ });
37
+ __classPrivateFieldGet(this, _Receipt_instances, "m", _Receipt_initFromApiPrediction).call(this, apiPrediction, pageId);
38
+ __classPrivateFieldGet(this, _Receipt_instances, "m", _Receipt_checklist).call(this);
39
+ __classPrivateFieldGet(this, _Receipt_instances, "m", _Receipt_reconstruct).call(this);
40
+ }
41
+ toString() {
42
+ const taxes = this.taxes.map((item) => item.toString()).join("\n ");
43
+ const outStr = `-----Receipt data-----
44
+ Filename: ${this.filename}
45
+ Total amount including taxes: ${this.totalIncl}
46
+ Total amount excluding taxes: ${this.totalExcl}
47
+ Date: ${this.date}
48
+ Category: ${this.category}
49
+ Time: ${this.time}
50
+ Merchant name: ${this.merchantName}
51
+ Taxes: ${taxes}
52
+ Total taxes: ${this.totalTax}
53
+ Locale: ${this.locale}
54
+ ----------------------
55
+ `;
56
+ return Receipt.cleanOutString(outStr);
57
+ }
58
+ }
59
+ exports.Receipt = Receipt;
60
+ _Receipt_instances = new WeakSet(), _Receipt_initFromApiPrediction = function _Receipt_initFromApiPrediction(apiPrediction, pageId) {
61
+ this.totalIncl = new fields_1.Amount({
62
+ prediction: apiPrediction.total_incl,
63
+ valueKey: "value",
64
+ pageId: pageId,
65
+ });
66
+ this.date = new fields_1.DateField({
67
+ prediction: apiPrediction.date,
68
+ pageId: pageId,
69
+ });
70
+ this.category = new fields_1.Field({
71
+ prediction: apiPrediction.category,
72
+ pageId: pageId,
73
+ });
74
+ this.merchantName = new fields_1.Field({
75
+ prediction: apiPrediction.supplier,
76
+ pageId: pageId,
77
+ });
78
+ this.time = new fields_1.Field({
79
+ prediction: apiPrediction.time,
80
+ pageId: pageId,
81
+ });
82
+ apiPrediction.taxes.map((taxPrediction) => this.taxes.push(new fields_1.TaxField({
83
+ prediction: taxPrediction,
84
+ pageId: pageId,
85
+ valueKey: "value",
86
+ rateKey: "rate",
87
+ codeKey: "code",
88
+ })));
89
+ if (pageId !== undefined) {
90
+ this.orientation = new fields_1.Orientation({
91
+ prediction: apiPrediction.orientation,
92
+ pageId,
93
+ });
94
+ }
95
+ }, _Receipt_checklist = function _Receipt_checklist() {
96
+ this.checklist = { taxesMatchTotalIncl: __classPrivateFieldGet(this, _Receipt_instances, "m", _Receipt_taxesMatchTotal).call(this) };
97
+ }, _Receipt_taxesMatchTotal = function _Receipt_taxesMatchTotal() {
98
+ // Check taxes and total amount exist
99
+ if (this.taxes.length === 0 || this.totalIncl.value === undefined) {
100
+ return false;
101
+ }
102
+ // Reconstruct total_incl from taxes
103
+ let totalVat = 0;
104
+ let reconstructedTotal = 0;
105
+ this.taxes.forEach((tax) => {
106
+ if (tax.value === undefined || !tax.rate)
107
+ return false;
108
+ totalVat += tax.value;
109
+ reconstructedTotal += tax.value + (100 * tax.value) / tax.rate;
110
+ });
111
+ // Sanity check
112
+ if (totalVat <= 0)
113
+ return false;
114
+ // Crate epsilon
115
+ const eps = 1 / (100 * totalVat);
116
+ if (this.totalIncl.value * (1 - eps) - 0.02 <= reconstructedTotal &&
117
+ reconstructedTotal <= this.totalIncl.value * (1 + eps) + 0.02) {
118
+ this.taxes.forEach((tax) => {
119
+ tax.confidence = 1.0;
120
+ });
121
+ this.totalTax.confidence = 1.0;
122
+ this.totalIncl.confidence = 1.0;
123
+ return true;
124
+ }
125
+ return false;
126
+ }, _Receipt_reconstruct = function _Receipt_reconstruct() {
127
+ __classPrivateFieldGet(this, _Receipt_instances, "m", _Receipt_reconstructTotalExclFromTCCAndTaxes).call(this);
128
+ __classPrivateFieldGet(this, _Receipt_instances, "m", _Receipt_reconstructTotalTax).call(this);
129
+ }, _Receipt_reconstructTotalExclFromTCCAndTaxes = function _Receipt_reconstructTotalExclFromTCCAndTaxes() {
130
+ if (this.taxes.length && this.totalIncl.value !== undefined) {
131
+ const totalExcl = {
132
+ value: this.totalIncl.value - fields_1.Field.arraySum(this.taxes),
133
+ confidence: fields_1.Field.arrayConfidence(this.taxes) *
134
+ this.totalIncl.confidence,
135
+ };
136
+ this.totalExcl = new fields_1.Amount({
137
+ prediction: totalExcl,
138
+ valueKey: "value",
139
+ reconstructed: true,
140
+ });
141
+ }
142
+ }, _Receipt_reconstructTotalTax = function _Receipt_reconstructTotalTax() {
143
+ if (this.taxes.length && this.totalTax.value === undefined) {
144
+ const totalTax = {
145
+ value: this.taxes
146
+ .map((tax) => tax.value || 0)
147
+ .reduce((a, b) => a + b, 0),
148
+ confidence: fields_1.Field.arrayConfidence(this.taxes),
149
+ };
150
+ if (totalTax.value > 0)
151
+ this.totalTax = new fields_1.Amount({
152
+ prediction: totalTax,
153
+ valueKey: "value",
154
+ reconstructed: true,
155
+ });
156
+ }
157
+ };
@@ -0,0 +1,7 @@
1
+ declare class ErrorHandler {
2
+ throwOnError: boolean;
3
+ constructor(throwOnError?: boolean);
4
+ throw(error: Error): void;
5
+ }
6
+ export declare const errorHandler: ErrorHandler;
7
+ export {};
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.errorHandler = void 0;
4
+ const logger_1 = require("../logger");
5
+ class ErrorHandler {
6
+ constructor(throwOnError = true) {
7
+ this.throwOnError = throwOnError;
8
+ }
9
+ throw(error) {
10
+ if (this.throwOnError) {
11
+ throw error;
12
+ }
13
+ else {
14
+ logger_1.logger.error(error.message);
15
+ }
16
+ }
17
+ }
18
+ exports.errorHandler = new ErrorHandler();
@@ -0,0 +1,13 @@
1
+ import { Field, FieldConstructor } from "./field";
2
+ export declare function floatToString(value: number): string;
3
+ export declare class Amount extends Field {
4
+ value: number | undefined;
5
+ /**
6
+ * @param {Object} prediction - Prediction object from HTTP response
7
+ * @param {String} valueKey - Key to use in the prediction dict
8
+ * @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
9
+ * @param {Integer} pageId - Page ID for multi-page document
10
+ */
11
+ constructor({ prediction, valueKey, reconstructed, pageId, }: FieldConstructor);
12
+ toString(): string;
13
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Amount = exports.floatToString = void 0;
4
+ const field_1 = require("./field");
5
+ function floatToString(value) {
6
+ if (Number.isInteger(value)) {
7
+ return `${value}.0`;
8
+ }
9
+ return value.toString();
10
+ }
11
+ exports.floatToString = floatToString;
12
+ class Amount extends field_1.Field {
13
+ /**
14
+ * @param {Object} prediction - Prediction object from HTTP response
15
+ * @param {String} valueKey - Key to use in the prediction dict
16
+ * @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
17
+ * @param {Integer} pageId - Page ID for multi-page document
18
+ */
19
+ constructor({ prediction, valueKey = "amount", reconstructed = false, pageId = undefined, }) {
20
+ super({ prediction, valueKey, reconstructed, pageId });
21
+ this.value = +parseFloat(prediction[valueKey]).toFixed(3);
22
+ if (isNaN(this.value)) {
23
+ this.value = undefined;
24
+ this.confidence = 0.0;
25
+ }
26
+ }
27
+ toString() {
28
+ if (this.value !== undefined) {
29
+ return floatToString(this.value);
30
+ }
31
+ return "";
32
+ }
33
+ }
34
+ exports.Amount = Amount;
@@ -0,0 +1,6 @@
1
+ import { Field, FieldConstructor } from "./field";
2
+ export declare class CompanyRegistration extends Field {
3
+ /** Type of company registration number */
4
+ type: string;
5
+ constructor({ prediction, valueKey, reconstructed, pageId, }: FieldConstructor);
6
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CompanyRegistration = void 0;
4
+ const field_1 = require("./field");
5
+ class CompanyRegistration extends field_1.Field {
6
+ constructor({ prediction, valueKey = "value", reconstructed = false, pageId, }) {
7
+ super({ prediction, valueKey, reconstructed, pageId });
8
+ this.type = prediction.type;
9
+ }
10
+ }
11
+ exports.CompanyRegistration = CompanyRegistration;
@@ -0,0 +1,42 @@
1
+ import { FieldConstructor, stringDict } from "./field";
2
+ import { Polygon } from "../geometry";
3
+ export declare class ClassificationField {
4
+ value: string;
5
+ confidence: number;
6
+ constructor({ prediction }: {
7
+ prediction: stringDict;
8
+ });
9
+ toString(): string;
10
+ }
11
+ export declare class ListFieldItem {
12
+ content: any;
13
+ /**
14
+ * The confidence score of the prediction.
15
+ * Note: Score is calculated on **word selection**, not its textual content (OCR).
16
+ */
17
+ confidence: number;
18
+ /**
19
+ * Contains exactly 4 relative vertices coordinates (points) of a right
20
+ * rectangle containing the word in the document.
21
+ */
22
+ bbox: Polygon;
23
+ /**
24
+ * Contains the relative vertices coordinates (points) of a polygon containing
25
+ * the word in the document.
26
+ */
27
+ polygon: Polygon;
28
+ constructor(prediction: stringDict);
29
+ toString(): string;
30
+ }
31
+ export declare class ListField {
32
+ values: ListFieldItem[];
33
+ confidence: number;
34
+ /** True if the field was reconstructed or computed using other fields. */
35
+ reconstructed: boolean;
36
+ /** The document page on which the information was found. */
37
+ pageId?: number;
38
+ constructor({ prediction, reconstructed, pageId }: FieldConstructor);
39
+ contentsList(): Array<any>;
40
+ contentsString(separator?: string): string;
41
+ toString(): string;
42
+ }
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ListField = exports.ListFieldItem = exports.ClassificationField = void 0;
4
+ const geometry_1 = require("../geometry");
5
+ class ClassificationField {
6
+ constructor({ prediction }) {
7
+ this.value = prediction["value"];
8
+ this.confidence = prediction["confidence"];
9
+ }
10
+ toString() {
11
+ return `${this.value}`;
12
+ }
13
+ }
14
+ exports.ClassificationField = ClassificationField;
15
+ class ListFieldItem {
16
+ constructor(prediction) {
17
+ /**
18
+ * Contains exactly 4 relative vertices coordinates (points) of a right
19
+ * rectangle containing the word in the document.
20
+ */
21
+ this.bbox = [];
22
+ /**
23
+ * Contains the relative vertices coordinates (points) of a polygon containing
24
+ * the word in the document.
25
+ */
26
+ this.polygon = [];
27
+ this.content = prediction["content"];
28
+ this.confidence = prediction["confidence"];
29
+ if (prediction["polygon"]) {
30
+ this.polygon = prediction["polygon"];
31
+ this.bbox = (0, geometry_1.getBboxAsPolygon)(prediction.polygon);
32
+ }
33
+ }
34
+ toString() {
35
+ return `${this.content}`;
36
+ }
37
+ }
38
+ exports.ListFieldItem = ListFieldItem;
39
+ class ListField {
40
+ constructor({ prediction, reconstructed = false, pageId }) {
41
+ this.values = [];
42
+ this.confidence = prediction["confidence"];
43
+ this.reconstructed = reconstructed;
44
+ this.pageId = pageId !== undefined ? pageId : prediction["page_id"];
45
+ if (Object.prototype.hasOwnProperty.call(prediction, "values")) {
46
+ prediction["values"].forEach((field) => {
47
+ this.values.push(new ListFieldItem(field));
48
+ });
49
+ }
50
+ }
51
+ contentsList() {
52
+ return this.values.map((item) => item.content);
53
+ }
54
+ contentsString(separator = " ") {
55
+ return this.values.map((item) => `${item.content}`).join(separator);
56
+ }
57
+ toString() {
58
+ return this.contentsString();
59
+ }
60
+ }
61
+ exports.ListField = ListField;
@@ -0,0 +1,12 @@
1
+ import { Field, FieldConstructor } from "./field";
2
+ export declare class DateField extends Field {
3
+ /**
4
+ * @param {Object} prediction - Prediction object from HTTP response
5
+ * @param {String} valueKey - Key to use in the prediction dict
6
+ * @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
7
+ * @param {Integer} pageId - Page ID for multi-page document
8
+ */
9
+ dateObject: Date | undefined;
10
+ constructor({ prediction, valueKey, reconstructed, pageId, }: FieldConstructor);
11
+ static compareDates(date1: Date, date2: Date): boolean;
12
+ }
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DateField = void 0;
4
+ const field_1 = require("./field");
5
+ class DateField extends field_1.Field {
6
+ constructor({ prediction, valueKey = "value", reconstructed = false, pageId, }) {
7
+ super({ prediction, valueKey, reconstructed, pageId });
8
+ this.dateObject = new Date(this.value);
9
+ if (isNaN(this.dateObject.valueOf())) {
10
+ this.dateObject = undefined;
11
+ this.confidence = 0.0;
12
+ this.value = undefined;
13
+ }
14
+ else {
15
+ this.dateObject.setUTCHours(0, 0, 0, 0);
16
+ }
17
+ }
18
+ static compareDates(date1, date2) {
19
+ const check = date1.getFullYear() === date2.getFullYear() &&
20
+ date1.getMonth() === date2.getMonth() &&
21
+ date1.getDate() === date2.getDate();
22
+ return check;
23
+ }
24
+ }
25
+ exports.DateField = DateField;
@@ -0,0 +1,74 @@
1
+ import { Polygon } from "../geometry";
2
+ export declare type stringDict = {
3
+ [index: string]: any;
4
+ };
5
+ export interface FieldConstructor {
6
+ prediction: {
7
+ [index: string]: any;
8
+ };
9
+ valueKey?: string;
10
+ reconstructed?: boolean;
11
+ pageId?: number | undefined;
12
+ }
13
+ interface BaseFieldConstructor {
14
+ prediction: stringDict;
15
+ valueKey: string;
16
+ }
17
+ export declare class BaseField {
18
+ value?: any;
19
+ /**
20
+ * @param {Object} prediction - Prediction object from HTTP response
21
+ * @param {String} valueKey - Key to use in the prediction dict
22
+ */
23
+ constructor({ prediction, valueKey }: BaseFieldConstructor);
24
+ }
25
+ export declare class Field extends BaseField {
26
+ /**
27
+ * Contains exactly 4 relative vertices coordinates (points) of a right
28
+ * rectangle containing the field in the document.
29
+ */
30
+ bbox: Polygon;
31
+ /**
32
+ * Contains the relative vertices coordinates (points) of a polygon containing
33
+ * the field in the document.
34
+ */
35
+ polygon: Polygon;
36
+ /** The document page on which the information was found. */
37
+ pageId: number | undefined;
38
+ /**
39
+ * The confidence score of the prediction.
40
+ */
41
+ confidence: number;
42
+ /**
43
+ * True if the field was reconstructed or computed using other fields.
44
+ */
45
+ reconstructed: boolean;
46
+ /**
47
+ * @param {Object} prediction - Prediction object from HTTP response
48
+ * @param {String} valueKey - Key to use in the prediction dict
49
+ * @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
50
+ * @param {Integer} pageId - Page ID for multi-page document
51
+ * @param {Array<String>} extraFields - Extra fields to get from the prediction and to set as attribute of the Field
52
+ */
53
+ constructor({ prediction, valueKey, reconstructed, pageId, }: FieldConstructor);
54
+ compare(other: Field): boolean;
55
+ /**
56
+ @param {Array<Field>} array1 - first Array of Fields
57
+ @param {Array<Field>} array2 - second Array of Fields
58
+ @param {String} attr - Attribute to compare
59
+ @returns {Boolean} - true if all elements in array1 exist in array2 and vice-versa, false otherwise
60
+ */
61
+ static compareArrays(array1: Field[], array2: Field[], attr?: string): boolean;
62
+ /**
63
+ * @param {Array<Field>} array - Array of Fields
64
+ * @returns {Number} product of all the fields probaility
65
+ */
66
+ static arrayConfidence(array: any): number;
67
+ /**
68
+ * @param {Array<Field>} array - Array of Fields
69
+ * @returns {Number} Sum of all the Fields values in the array
70
+ */
71
+ static arraySum(array: any): number;
72
+ toString(): string;
73
+ }
74
+ export {};
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Field = exports.BaseField = void 0;
4
+ const geometry_1 = require("../geometry");
5
+ class BaseField {
6
+ /**
7
+ * @param {Object} prediction - Prediction object from HTTP response
8
+ * @param {String} valueKey - Key to use in the prediction dict
9
+ */
10
+ constructor({ prediction, valueKey }) {
11
+ this.value = undefined;
12
+ if (valueKey in prediction && prediction[valueKey] !== null) {
13
+ this.value = prediction[valueKey];
14
+ }
15
+ }
16
+ }
17
+ exports.BaseField = BaseField;
18
+ class Field extends BaseField {
19
+ /**
20
+ * @param {Object} prediction - Prediction object from HTTP response
21
+ * @param {String} valueKey - Key to use in the prediction dict
22
+ * @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
23
+ * @param {Integer} pageId - Page ID for multi-page document
24
+ * @param {Array<String>} extraFields - Extra fields to get from the prediction and to set as attribute of the Field
25
+ */
26
+ constructor({ prediction, valueKey = "value", reconstructed = false, pageId, }) {
27
+ super({ prediction, valueKey });
28
+ /**
29
+ * Contains exactly 4 relative vertices coordinates (points) of a right
30
+ * rectangle containing the field in the document.
31
+ */
32
+ this.bbox = [];
33
+ /**
34
+ * Contains the relative vertices coordinates (points) of a polygon containing
35
+ * the field in the document.
36
+ */
37
+ this.polygon = [];
38
+ this.pageId = pageId !== undefined ? pageId : prediction["page_id"];
39
+ this.reconstructed = reconstructed;
40
+ this.confidence = prediction.confidence ? prediction.confidence : 0.0;
41
+ if (prediction.polygon) {
42
+ this.polygon = prediction.polygon;
43
+ this.bbox = (0, geometry_1.getBboxAsPolygon)(prediction.polygon);
44
+ }
45
+ }
46
+ compare(other) {
47
+ if (this.value === null && other.value === null)
48
+ return true;
49
+ if (this.value === null || other.value === null)
50
+ return false;
51
+ if (typeof this.value === "string") {
52
+ return this.value.toLowerCase() === other.value.toLowerCase();
53
+ }
54
+ return this.value === other.value;
55
+ }
56
+ /**
57
+ @param {Array<Field>} array1 - first Array of Fields
58
+ @param {Array<Field>} array2 - second Array of Fields
59
+ @param {String} attr - Attribute to compare
60
+ @returns {Boolean} - true if all elements in array1 exist in array2 and vice-versa, false otherwise
61
+ */
62
+ static compareArrays(array1, array2, attr = "value") {
63
+ const list1 = array1.map((item) => item[attr]);
64
+ const list2 = array2.map((item) => item[attr]);
65
+ if (list1.length !== list2.length)
66
+ return false;
67
+ for (const item1 of list1) {
68
+ if (!list2.includes(item1))
69
+ return false;
70
+ }
71
+ return true;
72
+ }
73
+ /**
74
+ * @param {Array<Field>} array - Array of Fields
75
+ * @returns {Number} product of all the fields probaility
76
+ */
77
+ static arrayConfidence(array) {
78
+ let total = 1.0;
79
+ for (const field of array) {
80
+ total *= field.confidence;
81
+ if (isNaN(total))
82
+ return 0.0;
83
+ }
84
+ return total;
85
+ }
86
+ /**
87
+ * @param {Array<Field>} array - Array of Fields
88
+ * @returns {Number} Sum of all the Fields values in the array
89
+ */
90
+ static arraySum(array) {
91
+ let total = 0.0;
92
+ for (const field of array) {
93
+ total += field.value;
94
+ if (isNaN(total))
95
+ return 0.0;
96
+ }
97
+ return total;
98
+ }
99
+ toString() {
100
+ if (this.value !== undefined) {
101
+ return `${this.value}`;
102
+ }
103
+ return "";
104
+ }
105
+ }
106
+ exports.Field = Field;
@@ -0,0 +1,24 @@
1
+ import * as geometry from "../geometry";
2
+ export declare type Word = {
3
+ /**
4
+ * Contains the relative vertices coordinates (points) of a polygon containing
5
+ * the field in the document.
6
+ */
7
+ polygon: geometry.Polygon;
8
+ text: string;
9
+ confidence: number;
10
+ };
11
+ declare type Line = Word[];
12
+ export declare class FullText {
13
+ words: Word[];
14
+ /**
15
+ * Order all text on a page into lines.
16
+ * WARNING: This feature is experimental.
17
+ */
18
+ toLines(): Array<Line>;
19
+ /**
20
+ * WARNING: This feature is experimental.
21
+ */
22
+ toString(): string;
23
+ }
24
+ export {};