4
4
import com .mindee .geometry .BboxUtils ;
5
5
import com .mindee .parsing .custom .ListField ;
6
6
import com .mindee .parsing .custom .ListFieldValue ;
7
- import java .util .Arrays ;
8
- import java .util .Collection ;
9
- import java .util .HashMap ;
10
- import java .util .Map ;
7
+
8
+ import java .util .*;
9
+
11
10
import org .apache .commons .math3 .util .Precision ;
12
11
13
12
@@ -19,54 +18,67 @@ public final class LineGenerator {
19
18
private LineGenerator () {
20
19
}
21
20
22
- public static Collection < Line > prepareLines (
21
+ public static PreparedLines prepareLines (
23
22
Map <String , ListField > fields ,
24
- Anchor fieldAsAnchor
23
+ List < Anchor > anchors
25
24
) {
26
- HashMap <Integer , Line > table = new HashMap <>();
27
-
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." );
25
+ Anchor bestAnchor = null ;
26
+ Collection <Line > lines = null ;
27
+ int lineCount = 0 ;
28
+ for (Anchor anchor : anchors ) {
29
+ ListField anchorColumn = fields .get (anchor .getName ());
30
+ if (anchorColumn == null ) {
31
+ throw new IllegalStateException ("The field selected for the anchor was not found." );
32
+ }
33
+ Collection <Line > currentLines = createLines (
34
+ anchorColumn .getValues (),
35
+ anchor .getTolerance ()
36
+ );
37
+ if (currentLines .size () > lineCount ) {
38
+ bestAnchor = anchor ;
39
+ lines = currentLines ;
40
+ lineCount = currentLines .size ();
41
+ }
32
42
}
33
-
34
- if (anchor .getValues ().isEmpty ()) {
35
- throw new IllegalStateException ("No lines have been detected." );
43
+ if (lines == null ) {
44
+ throw new IllegalStateException ("Could not determine which anchor to use." );
36
45
}
46
+ return new PreparedLines (
47
+ bestAnchor ,
48
+ new ArrayList <>(lines )
49
+ );
50
+ }
51
+
52
+ private static Collection <Line > createLines (List <ListFieldValue > anchorValues , double tolerance ) {
53
+ HashMap <Integer , Line > table = new HashMap <>();
37
54
38
55
// handle one value and the case of one line
39
56
int lineNumber = 1 ;
40
- Line currentLine = new Line (lineNumber , fieldAsAnchor . getTolerance () );
41
- ListFieldValue currentValue = anchor . getValues () .get (0 );
57
+ Line currentLine = new Line (lineNumber , tolerance );
58
+ ListFieldValue currentValue = anchorValues .get (0 );
42
59
currentLine .setBbox (currentValue .getPolygon ().getAsBbox ());
43
-
44
- for (int i = 1 ; i < anchor .getValues ().size (); i ++) {
45
-
46
- currentValue = anchor .getValues ().get (i );
60
+ for (ListFieldValue anchorValue : anchorValues ) {
61
+ currentValue = anchorValue ;
47
62
Bbox currentFieldBbox = currentValue .getPolygon ().getAsBbox ();
48
-
49
63
if (
50
- Precision .equals (
51
- currentLine .getBbox ().getMinY (),
52
- currentFieldBbox .getMinY (),
53
- fieldAsAnchor . getTolerance ()
54
- )
64
+ Precision .equals (
65
+ currentLine .getBbox ().getMinY (),
66
+ currentFieldBbox .getMinY (),
67
+ tolerance
68
+ )
55
69
) {
56
70
currentLine .setBbox (
57
- BboxUtils .merge (Arrays .asList (currentLine .getBbox (), currentFieldBbox ))
71
+ BboxUtils .merge (Arrays .asList (currentLine .getBbox (), currentFieldBbox ))
58
72
);
59
73
} else {
60
74
// when it is a new line
61
75
table .put (lineNumber , currentLine );
62
76
lineNumber ++;
63
- currentLine = new Line (lineNumber , fieldAsAnchor . getTolerance () );
77
+ currentLine = new Line (lineNumber , tolerance );
64
78
currentLine .setBbox (currentFieldBbox );
65
79
}
66
80
}
67
-
68
81
table .putIfAbsent (lineNumber , currentLine );
69
-
70
82
return table .values ();
71
83
}
72
84
0 commit comments