Skip to content

Commit b7a8db0

Browse files
authored
🐛 take line height tolerance into account when evaluating fields (#127)
1 parent b36e26c commit b7a8db0

2 files changed

Lines changed: 11 additions & 11 deletions

File tree

src/main/java/com/mindee/parsing/custom/lineitems/Anchor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
@Getter
99
public final class Anchor {
1010
private final String name;
11-
private final Double tolerance;
11+
private final double tolerance;
1212

1313
/**
1414
* Define an anchor with height tolerance.
1515
*/
16-
public Anchor(String name, Double tolerance) {
16+
public Anchor(String name, double tolerance) {
1717
this.name = name;
1818
this.tolerance = tolerance;
1919
}
@@ -23,6 +23,6 @@ public Anchor(String name, Double tolerance) {
2323
*/
2424
public Anchor(String name) {
2525
this.name = name;
26-
this.tolerance = 0.001d;
26+
this.tolerance = 0.01d;
2727
}
2828
}

src/main/java/com/mindee/parsing/custom/lineitems/LineItemsGenerator.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.mindee.parsing.custom.lineitems;
22

3+
import com.mindee.geometry.MinMax;
34
import com.mindee.geometry.PolygonUtils;
45
import com.mindee.parsing.custom.ListField;
56
import com.mindee.parsing.custom.ListFieldValue;
@@ -20,42 +21,41 @@ private LineItemsGenerator() {
2021
/**
2122
* WARNING: This feature is experimental!
2223
* Results may not always work as intended.
23-
* Don't use unless you know what you're doing ;-)
2424
*/
2525
public static LineItems generate(
2626
List<String> fieldNames,
2727
Map<String, ListField> fields,
2828
Anchor anchor
2929
) {
30-
3130
Map<String, ListField> fieldsToTransformIntoLines = fields.entrySet()
3231
.stream()
3332
.filter(field -> fieldNames.contains(field.getKey()))
3433
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
3534

3635
List<Line> lines = populateLines(
3736
fieldsToTransformIntoLines,
38-
new ArrayList<>(LineGenerator.prepareLines(fieldsToTransformIntoLines, anchor))
37+
new ArrayList<>(LineGenerator.prepareLines(fieldsToTransformIntoLines, anchor)),
38+
anchor.getTolerance()
3939
);
4040

4141
return new LineItems(lines);
4242
}
4343

4444
private static List<Line> populateLines(
4545
Map<String, ListField> fields,
46-
List<Line> lines
46+
List<Line> lines,
47+
double heightLineTolerance
4748
) {
48-
4949
List<Line> populatedLines = new ArrayList<>();
5050

5151
for (Line currentLine : lines) {
5252
for (Map.Entry<String, ListField> field : fields.entrySet()) {
5353
for (ListFieldValue listFieldValue : field.getValue().getValues()) {
54-
double minYCurrentValue = PolygonUtils.getMinYCoordinate(listFieldValue.getPolygon());
54+
MinMax minMaxY = PolygonUtils.getMinMaxY(listFieldValue.getPolygon().getCoordinates());
5555

5656
if (
57-
minYCurrentValue < currentLine.getBbox().getMaxY()
58-
&& minYCurrentValue >= currentLine.getBbox().getMinY()
57+
Math.abs(minMaxY.getMax() - currentLine.getBbox().getMaxY()) <= heightLineTolerance
58+
&& Math.abs(minMaxY.getMin() - currentLine.getBbox().getMinY()) <= heightLineTolerance
5959
) {
6060
currentLine.addField(field.getKey(), listFieldValue);
6161
}

0 commit comments

Comments
 (0)