RED-5276: First idea for complex tables
This commit is contained in:
parent
5feb6891e2
commit
cdb676a672
@ -1,13 +1,17 @@
|
||||
package com.iqser.red.service.redaction.v1.server.tableextraction.model;
|
||||
|
||||
import java.awt.geom.Point2D;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
|
||||
@ -261,30 +265,58 @@ public class Table extends AbstractTextContainer {
|
||||
if (cells.isEmpty()) {
|
||||
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();
|
||||
Cell c = iter.next();
|
||||
float lastTop = c.getBottom();
|
||||
List<Cell> lastRow = new ArrayList<>();
|
||||
lastRow.add(c);
|
||||
rv.add(lastRow);
|
||||
Set<Float> uniqueY = new HashSet<>();
|
||||
cells.forEach(c -> uniqueY.add(c.getBottom()));
|
||||
cells.forEach(c -> uniqueY.add(c.getTop()));
|
||||
|
||||
while (iter.hasNext()) {
|
||||
c = iter.next();
|
||||
if (!Utils.feq(c.getBottom(), lastTop)) {
|
||||
lastRow = new ArrayList<>();
|
||||
rv.add(lastRow);
|
||||
var sortedUniqueX = uniqueX.stream().sorted().collect(Collectors.toList());
|
||||
var sortedUniqueY = uniqueY.stream().sorted().collect(Collectors.toList());
|
||||
|
||||
List<List<Cell>> matrix = new ArrayList<>();
|
||||
|
||||
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
|
||||
public String getText() {
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user