RED-5276: First idea for complex tables

This commit is contained in:
deiflaender 2022-11-02 09:56:41 +01:00
parent 5feb6891e2
commit cdb676a672

View File

@ -1,13 +1,17 @@
package com.iqser.red.service.redaction.v1.server.tableextraction.model; package com.iqser.red.service.redaction.v1.server.tableextraction.model;
import java.awt.geom.Point2D;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
@ -261,30 +265,58 @@ public class Table extends AbstractTextContainer {
if (cells.isEmpty()) { if (cells.isEmpty()) {
return rv; return rv;
} }
cells.sort(Comparator.comparingDouble(Rectangle::getLeft));
cells.sort(Collections.reverseOrder((arg0, arg1) -> Float.compare(Utils.round(arg0.getBottom(), 2), Utils.round(arg1.getBottom(), 2)))); Set<Float> uniqueX = new HashSet<>();
cells.forEach(c -> uniqueX.add(c.getLeft()));
cells.forEach(c -> uniqueX.add(c.getRight()));
Iterator<Cell> iter = cells.iterator(); Set<Float> uniqueY = new HashSet<>();
Cell c = iter.next(); cells.forEach(c -> uniqueY.add(c.getBottom()));
float lastTop = c.getBottom(); cells.forEach(c -> uniqueY.add(c.getTop()));
List<Cell> lastRow = new ArrayList<>();
lastRow.add(c);
rv.add(lastRow);
while (iter.hasNext()) { var sortedUniqueX = uniqueX.stream().sorted().collect(Collectors.toList());
c = iter.next(); var sortedUniqueY = uniqueY.stream().sorted().collect(Collectors.toList());
if (!Utils.feq(c.getBottom(), lastTop)) {
lastRow = new ArrayList<>(); List<List<Cell>> matrix = new ArrayList<>();
rv.add(lastRow);
Float prevY = null;
var uniqueYIterator = sortedUniqueY.iterator();
while (uniqueYIterator.hasNext()) {
List<Cell> row = new ArrayList<>();
var y = uniqueYIterator.next();
var uniqueXIterator = sortedUniqueX.iterator();
Float prevX = null;
while (uniqueXIterator.hasNext()) {
var x = uniqueXIterator.next();
if (prevY != null && prevX != null) {
var cell = new Cell(new Point2D.Float(prevX, prevY), new Point2D.Float(x, y));
var intersectionCell = cells.stream().filter(c -> cell.intersects(c) && cell.overlapRatio(c) > 0.1f).findFirst();
if (intersectionCell.isPresent()) {
cell.getTextBlocks().addAll(intersectionCell.get().getTextBlocks());
}
row.add(cell);
}
prevX = x;
} }
lastRow.add(c);
lastTop = c.getBottom(); if (prevY != null && prevX != null) {
matrix.add(row);
}
prevY = y;
} }
return rv;
Collections.reverse(matrix);
return matrix;
} }
@Override @Override
public String getText() { public String getText() {