Skip to content

Commit 57810e2

Browse files
committed
✨ allow sending multiple anchors when building line items
1 parent 0fc5b1f commit 57810e2

File tree

3 files changed

+107
-40
lines changed

3 files changed

+107
-40
lines changed

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

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.mindee.parsing.custom.ListFieldValue;
77
import java.util.Arrays;
88
import java.util.Collection;
9+
import java.util.List;
910
import java.util.HashMap;
1011
import java.util.Map;
1112
import org.apache.commons.math3.util.Precision;
@@ -19,25 +20,41 @@ public final class LineGenerator {
1920
private LineGenerator() {
2021
}
2122

23+
public static String findBestAnchor(
24+
Map<String, ListField> fields,
25+
List<String> anchors
26+
) {
27+
String bestAnchor = null;
28+
int anchorRows = 0;
29+
for (String anchorName : anchors) {
30+
ListField anchorField = fields.get(anchorName);
31+
if (anchorField == null) {
32+
throw new IllegalStateException("The field selected for the anchor was not found.");
33+
}
34+
if (anchorField.getValues().size() > anchorRows) {
35+
anchorRows = anchorField.getValues().size();
36+
bestAnchor = anchorName;
37+
}
38+
}
39+
if (bestAnchor == null) {
40+
throw new IllegalStateException("Could not determine which anchor to use.");
41+
}
42+
return bestAnchor;
43+
}
44+
2245
public static Collection<Line> prepareLines(
2346
Map<String, ListField> fields,
24-
Anchor fieldAsAnchor
47+
List<String> anchors,
48+
double tolerance
2549
) {
2650
HashMap<Integer, Line> table = new HashMap<>();
2751

28-
ListField anchor = fields.get(fieldAsAnchor.getName());
29-
30-
if (anchor == null) {
31-
throw new IllegalStateException("The field selected for the anchor was not found.");
32-
}
33-
34-
if (anchor.getValues().isEmpty()) {
35-
throw new IllegalStateException("No lines have been detected.");
36-
}
52+
String anchorName = findBestAnchor(fields, anchors);
53+
ListField anchor = fields.get(anchorName);
3754

3855
// handle one value and the case of one line
3956
int lineNumber = 1;
40-
Line currentLine = new Line(lineNumber, fieldAsAnchor.getTolerance());
57+
Line currentLine = new Line(lineNumber, tolerance);
4158
ListFieldValue currentValue = anchor.getValues().get(0);
4259
currentLine.setBbox(currentValue.getPolygon().getAsBbox());
4360

@@ -50,7 +67,7 @@ public static Collection<Line> prepareLines(
5067
Precision.equals(
5168
currentLine.getBbox().getMinY(),
5269
currentFieldBbox.getMinY(),
53-
fieldAsAnchor.getTolerance()
70+
tolerance
5471
)
5572
) {
5673
currentLine.setBbox(
@@ -60,7 +77,7 @@ public static Collection<Line> prepareLines(
6077
// when it is a new line
6178
table.put(lineNumber, currentLine);
6279
lineNumber++;
63-
currentLine = new Line(lineNumber, fieldAsAnchor.getTolerance());
80+
currentLine = new Line(lineNumber, tolerance);
6481
currentLine.setBbox(currentFieldBbox);
6582
}
6683
}

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

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

33
import com.mindee.geometry.MinMax;
4-
import com.mindee.geometry.PolygonUtils;
54
import com.mindee.parsing.custom.ListField;
65
import com.mindee.parsing.custom.ListFieldValue;
6+
import java.util.Collections;
77
import java.util.ArrayList;
88
import java.util.List;
99
import java.util.Map;
@@ -19,8 +19,39 @@ private LineItemsGenerator() {
1919
}
2020

2121
/**
22-
* WARNING: This feature is experimental!
23-
* Results may not always work as intended.
22+
* Generate line items.
23+
* Use this method if you want to send a list of different possible anchor fields.
24+
* All anchor fields will use the same tolerance.
25+
*/
26+
public static LineItems generate(
27+
List<String> fieldNames,
28+
Map<String, ListField> fields,
29+
List<String> anchors,
30+
double tolerance
31+
) {
32+
Map<String, ListField> fieldsToTransformIntoLines = fields.entrySet()
33+
.stream()
34+
.filter(field -> fieldNames.contains(field.getKey()))
35+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
36+
return generate(fieldsToTransformIntoLines, anchors, tolerance);
37+
}
38+
39+
/**
40+
* Generate line items.
41+
* Use this method if you want to send a list of different possible anchor fields.
42+
* Use the default tolerance for all anchors.
43+
*/
44+
public static LineItems generate(
45+
List<String> fieldNames,
46+
Map<String, ListField> fields,
47+
List<String> anchors
48+
) {
49+
return generate(fieldNames, fields, anchors, 0.01d);
50+
}
51+
52+
/**
53+
* Generate line items.
54+
* Use this method if you want to use a single anchor field.
2455
*/
2556
public static LineItems generate(
2657
List<String> fieldNames,
@@ -31,13 +62,27 @@ public static LineItems generate(
3162
.stream()
3263
.filter(field -> fieldNames.contains(field.getKey()))
3364
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
65+
List<String> anchors = new ArrayList<>(
66+
Collections.singletonList(anchor.getName())
67+
);
68+
return generate(fieldsToTransformIntoLines, anchors, anchor.getTolerance());
69+
}
3470

71+
private static LineItems generate(
72+
Map<String, ListField> fieldsToTransformIntoLines,
73+
List<String> anchors,
74+
double tolerance
75+
) {
76+
List<Line> preparedLines = new ArrayList<>(LineGenerator.prepareLines(
77+
fieldsToTransformIntoLines,
78+
anchors,
79+
tolerance
80+
));
3581
List<Line> lines = populateLines(
3682
fieldsToTransformIntoLines,
37-
new ArrayList<>(LineGenerator.prepareLines(fieldsToTransformIntoLines, anchor)),
38-
anchor.getTolerance()
83+
preparedLines,
84+
tolerance
3985
);
40-
4186
return new LineItems(lines);
4287
}
4388

src/test/java/com/mindee/parsing/custom/lineitems/LineGeneratorTest.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,67 @@
11
package com.mindee.parsing.custom.lineitems;
22

3+
import java.util.ArrayList;
34
import java.util.Collection;
4-
5+
import java.util.Collections;
6+
import java.util.List;
57
import org.apache.commons.math3.util.Precision;
68
import org.junit.jupiter.api.Assertions;
79
import org.junit.jupiter.api.Test;
810

911
class LineGeneratorTest {
1012

13+
private static final List<String> anchors = new ArrayList<>(
14+
Collections.singletonList("names")
15+
);
16+
private static final double tolerance = 0.01d;
17+
1118
@Test
1219
void prepareLinesWith2ValuesOnEachLineWithPolygonValuesOnExactlyTheSameAxis() {
13-
Anchor anchor = new Anchor("names");
14-
1520
Collection<Line> table = LineGenerator.prepareLines(
1621
FakeListField.getWith2ValuesByExpectedLines(),
17-
anchor
22+
anchors,
23+
tolerance
1824
);
19-
2025
Assertions.assertEquals(2, table.size());
2126
}
2227

2328
@Test
2429
void prepareLinesWith1FieldValueForTheLastLine() {
25-
Anchor anchor = new Anchor("names");
26-
2730
Collection<Line> table = LineGenerator.prepareLines(
28-
FakeListField.getWith1FieldValueForTheLastLine(), anchor);
29-
31+
FakeListField.getWith1FieldValueForTheLastLine(),
32+
anchors,
33+
tolerance
34+
);
3035
Assertions.assertEquals(3, table.size());
3136
}
3237

3338
@Test
3439
void prepareLinesWith1ExpectedLine() {
35-
Anchor anchor = new Anchor("names");
36-
3740
Collection<Line> table = LineGenerator.prepareLines(
38-
FakeListField.getWith1ExpectedLines(), anchor);
39-
41+
FakeListField.getWith1ExpectedLines(),
42+
anchors,
43+
tolerance
44+
);
4045
Assertions.assertEquals(1, table.size());
4146
}
4247

4348
@Test
4449
void prepareLinesWithPolygonsNotExactlyOnTheSameAxis() {
45-
Anchor anchor = new Anchor("names", 0.005d);
46-
4750
Collection<Line> table = LineGenerator.prepareLines(
48-
FakeListField.getWithPolygonsNotExactlyOnTheSameAxis(), anchor);
49-
51+
FakeListField.getWithPolygonsNotExactlyOnTheSameAxis(),
52+
anchors,
53+
0.005d
54+
);
5055
Assertions.assertEquals(2, table.size());
5156
}
5257

5358
@Test
5459
void prepareLinesWhichRender2LinesInsteadOfOne() {
55-
Anchor anchor = new Anchor("names", 0.0d);
56-
5760
Collection<Line> table = LineGenerator.prepareLines(
58-
FakeListField.getSampleWichRender2LinesInsteadOfOne(), anchor);
59-
61+
FakeListField.getSampleWichRender2LinesInsteadOfOne(),
62+
anchors,
63+
0.0d
64+
);
6065
Assertions.assertEquals(1, table.size());
6166
}
6267

0 commit comments

Comments
 (0)