mindee 1.0.8 → 1.1.2

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.
@@ -2,20 +2,23 @@ const https = require("https");
2
2
  const { version: sdkVersion } = require("../../package.json");
3
3
  const { URL } = require("url");
4
4
  const FormData = require("form-data");
5
+ const os = require("os");
5
6
 
6
7
  const request = (url, method, headers, input, includeWords = false) => {
7
8
  return new Promise(function (resolve, reject) {
8
9
  const form = new FormData();
9
10
  let body;
10
- headers["User-Agent"] = `mindee-node/${sdkVersion} node/${process.version}`;
11
11
 
12
+ headers["User-Agent"] = `mindee-api-nodejs@v${sdkVersion} nodejs-${
13
+ process.version
14
+ } ${os.type().toLowerCase()}`;
12
15
  if (["path", "stream"].includes(input.inputType)) {
13
16
  const fileParams = { filename: input.filename };
14
- form.append("file", input.fileObject, fileParams);
17
+ form.append("document", input.fileObject, fileParams);
15
18
  if (includeWords) form.append("include_mvision", "true");
16
19
  headers = { ...headers, ...form.getHeaders() };
17
20
  } else if (input.inputType === "base64") {
18
- let body_obj = { file: input.fileObject };
21
+ let body_obj = { document: input.fileObject };
19
22
  if (includeWords) body_obj["include_mvision"] = "true";
20
23
  body = JSON.stringify(body_obj);
21
24
  headers["Content-Type"] = "application/json";
@@ -44,7 +47,7 @@ const request = (url, method, headers, input, includeWords = false) => {
44
47
  data: JSON.parse(responseBody),
45
48
  });
46
49
  } catch (error) {
47
- console.log(responseBody);
50
+ console.log(responseBody, error);
48
51
  }
49
52
  });
50
53
  });
@@ -1,4 +1,3 @@
1
- const Document = require("../documents").document;
2
1
  const Receipt = require("../documents").receipt;
3
2
  const Invoice = require("../documents").invoice;
4
3
  const FinancialDocument = require("../documents").financialDocument;
@@ -38,14 +37,14 @@ class Response {
38
37
  invoice: (params) => new Invoice(params),
39
38
  financialDocument: (params) => new FinancialDocument(params),
40
39
  };
41
- const predictions = this.httpResponse.data.predictions.entries();
40
+ const predictions = this.httpResponse.data.document.inference.pages.entries();
42
41
  this[`${this.documentType}s`] = [];
43
42
 
44
43
  // Create a list of Document (Receipt, Invoice...) for each page of the input document
45
44
  for (const [pageNumber, prediction] of predictions) {
46
45
  this[`${this.documentType}s`].push(
47
46
  constructors[this.documentType]({
48
- apiPrediction: prediction,
47
+ apiPrediction: prediction.prediction,
49
48
  inputFile: this.input,
50
49
  pageNumber: pageNumber,
51
50
  })
@@ -53,9 +52,12 @@ class Response {
53
52
  }
54
53
 
55
54
  // Merge the list of Document into a unique Document
56
- this[this.documentType] = Document.mergePages(
57
- this[`${this.documentType}s`]
58
- );
55
+ this[this.documentType] = constructors[this.documentType]({
56
+ apiPrediction: this.httpResponse.data.document.inference.prediction,
57
+ inputFile: this.input,
58
+ pageNumber: this.httpResponse.data.document.n_pages,
59
+ level: "document",
60
+ });
59
61
  }
60
62
  }
61
63
 
@@ -50,7 +50,11 @@ class Document {
50
50
  const attributes = Object.getOwnPropertyNames(finalDocument);
51
51
  for (const document of documents) {
52
52
  for (const attribute of attributes) {
53
- if (
53
+ if (Array.isArray(document?.[attribute])) {
54
+ finalDocument[attribute] = finalDocument[attribute]?.length
55
+ ? finalDocument[attribute]
56
+ : document?.[attribute];
57
+ } else if (
54
58
  document?.[attribute]?.probability >
55
59
  finalDocument[attribute].probability
56
60
  ) {
@@ -16,13 +16,10 @@ class Field {
16
16
  this.pageNumber = pageNumber;
17
17
  this.reconstructed = reconstructed;
18
18
  this.value = undefined;
19
- this.probability = 0.0;
20
- this.bbox = [];
21
- if (valueKey in prediction && prediction[valueKey] !== "N/A") {
19
+ this.probability = prediction.confidence ? prediction.confidence : 0.0;
20
+ this.bbox = prediction.polygon ? prediction.polygon : [];
21
+ if (valueKey in prediction && prediction[valueKey] !== null) {
22
22
  this.value = prediction[valueKey];
23
- if (prediction.probability) this.probability = prediction.probability;
24
- if (prediction.segmentation)
25
- this.bbox = prediction.segmentation.bounding_box || [];
26
23
  if (extraFields) {
27
24
  for (const fieldName of extraFields) {
28
25
  this[fieldName] = prediction[fieldName];
@@ -19,14 +19,19 @@ class FinancialDocument extends Document {
19
19
  * @param {Object} Date - date value for creating FinancialDocument object from scratch
20
20
  * @param {Object} InvoiceNumber - Invoice number value for creating FinancialDocument object from scratch
21
21
  * @param {Object} taxes - taxes value for creating FinancialDocument object from scratch
22
- * @param {Object} merchantName - merchant name value for creating FinancialDocument object from scratch
22
+ * @param {Object} supplier - supplier value for creating FinancialDocument object from scratch
23
+ * @param {Object} supplierAddress - supplier address value for creating FinancialDocument object from scratch
23
24
  * @param {Object} paymentDetails - payment details value for creating FinancialDocument object from scratch
24
25
  * @param {Object} companyNumber - company number value for creating FinancialDocument object from scratch
25
26
  * @param {Object} vatNumber - vat number value for creating FinancialDocument object from scratch
26
27
  * @param {Object} orientation - orientation value for creating FinancialDocument object from scratch
27
28
  * @param {Object} totalTax - total tax value for creating FinancialDocument object from scratch
28
29
  * @param {Object} time - time value for creating FinancialDocument object from scratch
30
+ * @param {Object} customerName - customer name value for creating FinancialDocument object from scratch
31
+ * @param {Object} customerAddress - customer address value for creating FinancialDocument object from scratch
32
+ * @param {Object} customerCompanyRegistration - customer company registration value for creating FinancialDocument object from scratch
29
33
  * @param {Object} pageNumber - pageNumber for multi pages pdf input
34
+ * @param {String} level - specify whether object is built from "page" level or "document" level prediction
30
35
  */
31
36
  constructor({
32
37
  apiPrediction = undefined,
@@ -38,16 +43,22 @@ class FinancialDocument extends Document {
38
43
  invoiceNumber = undefined,
39
44
  dueDate = undefined,
40
45
  taxes = undefined,
41
- merchantName = undefined,
46
+ supplier = undefined,
47
+ supplierAddress = undefined,
42
48
  paymentDetails = undefined,
43
49
  companyNumber = undefined,
44
50
  vatNumber = undefined,
45
51
  orientation = undefined,
46
52
  totalTax = undefined,
47
53
  time = undefined,
54
+ customerName = undefined,
55
+ customerAddress = undefined,
56
+ customerCompanyRegistration = undefined,
48
57
  pageNumber = 0,
58
+ level = "page",
49
59
  }) {
50
60
  super(inputFile);
61
+ this.level = level;
51
62
  if (apiPrediction === undefined) {
52
63
  this.#initFromScratch({
53
64
  locale,
@@ -57,7 +68,8 @@ class FinancialDocument extends Document {
57
68
  invoiceNumber,
58
69
  dueDate,
59
70
  taxes,
60
- merchantName,
71
+ supplier,
72
+ supplierAddress,
61
73
  paymentDetails,
62
74
  companyNumber,
63
75
  vatNumber,
@@ -65,6 +77,9 @@ class FinancialDocument extends Document {
65
77
  pageNumber,
66
78
  totalTax,
67
79
  time,
80
+ customerName,
81
+ customerAddress,
82
+ customerCompanyRegistration,
68
83
  });
69
84
  } else {
70
85
  this.#initFromApiPrediction(apiPrediction, inputFile, pageNumber);
@@ -86,8 +101,12 @@ class FinancialDocument extends Document {
86
101
  vatNumber,
87
102
  orientation,
88
103
  pageNumber,
89
- merchantName,
104
+ supplier,
105
+ supplierAddress,
90
106
  time,
107
+ customerName,
108
+ customerAddress,
109
+ customerCompanyRegistration,
91
110
  }) {
92
111
  const constructPrediction = function (item) {
93
112
  return { prediction: { value: item }, valueKey: "value", pageNumber };
@@ -98,13 +117,19 @@ class FinancialDocument extends Document {
98
117
  this.totalTax = new Amount(constructPrediction(totalTax));
99
118
  this.date = new Date(constructPrediction(date));
100
119
  this.dueDate = new Date(constructPrediction(dueDate));
101
- this.merchantName = new Field(constructPrediction(merchantName));
120
+ this.supplier = new Field(constructPrediction(supplier));
121
+ this.supplierAddress = new Field(this.constructPrediction(supplierAddress));
102
122
  this.time = new Field(constructPrediction(time));
103
123
  this.orientation = new Orientation(constructPrediction(orientation));
104
124
  this.invoiceNumber = new Field(constructPrediction(invoiceNumber));
105
125
  this.paymentDetails = new Field(constructPrediction(paymentDetails));
106
126
  this.companyNumber = new Field(constructPrediction(companyNumber));
107
127
  this.vatNumber = new Field(constructPrediction(vatNumber));
128
+ this.customerName = new Field(this.constructPrediction(customerName));
129
+ this.customerAddress = new Field(this.constructPrediction(customerAddress));
130
+ this.customerCompanyRegistration = new Field(
131
+ this.constructPrediction(customerCompanyRegistration)
132
+ );
108
133
  if (taxes !== undefined) {
109
134
  this.taxes = [];
110
135
  for (const t of taxes) {
@@ -122,32 +147,67 @@ class FinancialDocument extends Document {
122
147
 
123
148
  #initFromApiPrediction(apiPrediction, inputFile, pageNumber) {
124
149
  if (Object.keys(apiPrediction).includes("invoice_number")) {
125
- const invoice = new Invoice({ apiPrediction, inputFile, pageNumber });
126
- Object.assign(this, invoice);
127
- this.time = new Field({
128
- prediction: { value: undefined, probability: 0.0 },
150
+ const invoice = new Invoice({
151
+ apiPrediction,
152
+ inputFile,
153
+ pageNumber,
154
+ level: this.level,
129
155
  });
130
- this.merchantName = new Field({
131
- prediction: { value: undefined, probability: 0.0 },
156
+ this.locale = invoice.locale;
157
+ this.totalIncl = invoice.totalIncl;
158
+ this.totalExcl = invoice.totalExcl;
159
+ this.date = invoice.invoiceDate;
160
+ this.invoiceNumber = invoice.invoiceNumber;
161
+ this.dueDate = invoice.dueDate;
162
+ this.taxes = invoice.taxes;
163
+ this.supplier = invoice.supplier;
164
+ this.supplierAddress = invoice.supplierAddress;
165
+ this.paymentDetails = invoice.paymentDetails;
166
+ this.companyNumber = invoice.companyNumber;
167
+ this.orientation = invoice.orientation;
168
+ this.totalTax = invoice.totalTax;
169
+ this.time = new Field({
170
+ prediction: { value: undefined, confidence: 0.0 },
132
171
  });
172
+ this.customerName = invoice.customerName;
173
+ this.customerAddress = invoice.customerAddress;
174
+ this.customerCompanyRegistration = invoice.customerCompanyRegistration;
133
175
  } else {
134
- const receipt = new Receipt({ apiPrediction, inputFile, pageNumber });
135
- Object.assign(this, receipt);
136
- this.invoiceDate = new Field({
137
- prediction: { value: undefined, probability: 0.0 },
176
+ const receipt = new Receipt({
177
+ apiPrediction,
178
+ inputFile,
179
+ pageNumber,
180
+ level: this.level,
138
181
  });
139
- this.invoiceNumber = new Field({
140
- prediction: { value: undefined, probability: 0.0 },
182
+ this.orientation = receipt.orientation;
183
+ this.date = receipt.date;
184
+ this.dueDate = receipt.date;
185
+ this.taxes = receipt.taxes;
186
+ this.locale = receipt.locale;
187
+ this.totalIncl = receipt.totalIncl;
188
+ this.totalExcl = receipt.totalExcl;
189
+ this.supplier = receipt.merchantName;
190
+ this.supplierAddress = new Field({
191
+ prediction: { value: undefined, confidence: 0.0 },
141
192
  });
142
- this.dueDate = new Field({
143
- prediction: { value: undefined, probability: 0.0 },
193
+ this.time = receipt.time;
194
+ this.totalTax = receipt.totalTax;
195
+ this.invoiceNumber = new Field({
196
+ prediction: { value: undefined, confidence: 0.0 },
144
197
  });
145
198
  this.paymentDetails = new Field({
146
- prediction: { value: undefined, probability: 0.0 },
199
+ prediction: { value: undefined, confidence: 0.0 },
147
200
  });
148
201
  this.companyNumber = new Field({
149
- prediction: { value: undefined, probability: 0.0 },
202
+ prediction: { value: undefined, confidence: 0.0 },
203
+ });
204
+ this.customerName = new Field({
205
+ prediction: { value: undefined, confidence: 0.0 },
206
+ });
207
+ this.customerAddress = new Field({
208
+ prediction: { value: undefined, confidence: 0.0 },
150
209
  });
210
+ this.customerCompanyRegistration = [];
151
211
  }
152
212
  }
153
213
 
@@ -157,7 +217,7 @@ class FinancialDocument extends Document {
157
217
  Filename: ${this.filename}
158
218
  Total amount: ${this.totalIncl.value}
159
219
  Date: ${this.date.value}
160
- Merchant name: ${this.merchantName.value}
220
+ Supplier: ${this.supplier.value}
161
221
  Total taxes: ${this.totalTax.value}
162
222
  `;
163
223
  }
@@ -192,7 +252,7 @@ class FinancialDocument extends Document {
192
252
  this.totalIncl.value * (1 - eps) - 0.02 <= reconstructedTotal &&
193
253
  reconstructedTotal <= this.totalIncl.value * (1 + eps) + 0.02
194
254
  ) {
195
- this.taxes = this.taxes.map((tax) => ({ ...tax, probability: 1.0 }));
255
+ this.taxes = this.taxes.map((tax) => ({ ...tax, confidence: 1.0 }));
196
256
  this.totalTax.probability = 1.0;
197
257
  this.totalIncl.probability = 1.0;
198
258
  return true;
@@ -11,7 +11,6 @@ class Invoice extends Document {
11
11
  /**
12
12
  * @param {Object} apiPrediction - Json parsed prediction from HTTP response
13
13
  * @param {Input} input - Input object
14
- * @param {Integer} pageNumber - Page number for multi pages pdf input
15
14
  * @param {Object} locale - locale value for creating Invoice object from scratch
16
15
  * @param {Object} totalIncl - total tax included value for creating Invoice object from scratch
17
16
  * @param {Object} totalExcl - total tax excluded value for creating Invoice object from scratch
@@ -19,12 +18,17 @@ class Invoice extends Document {
19
18
  * @param {Object} invoiceNumber - invoice number value for creating Invoice object from scratch
20
19
  * @param {Object} taxes - taxes value for creating Invoice object from scratch
21
20
  * @param {Object} supplier - supplier value for creating Invoice object from scratch
21
+ * @param {Object} supplierAddress - supplier address value for creating Invoice object from scratch
22
22
  * @param {Object} paymentDetails - payment details value for creating Invoice object from scratch
23
23
  * @param {Object} companyNumber - company number value for creating Invoice object from scratch
24
24
  * @param {Object} vatNumber - vat number value for creating Invoice object from scratch
25
25
  * @param {Object} orientation - orientation value for creating Invoice object from scratch
26
26
  * @param {Object} totalTax - total tax value for creating Invoice object from scratch
27
- * @param {Object} pageNumber - pageNumber for multi pages pdf input
27
+ * @param {Object} customerName - customer name value for creating Invoice object from scratch
28
+ * @param {Object} customerAddress - customer address value for creating Invoice object from scratch
29
+ * @param {Object} customerCompanyRegistration - customer company registration value for creating Invoice object from scratch
30
+ * @param {Number} pageNumber - pageNumber for multi pages pdf input
31
+ * @param {String} level - specify whether object is built from "page" level or "document" level prediction
28
32
  */
29
33
  constructor({
30
34
  apiPrediction = undefined,
@@ -37,14 +41,23 @@ class Invoice extends Document {
37
41
  dueDate = undefined,
38
42
  taxes = undefined,
39
43
  supplier = undefined,
44
+ supplierAddress = undefined,
40
45
  paymentDetails = undefined,
41
46
  companyNumber = undefined,
42
47
  vatNumber = undefined,
43
48
  orientation = undefined,
44
49
  totalTax = undefined,
50
+ customerName = undefined,
51
+ customerAddress = undefined,
52
+ customerCompanyRegistration = undefined,
45
53
  pageNumber = 0,
54
+ level = "page",
46
55
  }) {
47
56
  super(inputFile);
57
+ this.level = level;
58
+ this.constructPrediction = function (item) {
59
+ return { prediction: { value: item }, valueKey: "value", pageNumber };
60
+ };
48
61
  if (apiPrediction === undefined) {
49
62
  this.#initFromScratch({
50
63
  locale,
@@ -55,12 +68,16 @@ class Invoice extends Document {
55
68
  dueDate,
56
69
  taxes,
57
70
  supplier,
71
+ supplierAddress,
58
72
  paymentDetails,
59
73
  companyNumber,
60
74
  vatNumber,
61
75
  orientation,
62
76
  pageNumber,
63
77
  totalTax,
78
+ customerName,
79
+ customerAddress,
80
+ customerCompanyRegistration,
64
81
  });
65
82
  } else {
66
83
  this.#initFromApiPrediction(apiPrediction, pageNumber);
@@ -79,28 +96,34 @@ class Invoice extends Document {
79
96
  dueDate,
80
97
  taxes,
81
98
  supplier,
99
+ supplierAddress,
82
100
  paymentDetails,
83
101
  companyNumber,
84
102
  vatNumber,
85
103
  orientation,
86
104
  pageNumber,
105
+ customerName,
106
+ customerAddress,
107
+ customerCompanyRegistration,
87
108
  }) {
88
- const constructPrediction = function (item) {
89
- return { prediction: { value: item }, valueKey: "value", pageNumber };
90
- };
91
- this.locale = new Locale(constructPrediction(locale));
92
- this.totalIncl = new Amount(constructPrediction(totalIncl));
93
- this.totalExcl = new Amount(constructPrediction(totalExcl));
94
- this.totalTax = new Amount(constructPrediction(totalTax));
95
- this.date = new Date(constructPrediction(invoiceDate));
96
- this.invoiceDate = new Date(constructPrediction(invoiceDate));
97
- this.dueDate = new Date(constructPrediction(dueDate));
98
- this.supplier = new Field(constructPrediction(supplier));
99
- this.orientation = new Orientation(constructPrediction(orientation));
100
- this.invoiceNumber = new Field(constructPrediction(invoiceNumber));
101
- this.paymentDetails = new Field(constructPrediction(paymentDetails));
102
- this.companyNumber = new Field(constructPrediction(companyNumber));
103
- this.vatNumber = new Field(constructPrediction(vatNumber));
109
+ this.locale = new Locale(this.constructPrediction(locale));
110
+ this.totalIncl = new Amount(this.constructPrediction(totalIncl));
111
+ this.totalExcl = new Amount(this.constructPrediction(totalExcl));
112
+ this.totalTax = new Amount(this.constructPrediction(totalTax));
113
+ this.date = new Date(this.constructPrediction(invoiceDate));
114
+ this.invoiceDate = new Date(this.constructPrediction(invoiceDate));
115
+ this.dueDate = new Date(this.constructPrediction(dueDate));
116
+ this.supplier = new Field(this.constructPrediction(supplier));
117
+ this.supplierAddress = new Field(this.constructPrediction(supplierAddress));
118
+ this.invoiceNumber = new Field(this.constructPrediction(invoiceNumber));
119
+ this.paymentDetails = new Field(this.constructPrediction(paymentDetails));
120
+ this.companyNumber = new Field(this.constructPrediction(companyNumber));
121
+ this.vatNumber = new Field(this.constructPrediction(vatNumber));
122
+ this.customerName = new Field(this.constructPrediction(customerName));
123
+ this.customerAddress = new Field(this.constructPrediction(customerAddress));
124
+ this.customerCompanyRegistration = new Field(
125
+ this.constructPrediction(customerCompanyRegistration)
126
+ );
104
127
  if (taxes !== undefined) {
105
128
  this.taxes = [];
106
129
  for (const t of taxes) {
@@ -114,6 +137,9 @@ class Invoice extends Document {
114
137
  );
115
138
  }
116
139
  }
140
+ if (this.level === "page") {
141
+ this.orientation = new Orientation(this.constructPrediction(orientation));
142
+ }
117
143
  }
118
144
 
119
145
  #initFromApiPrediction(apiPrediction, pageNumber) {
@@ -125,7 +151,7 @@ class Invoice extends Document {
125
151
  pageNumber,
126
152
  });
127
153
  this.totalTax = new Amount({
128
- prediction: { value: undefined, probability: 0.0 },
154
+ prediction: { value: undefined, confidence: 0.0 },
129
155
  valueKey: "value",
130
156
  pageNumber,
131
157
  });
@@ -153,10 +179,6 @@ class Invoice extends Document {
153
179
  codeKey: "code",
154
180
  });
155
181
  });
156
- this.orientation = new Orientation({
157
- prediction: apiPrediction.orientation,
158
- pageNumber,
159
- });
160
182
  this.companyNumber = apiPrediction.company_registration.map(function (
161
183
  companyNumber
162
184
  ) {
@@ -179,12 +201,49 @@ class Invoice extends Document {
179
201
  prediction: apiPrediction.supplier,
180
202
  pageNumber,
181
203
  });
204
+ this.supplierAddress = new Field({
205
+ prediction: apiPrediction.supplier_address,
206
+ pageNumber,
207
+ });
208
+ this.customerName = new Field({
209
+ prediction: apiPrediction.customer,
210
+ pageNumber,
211
+ });
212
+ this.customerAddress = new Field({
213
+ prediction: apiPrediction.customer_address,
214
+ pageNumber,
215
+ });
216
+ this.customerCompanyRegistration = apiPrediction.customer_company_registration.map(
217
+ function (customerCompanyRegistration) {
218
+ return new Field({
219
+ prediction: customerCompanyRegistration,
220
+ pageNumber,
221
+ extraFields: ["type"],
222
+ });
223
+ }
224
+ );
182
225
  this.paymentDetails = apiPrediction.payment_details.map(function (
183
226
  paymentDetail
184
227
  ) {
185
228
  return new PaymentDetails({ prediction: paymentDetail, pageNumber });
186
229
  });
187
-
230
+ if (this.level === "page") {
231
+ this.orientation = new Orientation({
232
+ prediction: apiPrediction.orientation,
233
+ pageNumber,
234
+ });
235
+ } else {
236
+ this.orientation = new Orientation(
237
+ this.constructPrediction({
238
+ prediction: {
239
+ value: undefined,
240
+ confidence: 0.0,
241
+ degrees: undefined,
242
+ },
243
+ })
244
+ );
245
+ }
246
+ // document.inference.ocr
188
247
  if ("mvision" in apiPrediction) this.words = apiPrediction.mvision;
189
248
  }
190
249
 
@@ -197,6 +256,9 @@ class Invoice extends Document {
197
256
  Total amount excluding taxes: ${this.totalExcl.value}
198
257
  Invoice Date: ${this.invoiceDate.value}
199
258
  Supplier name: ${this.supplier.value}
259
+ Supplier address: ${this.supplierAddress.value}
260
+ Customer name: ${this.customerName.value}
261
+ Customer address: ${this.customerAddress.value}
200
262
  Taxes: ${this.taxes.map((tax) => tax.toString()).join(" - ")}
201
263
  Total taxes: ${this.totalTax.value}
202
264
  `;
@@ -314,7 +376,7 @@ class Invoice extends Document {
314
376
  value: this.taxes.reduce((acc, tax) => {
315
377
  return tax.value !== undefined ? acc + tax.value : acc;
316
378
  }, 0),
317
- probability: Field.arrayProbability(this.taxes),
379
+ confidence: Field.arrayProbability(this.taxes),
318
380
  };
319
381
  if (totalTax.value > 0)
320
382
  this.totalTax = new Amount({
@@ -334,7 +396,7 @@ class Invoice extends Document {
334
396
  ) {
335
397
  const totalTax = {
336
398
  value: this.totalIncl.value - this.totalExcl.value,
337
- probability: this.totalIncl.probability * this.totalExcl.probability,
399
+ confidence: this.totalIncl.probability * this.totalExcl.probability,
338
400
  };
339
401
  if (totalTax.value > 0)
340
402
  this.totalTax = new Amount({
@@ -357,7 +419,7 @@ class Invoice extends Document {
357
419
  this.taxes.reduce((acc, tax) => {
358
420
  return tax.value !== undefined ? acc + tax.value : acc;
359
421
  }, 0),
360
- probability:
422
+ confidence:
361
423
  Field.arrayProbability(this.taxes) * this.totalIncl.probability,
362
424
  };
363
425
  this.totalExcl = new Amount({
@@ -380,7 +442,7 @@ class Invoice extends Document {
380
442
  this.taxes.reduce((acc, tax) => {
381
443
  return tax.value ? acc + tax.value : acc;
382
444
  }, 0.0),
383
- probability:
445
+ confidence:
384
446
  Field.arrayProbability(this.taxes) * this.totalExcl.probability,
385
447
  };
386
448
  this.totalIncl = new Amount({
@@ -22,6 +22,7 @@ class Receipt extends Document {
22
22
  * @param {Object} orientation - orientation value for creating Receipt object from scratch
23
23
  * @param {Object} totalTax - total taxes value for creating Receipt object from scratch
24
24
  * @param {Object} totalExcl - total taxes excluded value for creating Receipt object from scratch
25
+ * @param {String} level - specify whether object is built from "page" level or "document" level prediction
25
26
  */
26
27
  constructor({
27
28
  apiPrediction = undefined,
@@ -37,8 +38,13 @@ class Receipt extends Document {
37
38
  totalTax = undefined,
38
39
  totalExcl = undefined,
39
40
  pageNumber = 0,
41
+ level = "page",
40
42
  }) {
41
43
  super(inputFile);
44
+ this.level = level;
45
+ this.constructPrediction = function (item) {
46
+ return { prediction: { value: item }, valueKey: "value", pageNumber };
47
+ };
42
48
  if (apiPrediction === undefined) {
43
49
  this.#initFromScratch({
44
50
  locale,
@@ -73,15 +79,12 @@ class Receipt extends Document {
73
79
  totalTax,
74
80
  pageNumber,
75
81
  }) {
76
- const constructPrediction = function (item) {
77
- return { prediction: { value: item }, valueKey: "value", pageNumber };
78
- };
79
- this.locale = new Locale(constructPrediction(locale));
80
- this.totalIncl = new Amount(constructPrediction(totalIncl));
81
- this.date = new Date(constructPrediction(date));
82
- this.category = new Field(constructPrediction(category));
83
- this.merchantName = new Field(constructPrediction(merchantName));
84
- this.time = new Field(constructPrediction(time));
82
+ this.locale = new Locale(this.constructPrediction(locale));
83
+ this.totalIncl = new Amount(this.constructPrediction(totalIncl));
84
+ this.date = new Date(this.constructPrediction(date));
85
+ this.category = new Field(this.constructPrediction(category));
86
+ this.merchantName = new Field(this.constructPrediction(merchantName));
87
+ this.time = new Field(this.constructPrediction(time));
85
88
  if (taxes !== undefined) {
86
89
  this.taxes = [];
87
90
  for (const t of taxes) {
@@ -95,9 +98,9 @@ class Receipt extends Document {
95
98
  );
96
99
  }
97
100
  }
98
- this.orientation = new Orientation(constructPrediction(orientation));
99
- this.totalTax = new Amount(constructPrediction(totalTax));
100
- this.totalExcl = new Amount(constructPrediction(totalExcl));
101
+ this.orientation = new Orientation(this.constructPrediction(orientation));
102
+ this.totalTax = new Amount(this.constructPrediction(totalTax));
103
+ this.totalExcl = new Amount(this.constructPrediction(totalExcl));
101
104
  }
102
105
 
103
106
  /**
@@ -142,20 +145,32 @@ class Receipt extends Document {
142
145
  codeKey: "code",
143
146
  })
144
147
  );
145
- this.orientation = new Orientation({
146
- prediction: apiPrediction.orientation,
147
- pageNumber,
148
- });
149
148
  this.totalTax = new Amount({
150
- prediction: { value: undefined, probability: 0 },
149
+ prediction: { value: undefined, confidence: 0 },
151
150
  valueKey: "value",
152
151
  pageNumber,
153
152
  });
154
153
  this.totalExcl = new Amount({
155
- prediction: { value: undefined, probability: 0 },
154
+ prediction: { value: undefined, confidence: 0 },
156
155
  valueKey: "value",
157
156
  pageNumber,
158
157
  });
158
+ if (this.level === "page") {
159
+ this.orientation = new Orientation({
160
+ prediction: apiPrediction.orientation,
161
+ pageNumber,
162
+ });
163
+ } else {
164
+ this.orientation = new Orientation(
165
+ this.constructPrediction({
166
+ prediction: {
167
+ value: undefined,
168
+ confidence: 0.0,
169
+ degrees: undefined,
170
+ },
171
+ })
172
+ );
173
+ }
159
174
  if ("mvision" in apiPrediction) this.words = apiPrediction.mvision;
160
175
  }
161
176
 
@@ -235,7 +250,7 @@ class Receipt extends Document {
235
250
  if (this.taxes.length && this.totalIncl.value != null) {
236
251
  const totalExcl = {
237
252
  value: this.totalIncl.value - Field.arraySum(this.taxes),
238
- probability:
253
+ confidence:
239
254
  Field.arrayProbability(this.taxes) * this.totalIncl.probability,
240
255
  };
241
256
  this.totalExcl = new Amount({
@@ -257,7 +272,7 @@ class Receipt extends Document {
257
272
  value: this.taxes
258
273
  .map((tax) => tax.value || 0)
259
274
  .reduce((a, b) => a + b, 0),
260
- probability: Field.arrayProbability(this.taxes),
275
+ confidence: Field.arrayProbability(this.taxes),
261
276
  };
262
277
  if (totalTax.value > 0)
263
278
  this.totalTax = new Amount({