mindee 1.1.2 → 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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## v1.2.0
4
+
5
+ ### Fixes
6
+
7
+ - fix: 🐛 Get `words` values when `includeWords` parameter is `true`
8
+
3
9
  ## v1.1.2
4
10
 
5
11
  ### Fixes
@@ -37,10 +37,10 @@ class APIFinancialDocument extends APIObject {
37
37
  : this.receiptToken;
38
38
  const url =
39
39
  inputFile.fileExtension === "application/pdf"
40
- ? "/invoices/v3/predict"
41
- : "/expense_receipts/v3/predict";
40
+ ? `/invoices/v${version}/predict`
41
+ : `/expense_receipts/v${version}/predict`;
42
42
  super.parse();
43
- return await super._request(url, inputFile, version, includeWords);
43
+ return super._request(url, inputFile, includeWords);
44
44
  }
45
45
  }
46
46
 
@@ -10,7 +10,7 @@ const request = require("./request");
10
10
  class APIObject {
11
11
  constructor(apiToken = undefined, apiName = "") {
12
12
  this.apiToken = apiToken;
13
- this.baseUrl = "https://api.mindee.net/v1/products/mindee/";
13
+ this.baseUrl = "https://api.mindee.net/v1/products/mindee";
14
14
  this.apiName = apiName;
15
15
  }
16
16
 
@@ -6,7 +6,7 @@ const os = require("os");
6
6
 
7
7
  const request = (url, method, headers, input, includeWords = false) => {
8
8
  return new Promise(function (resolve, reject) {
9
- const form = new FormData();
9
+ let form = new FormData();
10
10
  let body;
11
11
 
12
12
  headers["User-Agent"] = `mindee-api-nodejs@v${sdkVersion} nodejs-${
@@ -32,31 +32,47 @@ class Response {
32
32
  }
33
33
 
34
34
  formatResponse() {
35
+ let http_data_document = this.httpResponse.data.document;
35
36
  const constructors = {
36
37
  receipt: (params) => new Receipt(params),
37
38
  invoice: (params) => new Invoice(params),
38
39
  financialDocument: (params) => new FinancialDocument(params),
39
40
  };
40
- const predictions = this.httpResponse.data.document.inference.pages.entries();
41
+
42
+ const predictions = http_data_document.inference.pages.entries();
41
43
  this[`${this.documentType}s`] = [];
44
+ let document_words_content = [];
42
45
 
43
46
  // Create a list of Document (Receipt, Invoice...) for each page of the input document
44
47
  for (const [pageNumber, prediction] of predictions) {
48
+ let page_words_content = [];
49
+ if (
50
+ "ocr" in http_data_document &&
51
+ Object.keys(http_data_document.ocr).length > 0
52
+ ) {
53
+ page_words_content =
54
+ http_data_document.ocr["mvision-v1"].pages[pageNumber].all_words;
55
+ document_words_content.push(
56
+ ...http_data_document.ocr["mvision-v1"].pages[pageNumber].all_words
57
+ );
58
+ }
45
59
  this[`${this.documentType}s`].push(
46
60
  constructors[this.documentType]({
47
61
  apiPrediction: prediction.prediction,
48
62
  inputFile: this.input,
49
63
  pageNumber: pageNumber,
64
+ words: page_words_content,
50
65
  })
51
66
  );
52
67
  }
53
68
 
54
69
  // Merge the list of Document into a unique Document
55
70
  this[this.documentType] = constructors[this.documentType]({
56
- apiPrediction: this.httpResponse.data.document.inference.prediction,
71
+ apiPrediction: http_data_document.inference.prediction,
57
72
  inputFile: this.input,
58
- pageNumber: this.httpResponse.data.document.n_pages,
73
+ pageNumber: http_data_document.n_pages,
59
74
  level: "document",
75
+ words: document_words_content,
60
76
  });
61
77
  }
62
78
  }
@@ -54,6 +54,7 @@ class FinancialDocument extends Document {
54
54
  customerName = undefined,
55
55
  customerAddress = undefined,
56
56
  customerCompanyRegistration = undefined,
57
+ words = undefined,
57
58
  pageNumber = 0,
58
59
  level = "page",
59
60
  }) {
@@ -82,7 +83,7 @@ class FinancialDocument extends Document {
82
83
  customerCompanyRegistration,
83
84
  });
84
85
  } else {
85
- this.#initFromApiPrediction(apiPrediction, inputFile, pageNumber);
86
+ this.#initFromApiPrediction(apiPrediction, inputFile, pageNumber, words);
86
87
  }
87
88
  this.#checklist();
88
89
  }
@@ -145,13 +146,14 @@ class FinancialDocument extends Document {
145
146
  }
146
147
  }
147
148
 
148
- #initFromApiPrediction(apiPrediction, inputFile, pageNumber) {
149
+ #initFromApiPrediction(apiPrediction, inputFile, pageNumber, words) {
149
150
  if (Object.keys(apiPrediction).includes("invoice_number")) {
150
151
  const invoice = new Invoice({
151
152
  apiPrediction,
152
153
  inputFile,
153
154
  pageNumber,
154
155
  level: this.level,
156
+ words: words,
155
157
  });
156
158
  this.locale = invoice.locale;
157
159
  this.totalIncl = invoice.totalIncl;
@@ -166,6 +168,7 @@ class FinancialDocument extends Document {
166
168
  this.companyNumber = invoice.companyNumber;
167
169
  this.orientation = invoice.orientation;
168
170
  this.totalTax = invoice.totalTax;
171
+ if (invoice.words) this.words = invoice.words;
169
172
  this.time = new Field({
170
173
  prediction: { value: undefined, confidence: 0.0 },
171
174
  });
@@ -178,6 +181,7 @@ class FinancialDocument extends Document {
178
181
  inputFile,
179
182
  pageNumber,
180
183
  level: this.level,
184
+ words: words,
181
185
  });
182
186
  this.orientation = receipt.orientation;
183
187
  this.date = receipt.date;
@@ -208,6 +212,7 @@ class FinancialDocument extends Document {
208
212
  prediction: { value: undefined, confidence: 0.0 },
209
213
  });
210
214
  this.customerCompanyRegistration = [];
215
+ if (receipt.words) this.words = receipt.words;
211
216
  }
212
217
  }
213
218
 
@@ -50,6 +50,7 @@ class Invoice extends Document {
50
50
  customerName = undefined,
51
51
  customerAddress = undefined,
52
52
  customerCompanyRegistration = undefined,
53
+ words = undefined,
53
54
  pageNumber = 0,
54
55
  level = "page",
55
56
  }) {
@@ -80,7 +81,7 @@ class Invoice extends Document {
80
81
  customerCompanyRegistration,
81
82
  });
82
83
  } else {
83
- this.#initFromApiPrediction(apiPrediction, pageNumber);
84
+ this.#initFromApiPrediction(apiPrediction, pageNumber, words);
84
85
  }
85
86
  this.#checklist();
86
87
  this.#reconstruct();
@@ -142,8 +143,7 @@ class Invoice extends Document {
142
143
  }
143
144
  }
144
145
 
145
- #initFromApiPrediction(apiPrediction, pageNumber) {
146
- this.words = [];
146
+ #initFromApiPrediction(apiPrediction, pageNumber, words) {
147
147
  this.locale = new Locale({ prediction: apiPrediction.locale, pageNumber });
148
148
  this.totalIncl = new Amount({
149
149
  prediction: apiPrediction.total_incl,
@@ -243,8 +243,7 @@ class Invoice extends Document {
243
243
  })
244
244
  );
245
245
  }
246
- // document.inference.ocr
247
- if ("mvision" in apiPrediction) this.words = apiPrediction.mvision;
246
+ if (words && words.length > 0) this.words = words;
248
247
  }
249
248
 
250
249
  toString() {
@@ -37,6 +37,7 @@ class Receipt extends Document {
37
37
  orientation = undefined,
38
38
  totalTax = undefined,
39
39
  totalExcl = undefined,
40
+ words = undefined,
40
41
  pageNumber = 0,
41
42
  level = "page",
42
43
  }) {
@@ -60,7 +61,7 @@ class Receipt extends Document {
60
61
  pageNumber,
61
62
  });
62
63
  } else {
63
- this.#initFromApiPrediction(apiPrediction, pageNumber);
64
+ this.#initFromApiPrediction(apiPrediction, pageNumber, words);
64
65
  }
65
66
  this.#checklist();
66
67
  this.#reconstruct();
@@ -108,8 +109,7 @@ class Receipt extends Document {
108
109
  @param apiPrediction: Raw prediction from HTTP response
109
110
  @param pageNumber: Page number for multi pages pdf input
110
111
  */
111
- #initFromApiPrediction(apiPrediction, pageNumber) {
112
- this.words = [];
112
+ #initFromApiPrediction(apiPrediction, pageNumber, words) {
113
113
  this.locale = new Locale({ prediction: apiPrediction.locale, pageNumber });
114
114
  this.totalIncl = new Amount({
115
115
  prediction: apiPrediction.total_incl,
@@ -171,7 +171,7 @@ class Receipt extends Document {
171
171
  })
172
172
  );
173
173
  }
174
- if ("mvision" in apiPrediction) this.words = apiPrediction.mvision;
174
+ if (words && words.length > 0) this.words = words;
175
175
  }
176
176
 
177
177
  toString() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mindee",
3
- "version": "1.1.2",
3
+ "version": "1.2.0",
4
4
  "description": "Mindee API SDK for Node.js",
5
5
  "main": "mindee/index.js",
6
6
  "license": "Apache-2.0",