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.
- package/CHANGELOG.md +47 -18
- package/README.md +43 -127
- package/bin/mindee +3 -0
- package/dist/package.json +70 -0
- package/dist/src/api/endpoint.d.ts +29 -0
- package/dist/src/api/endpoint.js +148 -0
- package/dist/src/api/index.d.ts +2 -0
- package/dist/src/api/index.js +18 -0
- package/dist/src/api/response.d.ts +52 -0
- package/dist/src/api/response.js +107 -0
- package/dist/src/cli.d.ts +1 -0
- package/dist/src/cli.js +117 -0
- package/dist/src/client.d.ts +48 -0
- package/dist/src/client.js +134 -0
- package/dist/src/constants.d.ts +13 -0
- package/dist/src/constants.js +54 -0
- package/dist/src/documents/custom.d.ts +14 -0
- package/dist/src/documents/custom.js +48 -0
- package/dist/src/documents/document.d.ts +38 -0
- package/dist/{documents → src/documents}/document.js +15 -35
- package/dist/src/documents/documentConfig.d.ts +44 -0
- package/dist/src/documents/documentConfig.js +121 -0
- package/dist/src/documents/financialDocument.d.ts +32 -0
- package/dist/src/documents/financialDocument.js +130 -0
- package/dist/src/documents/index.d.ts +12 -0
- package/dist/src/documents/index.js +20 -0
- package/dist/src/documents/invoice.d.ts +37 -0
- package/dist/src/documents/invoice.js +303 -0
- package/dist/src/documents/passport.d.ts +36 -0
- package/dist/src/documents/passport.js +218 -0
- package/dist/src/documents/receipt.d.ts +24 -0
- package/dist/src/documents/receipt.js +157 -0
- package/dist/src/errors/handler.d.ts +7 -0
- package/dist/{errors → src/errors}/handler.js +5 -4
- package/dist/src/fields/amount.d.ts +13 -0
- package/dist/{documents → src}/fields/amount.js +19 -6
- package/dist/src/fields/companyRegistration.d.ts +6 -0
- package/dist/src/fields/companyRegistration.js +11 -0
- package/dist/src/fields/customDocs.d.ts +42 -0
- package/dist/src/fields/customDocs.js +61 -0
- package/dist/src/fields/date.d.ts +12 -0
- package/dist/src/fields/date.js +25 -0
- package/dist/src/fields/field.d.ts +74 -0
- package/dist/{documents → src}/fields/field.js +48 -27
- package/dist/src/fields/fullText.d.ts +24 -0
- package/dist/src/fields/fullText.js +89 -0
- package/dist/src/fields/index.d.ts +11 -0
- package/dist/src/fields/index.js +27 -0
- package/dist/src/fields/locale.d.ts +13 -0
- package/dist/{documents → src}/fields/locale.js +20 -5
- package/dist/src/fields/orientation.d.ts +10 -0
- package/dist/{documents → src}/fields/orientation.js +5 -5
- package/dist/src/fields/paymentDetails.d.ts +31 -0
- package/dist/{documents → src}/fields/paymentDetails.js +14 -6
- package/dist/src/fields/tax.d.ts +30 -0
- package/dist/src/fields/tax.js +51 -0
- package/dist/src/geometry.d.ts +59 -0
- package/dist/src/geometry.js +113 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +12 -0
- package/dist/src/inputs.d.ts +69 -0
- package/dist/src/inputs.js +223 -0
- package/dist/src/logger.d.ts +20 -0
- package/dist/src/logger.js +35 -0
- package/package.json +45 -26
- package/.eslintrc +0 -24
- package/.mocharc.yaml +0 -2
- package/.nyc_output/f57ba3bc-1777-4ecc-81e1-230327c2a401.json +0 -1
- package/.nyc_output/processinfo/f57ba3bc-1777-4ecc-81e1-230327c2a401.json +0 -1
- package/.nyc_output/processinfo/index.json +0 -1
- package/.prettierrc +0 -4
- package/.tool-versions +0 -1
- package/CODE_OF_CONDUCT.md +0 -63
- package/CONTRIBUTING.md +0 -84
- package/ISSUE_TEMPLATE.md +0 -45
- package/PULL_REQUEST_TEMPLATE.md +0 -45
- package/babel.config.json +0 -10
- package/coverage/coverage.json +0 -1
- package/coverage/lcov-report/base.css +0 -224
- package/coverage/lcov-report/block-navigation.js +0 -79
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +0 -171
- package/coverage/lcov-report/prettify.css +0 -1
- package/coverage/lcov-report/prettify.js +0 -2
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +0 -170
- package/coverage/lcov.info +0 -1595
- package/coverage.lcov +0 -1595
- package/dist/api/financialDocument.js +0 -51
- package/dist/api/index.js +0 -13
- package/dist/api/invoice.js +0 -59
- package/dist/api/object.js +0 -80
- package/dist/api/receipt.js +0 -45
- package/dist/api/request.js +0 -90
- package/dist/api/response.js +0 -91
- package/dist/documents/fields/date.js +0 -17
- package/dist/documents/fields/index.js +0 -17
- package/dist/documents/fields/tax.js +0 -42
- package/dist/documents/financialDocument.js +0 -216
- package/dist/documents/index.js +0 -13
- package/dist/documents/invoice.js +0 -361
- package/dist/documents/receipt.js +0 -231
- package/dist/index.js +0 -23
- package/dist/inputs.js +0 -208
- package/dist/logger.js +0 -34
- package/examples/.env.example +0 -2
- package/examples/documents/invoices/credit_note.pdf +0 -900
- package/examples/documents/invoices/invoice.pdf +0 -0
- package/examples/documents/invoices/invoice_6p.pdf +0 -0
- package/examples/documents/receipts/receipt.jpg +0 -0
- package/examples/financialDocument.js +0 -32
- package/examples/invoice.js +0 -74
- package/examples/receipt.js +0 -45
- package/lib/api/financialDocument.js +0 -59
- package/lib/api/index.js +0 -7
- package/lib/api/invoice.js +0 -73
- package/lib/api/object.js +0 -90
- package/lib/api/receipt.js +0 -58
- package/lib/api/request.js +0 -84
- package/lib/api/response.js +0 -92
- package/lib/documents/document.js +0 -97
- package/lib/documents/fields/amount.js +0 -35
- package/lib/documents/fields/date.js +0 -36
- package/lib/documents/fields/field.js +0 -102
- package/lib/documents/fields/index.js +0 -9
- package/lib/documents/fields/locale.js +0 -35
- package/lib/documents/fields/orientation.js +0 -33
- package/lib/documents/fields/paymentDetails.js +0 -71
- package/lib/documents/fields/tax.js +0 -57
- package/lib/documents/financialDocument.js +0 -280
- package/lib/documents/index.js +0 -7
- package/lib/documents/invoice.js +0 -451
- package/lib/documents/receipt.js +0 -332
- package/lib/errors/handler.js +0 -19
- package/lib/index.js +0 -40
- package/lib/inputs.js +0 -190
- package/lib/logger.js +0 -52
- package/mindee/api/financialDocument.js +0 -47
- package/mindee/api/index.js +0 -5
- package/mindee/api/invoice.js +0 -53
- package/mindee/api/object.js +0 -82
- package/mindee/api/receipt.js +0 -39
- package/mindee/api/request.js +0 -72
- package/mindee/api/response.js +0 -80
- package/mindee/documents/document.js +0 -69
- package/mindee/documents/fields/amount.js +0 -25
- package/mindee/documents/fields/date.js +0 -29
- package/mindee/documents/fields/field.js +0 -86
- package/mindee/documents/fields/index.js +0 -7
- package/mindee/documents/fields/locale.js +0 -30
- package/mindee/documents/fields/orientation.js +0 -24
- package/mindee/documents/fields/paymentDetails.js +0 -52
- package/mindee/documents/fields/tax.js +0 -47
- package/mindee/documents/financialDocument.js +0 -269
- package/mindee/documents/index.js +0 -5
- package/mindee/documents/invoice.js +0 -456
- package/mindee/documents/receipt.js +0 -287
- package/mindee/errors/handler.js +0 -16
- package/mindee/index.js +0 -36
- package/mindee/inputs.js +0 -158
- package/mindee/logger.js +0 -33
package/mindee/api/invoice.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
const APIObject = require("./object");
|
|
2
|
-
const Input = require("../inputs");
|
|
3
|
-
|
|
4
|
-
class APIInvoice extends APIObject {
|
|
5
|
-
constructor(apiToken = undefined) {
|
|
6
|
-
super(apiToken, "invoice");
|
|
7
|
-
this.baseUrl = `${this.baseUrl}/invoices/`;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @param {Boolean} includeWords: , extract all words into http_response
|
|
12
|
-
* @param {Boolean} cutPdf: Automatically reconstruct pdf with more than 4 pages
|
|
13
|
-
* @param {String} inputType: String in {'path', 'stream', 'base64'}
|
|
14
|
-
* @param {String} file: Receipt filepath (allowed jpg, png, tiff, pdf)
|
|
15
|
-
* @param {String} version: expense_receipt api version
|
|
16
|
-
* @returns {Response} Wrapped response with Receipts objects parsed
|
|
17
|
-
*/
|
|
18
|
-
async parse({
|
|
19
|
-
input,
|
|
20
|
-
inputType = "path",
|
|
21
|
-
filename = undefined,
|
|
22
|
-
version = "3",
|
|
23
|
-
cutPdf = true,
|
|
24
|
-
includeWords = false,
|
|
25
|
-
}) {
|
|
26
|
-
super.parse();
|
|
27
|
-
const inputFile = new Input({
|
|
28
|
-
file: input,
|
|
29
|
-
inputType: inputType,
|
|
30
|
-
filename: filename,
|
|
31
|
-
allowCutPdf: cutPdf,
|
|
32
|
-
});
|
|
33
|
-
await inputFile.init();
|
|
34
|
-
const url = `v${version}/predict`;
|
|
35
|
-
return await super._request(url, inputFile, includeWords);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
@param {String} inputFile - Input object
|
|
40
|
-
@param {} response - HTTP response
|
|
41
|
-
@param {Document} documentType - Document class in {"Receipt", "Invoice", "Financial_document"}
|
|
42
|
-
@returns {Response}
|
|
43
|
-
*/
|
|
44
|
-
wrapResponse(inputFile, response, documentType) {
|
|
45
|
-
let result = super.wrapResponse(inputFile, response, documentType);
|
|
46
|
-
result.documentType =
|
|
47
|
-
result.httpResponse.data?.predictions?.[0]?.document_type?.value?.toLowerCase() ||
|
|
48
|
-
result.documentType;
|
|
49
|
-
return result;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
module.exports = APIInvoice;
|
package/mindee/api/object.js
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
const Response = require("./response");
|
|
2
|
-
const errorHandler = require("../errors/handler");
|
|
3
|
-
const request = require("./request");
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Base class for APIs (APIReceipt & APIInvoice)
|
|
7
|
-
* @param {String} apiToken - Token of the API used for parsing document
|
|
8
|
-
* @param {String} apiName - Name of the API used for parsing document
|
|
9
|
-
*/
|
|
10
|
-
class APIObject {
|
|
11
|
-
constructor(apiToken = undefined, apiName = "") {
|
|
12
|
-
this.apiToken = apiToken;
|
|
13
|
-
this.baseUrl = "https://api.mindee.net/v1/products/mindee";
|
|
14
|
-
this.apiName = apiName;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
*/
|
|
19
|
-
parse() {
|
|
20
|
-
if (!this.apiToken) {
|
|
21
|
-
errorHandler.throw({
|
|
22
|
-
error: new Error(
|
|
23
|
-
`Missing API token for ${this.apiName}. \
|
|
24
|
-
Have you create a mindee Client with a ${this.apiName}Token in parameters ?`
|
|
25
|
-
),
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
@param {String} url - API url for request
|
|
32
|
-
@param {Input} inputFile - input file for API
|
|
33
|
-
@param {Boolean} includeWords - Include Mindee vision words in Response
|
|
34
|
-
@returns {Response}
|
|
35
|
-
*/
|
|
36
|
-
async _request(url, inputFile, includeWords = false) {
|
|
37
|
-
const headers = {
|
|
38
|
-
"X-Inferuser-Token": this.apiToken,
|
|
39
|
-
};
|
|
40
|
-
const response = await request(
|
|
41
|
-
`${this.baseUrl}${url}`,
|
|
42
|
-
"POST",
|
|
43
|
-
headers,
|
|
44
|
-
inputFile,
|
|
45
|
-
includeWords
|
|
46
|
-
);
|
|
47
|
-
return this.wrapResponse(inputFile, response, this.apiName);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
@param {String} inputFile - Input object
|
|
52
|
-
@param {} response - HTTP response
|
|
53
|
-
@param {Document} documentType - Document class in {"Receipt", "Invoice", "Financial_document"}
|
|
54
|
-
@returns {Response}
|
|
55
|
-
*/
|
|
56
|
-
wrapResponse(inputFile, response, documentType) {
|
|
57
|
-
if (response.statusCode > 201) {
|
|
58
|
-
const errorMessage = JSON.stringify(response.data, null, 4);
|
|
59
|
-
errorHandler.throw(
|
|
60
|
-
new Error(
|
|
61
|
-
`${this.apiName} API ${response.statusCode} HTTP error: ${errorMessage}`
|
|
62
|
-
),
|
|
63
|
-
false
|
|
64
|
-
);
|
|
65
|
-
return new Response({
|
|
66
|
-
httpResponse: response,
|
|
67
|
-
documentType: documentType,
|
|
68
|
-
document: undefined,
|
|
69
|
-
input: inputFile,
|
|
70
|
-
error: true,
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
return new Response({
|
|
74
|
-
httpResponse: response,
|
|
75
|
-
documentType: documentType,
|
|
76
|
-
document: inputFile,
|
|
77
|
-
input: inputFile,
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
module.exports = APIObject;
|
package/mindee/api/receipt.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
const APIObject = require("./object");
|
|
2
|
-
const Input = require("../inputs");
|
|
3
|
-
|
|
4
|
-
class APIReceipt extends APIObject {
|
|
5
|
-
constructor(apiToken = undefined) {
|
|
6
|
-
super(apiToken, "receipt");
|
|
7
|
-
this.baseUrl = `${this.baseUrl}/expense_receipts/`;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @param {String} file: Receipt filepath (allowed jpg, png, tiff, pdf)
|
|
12
|
-
* @param {String} inputType: String in {'path', 'stream', 'base64'}
|
|
13
|
-
* @param {Boolean} includeWords: extract all words into http_response
|
|
14
|
-
* @param {String} version: expense_receipt api version
|
|
15
|
-
* @param {Boolean} cutPdf: Automatically reconstruct pdf with more than 4 pages
|
|
16
|
-
* @returns {Response} Wrapped response with Receipts objects parsed
|
|
17
|
-
*/
|
|
18
|
-
async parse({
|
|
19
|
-
input,
|
|
20
|
-
inputType = "path",
|
|
21
|
-
filename = undefined,
|
|
22
|
-
version = "3",
|
|
23
|
-
cutPdf = true,
|
|
24
|
-
includeWords = false,
|
|
25
|
-
}) {
|
|
26
|
-
super.parse();
|
|
27
|
-
const inputFile = new Input({
|
|
28
|
-
file: input,
|
|
29
|
-
inputType: inputType,
|
|
30
|
-
filename: filename,
|
|
31
|
-
allowCutPdf: cutPdf,
|
|
32
|
-
});
|
|
33
|
-
await inputFile.init();
|
|
34
|
-
const url = `v${version}/predict`;
|
|
35
|
-
return await super._request(url, inputFile, includeWords);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
module.exports = APIReceipt;
|
package/mindee/api/request.js
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
const https = require("https");
|
|
2
|
-
const { version: sdkVersion } = require("../../package.json");
|
|
3
|
-
const { URL } = require("url");
|
|
4
|
-
const FormData = require("form-data");
|
|
5
|
-
const os = require("os");
|
|
6
|
-
|
|
7
|
-
const request = (url, method, headers, input, includeWords = false) => {
|
|
8
|
-
return new Promise(function (resolve, reject) {
|
|
9
|
-
let form = new FormData();
|
|
10
|
-
|
|
11
|
-
headers["User-Agent"] = `mindee-api-nodejs@v${sdkVersion} nodejs-${
|
|
12
|
-
process.version
|
|
13
|
-
} ${os.type().toLowerCase()}`;
|
|
14
|
-
|
|
15
|
-
const fileParams = { filename: input.filename };
|
|
16
|
-
|
|
17
|
-
if (input.inputType === "base64") {
|
|
18
|
-
form.append(
|
|
19
|
-
"document",
|
|
20
|
-
Buffer.from(input.fileObject, "base64"),
|
|
21
|
-
fileParams
|
|
22
|
-
);
|
|
23
|
-
} else {
|
|
24
|
-
form.append("document", input.fileObject, fileParams);
|
|
25
|
-
}
|
|
26
|
-
if (includeWords) {
|
|
27
|
-
form.append("include_mvision", "true");
|
|
28
|
-
}
|
|
29
|
-
headers = { ...headers, ...form.getHeaders() };
|
|
30
|
-
|
|
31
|
-
const uri = new URL(url);
|
|
32
|
-
const options = {
|
|
33
|
-
method: method,
|
|
34
|
-
headers: headers,
|
|
35
|
-
hostname: uri.hostname,
|
|
36
|
-
path: `${uri.pathname}${uri.search}`,
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
const req = https.request(options, function (res) {
|
|
40
|
-
let responseBody = [];
|
|
41
|
-
|
|
42
|
-
res.on("data", function (chunk) {
|
|
43
|
-
responseBody += chunk;
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
res.on("end", function () {
|
|
47
|
-
try {
|
|
48
|
-
resolve({
|
|
49
|
-
...res,
|
|
50
|
-
data: JSON.parse(responseBody),
|
|
51
|
-
});
|
|
52
|
-
} catch (error) {
|
|
53
|
-
console.error(responseBody, error);
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
req.on("error", (err) => {
|
|
59
|
-
reject(err);
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
if (["path", "stream"].includes(input.inputType)) {
|
|
63
|
-
form.pipe(req);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (input.inputType === "base64") {
|
|
67
|
-
form.pipe(req);
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
module.exports = request;
|
package/mindee/api/response.js
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
const Receipt = require("../documents").receipt;
|
|
2
|
-
const Invoice = require("../documents").invoice;
|
|
3
|
-
const FinancialDocument = require("../documents").financialDocument;
|
|
4
|
-
const fs = require("fs").promises;
|
|
5
|
-
|
|
6
|
-
class Response {
|
|
7
|
-
constructor({
|
|
8
|
-
httpResponse,
|
|
9
|
-
documentType,
|
|
10
|
-
input,
|
|
11
|
-
error,
|
|
12
|
-
reconsctruted = false,
|
|
13
|
-
...args
|
|
14
|
-
}) {
|
|
15
|
-
this.httpResponse = httpResponse;
|
|
16
|
-
this.documentType = documentType;
|
|
17
|
-
this.input = input;
|
|
18
|
-
if (!error && !reconsctruted) this.formatResponse();
|
|
19
|
-
if (reconsctruted === true) {
|
|
20
|
-
Object.assign(this, args);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
async dump(path) {
|
|
25
|
-
return await fs.writeFile(path, JSON.stringify(Object.entries(this)));
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
static async load(path) {
|
|
29
|
-
const file = fs.readFile(path);
|
|
30
|
-
const args = JSON.parse(file);
|
|
31
|
-
return new Response({ reconsctruted: true, ...args });
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
formatResponse() {
|
|
35
|
-
let http_data_document = this.httpResponse.data.document;
|
|
36
|
-
const constructors = {
|
|
37
|
-
receipt: (params) => new Receipt(params),
|
|
38
|
-
invoice: (params) => new Invoice(params),
|
|
39
|
-
financialDocument: (params) => new FinancialDocument(params),
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const predictions = http_data_document.inference.pages.entries();
|
|
43
|
-
this[`${this.documentType}s`] = [];
|
|
44
|
-
let document_words_content = [];
|
|
45
|
-
|
|
46
|
-
// Create a list of Document (Receipt, Invoice...) for each page of the input document
|
|
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
|
-
}
|
|
59
|
-
this[`${this.documentType}s`].push(
|
|
60
|
-
constructors[this.documentType]({
|
|
61
|
-
apiPrediction: prediction.prediction,
|
|
62
|
-
inputFile: this.input,
|
|
63
|
-
pageNumber: pageNumber,
|
|
64
|
-
words: page_words_content,
|
|
65
|
-
})
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// Merge the list of Document into a unique Document
|
|
70
|
-
this[this.documentType] = constructors[this.documentType]({
|
|
71
|
-
apiPrediction: http_data_document.inference.prediction,
|
|
72
|
-
inputFile: this.input,
|
|
73
|
-
pageNumber: http_data_document.n_pages,
|
|
74
|
-
level: "document",
|
|
75
|
-
words: document_words_content,
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
module.exports = Response;
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
const fs = require("fs").promises;
|
|
2
|
-
|
|
3
|
-
class Document {
|
|
4
|
-
/**
|
|
5
|
-
* Takes a list of Documents and return one Document where
|
|
6
|
-
* each field is set with the maximum probability field
|
|
7
|
-
* @param {Input} inputFile - input file given to parse the document
|
|
8
|
-
*/
|
|
9
|
-
constructor(inputFile = undefined) {
|
|
10
|
-
this.filepath = undefined;
|
|
11
|
-
this.filename = undefined;
|
|
12
|
-
this.fileExtension = undefined;
|
|
13
|
-
|
|
14
|
-
if (inputFile != undefined) {
|
|
15
|
-
this.filepath = inputFile.filepath;
|
|
16
|
-
this.filename = inputFile.filename;
|
|
17
|
-
this.fileExtension = inputFile.fileExtension;
|
|
18
|
-
}
|
|
19
|
-
this.checklist = {};
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
clone() {
|
|
23
|
-
return JSON.parse(JSON.stringify(this));
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** return true if all checklist of the document if true */
|
|
27
|
-
checkAll() {
|
|
28
|
-
return this.checklist.every((item) => item == true);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/** Export document into a JSON file */
|
|
32
|
-
async dump(path) {
|
|
33
|
-
return await fs.writeFile(path, JSON.stringify(Object.entries(this)));
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** Create a Document from a JSON file */
|
|
37
|
-
static async load(path) {
|
|
38
|
-
const file = fs.readFile(path);
|
|
39
|
-
const args = JSON.parse(file);
|
|
40
|
-
return new Document({ reconsctruted: true, ...args });
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Takes a list of Documents and return one Document where
|
|
45
|
-
* each field is set with the maximum probability field
|
|
46
|
-
* @param {Array<Document>} documents - A list of Documents
|
|
47
|
-
*/
|
|
48
|
-
static mergePages(documents) {
|
|
49
|
-
const finalDocument = documents[0].clone();
|
|
50
|
-
const attributes = Object.getOwnPropertyNames(finalDocument);
|
|
51
|
-
for (const document of documents) {
|
|
52
|
-
for (const attribute of attributes) {
|
|
53
|
-
if (Array.isArray(document?.[attribute])) {
|
|
54
|
-
finalDocument[attribute] = finalDocument[attribute]?.length
|
|
55
|
-
? finalDocument[attribute]
|
|
56
|
-
: document?.[attribute];
|
|
57
|
-
} else if (
|
|
58
|
-
document?.[attribute]?.probability >
|
|
59
|
-
finalDocument[attribute].probability
|
|
60
|
-
) {
|
|
61
|
-
finalDocument[attribute] = document?.[attribute];
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return finalDocument;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
module.exports = Document;
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
const Field = require("./field");
|
|
2
|
-
|
|
3
|
-
class Amount extends Field {
|
|
4
|
-
/**
|
|
5
|
-
* @param {Object} prediction - Prediction object from HTTP response
|
|
6
|
-
* @param {String} valueKey - Key to use in the prediction dict
|
|
7
|
-
* @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
|
|
8
|
-
* @param {Integer} pageNumber - Page number for multi pages pdf
|
|
9
|
-
*/
|
|
10
|
-
constructor({
|
|
11
|
-
prediction,
|
|
12
|
-
valueKey = "amount",
|
|
13
|
-
reconstructed = false,
|
|
14
|
-
pageNumber = 0,
|
|
15
|
-
}) {
|
|
16
|
-
super({ prediction, valueKey, reconstructed, pageNumber });
|
|
17
|
-
this.value = +parseFloat(prediction[valueKey]).toFixed(3);
|
|
18
|
-
if (isNaN(this.value)) {
|
|
19
|
-
this.value = undefined;
|
|
20
|
-
this.probability = 0;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
module.exports = Amount;
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
const Field = require("./field");
|
|
2
|
-
|
|
3
|
-
class DateField extends Field {
|
|
4
|
-
/**
|
|
5
|
-
* @param {Object} prediction - Prediction object from HTTP response
|
|
6
|
-
* @param {String} valueKey - Key to use in the prediction dict
|
|
7
|
-
* @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
|
|
8
|
-
* @param {Integer} pageNumber - Page number for multi pages pdf
|
|
9
|
-
*/
|
|
10
|
-
constructor({
|
|
11
|
-
prediction,
|
|
12
|
-
valueKey = "iso",
|
|
13
|
-
reconstructed = false,
|
|
14
|
-
pageNumber = 0,
|
|
15
|
-
}) {
|
|
16
|
-
super({ prediction, valueKey, reconstructed, pageNumber });
|
|
17
|
-
this.dateObject = new Date(this.value);
|
|
18
|
-
if (
|
|
19
|
-
!(this.dateObject instanceof Date) ||
|
|
20
|
-
isNaN(this.dateObject.valueOf())
|
|
21
|
-
) {
|
|
22
|
-
this.dateObject = undefined;
|
|
23
|
-
this.probability = 0.0;
|
|
24
|
-
this.value = undefined;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
module.exports = DateField;
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
class Field {
|
|
2
|
-
/**
|
|
3
|
-
* @param {Object} prediction - Prediction object from HTTP response
|
|
4
|
-
* @param {String} valueKey - Key to use in the prediction dict
|
|
5
|
-
* @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
|
|
6
|
-
* @param {Integer} pageNumber - Page number for multi pages pdf
|
|
7
|
-
* @param {Array<String>} extraFields - Extra fields to get from the prediction and to set as attribute of the Field
|
|
8
|
-
*/
|
|
9
|
-
constructor({
|
|
10
|
-
prediction,
|
|
11
|
-
valueKey = "value",
|
|
12
|
-
reconstructed = false,
|
|
13
|
-
extraFields,
|
|
14
|
-
pageNumber,
|
|
15
|
-
}) {
|
|
16
|
-
this.pageNumber = pageNumber;
|
|
17
|
-
this.reconstructed = reconstructed;
|
|
18
|
-
this.value = undefined;
|
|
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
|
-
this.value = prediction[valueKey];
|
|
23
|
-
if (extraFields) {
|
|
24
|
-
for (const fieldName of extraFields) {
|
|
25
|
-
this[fieldName] = prediction[fieldName];
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
compare(other) {
|
|
32
|
-
if (this.value == null && other.value == null) return true;
|
|
33
|
-
else if (this.value == null || other.value == null) return false;
|
|
34
|
-
else {
|
|
35
|
-
if (typeof this.value == "string") {
|
|
36
|
-
return this.value.toLowerCase() === other.value.toLowerCase();
|
|
37
|
-
} else {
|
|
38
|
-
return this.value === other.value;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
@param {Array<Field>} array1 - first Array of Fields
|
|
45
|
-
@param {Array<Field>} array2 - second Array of Fields
|
|
46
|
-
@param {String} attr - Attribute to compare
|
|
47
|
-
@returns {Boolean} - true if all elements in array1 exist in array2 and vice-versa, false otherwise
|
|
48
|
-
*/
|
|
49
|
-
static compareArrays(array1, array2, attr = "value") {
|
|
50
|
-
const list1 = array1.map((item) => item[attr]);
|
|
51
|
-
const list2 = array2.map((item) => item[attr]);
|
|
52
|
-
if (list1.length !== list2.length) return false;
|
|
53
|
-
for (const item1 of list1) {
|
|
54
|
-
if (!list2.includes(item1)) return false;
|
|
55
|
-
}
|
|
56
|
-
return true;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* @param {Array<Field>} array - Array of Fields
|
|
61
|
-
* @returns {Number} product of all the fields probaility
|
|
62
|
-
*/
|
|
63
|
-
static arrayProbability(array) {
|
|
64
|
-
let total = 1.0;
|
|
65
|
-
for (const field of array) {
|
|
66
|
-
total *= field.probability;
|
|
67
|
-
if (isNaN(total)) return 0.0;
|
|
68
|
-
}
|
|
69
|
-
return total;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* @param {Array<Field>} array - Array of Fields
|
|
74
|
-
* @returns {Number} Sum of all the Fields values in the array
|
|
75
|
-
*/
|
|
76
|
-
static arraySum(array) {
|
|
77
|
-
let total = 0;
|
|
78
|
-
for (const field of array) {
|
|
79
|
-
total += field.value;
|
|
80
|
-
if (isNaN(total)) return 0.0;
|
|
81
|
-
}
|
|
82
|
-
return total;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
module.exports = Field;
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
exports.field = require("./field");
|
|
2
|
-
exports.date = require("./date");
|
|
3
|
-
exports.amount = require("./amount");
|
|
4
|
-
exports.locale = require("./locale");
|
|
5
|
-
exports.orientation = require("./orientation");
|
|
6
|
-
exports.paymentDetails = require("./paymentDetails");
|
|
7
|
-
exports.tax = require("./tax.js");
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
const Field = require("./field");
|
|
2
|
-
|
|
3
|
-
class Locale extends Field {
|
|
4
|
-
/**
|
|
5
|
-
* @param {Object} prediction - Prediction object from HTTP response
|
|
6
|
-
* @param {String} valueKey - Key to use in the prediction dict
|
|
7
|
-
* @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
|
|
8
|
-
* @param {Integer} pageNumber - Page number for multi pages pdf
|
|
9
|
-
*/
|
|
10
|
-
constructor({
|
|
11
|
-
prediction,
|
|
12
|
-
valueKey = "value",
|
|
13
|
-
reconstructed = false,
|
|
14
|
-
pageNumber = 0,
|
|
15
|
-
}) {
|
|
16
|
-
super({ prediction, valueKey, reconstructed, pageNumber });
|
|
17
|
-
|
|
18
|
-
this.language = undefined;
|
|
19
|
-
this.country = undefined;
|
|
20
|
-
this.currency = undefined;
|
|
21
|
-
if ("language" in prediction && prediction.language !== "N/A")
|
|
22
|
-
this.language = prediction.language;
|
|
23
|
-
if ("country" in prediction && prediction.country !== "N/A")
|
|
24
|
-
this.country = prediction.country;
|
|
25
|
-
if ("currency" in prediction && prediction.currency !== "N/A")
|
|
26
|
-
this.currency = prediction.currency;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
module.exports = Locale;
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
const Field = require("./field");
|
|
2
|
-
|
|
3
|
-
class Orientation extends Field {
|
|
4
|
-
/**
|
|
5
|
-
* @param {Object} prediction - Prediction object from HTTP response
|
|
6
|
-
* @param {String} valueKey - Key to use in the prediction dict
|
|
7
|
-
* @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
|
|
8
|
-
* @param {Integer} pageNumber - Page number for multi pages pdf
|
|
9
|
-
*/
|
|
10
|
-
constructor({
|
|
11
|
-
prediction,
|
|
12
|
-
valueKey = "degrees",
|
|
13
|
-
reconstructed = false,
|
|
14
|
-
pageNumber = 0,
|
|
15
|
-
}) {
|
|
16
|
-
const orientations = [0, 90, 180, 270];
|
|
17
|
-
super({ prediction, valueKey, reconstructed, pageNumber });
|
|
18
|
-
this.value = parseInt(prediction[valueKey]);
|
|
19
|
-
if (isNaN(this.value)) this.probability = 0.0;
|
|
20
|
-
if (!orientations.includes(this.value)) this.value = 0;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
module.exports = Orientation;
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
const Field = require("./field");
|
|
2
|
-
|
|
3
|
-
class PaymentDetails extends Field {
|
|
4
|
-
/**
|
|
5
|
-
* @param {Object} prediction - Prediction object from HTTP response
|
|
6
|
-
* @param {String} valueKey - Key to use in the prediction dict to get the iban
|
|
7
|
-
* @param {String} accountNumberKey - Key to use to get the account number in the prediction dict
|
|
8
|
-
* @param {String} ibanKey - Key to use to get the IBAN in the prediction dict
|
|
9
|
-
* @param {String} routingNumberKey - Key to use to get the routing number in the prediction dict
|
|
10
|
-
* @param {String} swiftKey - Key to use to get the SWIFT in the prediction dict
|
|
11
|
-
* @param {Boolean} reconstructed - Does the object is reconstructed (not extracted by the API)
|
|
12
|
-
* @param {Integer} pageNumber - Page number for multi pages pdf
|
|
13
|
-
*/
|
|
14
|
-
constructor({
|
|
15
|
-
prediction,
|
|
16
|
-
valueKey = "iban",
|
|
17
|
-
accountNumberKey = "account_number",
|
|
18
|
-
ibanKey = "iban",
|
|
19
|
-
routingNumberKey = "routing_number",
|
|
20
|
-
swiftKey = "swift",
|
|
21
|
-
reconstructed = false,
|
|
22
|
-
pageNumber = 0,
|
|
23
|
-
}) {
|
|
24
|
-
super({ prediction, valueKey, reconstructed, pageNumber });
|
|
25
|
-
|
|
26
|
-
this.accountNumber = undefined;
|
|
27
|
-
this.iban = undefined;
|
|
28
|
-
this.routingNumber = undefined;
|
|
29
|
-
this.swift = undefined;
|
|
30
|
-
|
|
31
|
-
this.#setKey(prediction[accountNumberKey], "accountNumber");
|
|
32
|
-
this.#setKey(prediction[ibanKey], "iban");
|
|
33
|
-
this.#setKey(prediction[routingNumberKey], "routingNumber");
|
|
34
|
-
this.#setKey(prediction[swiftKey], "swift");
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
#setKey(value, key) {
|
|
38
|
-
if (typeof value === "string" && value !== "N/A") this[key] = value;
|
|
39
|
-
else this[key] = undefined;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
toString() {
|
|
43
|
-
let str = "";
|
|
44
|
-
const keys = ["accountNumber", "iban", "routingNumber", "swift"];
|
|
45
|
-
for (const key of keys) {
|
|
46
|
-
if (this[key]) str += `${this[key]}; `;
|
|
47
|
-
}
|
|
48
|
-
return str;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
module.exports = PaymentDetails;
|