mindee 1.3.2 → 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 (161) hide show
  1. package/CHANGELOG.md +47 -18
  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/{documents → src/documents}/document.js +15 -35
  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/{errors → src/errors}/handler.js +5 -4
  35. package/dist/src/fields/amount.d.ts +13 -0
  36. package/dist/{documents → src}/fields/amount.js +19 -6
  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/{documents → src}/fields/field.js +48 -27
  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/{documents → src}/fields/locale.js +20 -5
  51. package/dist/src/fields/orientation.d.ts +10 -0
  52. package/dist/{documents → src}/fields/orientation.js +5 -5
  53. package/dist/src/fields/paymentDetails.d.ts +31 -0
  54. package/dist/{documents → src}/fields/paymentDetails.js +14 -6
  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 +45 -26
  66. package/.eslintrc +0 -24
  67. package/.mocharc.yaml +0 -2
  68. package/.nyc_output/f57ba3bc-1777-4ecc-81e1-230327c2a401.json +0 -1
  69. package/.nyc_output/processinfo/f57ba3bc-1777-4ecc-81e1-230327c2a401.json +0 -1
  70. package/.nyc_output/processinfo/index.json +0 -1
  71. package/.prettierrc +0 -4
  72. package/.tool-versions +0 -1
  73. package/CODE_OF_CONDUCT.md +0 -63
  74. package/CONTRIBUTING.md +0 -84
  75. package/ISSUE_TEMPLATE.md +0 -45
  76. package/PULL_REQUEST_TEMPLATE.md +0 -45
  77. package/babel.config.json +0 -10
  78. package/coverage/coverage.json +0 -1
  79. package/coverage/lcov-report/base.css +0 -224
  80. package/coverage/lcov-report/block-navigation.js +0 -79
  81. package/coverage/lcov-report/favicon.png +0 -0
  82. package/coverage/lcov-report/index.html +0 -171
  83. package/coverage/lcov-report/prettify.css +0 -1
  84. package/coverage/lcov-report/prettify.js +0 -2
  85. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  86. package/coverage/lcov-report/sorter.js +0 -170
  87. package/coverage/lcov.info +0 -1595
  88. package/coverage.lcov +0 -1595
  89. package/dist/api/financialDocument.js +0 -51
  90. package/dist/api/index.js +0 -13
  91. package/dist/api/invoice.js +0 -59
  92. package/dist/api/object.js +0 -80
  93. package/dist/api/receipt.js +0 -45
  94. package/dist/api/request.js +0 -90
  95. package/dist/api/response.js +0 -91
  96. package/dist/documents/fields/date.js +0 -17
  97. package/dist/documents/fields/index.js +0 -17
  98. package/dist/documents/fields/tax.js +0 -42
  99. package/dist/documents/financialDocument.js +0 -216
  100. package/dist/documents/index.js +0 -13
  101. package/dist/documents/invoice.js +0 -361
  102. package/dist/documents/receipt.js +0 -231
  103. package/dist/index.js +0 -23
  104. package/dist/inputs.js +0 -208
  105. package/dist/logger.js +0 -34
  106. package/examples/.env.example +0 -2
  107. package/examples/documents/invoices/credit_note.pdf +0 -900
  108. package/examples/documents/invoices/invoice.pdf +0 -0
  109. package/examples/documents/invoices/invoice_6p.pdf +0 -0
  110. package/examples/documents/receipts/receipt.jpg +0 -0
  111. package/examples/financialDocument.js +0 -32
  112. package/examples/invoice.js +0 -74
  113. package/examples/receipt.js +0 -45
  114. package/lib/api/financialDocument.js +0 -59
  115. package/lib/api/index.js +0 -7
  116. package/lib/api/invoice.js +0 -73
  117. package/lib/api/object.js +0 -90
  118. package/lib/api/receipt.js +0 -58
  119. package/lib/api/request.js +0 -84
  120. package/lib/api/response.js +0 -92
  121. package/lib/documents/document.js +0 -97
  122. package/lib/documents/fields/amount.js +0 -35
  123. package/lib/documents/fields/date.js +0 -36
  124. package/lib/documents/fields/field.js +0 -102
  125. package/lib/documents/fields/index.js +0 -9
  126. package/lib/documents/fields/locale.js +0 -35
  127. package/lib/documents/fields/orientation.js +0 -33
  128. package/lib/documents/fields/paymentDetails.js +0 -71
  129. package/lib/documents/fields/tax.js +0 -57
  130. package/lib/documents/financialDocument.js +0 -280
  131. package/lib/documents/index.js +0 -7
  132. package/lib/documents/invoice.js +0 -451
  133. package/lib/documents/receipt.js +0 -332
  134. package/lib/errors/handler.js +0 -19
  135. package/lib/index.js +0 -40
  136. package/lib/inputs.js +0 -190
  137. package/lib/logger.js +0 -52
  138. package/mindee/api/financialDocument.js +0 -47
  139. package/mindee/api/index.js +0 -5
  140. package/mindee/api/invoice.js +0 -53
  141. package/mindee/api/object.js +0 -82
  142. package/mindee/api/receipt.js +0 -39
  143. package/mindee/api/request.js +0 -72
  144. package/mindee/api/response.js +0 -80
  145. package/mindee/documents/document.js +0 -69
  146. package/mindee/documents/fields/amount.js +0 -25
  147. package/mindee/documents/fields/date.js +0 -29
  148. package/mindee/documents/fields/field.js +0 -86
  149. package/mindee/documents/fields/index.js +0 -7
  150. package/mindee/documents/fields/locale.js +0 -30
  151. package/mindee/documents/fields/orientation.js +0 -24
  152. package/mindee/documents/fields/paymentDetails.js +0 -52
  153. package/mindee/documents/fields/tax.js +0 -47
  154. package/mindee/documents/financialDocument.js +0 -269
  155. package/mindee/documents/index.js +0 -5
  156. package/mindee/documents/invoice.js +0 -456
  157. package/mindee/documents/receipt.js +0 -287
  158. package/mindee/errors/handler.js +0 -16
  159. package/mindee/index.js +0 -36
  160. package/mindee/inputs.js +0 -158
  161. package/mindee/logger.js +0 -33
@@ -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 {};
@@ -1,17 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.errorHandler = void 0;
4
- const logger_1 = require("@mindee/logger");
4
+ const logger_1 = require("../logger");
5
5
  class ErrorHandler {
6
6
  constructor(throwOnError = true) {
7
7
  this.throwOnError = throwOnError;
8
- this.throwOnError = throwOnError;
9
8
  }
10
9
  throw(error) {
11
- if (this.throwOnError || force)
10
+ if (this.throwOnError) {
12
11
  throw error;
13
- else
12
+ }
13
+ else {
14
14
  logger_1.logger.error(error.message);
15
+ }
15
16
  }
16
17
  }
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
+ }
@@ -1,21 +1,34 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Amount = void 0;
4
- const field_1 = require("@fields/field");
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;
5
12
  class Amount extends field_1.Field {
6
13
  /**
7
14
  * @param {Object} prediction - Prediction object from HTTP response
8
15
  * @param {String} valueKey - Key to use in the prediction dict
9
16
  * @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
10
- * @param {Integer} pageNumber - Page number for multi pages pdf
17
+ * @param {Integer} pageId - Page ID for multi-page document
11
18
  */
12
- constructor({ prediction, valueKey = "amount", reconstructed = false, pageNumber = 0, }) {
13
- super({ prediction, valueKey, reconstructed, pageNumber });
19
+ constructor({ prediction, valueKey = "amount", reconstructed = false, pageId = undefined, }) {
20
+ super({ prediction, valueKey, reconstructed, pageId });
14
21
  this.value = +parseFloat(prediction[valueKey]).toFixed(3);
15
22
  if (isNaN(this.value)) {
16
23
  this.value = undefined;
17
- this.probability = 0;
24
+ this.confidence = 0.0;
25
+ }
26
+ }
27
+ toString() {
28
+ if (this.value !== undefined) {
29
+ return floatToString(this.value);
18
30
  }
31
+ return "";
19
32
  }
20
33
  }
21
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 {};
@@ -1,42 +1,57 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Field = void 0;
4
- class Field {
3
+ exports.Field = exports.BaseField = void 0;
4
+ const geometry_1 = require("../geometry");
5
+ class BaseField {
5
6
  /**
6
7
  * @param {Object} prediction - Prediction object from HTTP response
7
8
  * @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} pageNumber - Page number for multi pages pdf
10
- * @param {Array<String>} extraFields - Extra fields to get from the prediction and to set as attribute of the Field
11
9
  */
12
- constructor({ prediction, valueKey = "value", reconstructed = false, extraFields, pageNumber, }) {
13
- this.pageNumber = pageNumber;
14
- this.reconstructed = reconstructed;
10
+ constructor({ prediction, valueKey }) {
15
11
  this.value = undefined;
16
- this.probability = prediction.confidence ? prediction.confidence : 0.0;
17
- this.bbox = prediction.polygon ? prediction.polygon : [];
18
12
  if (valueKey in prediction && prediction[valueKey] !== null) {
19
13
  this.value = prediction[valueKey];
20
- if (extraFields) {
21
- for (const fieldName of extraFields) {
22
- this[fieldName] = prediction[fieldName];
23
- }
24
- }
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);
25
44
  }
26
45
  }
27
46
  compare(other) {
28
- if (this.value == null && other.value == null)
47
+ if (this.value === null && other.value === null)
29
48
  return true;
30
- else if (this.value == null || other.value == null)
49
+ if (this.value === null || other.value === null)
31
50
  return false;
32
- else {
33
- if (typeof this.value == "string") {
34
- return this.value.toLowerCase() === other.value.toLowerCase();
35
- }
36
- else {
37
- return this.value === other.value;
38
- }
51
+ if (typeof this.value === "string") {
52
+ return this.value.toLowerCase() === other.value.toLowerCase();
39
53
  }
54
+ return this.value === other.value;
40
55
  }
41
56
  /**
42
57
  @param {Array<Field>} array1 - first Array of Fields
@@ -59,10 +74,10 @@ class Field {
59
74
  * @param {Array<Field>} array - Array of Fields
60
75
  * @returns {Number} product of all the fields probaility
61
76
  */
62
- static arrayProbability(array) {
77
+ static arrayConfidence(array) {
63
78
  let total = 1.0;
64
79
  for (const field of array) {
65
- total *= field.probability;
80
+ total *= field.confidence;
66
81
  if (isNaN(total))
67
82
  return 0.0;
68
83
  }
@@ -73,7 +88,7 @@ class Field {
73
88
  * @returns {Number} Sum of all the Fields values in the array
74
89
  */
75
90
  static arraySum(array) {
76
- let total = 0;
91
+ let total = 0.0;
77
92
  for (const field of array) {
78
93
  total += field.value;
79
94
  if (isNaN(total))
@@ -81,5 +96,11 @@ class Field {
81
96
  }
82
97
  return total;
83
98
  }
99
+ toString() {
100
+ if (this.value !== undefined) {
101
+ return `${this.value}`;
102
+ }
103
+ return "";
104
+ }
84
105
  }
85
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 {};