mindee 1.0.9 → 1.2.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.
@@ -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,
@@ -36,9 +37,15 @@ class Receipt extends Document {
36
37
  orientation = undefined,
37
38
  totalTax = undefined,
38
39
  totalExcl = undefined,
40
+ words = undefined,
39
41
  pageNumber = 0,
42
+ level = "page",
40
43
  }) {
41
44
  super(inputFile);
45
+ this.level = level;
46
+ this.constructPrediction = function (item) {
47
+ return { prediction: { value: item }, valueKey: "value", pageNumber };
48
+ };
42
49
  if (apiPrediction === undefined) {
43
50
  this.#initFromScratch({
44
51
  locale,
@@ -54,7 +61,7 @@ class Receipt extends Document {
54
61
  pageNumber,
55
62
  });
56
63
  } else {
57
- this.#initFromApiPrediction(apiPrediction, pageNumber);
64
+ this.#initFromApiPrediction(apiPrediction, pageNumber, words);
58
65
  }
59
66
  this.#checklist();
60
67
  this.#reconstruct();
@@ -73,15 +80,12 @@ class Receipt extends Document {
73
80
  totalTax,
74
81
  pageNumber,
75
82
  }) {
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));
83
+ this.locale = new Locale(this.constructPrediction(locale));
84
+ this.totalIncl = new Amount(this.constructPrediction(totalIncl));
85
+ this.date = new Date(this.constructPrediction(date));
86
+ this.category = new Field(this.constructPrediction(category));
87
+ this.merchantName = new Field(this.constructPrediction(merchantName));
88
+ this.time = new Field(this.constructPrediction(time));
85
89
  if (taxes !== undefined) {
86
90
  this.taxes = [];
87
91
  for (const t of taxes) {
@@ -95,9 +99,9 @@ class Receipt extends Document {
95
99
  );
96
100
  }
97
101
  }
98
- this.orientation = new Orientation(constructPrediction(orientation));
99
- this.totalTax = new Amount(constructPrediction(totalTax));
100
- this.totalExcl = new Amount(constructPrediction(totalExcl));
102
+ this.orientation = new Orientation(this.constructPrediction(orientation));
103
+ this.totalTax = new Amount(this.constructPrediction(totalTax));
104
+ this.totalExcl = new Amount(this.constructPrediction(totalExcl));
101
105
  }
102
106
 
103
107
  /**
@@ -105,8 +109,7 @@ class Receipt extends Document {
105
109
  @param apiPrediction: Raw prediction from HTTP response
106
110
  @param pageNumber: Page number for multi pages pdf input
107
111
  */
108
- #initFromApiPrediction(apiPrediction, pageNumber) {
109
- this.words = [];
112
+ #initFromApiPrediction(apiPrediction, pageNumber, words) {
110
113
  this.locale = new Locale({ prediction: apiPrediction.locale, pageNumber });
111
114
  this.totalIncl = new Amount({
112
115
  prediction: apiPrediction.total_incl,
@@ -142,21 +145,33 @@ 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
  });
159
- if ("mvision" in apiPrediction) this.words = apiPrediction.mvision;
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
+ }
174
+ if (words && words.length > 0) this.words = words;
160
175
  }
161
176
 
162
177
  toString() {
@@ -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({
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "mindee",
3
- "version": "1.0.9",
3
+ "version": "1.2.0",
4
4
  "description": "Mindee API SDK for Node.js",
5
5
  "main": "mindee/index.js",
6
- "license": "GPL-3.0",
6
+ "license": "Apache-2.0",
7
7
  "scripts": {
8
8
  "build": "babel mindee -d lib",
9
9
  "test": "npm run build && mocha tests/**/*.js",
@@ -38,7 +38,7 @@
38
38
  "base64-stream": "^1.0.0",
39
39
  "concat-stream": "^2.0.0",
40
40
  "file-type": "^16.5.3",
41
- "form-data": "^3.0.0",
41
+ "form-data": "^3.0.1",
42
42
  "pdf-lib": "^1.13.0"
43
43
  },
44
44
  "keywords": [