Skip to content

Commit 21a4932

Browse files
travisbealejazzido
authored andcommitted
Refine heuristic to filter out tall-ish whitespace elements that can throw off text chunking by considering realistic font sizes
1 parent ebc83ac commit 21a4932

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

src/main/java/technology/tabula/TextStripper.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
import java.util.List;
1414

1515
public class TextStripper extends PDFTextStripper {
16+
1617
private static final String NBSP = "\u00A0";
1718
private static final float AVG_HEIGHT_MULT_THRESHOLD = 6.0f;
19+
private static final float MAX_BLANK_FONT_SIZE = 40.0f;
20+
private static final float MIN_BLANK_FONT_SIZE = 2.0f;
1821
private PDDocument document;
1922
public ArrayList<TextElement> textElements;
2023
public RectangleSpatialIndex<TextElement> spatialIndex;
@@ -69,15 +72,24 @@ protected void writeString(String string, List<TextPosition> textPositions) thro
6972

7073
this.minCharWidth = (float) Math.min(this.minCharWidth, te.getWidth());
7174
this.minCharHeight = (float) Math.min(this.minCharHeight, te.getHeight());
72-
75+
7376
countHeight++;
7477
totalHeight += te.getHeight();
7578
float avgHeight = totalHeight / countHeight;
7679

77-
if (avgHeight > 0
78-
&& te.getHeight() >= (avgHeight * AVG_HEIGHT_MULT_THRESHOLD)
79-
&& (te.getText() == null || te.getText().trim().equals(""))) {
80-
continue;
80+
//We have an issue where tall blank cells throw off the row height calculation
81+
//Introspect a blank cell a bit here to see if it should be thrown away
82+
if ((te.getText() == null || te.getText().trim().equals(""))) {
83+
//if the cell height is more than AVG_HEIGHT_MULT_THRESHOLDxaverage, throw it away
84+
if (avgHeight > 0
85+
&& te.getHeight() >= (avgHeight * AVG_HEIGHT_MULT_THRESHOLD)) {
86+
continue;
87+
}
88+
89+
//if the font size is outside of reasonable ranges, throw it away
90+
if (textPosition.getFontSizeInPt() > MAX_BLANK_FONT_SIZE || textPosition.getFontSizeInPt() < MIN_BLANK_FONT_SIZE) {
91+
continue;
92+
}
8193
}
8294

8395
this.spatialIndex.add(te);

0 commit comments

Comments
 (0)