@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.
Files changed (148) hide show
  1. package/README.md +3 -0
  2. package/lib/Predictions.d.ts +11 -45
  3. package/lib/Predictions.js +24 -132
  4. package/lib/errors/PredictionsError.d.ts +4 -0
  5. package/lib/errors/PredictionsError.js +15 -0
  6. package/lib/errors/types/validation.d.ts +14 -0
  7. package/lib/errors/types/validation.js +50 -0
  8. package/lib/errors/utils/assertValidationError.d.ts +2 -0
  9. package/lib/errors/utils/assertValidationError.js +14 -0
  10. package/lib/index.d.ts +2 -4
  11. package/lib/index.js +2 -9
  12. package/{lib-esm/Providers → lib/providers}/AmazonAIConvertPredictionsProvider.d.ts +6 -7
  13. package/lib/providers/AmazonAIConvertPredictionsProvider.js +307 -0
  14. package/lib/{Providers → providers}/AmazonAIIdentifyPredictionsProvider.d.ts +5 -6
  15. package/lib/providers/AmazonAIIdentifyPredictionsProvider.js +334 -0
  16. package/lib/{Providers → providers}/AmazonAIInterpretPredictionsProvider.d.ts +3 -4
  17. package/lib/providers/AmazonAIInterpretPredictionsProvider.js +208 -0
  18. package/lib/providers/IdentifyTextUtils.d.ts +16 -0
  19. package/lib/{Providers → providers}/IdentifyTextUtils.js +97 -148
  20. package/lib/{Providers → providers}/Utils.d.ts +2 -2
  21. package/lib/{Providers → providers}/Utils.js +11 -12
  22. package/lib/{Providers → providers}/index.d.ts +1 -2
  23. package/lib/providers/index.js +11 -0
  24. package/lib/tsconfig.tsbuildinfo +1 -0
  25. package/lib/types/AWSTypes.d.ts +8 -9
  26. package/lib/types/AWSTypes.js +0 -1
  27. package/lib/types/Predictions.d.ts +63 -59
  28. package/lib/types/Predictions.js +41 -28
  29. package/lib/types/index.js +1 -2
  30. package/lib-esm/Predictions.d.ts +11 -45
  31. package/lib-esm/Predictions.js +23 -133
  32. package/lib-esm/errors/PredictionsError.d.ts +4 -0
  33. package/lib-esm/errors/PredictionsError.js +11 -0
  34. package/lib-esm/errors/types/validation.d.ts +14 -0
  35. package/lib-esm/errors/types/validation.js +47 -0
  36. package/lib-esm/errors/utils/assertValidationError.d.ts +2 -0
  37. package/lib-esm/errors/utils/assertValidationError.js +10 -0
  38. package/lib-esm/index.d.ts +2 -4
  39. package/lib-esm/index.js +1 -5
  40. package/{lib/Providers → lib-esm/providers}/AmazonAIConvertPredictionsProvider.d.ts +6 -7
  41. package/lib-esm/providers/AmazonAIConvertPredictionsProvider.js +303 -0
  42. package/lib-esm/{Providers → providers}/AmazonAIIdentifyPredictionsProvider.d.ts +5 -6
  43. package/lib-esm/providers/AmazonAIIdentifyPredictionsProvider.js +330 -0
  44. package/lib-esm/{Providers → providers}/AmazonAIInterpretPredictionsProvider.d.ts +3 -4
  45. package/lib-esm/providers/AmazonAIInterpretPredictionsProvider.js +204 -0
  46. package/lib-esm/providers/IdentifyTextUtils.d.ts +16 -0
  47. package/lib-esm/providers/IdentifyTextUtils.js +264 -0
  48. package/lib-esm/{Providers → providers}/Utils.d.ts +2 -2
  49. package/lib-esm/{Providers → providers}/Utils.js +10 -12
  50. package/lib-esm/{Providers → providers}/index.d.ts +1 -2
  51. package/lib-esm/{Providers → providers}/index.js +1 -3
  52. package/lib-esm/tsconfig.tsbuildinfo +1 -0
  53. package/lib-esm/types/AWSTypes.d.ts +8 -9
  54. package/lib-esm/types/AWSTypes.js +1 -1
  55. package/lib-esm/types/Predictions.d.ts +63 -59
  56. package/lib-esm/types/Predictions.js +34 -27
  57. package/lib-esm/types/index.js +0 -1
  58. package/package.json +39 -48
  59. package/src/Predictions.ts +33 -220
  60. package/src/errors/PredictionsError.ts +16 -0
  61. package/src/errors/types/validation.ts +51 -0
  62. package/src/errors/utils/assertValidationError.ts +18 -0
  63. package/src/index.ts +16 -16
  64. package/src/{Providers → providers}/AmazonAIConvertPredictionsProvider.ts +210 -171
  65. package/src/{Providers → providers}/AmazonAIIdentifyPredictionsProvider.ts +244 -246
  66. package/src/providers/AmazonAIInterpretPredictionsProvider.ts +293 -0
  67. package/src/{Providers → providers}/IdentifyTextUtils.ts +69 -52
  68. package/src/{Providers → providers}/Utils.ts +2 -2
  69. package/src/{Providers → providers}/index.ts +0 -2
  70. package/src/types/AWSTypes.ts +5 -6
  71. package/src/types/Predictions.ts +107 -62
  72. package/lib/.tsbuildinfo +0 -3
  73. package/lib/Predictions.js.map +0 -1
  74. package/lib/Providers/AmazonAIConvertPredictionsProvider.js +0 -377
  75. package/lib/Providers/AmazonAIConvertPredictionsProvider.js.map +0 -1
  76. package/lib/Providers/AmazonAIIdentifyPredictionsProvider.js +0 -441
  77. package/lib/Providers/AmazonAIIdentifyPredictionsProvider.js.map +0 -1
  78. package/lib/Providers/AmazonAIInterpretPredictionsProvider.js +0 -321
  79. package/lib/Providers/AmazonAIInterpretPredictionsProvider.js.map +0 -1
  80. package/lib/Providers/AmazonAIPredictionsProvider.d.ts +0 -14
  81. package/lib/Providers/AmazonAIPredictionsProvider.js +0 -43
  82. package/lib/Providers/AmazonAIPredictionsProvider.js.map +0 -1
  83. package/lib/Providers/IdentifyTextUtils.d.ts +0 -40
  84. package/lib/Providers/IdentifyTextUtils.js.map +0 -1
  85. package/lib/Providers/Utils.js.map +0 -1
  86. package/lib/Providers/index.js +0 -13
  87. package/lib/Providers/index.js.map +0 -1
  88. package/lib/index.js.map +0 -1
  89. package/lib/types/AWSTypes.js.map +0 -1
  90. package/lib/types/Predictions.js.map +0 -1
  91. package/lib/types/Providers/AbstractConvertPredictionsProvider.d.ts +0 -9
  92. package/lib/types/Providers/AbstractConvertPredictionsProvider.js +0 -44
  93. package/lib/types/Providers/AbstractConvertPredictionsProvider.js.map +0 -1
  94. package/lib/types/Providers/AbstractIdentifyPredictionsProvider.d.ts +0 -9
  95. package/lib/types/Providers/AbstractIdentifyPredictionsProvider.js +0 -44
  96. package/lib/types/Providers/AbstractIdentifyPredictionsProvider.js.map +0 -1
  97. package/lib/types/Providers/AbstractInterpretPredictionsProvider.d.ts +0 -7
  98. package/lib/types/Providers/AbstractInterpretPredictionsProvider.js +0 -27
  99. package/lib/types/Providers/AbstractInterpretPredictionsProvider.js.map +0 -1
  100. package/lib/types/Providers/AbstractPredictionsProvider.d.ts +0 -7
  101. package/lib/types/Providers/AbstractPredictionsProvider.js +0 -16
  102. package/lib/types/Providers/AbstractPredictionsProvider.js.map +0 -1
  103. package/lib/types/Providers/index.d.ts +0 -4
  104. package/lib/types/Providers/index.js +0 -10
  105. package/lib/types/Providers/index.js.map +0 -1
  106. package/lib/types/index.js.map +0 -1
  107. package/lib-esm/.tsbuildinfo +0 -3
  108. package/lib-esm/Predictions.js.map +0 -1
  109. package/lib-esm/Providers/AmazonAIConvertPredictionsProvider.js +0 -375
  110. package/lib-esm/Providers/AmazonAIConvertPredictionsProvider.js.map +0 -1
  111. package/lib-esm/Providers/AmazonAIIdentifyPredictionsProvider.js +0 -439
  112. package/lib-esm/Providers/AmazonAIIdentifyPredictionsProvider.js.map +0 -1
  113. package/lib-esm/Providers/AmazonAIInterpretPredictionsProvider.js +0 -319
  114. package/lib-esm/Providers/AmazonAIInterpretPredictionsProvider.js.map +0 -1
  115. package/lib-esm/Providers/AmazonAIPredictionsProvider.d.ts +0 -14
  116. package/lib-esm/Providers/AmazonAIPredictionsProvider.js +0 -41
  117. package/lib-esm/Providers/AmazonAIPredictionsProvider.js.map +0 -1
  118. package/lib-esm/Providers/IdentifyTextUtils.d.ts +0 -40
  119. package/lib-esm/Providers/IdentifyTextUtils.js +0 -313
  120. package/lib-esm/Providers/IdentifyTextUtils.js.map +0 -1
  121. package/lib-esm/Providers/Utils.js.map +0 -1
  122. package/lib-esm/Providers/index.js.map +0 -1
  123. package/lib-esm/index.js.map +0 -1
  124. package/lib-esm/types/AWSTypes.js.map +0 -1
  125. package/lib-esm/types/Predictions.js.map +0 -1
  126. package/lib-esm/types/Providers/AbstractConvertPredictionsProvider.d.ts +0 -9
  127. package/lib-esm/types/Providers/AbstractConvertPredictionsProvider.js +0 -42
  128. package/lib-esm/types/Providers/AbstractConvertPredictionsProvider.js.map +0 -1
  129. package/lib-esm/types/Providers/AbstractIdentifyPredictionsProvider.d.ts +0 -9
  130. package/lib-esm/types/Providers/AbstractIdentifyPredictionsProvider.js +0 -42
  131. package/lib-esm/types/Providers/AbstractIdentifyPredictionsProvider.js.map +0 -1
  132. package/lib-esm/types/Providers/AbstractInterpretPredictionsProvider.d.ts +0 -7
  133. package/lib-esm/types/Providers/AbstractInterpretPredictionsProvider.js +0 -25
  134. package/lib-esm/types/Providers/AbstractInterpretPredictionsProvider.js.map +0 -1
  135. package/lib-esm/types/Providers/AbstractPredictionsProvider.d.ts +0 -7
  136. package/lib-esm/types/Providers/AbstractPredictionsProvider.js +0 -14
  137. package/lib-esm/types/Providers/AbstractPredictionsProvider.js.map +0 -1
  138. package/lib-esm/types/Providers/index.d.ts +0 -4
  139. package/lib-esm/types/Providers/index.js +0 -7
  140. package/lib-esm/types/Providers/index.js.map +0 -1
  141. package/lib-esm/types/index.js.map +0 -1
  142. package/src/Providers/AmazonAIInterpretPredictionsProvider.ts +0 -281
  143. package/src/Providers/AmazonAIPredictionsProvider.ts +0 -69
  144. package/src/types/Providers/AbstractConvertPredictionsProvider.ts +0 -55
  145. package/src/types/Providers/AbstractIdentifyPredictionsProvider.ts +0 -57
  146. package/src/types/Providers/AbstractInterpretPredictionsProvider.ts +0 -26
  147. package/src/types/Providers/AbstractPredictionsProvider.ts +0 -20
  148. 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
- var tslib_1 = require("tslib");
4
- var Utils_1 = require("./Utils");
3
+ exports.categorizeTextractBlocks = exports.categorizeRekognitionBlocks = void 0;
4
+ const Utils_1 = require("./Utils");
5
5
  function getBoundingBox(geometry) {
6
- if (!geometry)
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
- var response = {
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(function (block) {
30
+ blocks.forEach(block => {
33
31
  switch (block.Type) {
34
32
  case 'LINE':
35
- response.text.lines.push(block.DetectedText);
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: null,
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
- var response = {
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
- var tableBlocks = Array();
88
- var keyValueBlocks = Array();
89
- var blockMap = {};
90
- blocks.forEach(function (block) {
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
- response.text.lines.push(block.Text);
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
- blockMap[block.Id] = block;
110
+ if (block.Id) {
111
+ blockMap[block.Id] = block;
112
+ }
109
113
  break;
110
114
  case 'SELECTION_ELEMENT':
111
- var selectionStatus = block.SelectionStatus === 'SELECTED' ? true : false;
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
- blockMap[block.Id] = block;
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
- blockMap[block.Id] = block;
132
+ if (block.Id) {
133
+ blockMap[block.Id] = block;
134
+ }
127
135
  break;
128
136
  default:
129
- blockMap[block.Id] = block;
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
- var tableResponse_1 = Array();
137
- tableBlocks.forEach(function (table) {
138
- tableResponse_1.push(constructTable(table, blockMap));
146
+ const tableResponse = Array();
147
+ tableBlocks.forEach(table => {
148
+ tableResponse.push(constructTable(table, blockMap));
139
149
  });
140
- response.text.tables = tableResponse_1;
150
+ response.text.tables = tableResponse;
141
151
  }
142
152
  if (keyValueBlocks.length !== 0) {
143
- var keyValueResponse_1 = Array();
144
- keyValueBlocks.forEach(function (keyValue) {
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
- var entityTypes = Array.from(keyValue.EntityTypes);
147
- if (entityTypes.indexOf('KEY') !== -1) {
148
- keyValueResponse_1.push(constructKeyValue(keyValue, blockMap));
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 = keyValueResponse_1;
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
- var e_1, _a, e_2, _b;
163
- var tableMatrix;
174
+ let tableMatrix;
164
175
  tableMatrix = [];
165
- try {
166
- // visit each of the cell associated with the table's relationship.
167
- for (var _c = tslib_1.__values(table.Relationships), _d = _c.next(); !_d.done; _d = _c.next()) {
168
- var tableRelation = _d.value;
169
- try {
170
- for (var _e = (e_2 = void 0, tslib_1.__values(tableRelation.Ids)), _f = _e.next(); !_f.done; _f = _e.next()) {
171
- var cellId = _f.value;
172
- var cellBlock = blockMap[cellId];
173
- var row = cellBlock.RowIndex - 1; // textract starts indexing at 1, so subtract it by 1.
174
- var col = cellBlock.ColumnIndex - 1; // textract starts indexing at 1, so subtract it by 1.
175
- // extract data contained inside the cell.
176
- var content = extractContentsFromBlock(cellBlock, blockMap);
177
- var cell = {
178
- text: content.text,
179
- boundingBox: getBoundingBox(cellBlock.Geometry),
180
- polygon: getPolygon(cellBlock.Geometry),
181
- selected: content.selected,
182
- rowSpan: cellBlock.RowSpan,
183
- columnSpan: cellBlock.ColumnSpan,
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
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
200
- finally {
201
- try {
202
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
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: getBoundingBox(table.Geometry),
213
- polygon: getPolygon(table.Geometry),
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
- var e_3, _a, e_4, _b;
224
- var keyText = '';
225
- var valueText = '';
226
- var valueSelected;
227
- try {
228
- for (var _c = tslib_1.__values(keyBlock.Relationships), _d = _c.next(); !_d.done; _d = _c.next()) {
229
- var keyValueRelation = _d.value;
230
- if (keyValueRelation.Type === 'CHILD') {
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
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
258
- finally {
259
- try {
260
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
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
- var e_5, _a, e_6, _b;
279
- var words = '';
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
- try {
286
- for (var _c = tslib_1.__values(block.Relationships), _d = _c.next(); !_d.done; _d = _c.next()) {
287
- var relation = _d.value;
288
- try {
289
- for (var _e = (e_6 = void 0, tslib_1.__values(relation.Ids)), _f = _e.next(); !_f.done; _f = _e.next()) {
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
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
301
- finally {
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: object, keys?: string[]): {};
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: object[], keys?: string[]): {}[];
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
- var newObj = {};
13
- var keysToRename = keys ? keys : Object.keys(obj);
14
- keysToRename.forEach(function (key) {
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
- var camelCaseKey = key.charAt(0).toLowerCase() + key.substr(1);
19
- Object.assign(newObj, (_a = {}, _a[camelCaseKey] = obj[key], _a));
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(function (obj) { return makeCamelCase(obj, keys); });
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(function (res, rej) {
39
- var reader = new FileReader();
40
- reader.onload = function (_event) {
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 = function (err) {
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