如何在java中使用AWS Textract检索pdf中存在的表

2023-12-27

我发现下面的文章是用 python 做的。

https://docs.aws.amazon.com/texttract/latest/dg/examples-export-table-csv.html https://docs.aws.amazon.com/textract/latest/dg/examples-export-table-csv.html

我还使用下面的文章来提取文本。

https://docs.aws.amazon.com/texttract/latest/dg/detecting-document-text.html https://docs.aws.amazon.com/textract/latest/dg/detecting-document-text.html

但上面的文章只帮助获取文本,我还使用了函数“block.getBlockType()” 的块,但没有一个块返回其类型为“CELL”,即使表格存在于图像/pdf中。

帮我找到类似于“boto3”的java库来提取所有表。


我所做的是,我在 json 响应中创建了每个数据集的模型,并可以使用该模型在 jsf 中构建表视图。

public static List<TableModel> getTablesFromTextract(TextractModel textractModel) {
    List<TableModel> tables = null;

    try {

        if (textractModel != null) {
            tables = new ArrayList<>();
            List<BlockModel> tableBlocks = new ArrayList<>();
            Map<String, BlockModel> blockMap = new HashMap<>();

            for (BlockModel block : textractModel.getBlocks()) {

                if (block.getBlockType().equals("TABLE")) {
                    tableBlocks.add(block);

                }
                blockMap.put(block.getId(), block);
            }

            for (BlockModel blockModel : tableBlocks) {

                Map<Long, Map<Long, String>> rowMap = new HashMap<>();

                for (RelationshipModel relationship : blockModel.getRelationships()) {

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

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

                            BlockModel cell = blockMap.get(id);

                            if (cell.getBlockType().equals("CELL")) {

                                long rowIndex = cell.getRowIndex();
                                long columnIndex = cell.getColumnIndex();

                                if (!rowMap.containsKey(rowIndex)) {
                                    rowMap.put(rowIndex, new HashMap<>());
                                }

                                Map<Long, String> columnMap = rowMap.get(rowIndex);
                                columnMap.put(columnIndex, getCellText(cell, blockMap));
                            }
                        }
                    }
                }
                tables.add(new TableModel(blockModel, rowMap));
            }
            System.out.println("row Map " + tables.toString());
        }
    } catch (Exception e) {
        LOG.error("Could not get table from textract model", e);
    }
    return tables;
}

private static String getCellText(BlockModel cell, Map<String, BlockModel> blockMap) {
    String text = "";

    try {

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

            for (RelationshipModel relationship : cell.getRelationships()) {

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

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

                        BlockModel word = blockMap.get(id);

                        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) {
        LOG.error("Could not get cell text of table", e);
    }
    return text;
}

用于创建视图的 TableModel:

public class TableModel {

private BlockModel table;
private Map<Long, Map<Long, String>> rowMap;

public TableModel(BlockModel table, Map<Long, Map<Long, String>> rowMap) {
    this.table = table;
    this.rowMap = rowMap;
}

public BlockModel getTable() {
    return table;
}

public void setTable(BlockModel table) {
    this.table = table;
}

public Map<Long, Map<Long, String>> getRowMap() {
    return rowMap;
}

public void setRowMap(Map<Long, Map<Long, String>> rowMap) {
    this.rowMap = rowMap;
}

@Override
public String toString() {
    return table.getId() + " - " + rowMap.toString();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在java中使用AWS Textract检索pdf中存在的表 的相关文章

随机推荐