use of com.amplifyframework.predictions.models.BoundedKeyValue in project amplify-android by aws-amplify.
the class AWSTextractService method processTextractBlocks.
private IdentifyDocumentTextResult processTextractBlocks(List<Block> blocks) {
StringBuilder fullTextBuilder = new StringBuilder();
List<String> rawLineText = new ArrayList<>();
List<IdentifiedText> words = new ArrayList<>();
List<IdentifiedText> lines = new ArrayList<>();
List<Selection> selections = new ArrayList<>();
List<Table> tables = new ArrayList<>();
List<BoundedKeyValue> keyValues = new ArrayList<>();
List<Block> tableBlocks = new ArrayList<>();
List<Block> keyValueBlocks = new ArrayList<>();
Map<String, Block> blockMap = new HashMap<>();
for (Block block : blocks) {
// This is the map that will be used for traversing the graph.
// Each block can contain "relationships", which point to other blocks by ID.
String id = block.getId();
blockMap.put(id, block);
BlockType type = BlockType.fromValue(block.getBlockType());
switch(type) {
case LINE:
rawLineText.add(block.getText());
lines.add(TextractResultTransformers.fetchIdentifiedText(block));
continue;
case WORD:
fullTextBuilder.append(block.getText()).append(" ");
words.add(TextractResultTransformers.fetchIdentifiedText(block));
continue;
case SELECTION_ELEMENT:
selections.add(TextractResultTransformers.fetchSelection(block));
continue;
case TABLE:
tableBlocks.add(block);
continue;
case KEY_VALUE_SET:
keyValueBlocks.add(block);
continue;
default:
}
}
for (Block tableBlock : tableBlocks) {
Table table = TextractResultTransformers.fetchTable(tableBlock, blockMap);
if (table != null) {
tables.add(table);
}
}
for (Block keyValueBlock : keyValueBlocks) {
BoundedKeyValue keyValue = TextractResultTransformers.fetchKeyValue(keyValueBlock, blockMap);
if (keyValue != null) {
keyValues.add(keyValue);
}
}
return IdentifyDocumentTextResult.builder().fullText(fullTextBuilder.toString().trim()).rawLineText(rawLineText).lines(lines).words(words).selections(selections).tables(tables).keyValues(keyValues).build();
}
use of com.amplifyframework.predictions.models.BoundedKeyValue in project amplify-android by aws-amplify.
the class TextractResultTransformersTest method testKeyValueConversion.
/**
* Tests that the graph of related blocks from Textract is properly
* converted to an Amplify key-value pair item.
*/
@Test
public void testKeyValueConversion() {
Block valueTextBlock = new Block().withId(RandomString.string()).withText(RandomString.string());
Block keyTextBlock = new Block().withId(RandomString.string()).withText(RandomString.string());
Block valueBlock = new Block().withId(RandomString.string()).withBlockType(BlockType.KEY_VALUE_SET).withEntityTypes(EntityType.VALUE.toString()).withRelationships(new Relationship().withIds(valueTextBlock.getId()));
Block keyBlock = new Block().withId(RandomString.string()).withBlockType(BlockType.KEY_VALUE_SET).withEntityTypes(EntityType.KEY.toString()).withConfidence(random.nextFloat()).withGeometry(randomGeometry()).withRelationships(new Relationship().withIds(keyTextBlock.getId(), valueBlock.getId()));
// Construct a map to act as a graph
Map<String, Block> blockMap = new HashMap<>();
Observable.fromArray(valueTextBlock, keyTextBlock, valueBlock, keyBlock).blockingForEach(block -> blockMap.put(block.getId(), block));
// Test block conversion
BoundedKeyValue keyValue = TextractResultTransformers.fetchKeyValue(keyBlock, blockMap);
assertEquals(keyTextBlock.getText(), keyValue.getKey());
assertEquals(valueTextBlock.getText(), keyValue.getKeyValue());
assertEquals(keyBlock.getConfidence(), keyValue.getConfidence(), DELTA);
}
use of com.amplifyframework.predictions.models.BoundedKeyValue in project amplify-android by aws-amplify.
the class AWSPredictionsIdentifyTextTest method testIdentifyForms.
/**
* Assert form detection works.
* @throws PredictionsException if prediction fails
*/
@Test
public void testIdentifyForms() throws PredictionsException {
final Bitmap image = Assets.readAsBitmap("sample-form.png");
// Identify the text inside given image and assert non-null result.
IdentifyDocumentTextResult result = (IdentifyDocumentTextResult) predictions.identify(TextFormatType.FORM, image);
// Assert that four key-values are detected.
List<BoundedKeyValue> keyValues = result.getKeyValues();
assertFalse(Empty.check(keyValues));
assertEquals(4, keyValues.size());
// Assert that key-value pairs have correct values.
FeatureAssert.assertContains(Pair.create("Name:", "Jane Doe"), keyValues);
FeatureAssert.assertContains(Pair.create("Address:", "123 Any Street, Anytown, USA"), keyValues);
FeatureAssert.assertContains(Pair.create("Birth date:", "12-26-1980"), keyValues);
// FeatureAssert.assertContains(Pair.create("Male:", "true"), keyValues); // Selection not supported
}
Aggregations