-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathBasicExtractionAlgorithm.java
More file actions
166 lines (132 loc) · 5.2 KB
/
BasicExtractionAlgorithm.java
File metadata and controls
166 lines (132 loc) · 5.2 KB
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
package technology.tabula.extractors;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Arrays;
import technology.tabula.Line;
import technology.tabula.Page;
import technology.tabula.Rectangle;
import technology.tabula.Ruling;
import technology.tabula.Table;
import technology.tabula.TextChunk;
import technology.tabula.TextElement;
public class BasicExtractionAlgorithm implements ExtractionAlgorithm {
private List<Ruling> verticalRulings = null;
public BasicExtractionAlgorithm() {
}
public BasicExtractionAlgorithm(List<Ruling> verticalRulings) {
this.verticalRulings = verticalRulings;
}
public List<Table> extract(Page page, List<Float> verticalRulingPositions) {
List<Ruling> verticalRulings = new ArrayList<>(verticalRulingPositions.size());
for (Float p: verticalRulingPositions) {
verticalRulings.add(new Ruling(page.getTop(), p, 0.0f, (float) page.getHeight()));
}
this.verticalRulings = verticalRulings;
return this.extract(page);
}
@Override
public List<Table> extract(Page page) {
List<TextElement> textElements = page.getText();
if (textElements.size() == 0) {
return Arrays.asList(new Table[] { Table.empty() });
}
List<TextChunk> textChunks = this.verticalRulings == null ? TextElement.mergeWords(page.getText()) : TextElement.mergeWords(page.getText(), this.verticalRulings);
List<Line> lines = TextChunk.groupByLines(textChunks);
List<Float> columns = null;
if (this.verticalRulings != null) {
Collections.sort(this.verticalRulings, new Comparator<Ruling>() {
@Override
public int compare(Ruling arg0, Ruling arg1) {
return Double.compare(arg0.getLeft(), arg1.getLeft());
}
});
columns = new ArrayList<>(this.verticalRulings.size());
for (Ruling vr: this.verticalRulings) {
columns.add(vr.getLeft());
}
}
else {
columns = columnPositions(lines);
}
Table table = new Table(this);
table.setRect(page.getLeft(), page.getTop(), page.getWidth(), page.getHeight());
table.setPageNumber(page.getPageNumber());
for (int i = 0; i < lines.size(); i++) {
Line line = lines.get(i);
List<TextChunk> elements = line.getTextElements();
Collections.sort(elements, new Comparator<TextChunk>() {
@Override
public int compare(TextChunk o1, TextChunk o2) {
return new java.lang.Float(o1.getLeft()).compareTo(o2.getLeft());
}
});
for (TextChunk tc: elements) {
if (tc.isSameChar(Line.WHITE_SPACE_CHARS)) {
continue;
}
int j = 0;
boolean found = false;
for(; j < columns.size(); j++) {
if (tc.getLeft() <= columns.get(j)) {
found = true;
break;
}
}
table.add(tc, i, found ? j : columns.size());
}
}
return Arrays.asList(new Table[] { table } );
}
@Override
public String toString() {
return "stream";
}
/**
* @param lines must be an array of lines sorted by their +top+ attribute
* @return a list of column boundaries (x axis)
*/
public static List<java.lang.Float> columnPositions(List<Line> lines) {
List<Rectangle> regions = new ArrayList<>();
for (TextChunk tc: lines.get(0).getTextElements()) {
if (tc.isSameChar(Line.WHITE_SPACE_CHARS)) {
continue;
}
Rectangle r = new Rectangle();
r.setRect(tc);
regions.add(r);
}
for (Line l: lines.subList(1, lines.size())) {
List<TextChunk> lineTextElements = new ArrayList<>();
for (TextChunk tc: l.getTextElements()) {
if (!tc.isSameChar(Line.WHITE_SPACE_CHARS)) {
lineTextElements.add(tc);
}
}
for (Rectangle cr: regions) {
List<TextChunk> overlaps = new ArrayList<>();
for (TextChunk te: lineTextElements) {
if (cr.horizontallyOverlaps(te)) {
overlaps.add(te);
}
}
for (TextChunk te: overlaps) {
cr.merge(te);
}
lineTextElements.removeAll(overlaps);
}
for (TextChunk te: lineTextElements) {
Rectangle r = new Rectangle();
r.setRect(te);
regions.add(r);
}
}
List<java.lang.Float> rv = new ArrayList<>();
for (Rectangle r: regions) {
rv.add(r.getRight());
}
Collections.sort(rv);
return rv;
}
}