AWS-Textract-Key-Value-Pair Java-线程"main"; java.lang.NullPointerException [英] AWS-Textract-Key-Value-Pair Java - thread "main" java.lang.NullPointerException

查看:111
本文介绍了AWS-Textract-Key-Value-Pair Java-线程"main"; java.lang.NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java Spring引导项目中使用AWS Textract.我已经设置了AWS CLI,并将SDK作为maven依赖项.

I am using AWS Textract in a Java Spring boot project. I have set up AWS CLI and have the SDK as a maven dependency.

我已经编写了从C#转换为提取键和值对的Java代码,并且在成功提取了一些单词后收到以下错误消息

I have written Java code, converted from C# in order to extract the Key and Value pairs and I am receiving the following error after successfully extracting some words

" AGENCYCUSTOMERID:FEIN(如果适用),MARITALSTATUS/CIVILUNION(如果适用),请确保保险代码BUSPRIMARYE-MAILADDRESS:FEIN(如果适用)LINEOFBUSINESSCELLMARITALSTATUScivilUNION(如果适用),CELLCELLHOME

AGENCYCUSTOMERID:FEIN(ifapplicable)MARITALSTATUS/CIVILUNION(ifapplicable)INSUREDLOCATIONCODEBUSPRIMARYE-MAILADDRESS:FEIN(ifapplicable)LINEOFBUSINESSCELLMARITALSTATUScivilUNION(ifapplicable)CELLCELLHOMEException in thread "main" java.lang.NullPointerException
at ai.tautona.lloyds.mailboxprocessor.service.AWSTextractService.Get_text(AWSTextractService.java:112)
at ai.tautona.lloyds.mailboxprocessor.service.AWSTextractService.getKVMapRelationship(AWSTextractService.java:74)
at ai.tautona.lloyds.mailboxprocessor.service.AWSTextractService.getKVMap(AWSTextractService.java:57)
at ai.tautona.lloyds.mailboxprocessor.service.AWSTextractService.main(AWSTextractService.java:148)

通过调试,我发现导致错误的行是:

Through debugging I found the line that is causing the error to be :

   text += "X ";

似乎在找到选择元素/复选框后失败了吗?

It appears that after finding a SELECTION ELEMENT / CHECKBOX it fails?

我的代码:

 public class AWSTextractService {


public static void getKVMap(String localFile) throws IOException {

    File file = new File(localFile);
    byte[] fileContent = Files.readAllBytes(file.toPath());
    AmazonTextract client = AmazonTextractClientBuilder.defaultClient();

    AnalyzeDocumentRequest request = new AnalyzeDocumentRequest()
        .withDocument(new Document()
            .withBytes(ByteBuffer.wrap(fileContent))).withFeatureTypes(FeatureType.FORMS);


    AnalyzeDocumentResult result = client.analyzeDocument(request);


    //Get the text blocks
    List<Block> blocks = result.getBlocks();

    //get key and value maps
    List<Block> key_map = new ArrayList<>();
    List<Block> value_map = new ArrayList<>();
    List<Block> block_map = new ArrayList<>();

    for (Block block : blocks) {
        block_map.add(block);
        if (block.getBlockType().equals("KEY_VALUE_SET")) {
            if (block.getEntityTypes().contains("KEY")) {
                key_map.add(block);
            } else {
                value_map.add(block);
            }

        }

    }

    //Get Key Value relationship
    getKVMapRelationship(key_map, value_map, block_map).forEach((k, v) -> System.out.println("key: " + k + " value:" + v));

   getKeyValueRelationship.forEach((k,v)-> System.out.println("key: "+k+" value:"+v));


}


@NotNull
public static HashMap<String, String> getKVMapRelationship(List<Block> key_map, List<Block> value_map, List<Block> block_map) throws IOException {
    HashMap<String, String> kvs = new HashMap<>();
    ;
    Block value_block;
    String key, val = "";
    for (Block key_block : key_map) {
        value_block = Find_value_block(key_block, value_map);
        key = Get_text(key_block, block_map);
        val = Get_text(value_block, block_map);
        System.out.printf(key, val);
        kvs.put("1", "2");
    }

    return kvs;

}

@NotNull
public static Block Find_value_block(Block block, List<Block> value_map) {
    Block value_block = new Block();
    for (Relationship relationship : block.getRelationships()) {
        if (relationship.getType().equals("VALUE")) {
            for (String value_id : relationship.getIds()) {

                for (Block value : value_map) {
                    if (value.getId().equals(value_id)) {
                        value_block = value;
                    }

                }

            }

        }

    }
    return value_block;

}

//null
@NotNull
public static String Get_text(Block result, List<Block> block_map) throws IOException {
    String text = "";
    Block word = new Block();
    Block word2 = null;
    if (result.getRelationships().stream().count() > 0) {
        for (Relationship relationship : result.getRelationships()) {
            if (relationship.getType().equals("CHILD")) {
                for (String child_id : relationship.getIds()) {

                    word = block_map.stream()
                        .filter((x)-> x.getId().equals(child_id)).findFirst().orElse(word2);


                    if (word.getBlockType().equals("WORD"))
                    {
                        text += (word.getText() ==null ? "" : word.getText()) + "";
                    }
                    if (word.getBlockType().equals("SELECTION_ELEMENT"))

                    {
                        if(word.getSelectionStatus().equals("SELECTED"))

                        {
                            text += "X ";

                        }
                    }
                }
            }
        }

    }

    return text;

}
public static void main (String[]args) throws IOException {

    String fileStr = "/home/daniel/Documents/atrium_sources/accordImage-1.png";

    AWSTextractService.getKVMap(fileStr);

    System.out.println("Done!");
}

}

我不确定是什么问题吗?

Im not sure what is the issue?

推荐答案

我非常确定其他Java开发人员会喜欢此代码.我在

I am very sure other Java Devs are going to appreciate this Code. I answered my question with the help of Rikus.

    package ai.tautona.lloyds.mailboxprocessor.service;
import com.amazonaws.services.textract.AmazonTextract;
import com.amazonaws.services.textract.AmazonTextractClientBuilder;
import com.amazonaws.services.textract.model.Document;
import java.nio.file.Files;
import com.amazonaws.services.textract.model.*;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.*;


@Service
@Transactional

public class AWSTextractService {

public static void getKVMap(String localFile) throws IOException {

    File file = new File(localFile);
    byte[] fileContent = Files.readAllBytes(file.toPath());
    AmazonTextract client = AmazonTextractClientBuilder.defaultClient();

    AnalyzeDocumentRequest request = new AnalyzeDocumentRequest()
        .withDocument(new Document()
            .withBytes(ByteBuffer.wrap(fileContent))).withFeatureTypes(FeatureType.FORMS);


    AnalyzeDocumentResult result = client.analyzeDocument(request);


    //Get the text blocks
    List<Block> blocks = result.getBlocks();

    //get key and value maps
    List<Block> key_map = new ArrayList<>();
    List<Block> value_map = new ArrayList<>();
    List<Block> block_map = new ArrayList<>();

    for (Block block : blocks) {
        block_map.add(block);
        if (block.getBlockType().equals("KEY_VALUE_SET")) {
            if (block.getEntityTypes().contains("KEY")) {
                key_map.add(block);
            } else {
                value_map.add(block);
            }

        }

    }

    //Get Key Value relationship
    getKVMapRelationship(key_map, value_map, block_map).forEach((k, v) -> System.out.println("key: " + k + " value:" + v));




}


@NotNull
public static HashMap<String, String> getKVMapRelationship(List<Block> key_map, List<Block> value_map, List<Block> block_map) throws IOException {
    HashMap<String, String> kvs = new HashMap<>();
    ;
    Block value_block;
    String key, val = "";
    for (Block key_block : key_map) {
        value_block = Find_value_block(key_block, value_map);
        key = Get_text(key_block, block_map);
        val = Get_text(value_block, block_map);

        kvs.put(key, val);
    }

    return kvs;

}

@NotNull
public static Block Find_value_block(Block block, List<Block> value_map) {
    Block value_block = new Block();
    for (Relationship relationship : block.getRelationships()) {
        if (relationship.getType().equals("VALUE")) {
            for (String value_id : relationship.getIds()) {

                for (Block value : value_map) {
                    if (value.getId().equals(value_id)) {
                        value_block = value;
                    }

                }

            }

        }

    }
    return value_block;

}

//null
@NotNull
public static String Get_text(Block result, List<Block> block_map) throws IOException {
    String text = "";
    Block word2= new Block();
    try {

        if (result != null
            && CollectionUtils.isNotEmpty(result.getRelationships())) {

            for (Relationship relationship : result.getRelationships()) {

                if (relationship.getType().equals("CHILD")) {

                    for (String id : relationship.getIds()) {

                        Block word= (block_map.stream().filter(x-> x.getId().equals(id)).findFirst().orElse(word2));


                        if (word.getBlockType().equals("WORD")) {
                            text += word.getText() + " ";
                        } else if (word.getBlockType().equals("SELECTION_ELEMENT")) {

                            if (word.getSelectionStatus().equals("SELECTED")) {
                                text += "X ";
                            }
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        System.out.println(e);
    }
    return text;
}

public static void main (String[]args) throws IOException {

    String fileStr = "/home/daniel/Documents/atrium_sources/accordImage-1.png";

    AWSTextractService.getKVMap(fileStr);

    System.out.println("Done!");
}

}

这篇关于AWS-Textract-Key-Value-Pair Java-线程"main"; java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆