-
Notifications
You must be signed in to change notification settings - Fork 429
/
TestSpreadsheetExtractor.java
547 lines (464 loc) · 28.1 KB
/
TestSpreadsheetExtractor.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
package technology.tabula;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.awt.geom.Point2D;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.junit.Test;
import technology.tabula.extractors.SpreadsheetExtractionAlgorithm;
import technology.tabula.writers.CSVWriter;
import technology.tabula.writers.JSONWriter;
public class TestSpreadsheetExtractor {
public static final Rectangle[] EXPECTED_RECTANGLES = {
new Rectangle(40.0f, 18.0f, 208.0f, 40.0f),
new Rectangle(84.0f, 18.0f, 962.0f, 464.0f)
};
private static final Ruling[] VERTICAL_RULING_LINES = {
new Ruling(40.0f, 18.0f, 0.0f, 40.0f),
new Ruling(44.0f, 70.0f, 0.0f, 36.0f),
new Ruling(40.0f, 226.0f, 0.0f, 40.0f)
};
private static final Ruling[] HORIZONTAL_RULING_LINES = {
new Ruling(40.0f, 18.0f, 208.0f, 0.0f),
new Ruling(44.0f, 18.0f, 208.0f, 0.0f),
new Ruling(50.0f, 18.0f, 208.0f, 0.0f),
new Ruling(54.0f, 18.0f, 208.0f, 0.0f),
new Ruling(60.0f, 18.0f, 208.0f, 0.0f),
new Ruling(64.0f, 18.0f, 208.0f, 0.0f),
new Ruling(70.0f, 18.0f, 208.0f, 0.0f),
new Ruling(74.0f, 18.0f, 208.0f, 0.0f),
new Ruling(80.0f, 18.0f, 208.0f, 0.0f)
};
private static final Cell[] EXPECTED_CELLS = {
new Cell(40.0f, 18.0f, 208.0f, 4.0f),
new Cell(44.0f, 18.0f, 52.0f, 6.0f),
new Cell(50.0f, 18.0f, 52.0f, 4.0f),
new Cell(54.0f, 18.0f, 52.0f, 6.0f),
new Cell(60.0f, 18.0f, 52.0f, 4.0f),
new Cell(64.0f, 18.0f, 52.0f, 6.0f),
new Cell(70.0f, 18.0f, 52.0f, 4.0f),
new Cell(74.0f, 18.0f, 52.0f, 6.0f),
new Cell(44.0f, 70.0f, 156.0f, 6.0f),
new Cell(50.0f, 70.0f, 156.0f, 4.0f),
new Cell(54.0f, 70.0f, 156.0f, 6.0f),
new Cell(60.0f, 70.0f, 156.0f, 4.0f),
new Cell(64.0f, 70.0f, 156.0f, 6.0f),
new Cell(70.0f, 70.0f, 156.0f, 4.0f),
new Cell(74.0f, 70.0f, 156.0f, 6.0f)};
private static final Ruling[][] SINGLE_CELL_RULINGS = {
{
new Ruling(new Point2D.Float(151.653545f, 185.66929f), new Point2D.Float(380.73438f, 185.66929f)),
new Ruling(new Point2D.Float(151.653545f, 314.64567f), new Point2D.Float(380.73438f, 314.64567f))
},
{
new Ruling(new Point2D.Float(151.653545f, 185.66929f), new Point2D.Float(151.653545f, 314.64567f)),
new Ruling(new Point2D.Float(380.73438f, 185.66929f), new Point2D.Float(380.73438f, 314.64567f))
}
};
private static final Ruling[][] TWO_SINGLE_CELL_RULINGS = {
{
new Ruling(new Point2D.Float(151.653545f, 185.66929f), new Point2D.Float(287.4074f, 185.66929f)),
new Ruling(new Point2D.Float(151.653545f, 262.101f), new Point2D.Float(287.4074f, 262.101f)),
new Ruling(new Point2D.Float(232.44095f, 280.62992f), new Point2D.Float(368.1948f, 280.62992f)),
new Ruling(new Point2D.Float(232.44095f, 357.06164f), new Point2D.Float(368.1948f, 357.06164f))
},
{
new Ruling(new Point2D.Float(151.653545f, 185.66929f), new Point2D.Float(151.653545f, 262.101f)),
new Ruling(new Point2D.Float(287.4074f, 185.66929f), new Point2D.Float(287.4074f, 262.101f)),
new Ruling(new Point2D.Float(232.44095f, 280.62992f), new Point2D.Float(232.44095f, 357.06164f)),
new Ruling(new Point2D.Float(368.1948f, 280.62992f), new Point2D.Float(368.1948f, 357.06164f))
}
};
private static final Ruling[] EXTERNALLY_DEFINED_RULINGS = {
new Ruling(new Point2D.Float(320.0f, 285.0f), new Point2D.Float(564.4409f, 285.0f)),
new Ruling(new Point2D.Float(320.0f, 457.0f), new Point2D.Float(564.4409f, 457.0f)),
new Ruling(new Point2D.Float(320.0f, 331.0f), new Point2D.Float(564.4409f, 331.0f)),
new Ruling(new Point2D.Float(320.0f, 315.0f), new Point2D.Float(564.4409f, 315.0f)),
new Ruling(new Point2D.Float(320.0f, 347.0f), new Point2D.Float(564.4409f, 347.0f)),
new Ruling(new Point2D.Float(320.0f, 363.0f), new Point2D.Float(564.44088f, 363.0f)),
new Ruling(new Point2D.Float(320.0f, 379.0f), new Point2D.Float(564.44087f, 379.0f)),
new Ruling(new Point2D.Float(320.0f, 395.5f), new Point2D.Float(564.44086f, 395.5f)),
new Ruling(new Point2D.Float(320.00006f, 415.0f), new Point2D.Float(564.4409f, 415.0f)),
new Ruling(new Point2D.Float(320.00007f, 431.0f), new Point2D.Float(564.4409f, 431.0f)),
new Ruling(new Point2D.Float(320.0f, 285.0f), new Point2D.Float(320.0f, 457.0f)),
new Ruling(new Point2D.Float(565.0f, 285.0f), new Point2D.Float(564.4409f, 457.0f)),
new Ruling(new Point2D.Float(470.5542f, 285.0f), new Point2D.Float(470.36865f, 457.0f))
};
private static final Ruling[] EXTERNALLY_DEFINED_RULINGS2 = {
new Ruling(new Point2D.Float(51.796964f, 180.0f), new Point2D.Float(560.20312f, 180.0f)),
new Ruling(new Point2D.Float(51.797017f, 219.0f), new Point2D.Float(560.2031f, 219.0f)),
new Ruling(new Point2D.Float(51.797f, 239.0f), new Point2D.Float(560.2031f, 239.0f)),
new Ruling(new Point2D.Float(51.797f, 262.0f), new Point2D.Float(560.20312f, 262.0f)),
new Ruling(new Point2D.Float(51.797f, 283.50247f), new Point2D.Float(560.05024f, 283.50247f)),
new Ruling(new Point2D.Float(51.796964f, 309.0f), new Point2D.Float(560.20312f, 309.0f)),
new Ruling(new Point2D.Float(51.796982f, 333.0f), new Point2D.Float(560.20312f, 333.0f)),
new Ruling(new Point2D.Float(51.797f, 366.0f), new Point2D.Float(560.20312f, 366.0f)),
new Ruling(new Point2D.Float(52.0f, 181.0f), new Point2D.Float(51.797f, 366.0f)),
new Ruling(new Point2D.Float(208.62891f, 181.0f), new Point2D.Float(208.62891f, 366.0f)),
new Ruling(new Point2D.Float(357.11328f, 180.0f), new Point2D.Float(357.0f, 366.0f)),
new Ruling(new Point2D.Float(560.11328f, 180.0f), new Point2D.Float(560.0f, 366.0f))
};
@Test
public void testLinesToCells() {
List<Cell> cells = SpreadsheetExtractionAlgorithm.findCells(Arrays.asList(HORIZONTAL_RULING_LINES), Arrays.asList(VERTICAL_RULING_LINES));
Collections.sort(cells, Rectangle.ILL_DEFINED_ORDER);
List<Cell> expected = Arrays.asList(EXPECTED_CELLS);
Collections.sort(expected, Rectangle.ILL_DEFINED_ORDER);
assertTrue(cells.equals(expected));
}
@Test
public void testDetectSingleCell() {
List<Cell> cells = SpreadsheetExtractionAlgorithm.findCells(Arrays.asList(SINGLE_CELL_RULINGS[0]),
Arrays.asList(SINGLE_CELL_RULINGS[1]));
assertEquals(1, cells.size());
Cell cell = cells.get(0);
assertTrue(Utils.feq(151.65355, cell.getLeft()));
assertTrue(Utils.feq(185.6693, cell.getTop()));
assertTrue(Utils.feq(229.08083, cell.getWidth()));
assertTrue(Utils.feq(128.97636, cell.getHeight()));
}
@Test
public void testDetectTwoSingleCells() {
List<Cell> cells = SpreadsheetExtractionAlgorithm.findCells(Arrays.asList(TWO_SINGLE_CELL_RULINGS[0]),
Arrays.asList(TWO_SINGLE_CELL_RULINGS[1]));
assertEquals(2, cells.size());
// should not overlap
assertFalse(cells.get(0).intersects(cells.get(1)));
}
@Test
public void testFindSpreadsheetsFromCells() throws IOException {
CSVParser parse = org.apache.commons.csv.CSVParser.parse(new File("src/test/resources/technology/tabula/csv/TestSpreadsheetExtractor-CELLS.csv"),
Charset.forName("utf-8"),
CSVFormat.DEFAULT);
List<Cell> cells = new ArrayList<>();
for (CSVRecord record : parse) {
cells.add(new Cell(Float.parseFloat(record.get(0)),
Float.parseFloat(record.get(1)),
Float.parseFloat(record.get(2)),
Float.parseFloat(record.get(3))));
}
List<Rectangle> expected = Arrays.asList(EXPECTED_RECTANGLES);
Collections.sort(expected, Rectangle.ILL_DEFINED_ORDER);
List<Rectangle> foundRectangles = SpreadsheetExtractionAlgorithm.findSpreadsheetsFromCells(cells);
Collections.sort(foundRectangles, Rectangle.ILL_DEFINED_ORDER);
assertTrue(foundRectangles.equals(expected));
}
// TODO Add assertions
@Test
public void testSpreadsheetExtraction() throws IOException {
Page page = UtilsForTesting
.getAreaFromFirstPage(
"src/test/resources/technology/tabula/argentina_diputados_voting_record.pdf",
269.875f, 12.75f, 790.5f, 561f);
SpreadsheetExtractionAlgorithm.findCells(page.getHorizontalRulings(), page.getVerticalRulings());
page.getPDDoc().close();
}
@Test
public void testSpanningCells() throws IOException {
Page page = UtilsForTesting
.getPage("src/test/resources/technology/tabula/spanning_cells.pdf", 1);
String expectedJson = UtilsForTesting.loadJson("src/test/resources/technology/tabula/json/spanning_cells.json");
SpreadsheetExtractionAlgorithm se = new SpreadsheetExtractionAlgorithm();
List<Table> tables = se.extract(page);
assertEquals(2, tables.size());
StringBuilder sb = new StringBuilder();
(new JSONWriter()).write(sb, tables);
assertEquals(expectedJson, sb.toString());
page.getPDDoc().close();
}
@Test
public void testSpanningCellsToCsv() throws IOException {
Page page = UtilsForTesting
.getPage("src/test/resources/technology/tabula/spanning_cells.pdf", 1);
String expectedCsv = UtilsForTesting.loadCsv("src/test/resources/technology/tabula/csv/spanning_cells.csv");
SpreadsheetExtractionAlgorithm se = new SpreadsheetExtractionAlgorithm();
List<Table> tables = se.extract(page);
assertEquals(2, tables.size());
StringBuilder sb = new StringBuilder();
(new CSVWriter()).write(sb, tables);
assertEquals(expectedCsv, sb.toString());
page.getPDDoc().close();
}
@Test
public void testIncompleteGrid() throws IOException {
Page page = UtilsForTesting.getPage("src/test/resources/technology/tabula/china.pdf", 1);
SpreadsheetExtractionAlgorithm se = new SpreadsheetExtractionAlgorithm();
List<? extends Table> tables = se.extract(page);
assertEquals(2, tables.size());
page.getPDDoc().close();
}
@Test
public void testNaturalOrderOfRectanglesDoesNotBreakContract() throws IOException {
Page page = UtilsForTesting.getPage("src/test/resources/technology/tabula/us-017.pdf", 2);
SpreadsheetExtractionAlgorithm se = new SpreadsheetExtractionAlgorithm();
List<? extends Table> tables = se.extract(page);
StringBuilder sb = new StringBuilder();
(new CSVWriter()).write(sb, tables.get(0));
String result = sb.toString();
String expected = "Project,Agency,Institution\r\nNanotechnology and its publics,NSF,Pennsylvania State University\r\n\"Public information and deliberation in nanoscience and\rnanotechnology policy (SGER)\",Interagency,\"North Carolina State\rUniversity\"\r\n\"Social and ethical research and education in agrifood\rnanotechnology (NIRT)\",NSF,Michigan State University\r\n\"From laboratory to society: developing an informed\rapproach to nanoscale science and engineering (NIRT)\",NSF,University of South Carolina\r\nDatabase and innovation timeline for nanotechnology,NSF,UCLA\r\nSocial and ethical dimensions of nanotechnology,NSF,University of Virginia\r\n\"Undergraduate exploration of nanoscience,\rapplications and societal implications (NUE)\",NSF,\"Michigan Technological\rUniversity\"\r\n\"Ethics and belief inside the development of\rnanotechnology (CAREER)\",NSF,University of Virginia\r\n\"All centers, NNIN and NCN have a societal\rimplications components\",\"NSF, DOE,\rDOD, and NIH\",\"All nanotechnology centers\rand networks\"\r\n";
assertEquals(expected, result);
page.getPDDoc().close();
}
@Test
public void testMergeLinesCloseToEachOther() throws IOException {
Page page = UtilsForTesting.getPage("src/test/resources/technology/tabula/20.pdf", 1);
List<Ruling> rulings = page.getVerticalRulings();
float[] expectedRulings = new float[]{105.549774f, 107.52332f, 160.58167f, 377.1792f, 434.95804f, 488.21783f};
for (int i = 0; i < rulings.size(); i++) {
assertEquals(expectedRulings[i], rulings.get(i).getLeft(), 0.1);
}
assertEquals(6, rulings.size());
page.getPDDoc().close();
}
@Test
public void testSpreadsheetWithNoBoundingFrameShouldBeSpreadsheet() throws IOException {
Page page = UtilsForTesting.getAreaFromPage("src/test/resources/technology/tabula/spreadsheet_no_bounding_frame.pdf", 1,
150.56f, 58.9f, 654.7f, 536.12f);
String expectedCsv = UtilsForTesting.loadCsv("src/test/resources/technology/tabula/csv/spreadsheet_no_bounding_frame.csv");
SpreadsheetExtractionAlgorithm se = new SpreadsheetExtractionAlgorithm();
boolean isTabular = se.isTabular(page);
assertTrue(isTabular);
List<? extends Table> tables = se.extract(page);
StringBuilder sb = new StringBuilder();
(new CSVWriter()).write(sb, tables.get(0));
assertEquals(expectedCsv, sb.toString());
page.getPDDoc().close();
}
@Test
public void testExtractSpreadsheetWithinAnArea() throws IOException {
Page page = UtilsForTesting.getAreaFromPage(
"src/test/resources/technology/tabula/puertos1.pdf",
1,
273.9035714285714f, 30.32142857142857f, 554.8821428571429f, 546.7964285714286f);
SpreadsheetExtractionAlgorithm se = new SpreadsheetExtractionAlgorithm();
List<? extends Table> tables = se.extract(page);
Table table = tables.get(0);
assertEquals(15, table.getRows().size());
String expected = "\"\",TM,M.U$S,TM,M.U$S,TM,M.U$S,TM,M.U$S,TM,M.U$S,TM,M.U$S,TM\n" +
"Peces vivos,1,25,1,23,2,38,1,37,2,67,2,89,1\n" +
"\"Pescado fresco\n" +
"o refrigerado.\n" +
"exc. filetes\",7.704,7.175,8.931,6.892,12.635,10.255,16.742,13.688,14.357,11.674,13.035,13.429,9.727\n" +
"\"Pescado congelado\n" +
"exc. filetes\",90.560,105.950,112.645,108.416,132.895,115.874,152.767,133.765,148.882,134.847,156.619,165.134,137.179\n" +
"\"Filetes y demás car-\n" +
"nes de pescado\",105.434,200.563,151.142,218.389,152.174,227.780,178.123,291.863,169.422,313.735,176.427,381.640,144.814\n" +
"\"Pescado sec./sal./\n" +
"en salm. har./pol./\n" +
"pell. aptos\n" +
"p/c humano\",6.837,14.493,6.660,9.167,14.630,17.579,18.150,21.302,18.197,25.739,13.460,23.549,11.709\n" +
"Crustáceos,61.691,375.798,52.488,251.043,47.635,387.783,27.815,217.443,7.123,86.019,39.488,373.583,45.191\n" +
"Moluscos,162.027,174.507,109.436,111.443,90.834,104.741,57.695,109.141,98.182,206.304,187.023,251.352,157.531\n" +
"\"Prod. no exp. en\n" +
"otros capítulos.\n" +
"No apto p/c humano\",203,328,7,35,521,343,\"1,710\",\"1,568\",125,246,124,263,131\n" +
"\"Grasas y aceites de\n" +
"pescado y mamíferos\n" +
"marinos\",913,297,\"1,250\",476,\"1,031\",521,\"1,019\",642,690,483,489,710,959\n" +
"\"Extractos y jugos de\n" +
"pescado y mariscos\",5,25,1,3,4,4,31,93,39,117,77,230,80\n" +
"\"Preparaciones y con-\n" +
"servas de pescado\",846,\"3,737\",\"1,688\",\"4,411\",\"1,556\",\"3,681\",\"2,292\",\"5,474\",\"2,167\",\"7,494\",\"2,591\",\"8,833\",\"2,795\"\n" +
"\"Preparaciones y con-\n" +
"servas de mariscos\",348,\"3,667\",345,\"1,771\",738,\"3,627\",561,\"2,620\",607,\"3,928\",314,\"2,819\",250\n" +
"\"Harina, polvo y pe-\n" +
"llets de pescado.No\n" +
"aptos p/c humano\",\"16,947\",\"8,547\",\"11,867\",\"6,315\",\"32,528\",\"13,985\",\"37,313\",\"18,989\",\"35,787\",\"19,914\",\"37,821\",\"27,174\",\"30,000\"\n" +
"TOTAL,\"453,515\",\"895,111\",\"456,431\",\"718,382\",\"487,183\",\"886,211\",\"494,220\",\"816,623\",\"495,580\",\"810,565\",\"627,469\",\"1,248,804\",\"540,367\"\n";
// TODO add better assertions
StringBuilder sb = new StringBuilder();
(new CSVWriter()).write(sb, tables.get(0));
String result = sb.toString();
List<CSVRecord> parsedExpected = org.apache.commons.csv.CSVParser.parse(expected, CSVFormat.EXCEL).getRecords();
List<CSVRecord> parsedResult = org.apache.commons.csv.CSVParser.parse(result, CSVFormat.EXCEL).getRecords();
assertEquals(parsedResult.size(), parsedExpected.size());
for (int i = 0; i < parsedResult.size(); i++) {
assertEquals(parsedResult.get(i).size(), parsedExpected.get(i).size());
}
page.getPDDoc().close();
}
@Test
public void testAlmostIntersectingRulingsShouldIntersect() {
Ruling v = new Ruling(new Point2D.Float(555.960876f, 271.569641f), new Point2D.Float(555.960876f, 786.899902f));
Ruling h = new Ruling(new Point2D.Float(25.620499f, 786.899902f), new Point2D.Float(555.960754f, 786.899902f));
Map<Point2D, Ruling[]> m = Ruling.findIntersections(Arrays.asList(new Ruling[]{h}), Arrays.asList(new Ruling[]{v}));
assertEquals(m.values().size(), 1);
}
// TODO add assertions
@Test
public void testDontRaiseSortException() throws IOException {
Page page = UtilsForTesting.getAreaFromPage(
"src/test/resources/technology/tabula/us-017.pdf",
2,
446.0f, 97.0f, 685.0f, 520.0f);
page.getText();
SpreadsheetExtractionAlgorithm bea = new SpreadsheetExtractionAlgorithm();
bea.extract(page).get(0);
page.getPDDoc().close();
}
@Test
public void testShouldDetectASingleSpreadsheet() throws IOException {
Page page = UtilsForTesting.getAreaFromPage(
"src/test/resources/technology/tabula/offense.pdf",
1,
68.08f, 16.44f, 680.85f, 597.84f);
SpreadsheetExtractionAlgorithm bea = new SpreadsheetExtractionAlgorithm();
List<Table> tables = bea.extract(page);
assertEquals(1, tables.size());
page.getPDDoc().close();
}
@Test
public void testExtractTableWithExternallyDefinedRulings() throws IOException {
Page page = UtilsForTesting.getPage("src/test/resources/technology/tabula/us-007.pdf",
1);
SpreadsheetExtractionAlgorithm bea = new SpreadsheetExtractionAlgorithm();
List<Table> tables = bea.extract(page,
Arrays.asList(EXTERNALLY_DEFINED_RULINGS));
assertEquals(1, tables.size());
Table table = tables.get(0);
assertEquals("Payroll Period", table.getRows().get(0).get(0).getText());
assertEquals("One Withholding\rAllowance", table.getRows().get(0).get(1).getText());
assertEquals("Weekly", table.getRows().get(1).get(0).getText());
assertEquals("$71.15", table.getRows().get(1).get(1).getText());
assertEquals("Biweekly", table.getRows().get(2).get(0).getText());
assertEquals("142.31", table.getRows().get(2).get(1).getText());
assertEquals("Semimonthly", table.getRows().get(3).get(0).getText());
assertEquals("154.17", table.getRows().get(3).get(1).getText());
assertEquals("Monthly", table.getRows().get(4).get(0).getText());
assertEquals("308.33", table.getRows().get(4).get(1).getText());
assertEquals("Quarterly", table.getRows().get(5).get(0).getText());
assertEquals("925.00", table.getRows().get(5).get(1).getText());
assertEquals("Semiannually", table.getRows().get(6).get(0).getText());
assertEquals("1,850.00", table.getRows().get(6).get(1).getText());
assertEquals("Annually", table.getRows().get(7).get(0).getText());
assertEquals("3,700.00", table.getRows().get(7).get(1).getText());
assertEquals("Daily or Miscellaneous\r(each day of the payroll period)", table.getRows().get(8).get(0).getText());
assertEquals("14.23", table.getRows().get(8).get(1).getText());
page.getPDDoc().close();
}
@Test
public void testAnotherExtractTableWithExternallyDefinedRulings() throws IOException {
Page page = UtilsForTesting.getPage("src/test/resources/technology/tabula/us-024.pdf",
1);
SpreadsheetExtractionAlgorithm bea = new SpreadsheetExtractionAlgorithm();
List<Table> tables = bea.extract(page,
Arrays.asList(EXTERNALLY_DEFINED_RULINGS2));
assertEquals(1, tables.size());
Table table = tables.get(0);
assertEquals("Total Supply", table.getRows().get(4).get(0).getText());
assertEquals("6.6", table.getRows().get(6).get(2).getText());
page.getPDDoc().close();
}
@Test
public void testSpreadsheetsSortedByTopAndRight() throws IOException {
Page page = UtilsForTesting.getPage("src/test/resources/technology/tabula/sydney_disclosure_contract.pdf",
1);
SpreadsheetExtractionAlgorithm sea = new SpreadsheetExtractionAlgorithm();
List<Table> tables = sea.extract(page);
for (int i = 1; i < tables.size(); i++) {
assert (tables.get(i - 1).getTop() <= tables.get(i).getTop());
}
page.getPDDoc().close();
}
@Test
public void testDontStackOverflowQuicksort() throws IOException {
Page page = UtilsForTesting.getPage("src/test/resources/technology/tabula/failing_sort.pdf",
1);
SpreadsheetExtractionAlgorithm sea = new SpreadsheetExtractionAlgorithm();
List<Table> tables = sea.extract(page);
for (int i = 1; i < tables.size(); i++) {
assert (tables.get(i - 1).getTop() <= tables.get(i).getTop());
}
page.getPDDoc().close();
}
@Test
public void testRTL() throws IOException {
Page page = UtilsForTesting.getPage("src/test/resources/technology/tabula/arabic.pdf",
1);
SpreadsheetExtractionAlgorithm sea = new SpreadsheetExtractionAlgorithm();
List<Table> tables = sea.extract(page);
// assertEquals(1, tables.size());
Table table = tables.get(0);
assertEquals("اسمي سلطان", table.getRows().get(1).get(1).getText());
assertEquals("من اين انت؟", table.getRows().get(2).get(1).getText());
assertEquals("1234", table.getRows().get(3).get(0).getText());
assertEquals("هل انت شباك؟", table.getRows().get(4).get(0).getText());
assertEquals("انا من ولاية كارولينا الشمال", table.getRows().get(2).get(0).getText()); // conjoined lam-alif gets missed
assertEquals("اسمي Jeremy في الانجليزية", table.getRows().get(4).get(1).getText()); // conjoined lam-alif gets missed
assertEquals("عندي 47 قطط", table.getRows().get(3).get(1).getText()); // the real right answer is 47.
assertEquals("Jeremy is جرمي in Arabic", table.getRows().get(5).get(0).getText()); // the real right answer is 47.
assertEquals("مرحباً", table.getRows().get(1).get(0).getText()); // really ought to be ً, but this is forgiveable for now
// there is one remaining problems that are not yet addressed
// - diacritics (e.g. Arabic's tanwinً and probably Hebrew nekudot) are put in the wrong place.
// this should get fixed, but this is a good first stab at the problem.
// these (commented-out) tests reflect the theoretical correct answer,
// which is not currently possible because of the two problems listed above
// assertEquals("مرحباً", table.getRows().get(0).get(0).getText()); // really ought to be ً, but this is forgiveable for now
page.getPDDoc().close();
}
@Test
public void testRealLifeRTL() throws IOException {
Page page = UtilsForTesting.getPage("src/test/resources/technology/tabula/mednine.pdf",
1);
SpreadsheetExtractionAlgorithm sea = new SpreadsheetExtractionAlgorithm();
List<Table> tables = sea.extract(page);
// assertEquals(1, tables.size());
Table table = tables.get(0);
assertEquals("الانتخابات التشريعية 2014", table.getRows().get(0).get(0).getText()); // the doubled spaces might be a bug in my implementation.
assertEquals("ورقة كشف نتائج دائرة مدنين", table.getRows().get(1).get(0).getText());
assertEquals("426", table.getRows().get(4).get(0).getText());
assertEquals("63", table.getRows().get(4).get(1).getText());
assertEquals("43", table.getRows().get(4).get(2).getText());
assertEquals("56", table.getRows().get(4).get(3).getText());
assertEquals("58", table.getRows().get(4).get(4).getText());
assertEquals("49", table.getRows().get(4).get(5).getText());
assertEquals("55", table.getRows().get(4).get(6).getText());
assertEquals("33", table.getRows().get(4).get(7).getText());
assertEquals("32", table.getRows().get(4).get(8).getText());
assertEquals("37", table.getRows().get(4).get(9).getText());
assertEquals("قائمة من أجل تحقيق سلطة الشعب", table.getRows().get(4).get(10).getText());
// there is one remaining problems that are not yet addressed
// - diacritics (e.g. Arabic's tanwinً and probably Hebrew nekudot) are put in the wrong place.
// this should get fixed, but this is a good first stab at the problem.
// these (commented-out) tests reflect the theoretical correct answer,
// which is not currently possible because of the two problems listed above
// assertEquals("مرحباً", table.getRows().get(0).get(0).getText()); // really ought to be ً, but this is forgiveable for now
page.getPDDoc().close();
}
@Test
public void testExtractColumnsCorrectly3() throws IOException {
Page page = UtilsForTesting.getAreaFromFirstPage("src/test/resources/technology/tabula/frx_2012_disclosure.pdf",
106.01f, 48.09f, 227.31f, 551.89f);
SpreadsheetExtractionAlgorithm sea = new SpreadsheetExtractionAlgorithm();
Table table = sea.extract(page).get(0);
assertEquals("REGIONAL PULMONARY & SLEEP\rMEDICINE", table.getRows().get(8).get(1).getText());
page.getPDDoc().close();
}
@Test
public void testSpreadsheetExtractionIssue656() throws IOException {
Page page = UtilsForTesting
.getAreaFromFirstPage(
"src/test/resources/technology/tabula/Publication_of_award_of_Bids_for_Transport_Sector__August_2016.pdf",
56.925f,24.255f,549.945f,786.555f);
String expectedCsv = UtilsForTesting.loadCsv("src/test/resources/technology/tabula/csv/Publication_of_award_of_Bids_for_Transport_Sector__August_2016.csv");
SpreadsheetExtractionAlgorithm sea = new SpreadsheetExtractionAlgorithm();
List<Table> tables = sea.extract(page);
assertEquals(1, tables.size());
Table table = tables.get(0);
StringBuilder sb = new StringBuilder();
(new CSVWriter()).write(sb, table);
String result = sb.toString();
assertEquals(expectedCsv, result);
page.getPDDoc().close();
}
}