-
Notifications
You must be signed in to change notification settings - Fork 428
/
SpreadsheetExtractionAlgorithm.java
326 lines (275 loc) · 12.2 KB
/
SpreadsheetExtractionAlgorithm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package technology.tabula.extractors;
import technology.tabula.*;
import java.awt.geom.Point2D;
import java.util.*;
/**
* @author manuel
*
*/
public class SpreadsheetExtractionAlgorithm implements ExtractionAlgorithm {
private static final float MAGIC_HEURISTIC_NUMBER = 0.65f;
private static final Comparator<Point2D> Y_FIRST_POINT_COMPARATOR = (point1, point2) -> {
int compareY = compareRounded(point1.getY(), point2.getY());
if (compareY == 0) {
return compareRounded(point1.getX(), point2.getX());
}
return compareY;
};
private static final Comparator<Point2D> X_FIRST_POINT_COMPARATOR = (point1, point2) -> {
int compareX = compareRounded(point1.getX(), point2.getX());
if (compareX == 0) {
return compareRounded(point1.getY(), point2.getY());
}
return compareX;
};
private static int compareRounded(double d1, double d2) {
float d1Rounded = Utils.round(d1, 2);
float d2Rounded = Utils.round(d2, 2);
return Float.compare(d1Rounded, d2Rounded);
}
@Override
public List<Table> extract(Page page) {
return extract(page, page.getRulings());
}
/**
* Extract a list of Table from page using rulings as separators
*/
public List<Table> extract(Page page, List<Ruling> rulings) {
// split rulings into horizontal and vertical
List<Ruling> horizontalR = new ArrayList<>();
List<Ruling> verticalR = new ArrayList<>();
for (Ruling r: rulings) {
if (r.horizontal()) {
horizontalR.add(r);
}
else if (r.vertical()) {
verticalR.add(r);
}
}
horizontalR = Ruling.collapseOrientedRulings(horizontalR);
verticalR = Ruling.collapseOrientedRulings(verticalR);
List<Cell> cells = findCells(horizontalR, verticalR);
List<Rectangle> spreadsheetAreas = findSpreadsheetsFromCells(cells);
List<Table> spreadsheets = new ArrayList<>();
for (Rectangle area: spreadsheetAreas) {
List<Cell> overlappingCells = new ArrayList<>();
for (Cell c: cells) {
if (c.intersects(area)) {
c.setTextElements(TextElement.mergeWords(page.getText(c)));
overlappingCells.add(c);
}
}
List<Ruling> horizontalOverlappingRulings = new ArrayList<>();
for (Ruling hr: horizontalR) {
if (area.intersectsLine(hr)) {
horizontalOverlappingRulings.add(hr);
}
}
List<Ruling> verticalOverlappingRulings = new ArrayList<>();
for (Ruling vr: verticalR) {
if (area.intersectsLine(vr)) {
verticalOverlappingRulings.add(vr);
}
}
TableWithRulingLines t = new TableWithRulingLines(area, overlappingCells, horizontalOverlappingRulings, verticalOverlappingRulings, this, page.getPageNumber());
spreadsheets.add(t);
}
Utils.sort(spreadsheets, Rectangle.ILL_DEFINED_ORDER);
return spreadsheets;
}
public boolean isTabular(Page page) {
// if there's no text at all on the page, it's not a table
// (we won't be able to do anything with it though)
if (page.getText().isEmpty()){
return false;
}
// get minimal region of page that contains every character (in effect,
// removes white "margins")
Page minimalRegion = page.getArea(Utils.bounds(page.getText()));
List<? extends Table> tables = new SpreadsheetExtractionAlgorithm().extract(minimalRegion);
if (tables.isEmpty()) {
return false;
}
Table table = tables.get(0);
int rowsDefinedByLines = table.getRowCount();
int colsDefinedByLines = table.getColCount();
tables = new BasicExtractionAlgorithm().extract(minimalRegion);
if (tables.isEmpty()) {
return false;
}
table = tables.get(0);
int rowsDefinedWithoutLines = table.getRowCount();
int colsDefinedWithoutLines = table.getColCount();
float ratio = (((float) colsDefinedByLines / colsDefinedWithoutLines) +
((float) rowsDefinedByLines / rowsDefinedWithoutLines)) / 2.0f;
return ratio > MAGIC_HEURISTIC_NUMBER && ratio < (1 / MAGIC_HEURISTIC_NUMBER);
}
public static List<Cell> findCells(List<Ruling> horizontalRulingLines, List<Ruling> verticalRulingLines) {
List<Cell> cellsFound = new ArrayList<>();
Map<Point2D, Ruling[]> intersectionPoints = Ruling.findIntersections(horizontalRulingLines, verticalRulingLines);
List<Point2D> intersectionPointsList = new ArrayList<>(intersectionPoints.keySet());
intersectionPointsList.sort(Y_FIRST_POINT_COMPARATOR);
for (int i = 0; i < intersectionPointsList.size(); i++) {
Point2D topLeft = intersectionPointsList.get(i);
Ruling[] hv = intersectionPoints.get(topLeft);
List<Point2D> xPoints = new ArrayList<>();
List<Point2D> yPoints = new ArrayList<>();
for (Point2D p: intersectionPointsList.subList(i, intersectionPointsList.size())) {
if (p.getX() == topLeft.getX() && p.getY() > topLeft.getY()) {
xPoints.add(p);
}
if (p.getY() == topLeft.getY() && p.getX() > topLeft.getX()) {
yPoints.add(p);
}
}
outer:
for (Point2D xPoint: xPoints) {
// is there a vertical edge b/w topLeft and xPoint?
if (!hv[1].equals(intersectionPoints.get(xPoint)[1])) {
continue;
}
for (Point2D yPoint: yPoints) {
// is there an horizontal edge b/w topLeft and yPoint ?
if (!hv[0].equals(intersectionPoints.get(yPoint)[0])) {
continue;
}
Point2D btmRight = new Point2D.Float((float) yPoint.getX(), (float) xPoint.getY());
if (intersectionPoints.containsKey(btmRight)
&& intersectionPoints.get(btmRight)[0].equals(intersectionPoints.get(xPoint)[0])
&& intersectionPoints.get(btmRight)[1].equals(intersectionPoints.get(yPoint)[1])) {
cellsFound.add(new Cell(topLeft, btmRight));
break outer;
}
}
}
}
// TODO create cells for vertical ruling lines with aligned endpoints at the top/bottom of a grid
// that aren't connected with an horizontal ruler?
// see: https://github.com/jazzido/tabula-extractor/issues/78#issuecomment-41481207
return cellsFound;
}
public static List<Rectangle> findSpreadsheetsFromCells(List<? extends Rectangle> cells) {
// via: http://stackoverflow.com/questions/13746284/merging-multiple-adjacent-rectangles-into-one-polygon
List<Rectangle> rectangles = new ArrayList<>();
Set<Point2D> pointSet = new HashSet<>();
Map<Point2D, Point2D> edgesH = new HashMap<>();
Map<Point2D, Point2D> edgesV = new HashMap<>();
int i = 0;
cells = new ArrayList<>(new HashSet<>(cells));
Utils.sort(cells, Rectangle.ILL_DEFINED_ORDER);
for (Rectangle cell: cells) {
for(Point2D pt: cell.getPoints()) {
if (pointSet.contains(pt)) { // shared vertex, remove it
pointSet.remove(pt);
}
else {
pointSet.add(pt);
}
}
}
// X first sort
List<Point2D> pointsSortX = new ArrayList<>(pointSet);
pointsSortX.sort(X_FIRST_POINT_COMPARATOR);
// Y first sort
List<Point2D> pointsSortY = new ArrayList<>(pointSet);
pointsSortY.sort(Y_FIRST_POINT_COMPARATOR);
while (i < pointSet.size()) {
float currY = (float) pointsSortY.get(i).getY();
while (i < pointSet.size() && Utils.feq(pointsSortY.get(i).getY(), currY)) {
edgesH.put(pointsSortY.get(i), pointsSortY.get(i+1));
edgesH.put(pointsSortY.get(i+1), pointsSortY.get(i));
i += 2;
}
}
i = 0;
while (i < pointSet.size()) {
float currX = (float) pointsSortX.get(i).getX();
while (i < pointSet.size() && Utils.feq(pointsSortX.get(i).getX(), currX)) {
edgesV.put(pointsSortX.get(i), pointsSortX.get(i+1));
edgesV.put(pointsSortX.get(i+1), pointsSortX.get(i));
i += 2;
}
}
// Get all the polygons
List<List<PolygonVertex>> polygons = new ArrayList<>();
Point2D nextVertex;
while (!edgesH.isEmpty()) {
ArrayList<PolygonVertex> polygon = new ArrayList<>();
Point2D first = edgesH.keySet().iterator().next();
polygon.add(new PolygonVertex(first, Direction.HORIZONTAL));
edgesH.remove(first);
while (true) {
PolygonVertex curr = polygon.get(polygon.size() - 1);
PolygonVertex lastAddedVertex;
if (curr.direction == Direction.HORIZONTAL) {
nextVertex = edgesV.get(curr.point);
edgesV.remove(curr.point);
lastAddedVertex = new PolygonVertex(nextVertex, Direction.VERTICAL);
}
else {
nextVertex = edgesH.get(curr.point);
edgesH.remove(curr.point);
lastAddedVertex = new PolygonVertex(nextVertex, Direction.HORIZONTAL);
}
polygon.add(lastAddedVertex);
if (lastAddedVertex.equals(polygon.get(0))) {
// closed polygon
polygon.remove(polygon.size() - 1);
break;
}
}
for (PolygonVertex vertex: polygon) {
edgesH.remove(vertex.point);
edgesV.remove(vertex.point);
}
polygons.add(polygon);
}
// calculate grid-aligned minimum area rectangles for each found polygon
for(List<PolygonVertex> poly: polygons) {
float top = java.lang.Float.MAX_VALUE;
float left = java.lang.Float.MAX_VALUE;
float bottom = java.lang.Float.MIN_VALUE;
float right = java.lang.Float.MIN_VALUE;
for (PolygonVertex pt: poly) {
top = (float) Math.min(top, pt.point.getY());
left = (float) Math.min(left, pt.point.getX());
bottom = (float) Math.max(bottom, pt.point.getY());
right = (float) Math.max(right, pt.point.getX());
}
rectangles.add(new Rectangle(top, left, right - left, bottom - top));
}
return rectangles;
}
@Override
public String toString() {
return "lattice";
}
private enum Direction {
HORIZONTAL,
VERTICAL
}
static class PolygonVertex {
Point2D point;
Direction direction;
public PolygonVertex(Point2D point, Direction direction) {
this.direction = direction;
this.point = point;
}
@Override
public boolean equals(Object other) {
if (this == other)
return true;
if (!(other instanceof PolygonVertex))
return false;
return this.point.equals(((PolygonVertex) other).point);
}
@Override
public int hashCode() {
return this.point.hashCode();
}
@Override
public String toString() {
return String.format("%s[point=%s,direction=%s]", this.getClass().getName(), this.point.toString(), this.direction.toString());
}
}
}