mindee 2.0.0 → 2.1.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 +11 -1
- package/README.md +15 -11
- package/bin/mindee.d.ts +2 -0
- package/bin/mindee.js +5 -0
- package/package.json +68 -68
- package/{dist/src → src}/api/endpoint.d.ts +4 -1
- package/{dist/src → src}/api/endpoint.js +20 -21
- package/src/api/index.d.ts +2 -0
- package/{dist/src → src}/api/index.js +3 -1
- package/{dist/src → src}/api/response.d.ts +5 -2
- package/{dist/src → src}/api/response.js +19 -5
- package/{dist/src → src}/cli.d.ts +0 -0
- package/src/cli.js +125 -0
- package/{dist/src → src}/client.d.ts +1 -0
- package/{dist/src → src}/client.js +39 -46
- package/{dist/src → src}/constants.d.ts +0 -0
- package/{dist/src → src}/constants.js +6 -0
- package/src/documents/cropper.d.ts +7 -0
- package/src/documents/cropper.js +31 -0
- package/{dist/src → src}/documents/custom.d.ts +2 -2
- package/{dist/src → src}/documents/custom.js +4 -4
- package/src/documents/document.d.ts +42 -0
- package/{dist/src → src}/documents/document.js +24 -14
- package/{dist/src → src}/documents/documentConfig.d.ts +6 -2
- package/{dist/src → src}/documents/documentConfig.js +29 -39
- package/{dist/src → src}/documents/financialDocument.d.ts +2 -9
- package/{dist/src → src}/documents/financialDocument.js +14 -13
- package/{dist/src → src}/documents/index.d.ts +3 -1
- package/{dist/src → src}/documents/index.js +4 -1
- package/{dist/src → src}/documents/invoice.d.ts +2 -9
- package/{dist/src → src}/documents/invoice.js +3 -15
- package/{dist/src → src}/documents/passport.d.ts +1 -7
- package/{dist/src → src}/documents/passport.js +13 -19
- package/src/documents/receipt.d.ts +17 -0
- package/{dist/src → src}/documents/receipt.js +4 -16
- package/{dist/src → src}/errors/handler.d.ts +0 -0
- package/{dist/src → src}/errors/handler.js +0 -0
- package/{dist/src → src}/fields/amount.d.ts +0 -0
- package/{dist/src → src}/fields/amount.js +0 -0
- package/{dist/src → src}/fields/companyRegistration.d.ts +0 -0
- package/{dist/src → src}/fields/companyRegistration.js +0 -0
- package/src/fields/cropper.d.ts +16 -0
- package/src/fields/cropper.js +18 -0
- package/{dist/src → src}/fields/customDocs.d.ts +2 -2
- package/{dist/src → src}/fields/customDocs.js +4 -4
- package/{dist/src → src}/fields/date.d.ts +0 -0
- package/{dist/src → src}/fields/date.js +0 -0
- package/{dist/src → src}/fields/field.d.ts +10 -11
- package/{dist/src → src}/fields/field.js +4 -3
- package/{dist/src → src}/fields/fullText.d.ts +0 -0
- package/{dist/src → src}/fields/fullText.js +0 -0
- package/{dist/src → src}/fields/index.d.ts +4 -4
- package/{dist/src → src}/fields/index.js +6 -5
- package/{dist/src → src}/fields/locale.d.ts +0 -0
- package/{dist/src → src}/fields/locale.js +0 -0
- package/{dist/src → src}/fields/orientation.d.ts +10 -3
- package/{dist/src → src}/fields/orientation.js +9 -7
- package/{dist/src → src}/fields/paymentDetails.d.ts +0 -0
- package/{dist/src → src}/fields/paymentDetails.js +0 -0
- package/{dist/src → src}/fields/tax.d.ts +0 -0
- package/{dist/src → src}/fields/tax.js +1 -2
- package/{dist/src → src}/geometry.d.ts +0 -0
- package/{dist/src → src}/geometry.js +0 -0
- package/{dist/src → src}/index.d.ts +1 -1
- package/{dist/src → src}/index.js +2 -1
- package/{dist/src → src}/inputs.d.ts +0 -0
- package/src/inputs.js +196 -0
- package/{dist/src → src}/logger.d.ts +0 -0
- package/{dist/src → src}/logger.js +0 -0
- package/bin/mindee +0 -3
- package/dist/package.json +0 -70
- package/dist/src/api/index.d.ts +0 -2
- package/dist/src/cli.js +0 -117
- package/dist/src/documents/document.d.ts +0 -38
- package/dist/src/documents/receipt.d.ts +0 -24
- package/dist/src/inputs.js +0 -223
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.Client = void 0;
|
|
13
4
|
const inputs_1 = require("./inputs");
|
|
@@ -22,45 +13,46 @@ class DocumentClient {
|
|
|
22
13
|
this.inputDoc = inputDoc;
|
|
23
14
|
this.docConfigs = docConfigs;
|
|
24
15
|
}
|
|
25
|
-
parse(responseClass, params = {
|
|
16
|
+
async parse(responseClass, params = {
|
|
26
17
|
docType: "",
|
|
27
18
|
username: "",
|
|
28
19
|
cutPages: true,
|
|
29
20
|
fullText: false,
|
|
21
|
+
cropper: false,
|
|
30
22
|
}) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
if (found.length === 0) {
|
|
45
|
-
throw `Document type not configured: '${docType}'`;
|
|
23
|
+
// seems like there should be a better way of doing this
|
|
24
|
+
const fullText = params?.fullText !== undefined ? params.fullText : false;
|
|
25
|
+
const cropper = params?.cropper !== undefined ? params.cropper : false;
|
|
26
|
+
const cutPages = params?.cutPages !== undefined ? params.cutPages : true;
|
|
27
|
+
const docType = params.docType === undefined || params.docType === ""
|
|
28
|
+
? this.getDocType(responseClass.name)
|
|
29
|
+
: params.docType;
|
|
30
|
+
const found = [];
|
|
31
|
+
this.docConfigs.forEach((config, configKey) => {
|
|
32
|
+
if (configKey[1] === docType) {
|
|
33
|
+
found.push(configKey);
|
|
46
34
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
35
|
+
});
|
|
36
|
+
if (found.length === 0) {
|
|
37
|
+
throw `Document type not configured: '${docType}'`;
|
|
38
|
+
}
|
|
39
|
+
let configKey = [];
|
|
40
|
+
if (found.length === 1) {
|
|
41
|
+
configKey = found[0];
|
|
42
|
+
}
|
|
43
|
+
else if (params.username) {
|
|
44
|
+
configKey = [params.username, docType];
|
|
45
|
+
}
|
|
46
|
+
const docConfig = this.docConfigs.get(configKey);
|
|
47
|
+
if (docConfig === undefined) {
|
|
48
|
+
// TODO: raise error printing all usernames
|
|
49
|
+
throw `Couldn't find the config '${configKey}'`;
|
|
50
|
+
}
|
|
51
|
+
return await docConfig.predict(responseClass, {
|
|
52
|
+
inputDoc: this.inputDoc,
|
|
53
|
+
includeWords: fullText,
|
|
54
|
+
cutPages: cutPages,
|
|
55
|
+
cropper: cropper,
|
|
64
56
|
});
|
|
65
57
|
}
|
|
66
58
|
getDocType(responseClass) {
|
|
@@ -75,13 +67,13 @@ class Client {
|
|
|
75
67
|
* @param options
|
|
76
68
|
*/
|
|
77
69
|
constructor(options) {
|
|
78
|
-
const throwOnError =
|
|
79
|
-
const debug =
|
|
80
|
-
this.apiKey =
|
|
70
|
+
const throwOnError = options?.throwOnError === undefined ? true : options.throwOnError;
|
|
71
|
+
const debug = options?.debug === undefined ? false : options.debug;
|
|
72
|
+
this.apiKey = options?.apiKey === undefined ? "" : options.apiKey;
|
|
81
73
|
this.docConfigs = new Map();
|
|
82
74
|
handler_1.errorHandler.throwOnError = throwOnError;
|
|
83
75
|
logger_1.logger.level =
|
|
84
|
-
|
|
76
|
+
debug ?? process.env.MINDEE_DEBUG
|
|
85
77
|
? logger_1.LOG_LEVELS["debug"]
|
|
86
78
|
: logger_1.LOG_LEVELS["warn"];
|
|
87
79
|
logger_1.logger.debug("Client initialized");
|
|
@@ -93,6 +85,7 @@ class Client {
|
|
|
93
85
|
this.docConfigs.set([api_1.STANDARD_API_OWNER, documents_1.DOC_TYPE_RECEIPT], new documentConfig_1.ReceiptConfig(this.apiKey));
|
|
94
86
|
this.docConfigs.set([api_1.STANDARD_API_OWNER, documents_1.DOC_TYPE_FINANCIAL], new documentConfig_1.FinancialDocConfig(this.apiKey));
|
|
95
87
|
this.docConfigs.set([api_1.STANDARD_API_OWNER, documents_1.DOC_TYPE_PASSPORT], new documentConfig_1.PassportConfig(this.apiKey));
|
|
88
|
+
this.docConfigs.set([api_1.STANDARD_API_OWNER, documents_1.DOC_TYPE_CROPPER], new documentConfig_1.CropperConfig(this.apiKey));
|
|
96
89
|
}
|
|
97
90
|
addEndpoint({ accountName, documentType, version = "1", }) {
|
|
98
91
|
this.docConfigs.set([accountName, documentType], new documentConfig_1.CustomDocConfig({
|
|
File without changes
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Document, DocumentConstructorProps } from "./document";
|
|
2
|
+
import { CropperField } from "../fields";
|
|
3
|
+
export declare class Cropper extends Document {
|
|
4
|
+
cropping: CropperField[];
|
|
5
|
+
constructor({ prediction, orientation, inputFile, pageId, }: DocumentConstructorProps);
|
|
6
|
+
toString(): string;
|
|
7
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Cropper = void 0;
|
|
4
|
+
const document_1 = require("./document");
|
|
5
|
+
const fields_1 = require("../fields");
|
|
6
|
+
class Cropper extends document_1.Document {
|
|
7
|
+
constructor({ prediction, orientation = undefined, inputFile = undefined, pageId = undefined, }) {
|
|
8
|
+
super({ inputFile, pageId, orientation });
|
|
9
|
+
this.cropping = [];
|
|
10
|
+
if (pageId !== undefined) {
|
|
11
|
+
prediction.cropping.forEach((crop) => {
|
|
12
|
+
this.cropping.push(new fields_1.CropperField({
|
|
13
|
+
prediction: crop,
|
|
14
|
+
pageId: pageId,
|
|
15
|
+
}));
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
toString() {
|
|
20
|
+
const cropping = this.cropping
|
|
21
|
+
.map((crop) => crop.toString())
|
|
22
|
+
.join("\n ");
|
|
23
|
+
const outStr = `----- Cropper Data -----
|
|
24
|
+
Filename: ${this.filename}
|
|
25
|
+
Cropping: ${cropping}
|
|
26
|
+
------------------------
|
|
27
|
+
`;
|
|
28
|
+
return Cropper.cleanOutString(outStr);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.Cropper = Cropper;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Document, DocumentConstructorProps } from "./document";
|
|
2
2
|
import { ClassificationField, ListField } from "../fields";
|
|
3
|
-
import { stringDict } from "../fields
|
|
3
|
+
import { stringDict } from "../fields";
|
|
4
4
|
export interface CustomDocConstructorProps extends DocumentConstructorProps {
|
|
5
5
|
documentType: string;
|
|
6
6
|
}
|
|
@@ -8,7 +8,7 @@ export declare class CustomDocument extends Document {
|
|
|
8
8
|
fields: Map<string, ListField>;
|
|
9
9
|
classifications: Map<string, ClassificationField>;
|
|
10
10
|
readonly documentType: string;
|
|
11
|
-
constructor({ inputFile,
|
|
11
|
+
constructor({ inputFile, prediction, extras, orientation, pageId, documentType, }: CustomDocConstructorProps);
|
|
12
12
|
protected setField(fieldName: string, apiPrediction: stringDict, pageId: number | undefined): void;
|
|
13
13
|
toString(): string;
|
|
14
14
|
}
|
|
@@ -4,13 +4,13 @@ exports.CustomDocument = void 0;
|
|
|
4
4
|
const document_1 = require("./document");
|
|
5
5
|
const fields_1 = require("../fields");
|
|
6
6
|
class CustomDocument extends document_1.Document {
|
|
7
|
-
constructor({ inputFile,
|
|
8
|
-
super(inputFile, pageId);
|
|
7
|
+
constructor({ inputFile, prediction, extras = undefined, orientation = undefined, pageId, documentType, }) {
|
|
8
|
+
super({ inputFile, pageId, orientation, extras });
|
|
9
9
|
this.fields = new Map();
|
|
10
10
|
this.classifications = new Map();
|
|
11
11
|
this.documentType = documentType;
|
|
12
|
-
Object.keys(
|
|
13
|
-
this.setField(fieldName,
|
|
12
|
+
Object.keys(prediction).forEach((fieldName) => {
|
|
13
|
+
this.setField(fieldName, prediction, pageId);
|
|
14
14
|
});
|
|
15
15
|
}
|
|
16
16
|
setField(fieldName, apiPrediction, pageId) {
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Input } from "../inputs";
|
|
2
|
+
import { CropperField, FullText, OrientationField, stringDict } from "../fields";
|
|
3
|
+
export interface DocumentConstructorProps extends BaseDocumentConstructorProps {
|
|
4
|
+
/** JSON parsed prediction from HTTP response */
|
|
5
|
+
prediction: stringDict;
|
|
6
|
+
}
|
|
7
|
+
interface BaseDocumentConstructorProps {
|
|
8
|
+
/** Orientation JSON for page-level document */
|
|
9
|
+
orientation?: stringDict;
|
|
10
|
+
/** Extras JSON */
|
|
11
|
+
extras?: stringDict;
|
|
12
|
+
/** input file given to parse the document */
|
|
13
|
+
inputFile?: Input;
|
|
14
|
+
/** Page ID for page-level document */
|
|
15
|
+
pageId?: number;
|
|
16
|
+
/** full OCR extracted text */
|
|
17
|
+
fullText?: FullText;
|
|
18
|
+
}
|
|
19
|
+
export declare class Document {
|
|
20
|
+
checklist: {
|
|
21
|
+
[index: string]: boolean;
|
|
22
|
+
};
|
|
23
|
+
mimeType?: string;
|
|
24
|
+
filename: string;
|
|
25
|
+
filepath?: string;
|
|
26
|
+
fullText?: FullText;
|
|
27
|
+
pageId?: number | undefined;
|
|
28
|
+
orientation?: OrientationField;
|
|
29
|
+
cropper: CropperField[];
|
|
30
|
+
constructor({ orientation, extras, inputFile, fullText, pageId, }: BaseDocumentConstructorProps);
|
|
31
|
+
clone(): any;
|
|
32
|
+
/** return true if all checklist of the document if true */
|
|
33
|
+
checkAll(): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Takes a list of Documents and return one Document where
|
|
36
|
+
* each field is set with the maximum probability field
|
|
37
|
+
* @param {Array<Document>} documents - A list of Documents
|
|
38
|
+
*/
|
|
39
|
+
static mergePages(documents: any): any;
|
|
40
|
+
static cleanOutString(outStr: string): string;
|
|
41
|
+
}
|
|
42
|
+
export {};
|
|
@@ -1,18 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Document = void 0;
|
|
4
|
+
const fields_1 = require("../fields");
|
|
4
5
|
class Document {
|
|
5
|
-
|
|
6
|
-
* Takes a list of Documents and return one Document where
|
|
7
|
-
* each field is set with the maximum probability field
|
|
8
|
-
* @param {Input} inputFile - input file given to parse the document
|
|
9
|
-
* @param {number} pageId - Page ID for multi-page document
|
|
10
|
-
* @param {FullText} fullText - full OCR extracted text
|
|
11
|
-
*/
|
|
12
|
-
constructor(inputFile, pageId, fullText) {
|
|
6
|
+
constructor({ orientation = undefined, extras = undefined, inputFile = undefined, fullText = undefined, pageId = undefined, }) {
|
|
13
7
|
this.filename = "";
|
|
8
|
+
this.cropper = [];
|
|
14
9
|
this.filepath = undefined;
|
|
15
10
|
this.pageId = pageId;
|
|
11
|
+
if (pageId !== undefined && orientation !== undefined) {
|
|
12
|
+
this.orientation = new fields_1.OrientationField({
|
|
13
|
+
prediction: orientation,
|
|
14
|
+
pageId: pageId,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
if (extras !== undefined) {
|
|
18
|
+
if (extras.cropper !== undefined) {
|
|
19
|
+
extras.cropper.cropping.forEach((crop) => {
|
|
20
|
+
this.cropper.push(new fields_1.CropperField({
|
|
21
|
+
prediction: crop,
|
|
22
|
+
pageId: pageId,
|
|
23
|
+
}));
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
16
27
|
if (inputFile !== undefined) {
|
|
17
28
|
this.filepath = inputFile.filepath;
|
|
18
29
|
this.filename = inputFile.filename;
|
|
@@ -34,19 +45,18 @@ class Document {
|
|
|
34
45
|
* @param {Array<Document>} documents - A list of Documents
|
|
35
46
|
*/
|
|
36
47
|
static mergePages(documents) {
|
|
37
|
-
var _a, _b;
|
|
38
48
|
const finalDocument = documents[0].clone();
|
|
39
49
|
const attributes = Object.getOwnPropertyNames(finalDocument);
|
|
40
50
|
for (const document of documents) {
|
|
41
51
|
for (const attribute of attributes) {
|
|
42
|
-
if (Array.isArray(document
|
|
43
|
-
finalDocument[attribute] =
|
|
52
|
+
if (Array.isArray(document?.[attribute])) {
|
|
53
|
+
finalDocument[attribute] = finalDocument[attribute]?.length
|
|
44
54
|
? finalDocument[attribute]
|
|
45
|
-
: document
|
|
55
|
+
: document?.[attribute];
|
|
46
56
|
}
|
|
47
|
-
else if (
|
|
57
|
+
else if (document?.[attribute]?.confidence >
|
|
48
58
|
finalDocument[attribute].confidence) {
|
|
49
|
-
finalDocument[attribute] = document
|
|
59
|
+
finalDocument[attribute] = document?.[attribute];
|
|
50
60
|
}
|
|
51
61
|
}
|
|
52
62
|
}
|
|
@@ -15,12 +15,13 @@ export declare class DocumentConfig {
|
|
|
15
15
|
readonly documentType: string;
|
|
16
16
|
readonly endpoints: Array<Endpoint>;
|
|
17
17
|
constructor(documentType: string, endpoints: Array<Endpoint>);
|
|
18
|
-
predictRequest(inputDoc: Input, includeWords
|
|
18
|
+
predictRequest(inputDoc: Input, includeWords: boolean, cropping: boolean): Promise<unknown>;
|
|
19
19
|
buildResult<RespType extends Response<Document>>(responseType: responseSig<RespType>, inputFile: Input, response: any): RespType;
|
|
20
20
|
predict<RespType extends Response<Document>>(responseType: responseSig<RespType>, params: {
|
|
21
21
|
inputDoc: Input;
|
|
22
22
|
includeWords: boolean;
|
|
23
23
|
cutPages: boolean;
|
|
24
|
+
cropper: boolean;
|
|
24
25
|
}): Promise<RespType>;
|
|
25
26
|
cutDocPages(inputDoc: Input, cutPages: boolean): Promise<void>;
|
|
26
27
|
protected checkApiKeys(): void;
|
|
@@ -33,11 +34,14 @@ export declare class ReceiptConfig extends DocumentConfig {
|
|
|
33
34
|
}
|
|
34
35
|
export declare class FinancialDocConfig extends DocumentConfig {
|
|
35
36
|
constructor(apiKey: string);
|
|
36
|
-
predictRequest(inputDoc: Input, includeWords
|
|
37
|
+
predictRequest(inputDoc: Input, includeWords: boolean, cropping: boolean): Promise<unknown>;
|
|
37
38
|
}
|
|
38
39
|
export declare class PassportConfig extends DocumentConfig {
|
|
39
40
|
constructor(apiKey: string);
|
|
40
41
|
}
|
|
42
|
+
export declare class CropperConfig extends DocumentConfig {
|
|
43
|
+
constructor(apiKey: string);
|
|
44
|
+
}
|
|
41
45
|
export declare class CustomDocConfig extends DocumentConfig {
|
|
42
46
|
constructor({ documentType, accountName, version, apiKey, }: CustomDocConstructor);
|
|
43
47
|
}
|
|
@@ -1,15 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.CustomDocConfig = exports.PassportConfig = exports.FinancialDocConfig = exports.ReceiptConfig = exports.InvoiceConfig = exports.DocumentConfig = void 0;
|
|
3
|
+
exports.CustomDocConfig = exports.CropperConfig = exports.PassportConfig = exports.FinancialDocConfig = exports.ReceiptConfig = exports.InvoiceConfig = exports.DocumentConfig = void 0;
|
|
13
4
|
const api_1 = require("../api");
|
|
14
5
|
const index_1 = require("./index");
|
|
15
6
|
const handler_1 = require("../errors/handler");
|
|
@@ -18,10 +9,8 @@ class DocumentConfig {
|
|
|
18
9
|
this.documentType = documentType;
|
|
19
10
|
this.endpoints = endpoints;
|
|
20
11
|
}
|
|
21
|
-
predictRequest(inputDoc, includeWords
|
|
22
|
-
return
|
|
23
|
-
return yield this.endpoints[0].predictRequest(inputDoc, includeWords);
|
|
24
|
-
});
|
|
12
|
+
async predictRequest(inputDoc, includeWords, cropping) {
|
|
13
|
+
return await this.endpoints[0].predictRequest(inputDoc, includeWords, cropping);
|
|
25
14
|
}
|
|
26
15
|
buildResult(responseType, inputFile, response) {
|
|
27
16
|
if (response.statusCode > 201) {
|
|
@@ -41,21 +30,17 @@ class DocumentConfig {
|
|
|
41
30
|
error: false,
|
|
42
31
|
});
|
|
43
32
|
}
|
|
44
|
-
predict(responseType, params) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return this.buildResult(responseType, params.inputDoc, response);
|
|
51
|
-
});
|
|
33
|
+
async predict(responseType, params) {
|
|
34
|
+
this.checkApiKeys();
|
|
35
|
+
await params.inputDoc.init();
|
|
36
|
+
await this.cutDocPages(params.inputDoc, params.cutPages);
|
|
37
|
+
const response = await this.predictRequest(params.inputDoc, params.includeWords, params.cropper);
|
|
38
|
+
return this.buildResult(responseType, params.inputDoc, response);
|
|
52
39
|
}
|
|
53
|
-
cutDocPages(inputDoc, cutPages) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
});
|
|
40
|
+
async cutDocPages(inputDoc, cutPages) {
|
|
41
|
+
if (cutPages && inputDoc.isPdf()) {
|
|
42
|
+
await inputDoc.cutPdf();
|
|
43
|
+
}
|
|
59
44
|
}
|
|
60
45
|
checkApiKeys() {
|
|
61
46
|
this.endpoints.forEach((endpoint) => {
|
|
@@ -89,17 +74,15 @@ class FinancialDocConfig extends DocumentConfig {
|
|
|
89
74
|
];
|
|
90
75
|
super(index_1.DOC_TYPE_FINANCIAL, endpoints);
|
|
91
76
|
}
|
|
92
|
-
predictRequest(inputDoc, includeWords
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
return yield endpoint.predictRequest(inputDoc, includeWords);
|
|
102
|
-
});
|
|
77
|
+
async predictRequest(inputDoc, includeWords, cropping) {
|
|
78
|
+
let endpoint;
|
|
79
|
+
if (inputDoc.isPdf()) {
|
|
80
|
+
endpoint = this.endpoints[0];
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
endpoint = this.endpoints[1];
|
|
84
|
+
}
|
|
85
|
+
return await endpoint.predictRequest(inputDoc, includeWords, cropping);
|
|
103
86
|
}
|
|
104
87
|
}
|
|
105
88
|
exports.FinancialDocConfig = FinancialDocConfig;
|
|
@@ -110,6 +93,13 @@ class PassportConfig extends DocumentConfig {
|
|
|
110
93
|
}
|
|
111
94
|
}
|
|
112
95
|
exports.PassportConfig = PassportConfig;
|
|
96
|
+
class CropperConfig extends DocumentConfig {
|
|
97
|
+
constructor(apiKey) {
|
|
98
|
+
const endpoints = [new api_1.CropperEndpoint(apiKey)];
|
|
99
|
+
super(index_1.DOC_TYPE_CROPPER, endpoints);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
exports.CropperConfig = CropperConfig;
|
|
113
103
|
class CustomDocConfig extends DocumentConfig {
|
|
114
104
|
constructor({ documentType, accountName, version, apiKey, }) {
|
|
115
105
|
const endpoints = [
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Document, DocumentConstructorProps } from "./document";
|
|
2
|
-
import { TaxField, Field, Amount, Locale,
|
|
2
|
+
import { TaxField, Field, Amount, Locale, DateField as Date, CompanyRegistration } from "../fields";
|
|
3
3
|
export declare class FinancialDocument extends Document {
|
|
4
4
|
#private;
|
|
5
5
|
pageId: number | undefined;
|
|
@@ -9,7 +9,6 @@ export declare class FinancialDocument extends Document {
|
|
|
9
9
|
dueDate: Date;
|
|
10
10
|
category: Field;
|
|
11
11
|
time: Field;
|
|
12
|
-
orientation: Orientation | undefined;
|
|
13
12
|
taxes: TaxField[];
|
|
14
13
|
totalTax: Amount;
|
|
15
14
|
totalExcl: Amount;
|
|
@@ -21,12 +20,6 @@ export declare class FinancialDocument extends Document {
|
|
|
21
20
|
customerAddress: Field;
|
|
22
21
|
paymentDetails: Field[];
|
|
23
22
|
customerCompanyRegistration: CompanyRegistration[];
|
|
24
|
-
|
|
25
|
-
* @param {Object} apiPrediction - Json parsed prediction from HTTP response
|
|
26
|
-
* @param {Input} inputFile - input file given to parse the document
|
|
27
|
-
* @param {number} pageId - Page ID for multi-page document
|
|
28
|
-
* @param {FullText} fullText - full OCR extracted text
|
|
29
|
-
*/
|
|
30
|
-
constructor({ apiPrediction, inputFile, fullText, pageId, }: DocumentConstructorProps);
|
|
23
|
+
constructor({ prediction, orientation, extras, inputFile, fullText, pageId, }: DocumentConstructorProps);
|
|
31
24
|
toString(): string;
|
|
32
25
|
}
|
|
@@ -12,20 +12,14 @@ const invoice_1 = require("./invoice");
|
|
|
12
12
|
const receipt_1 = require("./receipt");
|
|
13
13
|
const fields_1 = require("../fields");
|
|
14
14
|
class FinancialDocument extends document_1.Document {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
* @param {Input} inputFile - input file given to parse the document
|
|
18
|
-
* @param {number} pageId - Page ID for multi-page document
|
|
19
|
-
* @param {FullText} fullText - full OCR extracted text
|
|
20
|
-
*/
|
|
21
|
-
constructor({ apiPrediction, inputFile = undefined, fullText = undefined, pageId = undefined, }) {
|
|
22
|
-
super(inputFile, pageId, fullText);
|
|
15
|
+
constructor({ prediction, orientation = undefined, extras = undefined, inputFile = undefined, fullText = undefined, pageId = undefined, }) {
|
|
16
|
+
super({ inputFile, pageId, fullText, orientation, extras });
|
|
23
17
|
_FinancialDocument_instances.add(this);
|
|
24
18
|
this.taxes = [];
|
|
25
19
|
this.companyRegistration = [];
|
|
26
20
|
this.paymentDetails = [];
|
|
27
21
|
this.customerCompanyRegistration = [];
|
|
28
|
-
__classPrivateFieldGet(this, _FinancialDocument_instances, "m", _FinancialDocument_initFromApiPrediction).call(this,
|
|
22
|
+
__classPrivateFieldGet(this, _FinancialDocument_instances, "m", _FinancialDocument_initFromApiPrediction).call(this, prediction, inputFile, pageId, orientation, extras);
|
|
29
23
|
__classPrivateFieldGet(this, _FinancialDocument_instances, "m", _FinancialDocument_checklist).call(this);
|
|
30
24
|
}
|
|
31
25
|
toString() {
|
|
@@ -41,12 +35,14 @@ Total taxes: ${this.totalTax.value}
|
|
|
41
35
|
}
|
|
42
36
|
}
|
|
43
37
|
exports.FinancialDocument = FinancialDocument;
|
|
44
|
-
_FinancialDocument_instances = new WeakSet(), _FinancialDocument_initFromApiPrediction = function _FinancialDocument_initFromApiPrediction(prediction, inputFile, pageNumber) {
|
|
38
|
+
_FinancialDocument_instances = new WeakSet(), _FinancialDocument_initFromApiPrediction = function _FinancialDocument_initFromApiPrediction(prediction, inputFile, pageNumber, orientation, extras) {
|
|
45
39
|
if (Object.keys(prediction).includes("invoice_number")) {
|
|
46
40
|
const invoice = new invoice_1.Invoice({
|
|
47
|
-
|
|
41
|
+
prediction: prediction,
|
|
48
42
|
inputFile,
|
|
49
43
|
pageId: pageNumber,
|
|
44
|
+
orientation: orientation,
|
|
45
|
+
extras: extras,
|
|
50
46
|
});
|
|
51
47
|
this.locale = invoice.locale;
|
|
52
48
|
this.totalIncl = invoice.totalIncl;
|
|
@@ -70,9 +66,11 @@ _FinancialDocument_instances = new WeakSet(), _FinancialDocument_initFromApiPred
|
|
|
70
66
|
}
|
|
71
67
|
else {
|
|
72
68
|
const receipt = new receipt_1.Receipt({
|
|
73
|
-
|
|
69
|
+
prediction: prediction,
|
|
74
70
|
inputFile,
|
|
75
71
|
pageId: pageNumber,
|
|
72
|
+
orientation: orientation,
|
|
73
|
+
extras: extras,
|
|
76
74
|
});
|
|
77
75
|
this.orientation = receipt.orientation;
|
|
78
76
|
this.date = receipt.date;
|
|
@@ -121,7 +119,10 @@ _FinancialDocument_instances = new WeakSet(), _FinancialDocument_initFromApiPred
|
|
|
121
119
|
const eps = 1 / (100 * totalVat);
|
|
122
120
|
if (this.totalIncl.value * (1 - eps) - 0.02 <= reconstructedTotal &&
|
|
123
121
|
reconstructedTotal <= this.totalIncl.value * (1 + eps) + 0.02) {
|
|
124
|
-
this.taxes = this.taxes.map((tax) => (
|
|
122
|
+
this.taxes = this.taxes.map((tax) => ({
|
|
123
|
+
...tax,
|
|
124
|
+
confidence: 1.0,
|
|
125
|
+
}));
|
|
125
126
|
this.totalTax.confidence = 1.0;
|
|
126
127
|
this.totalIncl.confidence = 1.0;
|
|
127
128
|
return true;
|
|
@@ -3,10 +3,12 @@ import { Receipt } from "./receipt";
|
|
|
3
3
|
import { Passport } from "./passport";
|
|
4
4
|
import { FinancialDocument } from "./financialDocument";
|
|
5
5
|
import { CustomDocument } from "./custom";
|
|
6
|
+
import { Cropper } from "./cropper";
|
|
6
7
|
export { Document, DocumentConstructorProps } from "./document";
|
|
7
|
-
export { Receipt, Invoice, FinancialDocument, Passport, CustomDocument };
|
|
8
|
+
export { Receipt, Invoice, FinancialDocument, Passport, CustomDocument, Cropper, };
|
|
8
9
|
export declare const DOC_TYPE_INVOICE: string;
|
|
9
10
|
export declare const DOC_TYPE_RECEIPT: string;
|
|
10
11
|
export declare const DOC_TYPE_PASSPORT: string;
|
|
11
12
|
export declare const DOC_TYPE_FINANCIAL: string;
|
|
12
13
|
export declare const DOC_TYPE_CUSTOM: string;
|
|
14
|
+
export declare const DOC_TYPE_CROPPER: string;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DOC_TYPE_CUSTOM = exports.DOC_TYPE_FINANCIAL = exports.DOC_TYPE_PASSPORT = exports.DOC_TYPE_RECEIPT = exports.DOC_TYPE_INVOICE = exports.CustomDocument = exports.Passport = exports.FinancialDocument = exports.Invoice = exports.Receipt = exports.Document = void 0;
|
|
3
|
+
exports.DOC_TYPE_CROPPER = exports.DOC_TYPE_CUSTOM = exports.DOC_TYPE_FINANCIAL = exports.DOC_TYPE_PASSPORT = exports.DOC_TYPE_RECEIPT = exports.DOC_TYPE_INVOICE = exports.Cropper = exports.CustomDocument = exports.Passport = exports.FinancialDocument = exports.Invoice = exports.Receipt = exports.Document = void 0;
|
|
4
4
|
const invoice_1 = require("./invoice");
|
|
5
5
|
Object.defineProperty(exports, "Invoice", { enumerable: true, get: function () { return invoice_1.Invoice; } });
|
|
6
6
|
const receipt_1 = require("./receipt");
|
|
@@ -11,6 +11,8 @@ const financialDocument_1 = require("./financialDocument");
|
|
|
11
11
|
Object.defineProperty(exports, "FinancialDocument", { enumerable: true, get: function () { return financialDocument_1.FinancialDocument; } });
|
|
12
12
|
const custom_1 = require("./custom");
|
|
13
13
|
Object.defineProperty(exports, "CustomDocument", { enumerable: true, get: function () { return custom_1.CustomDocument; } });
|
|
14
|
+
const cropper_1 = require("./cropper");
|
|
15
|
+
Object.defineProperty(exports, "Cropper", { enumerable: true, get: function () { return cropper_1.Cropper; } });
|
|
14
16
|
var document_1 = require("./document");
|
|
15
17
|
Object.defineProperty(exports, "Document", { enumerable: true, get: function () { return document_1.Document; } });
|
|
16
18
|
exports.DOC_TYPE_INVOICE = invoice_1.Invoice.name;
|
|
@@ -18,3 +20,4 @@ exports.DOC_TYPE_RECEIPT = receipt_1.Receipt.name;
|
|
|
18
20
|
exports.DOC_TYPE_PASSPORT = passport_1.Passport.name;
|
|
19
21
|
exports.DOC_TYPE_FINANCIAL = financialDocument_1.FinancialDocument.name;
|
|
20
22
|
exports.DOC_TYPE_CUSTOM = custom_1.CustomDocument.name;
|
|
23
|
+
exports.DOC_TYPE_CROPPER = cropper_1.Cropper.name;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Document, DocumentConstructorProps } from "./document";
|
|
2
|
-
import { BaseField, TaxField, PaymentDetails,
|
|
2
|
+
import { BaseField, TaxField, PaymentDetails, Locale, Amount, Field, DateField, CompanyRegistration } from "../fields";
|
|
3
3
|
export declare class Invoice extends Document {
|
|
4
4
|
#private;
|
|
5
5
|
locale: Locale;
|
|
@@ -8,7 +8,6 @@ export declare class Invoice extends Document {
|
|
|
8
8
|
date: DateField;
|
|
9
9
|
dueDate: DateField;
|
|
10
10
|
time: Field;
|
|
11
|
-
orientation: Orientation;
|
|
12
11
|
totalTax: Amount;
|
|
13
12
|
totalExcl: Amount;
|
|
14
13
|
supplier: Field;
|
|
@@ -20,13 +19,7 @@ export declare class Invoice extends Document {
|
|
|
20
19
|
taxes: TaxField[];
|
|
21
20
|
paymentDetails: PaymentDetails[];
|
|
22
21
|
customerCompanyRegistration: CompanyRegistration[];
|
|
23
|
-
|
|
24
|
-
* @param {Object} apiPrediction - Json parsed prediction from HTTP response
|
|
25
|
-
* @param {Input} inputFile - input file given to parse the document
|
|
26
|
-
* @param {number} pageId - Page ID for multi-page document
|
|
27
|
-
* @param {FullText} fullText - full OCR extracted text
|
|
28
|
-
*/
|
|
29
|
-
constructor({ apiPrediction, inputFile, fullText, pageId, }: DocumentConstructorProps);
|
|
22
|
+
constructor({ prediction, orientation, extras, inputFile, fullText, pageId, }: DocumentConstructorProps);
|
|
30
23
|
toString(): string;
|
|
31
24
|
private taxesMatchTotalIncl;
|
|
32
25
|
/**
|