@aws-amplify/predictions 5.5.6-api-v6-models.b3abc9b.0 → 6.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/README.md +3 -0
- package/lib/Predictions.d.ts +11 -45
- package/lib/Predictions.js +24 -132
- package/lib/errors/PredictionsError.d.ts +4 -0
- package/lib/errors/PredictionsError.js +15 -0
- package/lib/errors/types/validation.d.ts +14 -0
- package/lib/errors/types/validation.js +50 -0
- package/lib/errors/utils/assertValidationError.d.ts +2 -0
- package/lib/errors/utils/assertValidationError.js +14 -0
- package/lib/index.d.ts +2 -4
- package/lib/index.js +2 -9
- package/{lib-esm/Providers → lib/providers}/AmazonAIConvertPredictionsProvider.d.ts +6 -7
- package/lib/providers/AmazonAIConvertPredictionsProvider.js +307 -0
- package/lib/{Providers → providers}/AmazonAIIdentifyPredictionsProvider.d.ts +5 -6
- package/lib/providers/AmazonAIIdentifyPredictionsProvider.js +334 -0
- package/lib/{Providers → providers}/AmazonAIInterpretPredictionsProvider.d.ts +3 -4
- package/lib/providers/AmazonAIInterpretPredictionsProvider.js +208 -0
- package/lib/providers/IdentifyTextUtils.d.ts +16 -0
- package/lib/{Providers → providers}/IdentifyTextUtils.js +97 -148
- package/lib/{Providers → providers}/Utils.d.ts +2 -2
- package/lib/{Providers → providers}/Utils.js +11 -12
- package/lib/{Providers → providers}/index.d.ts +1 -2
- package/lib/providers/index.js +11 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/types/AWSTypes.d.ts +8 -9
- package/lib/types/AWSTypes.js +0 -1
- package/lib/types/Predictions.d.ts +63 -59
- package/lib/types/Predictions.js +41 -28
- package/lib/types/index.js +1 -2
- package/lib-esm/Predictions.d.ts +11 -45
- package/lib-esm/Predictions.js +23 -133
- package/lib-esm/errors/PredictionsError.d.ts +4 -0
- package/lib-esm/errors/PredictionsError.js +11 -0
- package/lib-esm/errors/types/validation.d.ts +14 -0
- package/lib-esm/errors/types/validation.js +47 -0
- package/lib-esm/errors/utils/assertValidationError.d.ts +2 -0
- package/lib-esm/errors/utils/assertValidationError.js +10 -0
- package/lib-esm/index.d.ts +2 -4
- package/lib-esm/index.js +1 -5
- package/{lib/Providers → lib-esm/providers}/AmazonAIConvertPredictionsProvider.d.ts +6 -7
- package/lib-esm/providers/AmazonAIConvertPredictionsProvider.js +303 -0
- package/lib-esm/{Providers → providers}/AmazonAIIdentifyPredictionsProvider.d.ts +5 -6
- package/lib-esm/providers/AmazonAIIdentifyPredictionsProvider.js +330 -0
- package/lib-esm/{Providers → providers}/AmazonAIInterpretPredictionsProvider.d.ts +3 -4
- package/lib-esm/providers/AmazonAIInterpretPredictionsProvider.js +204 -0
- package/lib-esm/providers/IdentifyTextUtils.d.ts +16 -0
- package/lib-esm/providers/IdentifyTextUtils.js +264 -0
- package/lib-esm/{Providers → providers}/Utils.d.ts +2 -2
- package/lib-esm/{Providers → providers}/Utils.js +10 -12
- package/lib-esm/{Providers → providers}/index.d.ts +1 -2
- package/lib-esm/{Providers → providers}/index.js +1 -3
- package/lib-esm/tsconfig.tsbuildinfo +1 -0
- package/lib-esm/types/AWSTypes.d.ts +8 -9
- package/lib-esm/types/AWSTypes.js +1 -1
- package/lib-esm/types/Predictions.d.ts +63 -59
- package/lib-esm/types/Predictions.js +34 -27
- package/lib-esm/types/index.js +0 -1
- package/package.json +39 -48
- package/src/Predictions.ts +33 -220
- package/src/errors/PredictionsError.ts +16 -0
- package/src/errors/types/validation.ts +51 -0
- package/src/errors/utils/assertValidationError.ts +18 -0
- package/src/index.ts +16 -16
- package/src/{Providers → providers}/AmazonAIConvertPredictionsProvider.ts +210 -171
- package/src/{Providers → providers}/AmazonAIIdentifyPredictionsProvider.ts +244 -246
- package/src/providers/AmazonAIInterpretPredictionsProvider.ts +293 -0
- package/src/{Providers → providers}/IdentifyTextUtils.ts +69 -52
- package/src/{Providers → providers}/Utils.ts +2 -2
- package/src/{Providers → providers}/index.ts +0 -2
- package/src/types/AWSTypes.ts +5 -6
- package/src/types/Predictions.ts +107 -62
- package/lib/.tsbuildinfo +0 -3
- package/lib/Predictions.js.map +0 -1
- package/lib/Providers/AmazonAIConvertPredictionsProvider.js +0 -377
- package/lib/Providers/AmazonAIConvertPredictionsProvider.js.map +0 -1
- package/lib/Providers/AmazonAIIdentifyPredictionsProvider.js +0 -441
- package/lib/Providers/AmazonAIIdentifyPredictionsProvider.js.map +0 -1
- package/lib/Providers/AmazonAIInterpretPredictionsProvider.js +0 -321
- package/lib/Providers/AmazonAIInterpretPredictionsProvider.js.map +0 -1
- package/lib/Providers/AmazonAIPredictionsProvider.d.ts +0 -14
- package/lib/Providers/AmazonAIPredictionsProvider.js +0 -43
- package/lib/Providers/AmazonAIPredictionsProvider.js.map +0 -1
- package/lib/Providers/IdentifyTextUtils.d.ts +0 -40
- package/lib/Providers/IdentifyTextUtils.js.map +0 -1
- package/lib/Providers/Utils.js.map +0 -1
- package/lib/Providers/index.js +0 -13
- package/lib/Providers/index.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/types/AWSTypes.js.map +0 -1
- package/lib/types/Predictions.js.map +0 -1
- package/lib/types/Providers/AbstractConvertPredictionsProvider.d.ts +0 -9
- package/lib/types/Providers/AbstractConvertPredictionsProvider.js +0 -44
- package/lib/types/Providers/AbstractConvertPredictionsProvider.js.map +0 -1
- package/lib/types/Providers/AbstractIdentifyPredictionsProvider.d.ts +0 -9
- package/lib/types/Providers/AbstractIdentifyPredictionsProvider.js +0 -44
- package/lib/types/Providers/AbstractIdentifyPredictionsProvider.js.map +0 -1
- package/lib/types/Providers/AbstractInterpretPredictionsProvider.d.ts +0 -7
- package/lib/types/Providers/AbstractInterpretPredictionsProvider.js +0 -27
- package/lib/types/Providers/AbstractInterpretPredictionsProvider.js.map +0 -1
- package/lib/types/Providers/AbstractPredictionsProvider.d.ts +0 -7
- package/lib/types/Providers/AbstractPredictionsProvider.js +0 -16
- package/lib/types/Providers/AbstractPredictionsProvider.js.map +0 -1
- package/lib/types/Providers/index.d.ts +0 -4
- package/lib/types/Providers/index.js +0 -10
- package/lib/types/Providers/index.js.map +0 -1
- package/lib/types/index.js.map +0 -1
- package/lib-esm/.tsbuildinfo +0 -3
- package/lib-esm/Predictions.js.map +0 -1
- package/lib-esm/Providers/AmazonAIConvertPredictionsProvider.js +0 -375
- package/lib-esm/Providers/AmazonAIConvertPredictionsProvider.js.map +0 -1
- package/lib-esm/Providers/AmazonAIIdentifyPredictionsProvider.js +0 -439
- package/lib-esm/Providers/AmazonAIIdentifyPredictionsProvider.js.map +0 -1
- package/lib-esm/Providers/AmazonAIInterpretPredictionsProvider.js +0 -319
- package/lib-esm/Providers/AmazonAIInterpretPredictionsProvider.js.map +0 -1
- package/lib-esm/Providers/AmazonAIPredictionsProvider.d.ts +0 -14
- package/lib-esm/Providers/AmazonAIPredictionsProvider.js +0 -41
- package/lib-esm/Providers/AmazonAIPredictionsProvider.js.map +0 -1
- package/lib-esm/Providers/IdentifyTextUtils.d.ts +0 -40
- package/lib-esm/Providers/IdentifyTextUtils.js +0 -313
- package/lib-esm/Providers/IdentifyTextUtils.js.map +0 -1
- package/lib-esm/Providers/Utils.js.map +0 -1
- package/lib-esm/Providers/index.js.map +0 -1
- package/lib-esm/index.js.map +0 -1
- package/lib-esm/types/AWSTypes.js.map +0 -1
- package/lib-esm/types/Predictions.js.map +0 -1
- package/lib-esm/types/Providers/AbstractConvertPredictionsProvider.d.ts +0 -9
- package/lib-esm/types/Providers/AbstractConvertPredictionsProvider.js +0 -42
- package/lib-esm/types/Providers/AbstractConvertPredictionsProvider.js.map +0 -1
- package/lib-esm/types/Providers/AbstractIdentifyPredictionsProvider.d.ts +0 -9
- package/lib-esm/types/Providers/AbstractIdentifyPredictionsProvider.js +0 -42
- package/lib-esm/types/Providers/AbstractIdentifyPredictionsProvider.js.map +0 -1
- package/lib-esm/types/Providers/AbstractInterpretPredictionsProvider.d.ts +0 -7
- package/lib-esm/types/Providers/AbstractInterpretPredictionsProvider.js +0 -25
- package/lib-esm/types/Providers/AbstractInterpretPredictionsProvider.js.map +0 -1
- package/lib-esm/types/Providers/AbstractPredictionsProvider.d.ts +0 -7
- package/lib-esm/types/Providers/AbstractPredictionsProvider.js +0 -14
- package/lib-esm/types/Providers/AbstractPredictionsProvider.js.map +0 -1
- package/lib-esm/types/Providers/index.d.ts +0 -4
- package/lib-esm/types/Providers/index.js +0 -7
- package/lib-esm/types/Providers/index.js.map +0 -1
- package/lib-esm/types/index.js.map +0 -1
- package/src/Providers/AmazonAIInterpretPredictionsProvider.ts +0 -281
- package/src/Providers/AmazonAIPredictionsProvider.ts +0 -69
- package/src/types/Providers/AbstractConvertPredictionsProvider.ts +0 -55
- package/src/types/Providers/AbstractIdentifyPredictionsProvider.ts +0 -57
- package/src/types/Providers/AbstractInterpretPredictionsProvider.ts +0 -26
- package/src/types/Providers/AbstractPredictionsProvider.ts +0 -20
- package/src/types/Providers/index.ts +0 -6
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AmazonAIInterpretPredictionsProvider = void 0;
|
|
4
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
5
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
6
|
+
const core_1 = require("@aws-amplify/core");
|
|
7
|
+
const utils_1 = require("@aws-amplify/core/internals/utils");
|
|
8
|
+
const client_comprehend_1 = require("@aws-sdk/client-comprehend");
|
|
9
|
+
const validation_1 = require("../errors/types/validation");
|
|
10
|
+
const assertValidationError_1 = require("../errors/utils/assertValidationError");
|
|
11
|
+
const types_1 = require("../types");
|
|
12
|
+
class AmazonAIInterpretPredictionsProvider {
|
|
13
|
+
getProviderName() {
|
|
14
|
+
return 'AmazonAIInterpretPredictionsProvider';
|
|
15
|
+
}
|
|
16
|
+
interpret(input) {
|
|
17
|
+
(0, assertValidationError_1.assertValidationError)((0, types_1.isValidInterpretInput)(input), validation_1.PredictionsValidationErrorCode.InvalidInput);
|
|
18
|
+
return this.interpretText(input);
|
|
19
|
+
}
|
|
20
|
+
async interpretText(input) {
|
|
21
|
+
const { credentials } = await (0, core_1.fetchAuthSession)();
|
|
22
|
+
(0, assertValidationError_1.assertValidationError)(!!credentials, validation_1.PredictionsValidationErrorCode.NoCredentials);
|
|
23
|
+
const { interpretText = {} } = core_1.Amplify.getConfig().Predictions?.interpret ?? {};
|
|
24
|
+
const { region = '', defaults = {} } = interpretText;
|
|
25
|
+
const { type: defaultType = '' } = defaults;
|
|
26
|
+
const { text: textSource } = input;
|
|
27
|
+
const { source, type = defaultType } = textSource;
|
|
28
|
+
const { text } = source;
|
|
29
|
+
let language;
|
|
30
|
+
if ((0, types_1.isInterpretTextOthers)(textSource)) {
|
|
31
|
+
language = textSource.source.language;
|
|
32
|
+
}
|
|
33
|
+
this.comprehendClient = new client_comprehend_1.ComprehendClient({
|
|
34
|
+
credentials,
|
|
35
|
+
region,
|
|
36
|
+
customUserAgent: (0, utils_1.getAmplifyUserAgentObject)({
|
|
37
|
+
category: utils_1.Category.Predictions,
|
|
38
|
+
action: utils_1.PredictionsAction.Interpret,
|
|
39
|
+
}),
|
|
40
|
+
});
|
|
41
|
+
const doAll = type === 'all';
|
|
42
|
+
let languageCode = language;
|
|
43
|
+
if (doAll || type === 'language') {
|
|
44
|
+
const languageDetectionParams = {
|
|
45
|
+
Text: text,
|
|
46
|
+
};
|
|
47
|
+
languageCode = await this.detectLanguage(languageDetectionParams);
|
|
48
|
+
}
|
|
49
|
+
let entitiesPromise;
|
|
50
|
+
if (doAll || type === 'entities') {
|
|
51
|
+
(0, assertValidationError_1.assertValidationError)(!!languageCode, validation_1.PredictionsValidationErrorCode.NoLanguage);
|
|
52
|
+
const entitiesDetectionParams = {
|
|
53
|
+
Text: text,
|
|
54
|
+
LanguageCode: languageCode,
|
|
55
|
+
};
|
|
56
|
+
entitiesPromise = this.detectEntities(entitiesDetectionParams);
|
|
57
|
+
}
|
|
58
|
+
let sentimentPromise;
|
|
59
|
+
if (doAll || type === 'sentiment') {
|
|
60
|
+
(0, assertValidationError_1.assertValidationError)(!!languageCode, validation_1.PredictionsValidationErrorCode.NoLanguage);
|
|
61
|
+
const sentimentParams = {
|
|
62
|
+
Text: text,
|
|
63
|
+
LanguageCode: languageCode,
|
|
64
|
+
};
|
|
65
|
+
sentimentPromise = this.detectSentiment(sentimentParams);
|
|
66
|
+
}
|
|
67
|
+
let syntaxPromise;
|
|
68
|
+
if (doAll || type === 'syntax') {
|
|
69
|
+
(0, assertValidationError_1.assertValidationError)(!!languageCode, validation_1.PredictionsValidationErrorCode.NoLanguage);
|
|
70
|
+
const syntaxParams = {
|
|
71
|
+
Text: text,
|
|
72
|
+
LanguageCode: languageCode,
|
|
73
|
+
};
|
|
74
|
+
syntaxPromise = this.detectSyntax(syntaxParams);
|
|
75
|
+
}
|
|
76
|
+
let keyPhrasesPromise;
|
|
77
|
+
if (doAll || type === 'keyPhrases') {
|
|
78
|
+
(0, assertValidationError_1.assertValidationError)(!!languageCode, validation_1.PredictionsValidationErrorCode.NoLanguage);
|
|
79
|
+
const keyPhrasesParams = {
|
|
80
|
+
Text: text,
|
|
81
|
+
LanguageCode: languageCode,
|
|
82
|
+
};
|
|
83
|
+
keyPhrasesPromise = this.detectKeyPhrases(keyPhrasesParams);
|
|
84
|
+
}
|
|
85
|
+
const [textEntities, sentiment, syntax, keyPhrases] = await Promise.all([
|
|
86
|
+
entitiesPromise,
|
|
87
|
+
sentimentPromise,
|
|
88
|
+
syntaxPromise,
|
|
89
|
+
keyPhrasesPromise,
|
|
90
|
+
]);
|
|
91
|
+
return {
|
|
92
|
+
textInterpretation: {
|
|
93
|
+
keyPhrases,
|
|
94
|
+
language: languageCode,
|
|
95
|
+
sentiment,
|
|
96
|
+
syntax,
|
|
97
|
+
textEntities,
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
async detectKeyPhrases(params) {
|
|
102
|
+
try {
|
|
103
|
+
const detectKeyPhrasesCommand = new client_comprehend_1.DetectKeyPhrasesCommand(params);
|
|
104
|
+
const data = await this.comprehendClient.send(detectKeyPhrasesCommand);
|
|
105
|
+
const { KeyPhrases = [] } = data || {};
|
|
106
|
+
return KeyPhrases.map(({ Text: text }) => {
|
|
107
|
+
return { text };
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
catch (err) {
|
|
111
|
+
if (err.code === 'AccessDeniedException') {
|
|
112
|
+
throw new Error('Not authorized, did you enable Interpret Text on predictions category Amplify CLI? try: ' +
|
|
113
|
+
'amplify predictions add');
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
throw err;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
async detectSyntax(params) {
|
|
121
|
+
try {
|
|
122
|
+
const detectSyntaxCommand = new client_comprehend_1.DetectSyntaxCommand(params);
|
|
123
|
+
const data = await this.comprehendClient.send(detectSyntaxCommand);
|
|
124
|
+
const { SyntaxTokens = [] } = data || {};
|
|
125
|
+
return this.serializeSyntaxFromComprehend(SyntaxTokens);
|
|
126
|
+
}
|
|
127
|
+
catch (err) {
|
|
128
|
+
if (err.code === 'AccessDeniedException') {
|
|
129
|
+
throw new Error('Not authorized, did you enable Interpret Text on predictions category Amplify CLI? try: ' +
|
|
130
|
+
'amplify predictions add');
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
throw err;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
serializeSyntaxFromComprehend(tokens) {
|
|
138
|
+
let response = [];
|
|
139
|
+
if (tokens && Array.isArray(tokens)) {
|
|
140
|
+
response = tokens.map(({ Text: text = '', PartOfSpeech: { Tag: syntax = '' } = {} }) => {
|
|
141
|
+
return { text, syntax };
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
return response;
|
|
145
|
+
}
|
|
146
|
+
async detectSentiment(params) {
|
|
147
|
+
try {
|
|
148
|
+
const detectSentimentCommand = new client_comprehend_1.DetectSentimentCommand(params);
|
|
149
|
+
const data = await this.comprehendClient.send(detectSentimentCommand);
|
|
150
|
+
const { Sentiment: predominant = '', SentimentScore: { Positive: positive = 0, Negative: negative = 0, Neutral: neutral = 0, Mixed: mixed = 0, } = {}, } = ({} = data);
|
|
151
|
+
return { predominant, positive, negative, neutral, mixed };
|
|
152
|
+
}
|
|
153
|
+
catch (err) {
|
|
154
|
+
if (err.code === 'AccessDeniedException') {
|
|
155
|
+
throw new Error('Not authorized, did you enable Interpret Text on predictions category Amplify CLI? try: ' +
|
|
156
|
+
'amplify predictions add');
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
throw err;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
async detectEntities(params) {
|
|
164
|
+
try {
|
|
165
|
+
const detectEntitiesCommand = new client_comprehend_1.DetectEntitiesCommand(params);
|
|
166
|
+
const data = await this.comprehendClient.send(detectEntitiesCommand);
|
|
167
|
+
const { Entities = [] } = data || {};
|
|
168
|
+
return this.serializeEntitiesFromComprehend(Entities);
|
|
169
|
+
}
|
|
170
|
+
catch (err) {
|
|
171
|
+
if (err.code === 'AccessDeniedException') {
|
|
172
|
+
throw new Error('Not authorized, did you enable Interpret Text on predictions category Amplify CLI? try: ' +
|
|
173
|
+
'amplify predictions add');
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
throw err;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
serializeEntitiesFromComprehend(data) {
|
|
181
|
+
let response = [];
|
|
182
|
+
if (data && Array.isArray(data)) {
|
|
183
|
+
response = data.map(({ Type: type, Text: text }) => {
|
|
184
|
+
return { type, text };
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
return response;
|
|
188
|
+
}
|
|
189
|
+
async detectLanguage(params) {
|
|
190
|
+
try {
|
|
191
|
+
const detectDominantLanguageCommand = new client_comprehend_1.DetectDominantLanguageCommand(params);
|
|
192
|
+
const data = await this.comprehendClient.send(detectDominantLanguageCommand);
|
|
193
|
+
const { Languages: [{ LanguageCode }] = [{}] } = ({} = data || {});
|
|
194
|
+
(0, assertValidationError_1.assertValidationError)(!!LanguageCode, validation_1.PredictionsValidationErrorCode.NoLanguage);
|
|
195
|
+
return LanguageCode;
|
|
196
|
+
}
|
|
197
|
+
catch (err) {
|
|
198
|
+
if (err.code === 'AccessDeniedException') {
|
|
199
|
+
throw new Error('Not authorized, did you enable Interpret Text on predictions category Amplify CLI? try: ' +
|
|
200
|
+
'amplify predictions add');
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
throw err;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
exports.AmazonAIInterpretPredictionsProvider = AmazonAIInterpretPredictionsProvider;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { IdentifyTextOutput } from '../types';
|
|
2
|
+
import { BlockList, TextDetectionList } from '../types/AWSTypes';
|
|
3
|
+
/**
|
|
4
|
+
* Organizes blocks from Rekognition API to each of the categories and and structures
|
|
5
|
+
* their data accordingly.
|
|
6
|
+
* @param {BlockList} source - Array containing blocks returned from Textract API.
|
|
7
|
+
* @return {IdentifyTextOutput} - Object that categorizes each block and its information.
|
|
8
|
+
*/
|
|
9
|
+
export declare function categorizeRekognitionBlocks(blocks: TextDetectionList): IdentifyTextOutput;
|
|
10
|
+
/**
|
|
11
|
+
* Organizes blocks from Textract API to each of the categories and and structures
|
|
12
|
+
* their data accordingly.
|
|
13
|
+
* @param {BlockList} source - Array containing blocks returned from Textract API.
|
|
14
|
+
* @return {IdentifyTextOutput} - Object that categorizes each block and its information.
|
|
15
|
+
*/
|
|
16
|
+
export declare function categorizeTextractBlocks(blocks: BlockList): IdentifyTextOutput;
|
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
exports.categorizeTextractBlocks = exports.categorizeRekognitionBlocks = void 0;
|
|
4
|
+
const Utils_1 = require("./Utils");
|
|
5
5
|
function getBoundingBox(geometry) {
|
|
6
|
-
|
|
7
|
-
return undefined;
|
|
8
|
-
return Utils_1.makeCamelCase(geometry.BoundingBox);
|
|
6
|
+
return (0, Utils_1.makeCamelCase)(geometry?.BoundingBox);
|
|
9
7
|
}
|
|
10
8
|
function getPolygon(geometry) {
|
|
11
|
-
if (!geometry)
|
|
9
|
+
if (!geometry?.Polygon)
|
|
12
10
|
return undefined;
|
|
13
|
-
return Utils_1.makeCamelCaseArray(Array.from(geometry.Polygon));
|
|
11
|
+
return (0, Utils_1.makeCamelCaseArray)(Array.from(geometry.Polygon));
|
|
14
12
|
}
|
|
15
13
|
/**
|
|
16
14
|
* Organizes blocks from Rekognition API to each of the categories and and structures
|
|
@@ -20,7 +18,7 @@ function getPolygon(geometry) {
|
|
|
20
18
|
*/
|
|
21
19
|
function categorizeRekognitionBlocks(blocks) {
|
|
22
20
|
// Skeleton IdentifyText API response. We will populate it as we iterate through blocks.
|
|
23
|
-
|
|
21
|
+
const response = {
|
|
24
22
|
text: {
|
|
25
23
|
fullText: '',
|
|
26
24
|
words: [],
|
|
@@ -29,15 +27,17 @@ function categorizeRekognitionBlocks(blocks) {
|
|
|
29
27
|
},
|
|
30
28
|
};
|
|
31
29
|
// We categorize each block by running a forEach loop through them.
|
|
32
|
-
blocks.forEach(
|
|
30
|
+
blocks.forEach(block => {
|
|
33
31
|
switch (block.Type) {
|
|
34
32
|
case 'LINE':
|
|
35
|
-
|
|
33
|
+
if (block.DetectedText) {
|
|
34
|
+
response.text.lines.push(block.DetectedText);
|
|
35
|
+
}
|
|
36
36
|
response.text.linesDetailed.push({
|
|
37
37
|
text: block.DetectedText,
|
|
38
38
|
polygon: getPolygon(block.Geometry),
|
|
39
39
|
boundingBox: getBoundingBox(block.Geometry),
|
|
40
|
-
page:
|
|
40
|
+
page: undefined, // rekognition doesn't have this info
|
|
41
41
|
});
|
|
42
42
|
break;
|
|
43
43
|
case 'WORD':
|
|
@@ -63,7 +63,7 @@ exports.categorizeRekognitionBlocks = categorizeRekognitionBlocks;
|
|
|
63
63
|
*/
|
|
64
64
|
function categorizeTextractBlocks(blocks) {
|
|
65
65
|
// Skeleton IdentifyText API response. We will populate it as we iterate through blocks.
|
|
66
|
-
|
|
66
|
+
const response = {
|
|
67
67
|
text: {
|
|
68
68
|
fullText: '',
|
|
69
69
|
words: [],
|
|
@@ -84,13 +84,15 @@ function categorizeTextractBlocks(blocks) {
|
|
|
84
84
|
* Note that we do not map `WORD` and `TABLE` in `blockMap` because they will not be referenced by any other
|
|
85
85
|
* block except the Page block.
|
|
86
86
|
*/
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
blocks.forEach(
|
|
87
|
+
const tableBlocks = Array();
|
|
88
|
+
const keyValueBlocks = Array();
|
|
89
|
+
const blockMap = {};
|
|
90
|
+
blocks.forEach(block => {
|
|
91
91
|
switch (block.BlockType) {
|
|
92
92
|
case 'LINE':
|
|
93
|
-
|
|
93
|
+
if (block.Text) {
|
|
94
|
+
response.text.lines.push(block.Text);
|
|
95
|
+
}
|
|
94
96
|
response.text.linesDetailed.push({
|
|
95
97
|
text: block.Text,
|
|
96
98
|
polygon: getPolygon(block.Geometry),
|
|
@@ -105,10 +107,12 @@ function categorizeTextractBlocks(blocks) {
|
|
|
105
107
|
polygon: getPolygon(block.Geometry),
|
|
106
108
|
boundingBox: getBoundingBox(block.Geometry),
|
|
107
109
|
});
|
|
108
|
-
|
|
110
|
+
if (block.Id) {
|
|
111
|
+
blockMap[block.Id] = block;
|
|
112
|
+
}
|
|
109
113
|
break;
|
|
110
114
|
case 'SELECTION_ELEMENT':
|
|
111
|
-
|
|
115
|
+
const selectionStatus = block.SelectionStatus === 'SELECTED' ? true : false;
|
|
112
116
|
if (!response.text.selections)
|
|
113
117
|
response.text.selections = [];
|
|
114
118
|
response.text.selections.push({
|
|
@@ -116,39 +120,47 @@ function categorizeTextractBlocks(blocks) {
|
|
|
116
120
|
polygon: getPolygon(block.Geometry),
|
|
117
121
|
boundingBox: getBoundingBox(block.Geometry),
|
|
118
122
|
});
|
|
119
|
-
|
|
123
|
+
if (block.Id) {
|
|
124
|
+
blockMap[block.Id] = block;
|
|
125
|
+
}
|
|
120
126
|
break;
|
|
121
127
|
case 'TABLE':
|
|
122
128
|
tableBlocks.push(block);
|
|
123
129
|
break;
|
|
124
130
|
case 'KEY_VALUE_SET':
|
|
125
131
|
keyValueBlocks.push(block);
|
|
126
|
-
|
|
132
|
+
if (block.Id) {
|
|
133
|
+
blockMap[block.Id] = block;
|
|
134
|
+
}
|
|
127
135
|
break;
|
|
128
136
|
default:
|
|
129
|
-
|
|
137
|
+
if (block.Id) {
|
|
138
|
+
blockMap[block.Id] = block;
|
|
139
|
+
}
|
|
130
140
|
}
|
|
131
141
|
});
|
|
132
142
|
// remove trailing space in fullText
|
|
133
143
|
response.text.fullText = response.text.fullText.substr(0, response.text.fullText.length - 1);
|
|
134
144
|
// Post-process complex structures if they exist.
|
|
135
145
|
if (tableBlocks.length !== 0) {
|
|
136
|
-
|
|
137
|
-
tableBlocks.forEach(
|
|
138
|
-
|
|
146
|
+
const tableResponse = Array();
|
|
147
|
+
tableBlocks.forEach(table => {
|
|
148
|
+
tableResponse.push(constructTable(table, blockMap));
|
|
139
149
|
});
|
|
140
|
-
response.text.tables =
|
|
150
|
+
response.text.tables = tableResponse;
|
|
141
151
|
}
|
|
142
152
|
if (keyValueBlocks.length !== 0) {
|
|
143
|
-
|
|
144
|
-
keyValueBlocks.forEach(
|
|
153
|
+
const keyValueResponse = Array();
|
|
154
|
+
keyValueBlocks.forEach(keyValue => {
|
|
145
155
|
// We need the KeyValue blocks of EntityType = `KEY`, which has both key and value references.
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
156
|
+
if (keyValue.EntityTypes) {
|
|
157
|
+
const entityTypes = Array.from(keyValue.EntityTypes);
|
|
158
|
+
if (entityTypes.indexOf('KEY') !== -1) {
|
|
159
|
+
keyValueResponse.push(constructKeyValue(keyValue, blockMap));
|
|
160
|
+
}
|
|
149
161
|
}
|
|
150
162
|
});
|
|
151
|
-
response.text.keyValues =
|
|
163
|
+
response.text.keyValues = keyValueResponse;
|
|
152
164
|
}
|
|
153
165
|
return response;
|
|
154
166
|
}
|
|
@@ -159,107 +171,68 @@ exports.categorizeTextractBlocks = categorizeTextractBlocks;
|
|
|
159
171
|
* @param {[id: string]: Block} blockMap - Maps block Ids to blocks.
|
|
160
172
|
*/
|
|
161
173
|
function constructTable(table, blockMap) {
|
|
162
|
-
|
|
163
|
-
var tableMatrix;
|
|
174
|
+
let tableMatrix;
|
|
164
175
|
tableMatrix = [];
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
for (
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
if (!tableMatrix[row])
|
|
186
|
-
tableMatrix[row] = [];
|
|
187
|
-
tableMatrix[row][col] = cell;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
191
|
-
finally {
|
|
192
|
-
try {
|
|
193
|
-
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
|
|
194
|
-
}
|
|
195
|
-
finally { if (e_2) throw e_2.error; }
|
|
176
|
+
// visit each of the cell associated with the table's relationship.
|
|
177
|
+
for (const tableRelation of table.Relationships ?? []) {
|
|
178
|
+
for (const cellId of tableRelation.Ids ?? []) {
|
|
179
|
+
const cellBlock = blockMap[cellId];
|
|
180
|
+
if (cellBlock.RowIndex && cellBlock.ColumnIndex) {
|
|
181
|
+
const row = cellBlock.RowIndex - 1; // textract starts indexing at 1, so subtract it by 1.
|
|
182
|
+
const col = cellBlock.ColumnIndex - 1; // textract starts indexing at 1, so subtract it by 1.
|
|
183
|
+
// extract data contained inside the cell.
|
|
184
|
+
const content = extractContentsFromBlock(cellBlock, blockMap);
|
|
185
|
+
const cell = {
|
|
186
|
+
text: content.text,
|
|
187
|
+
boundingBox: getBoundingBox(cellBlock.Geometry),
|
|
188
|
+
polygon: getPolygon(cellBlock.Geometry),
|
|
189
|
+
selected: content.selected,
|
|
190
|
+
rowSpan: cellBlock.RowSpan,
|
|
191
|
+
columnSpan: cellBlock.ColumnSpan,
|
|
192
|
+
};
|
|
193
|
+
if (!tableMatrix[row])
|
|
194
|
+
tableMatrix[row] = [];
|
|
195
|
+
tableMatrix[row][col] = cell;
|
|
196
196
|
}
|
|
197
197
|
}
|
|
198
198
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
}
|
|
204
|
-
finally { if (e_1) throw e_1.error; }
|
|
205
|
-
}
|
|
206
|
-
var rowSize = tableMatrix.length;
|
|
207
|
-
var columnSize = tableMatrix[0].length;
|
|
199
|
+
const rowSize = tableMatrix.length;
|
|
200
|
+
const columnSize = tableMatrix[0].length;
|
|
201
|
+
const boundingBox = getBoundingBox(table.Geometry);
|
|
202
|
+
const polygon = getPolygon(table.Geometry);
|
|
208
203
|
// Note that we leave spanned cells undefined for distinction
|
|
209
204
|
return {
|
|
210
205
|
size: { rows: rowSize, columns: columnSize },
|
|
211
206
|
table: tableMatrix,
|
|
212
|
-
boundingBox
|
|
213
|
-
polygon
|
|
207
|
+
boundingBox,
|
|
208
|
+
polygon,
|
|
214
209
|
};
|
|
215
210
|
}
|
|
216
|
-
exports.constructTable = constructTable;
|
|
217
211
|
/**
|
|
218
212
|
* Constructs a key value object from its children key and value blocks.
|
|
219
213
|
* @param {Block} KeyValue - KeyValue block that has references (`Relationships`) to its children.
|
|
220
214
|
* @param {[id: string]: Block} blockMap - Maps block Ids to blocks.
|
|
221
215
|
*/
|
|
222
216
|
function constructKeyValue(keyBlock, blockMap) {
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
// relation refers to key
|
|
232
|
-
var contents = extractContentsFromBlock(keyBlock, blockMap);
|
|
233
|
-
keyText = contents.text;
|
|
234
|
-
}
|
|
235
|
-
else if (keyValueRelation.Type === 'VALUE') {
|
|
236
|
-
try {
|
|
237
|
-
// relation refers to value
|
|
238
|
-
for (var _e = (e_4 = void 0, tslib_1.__values(keyValueRelation.Ids)), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
239
|
-
var valueId = _f.value;
|
|
240
|
-
var valueBlock = blockMap[valueId];
|
|
241
|
-
var contents = extractContentsFromBlock(valueBlock, blockMap);
|
|
242
|
-
valueText = contents.text;
|
|
243
|
-
if (contents.selected != null)
|
|
244
|
-
valueSelected = contents.selected;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
248
|
-
finally {
|
|
249
|
-
try {
|
|
250
|
-
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
|
|
251
|
-
}
|
|
252
|
-
finally { if (e_4) throw e_4.error; }
|
|
253
|
-
}
|
|
254
|
-
}
|
|
217
|
+
let keyText = '';
|
|
218
|
+
let valueText = '';
|
|
219
|
+
let valueSelected = false;
|
|
220
|
+
for (const keyValueRelation of keyBlock.Relationships ?? []) {
|
|
221
|
+
if (keyValueRelation.Type === 'CHILD') {
|
|
222
|
+
// relation refers to key
|
|
223
|
+
const contents = extractContentsFromBlock(keyBlock, blockMap);
|
|
224
|
+
keyText = contents.text ?? '';
|
|
255
225
|
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
226
|
+
else if (keyValueRelation.Type === 'VALUE') {
|
|
227
|
+
// relation refers to value
|
|
228
|
+
for (const valueId of keyValueRelation.Ids ?? []) {
|
|
229
|
+
const valueBlock = blockMap[valueId];
|
|
230
|
+
const contents = extractContentsFromBlock(valueBlock, blockMap);
|
|
231
|
+
valueText = contents.text ?? '';
|
|
232
|
+
if (contents.selected != null)
|
|
233
|
+
valueSelected = contents.selected;
|
|
234
|
+
}
|
|
261
235
|
}
|
|
262
|
-
finally { if (e_3) throw e_3.error; }
|
|
263
236
|
}
|
|
264
237
|
return {
|
|
265
238
|
key: keyText,
|
|
@@ -268,53 +241,29 @@ function constructKeyValue(keyBlock, blockMap) {
|
|
|
268
241
|
boundingBox: getBoundingBox(keyBlock.Geometry),
|
|
269
242
|
};
|
|
270
243
|
}
|
|
271
|
-
exports.constructKeyValue = constructKeyValue;
|
|
272
244
|
/**
|
|
273
245
|
* Extracts text and selection from input block's children.
|
|
274
246
|
* @param {Block}} block - Block that we want to extract contents from.
|
|
275
247
|
* @param {[id: string]: Block} blockMap - Maps block Ids to blocks.
|
|
276
248
|
*/
|
|
277
249
|
function extractContentsFromBlock(block, blockMap) {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
var isSelected;
|
|
250
|
+
let words = '';
|
|
251
|
+
let isSelected = false;
|
|
281
252
|
if (!block.Relationships) {
|
|
282
253
|
// some block might have no content
|
|
283
254
|
return { text: '', selected: undefined };
|
|
284
255
|
}
|
|
285
|
-
|
|
286
|
-
for (
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
var contentId = _f.value;
|
|
291
|
-
var contentBlock = blockMap[contentId];
|
|
292
|
-
if (contentBlock.BlockType === 'WORD') {
|
|
293
|
-
words += contentBlock.Text + ' ';
|
|
294
|
-
}
|
|
295
|
-
else if (contentBlock.BlockType === 'SELECTION_ELEMENT') {
|
|
296
|
-
isSelected = contentBlock.SelectionStatus === 'SELECTED' ? true : false;
|
|
297
|
-
}
|
|
298
|
-
}
|
|
256
|
+
for (const relation of block.Relationships) {
|
|
257
|
+
for (const contentId of relation.Ids ?? []) {
|
|
258
|
+
const contentBlock = blockMap[contentId];
|
|
259
|
+
if (contentBlock.BlockType === 'WORD') {
|
|
260
|
+
words += contentBlock.Text + ' ';
|
|
299
261
|
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
try {
|
|
303
|
-
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
|
|
304
|
-
}
|
|
305
|
-
finally { if (e_6) throw e_6.error; }
|
|
262
|
+
else if (contentBlock.BlockType === 'SELECTION_ELEMENT') {
|
|
263
|
+
isSelected = contentBlock.SelectionStatus === 'SELECTED' ? true : false;
|
|
306
264
|
}
|
|
307
265
|
}
|
|
308
266
|
}
|
|
309
|
-
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
310
|
-
finally {
|
|
311
|
-
try {
|
|
312
|
-
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
|
313
|
-
}
|
|
314
|
-
finally { if (e_5) throw e_5.error; }
|
|
315
|
-
}
|
|
316
267
|
words = words.substr(0, words.length - 1); // remove trailing space.
|
|
317
268
|
return { text: words, selected: isSelected };
|
|
318
269
|
}
|
|
319
|
-
exports.extractContentsFromBlock = extractContentsFromBlock;
|
|
320
|
-
//# sourceMappingURL=IdentifyTextUtils.js.map
|
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
* Changes object keys to camel case. If optional parameter `keys` is given, then we extract only the
|
|
3
3
|
* keys specified in `keys`.
|
|
4
4
|
*/
|
|
5
|
-
export declare function makeCamelCase(obj
|
|
5
|
+
export declare function makeCamelCase(obj?: any, keys?: string[]): {} | undefined;
|
|
6
6
|
/**
|
|
7
7
|
* Given an array of object, call makeCamelCase(...) on each option.
|
|
8
8
|
*/
|
|
9
|
-
export declare function makeCamelCaseArray(objArr
|
|
9
|
+
export declare function makeCamelCaseArray(objArr?: object[], keys?: string[]): ({} | undefined)[] | undefined;
|
|
10
10
|
/**
|
|
11
11
|
* Converts blob to array buffer
|
|
12
12
|
*/
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.blobToArrayBuffer = exports.makeCamelCaseArray = exports.makeCamelCase = void 0;
|
|
3
4
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
4
5
|
// SPDX-License-Identifier: Apache-2.0
|
|
5
6
|
/**
|
|
@@ -9,14 +10,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
9
10
|
function makeCamelCase(obj, keys) {
|
|
10
11
|
if (!obj)
|
|
11
12
|
return undefined;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
keysToRename.forEach(
|
|
15
|
-
var _a;
|
|
13
|
+
const newObj = {};
|
|
14
|
+
const keysToRename = keys ? keys : Object.keys(obj);
|
|
15
|
+
keysToRename.forEach(key => {
|
|
16
16
|
if (obj.hasOwnProperty(key)) {
|
|
17
17
|
// change the key to camelcase.
|
|
18
|
-
|
|
19
|
-
Object.assign(newObj,
|
|
18
|
+
const camelCaseKey = key.charAt(0).toLowerCase() + key.substr(1);
|
|
19
|
+
Object.assign(newObj, { [camelCaseKey]: obj[key] });
|
|
20
20
|
}
|
|
21
21
|
});
|
|
22
22
|
return newObj;
|
|
@@ -28,19 +28,19 @@ exports.makeCamelCase = makeCamelCase;
|
|
|
28
28
|
function makeCamelCaseArray(objArr, keys) {
|
|
29
29
|
if (!objArr)
|
|
30
30
|
return undefined;
|
|
31
|
-
return objArr.map(
|
|
31
|
+
return objArr.map(obj => makeCamelCase(obj, keys));
|
|
32
32
|
}
|
|
33
33
|
exports.makeCamelCaseArray = makeCamelCaseArray;
|
|
34
34
|
/**
|
|
35
35
|
* Converts blob to array buffer
|
|
36
36
|
*/
|
|
37
37
|
function blobToArrayBuffer(blob) {
|
|
38
|
-
return new Promise(
|
|
39
|
-
|
|
40
|
-
reader.onload =
|
|
38
|
+
return new Promise((res, rej) => {
|
|
39
|
+
const reader = new FileReader();
|
|
40
|
+
reader.onload = _event => {
|
|
41
41
|
res(reader.result);
|
|
42
42
|
};
|
|
43
|
-
reader.onerror =
|
|
43
|
+
reader.onerror = err => {
|
|
44
44
|
rej(err);
|
|
45
45
|
};
|
|
46
46
|
try {
|
|
@@ -52,4 +52,3 @@ function blobToArrayBuffer(blob) {
|
|
|
52
52
|
});
|
|
53
53
|
}
|
|
54
54
|
exports.blobToArrayBuffer = blobToArrayBuffer;
|
|
55
|
-
//# sourceMappingURL=Utils.js.map
|