Skip to content

Commit 8960907

Browse files
committed
✨ add chaining for v2 utilities
1 parent 41f45b0 commit 8960907

7 files changed

Lines changed: 165 additions & 30 deletions

File tree

src/main/java/com/mindee/v2/product/classification/ClassificationClassifier.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
44
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.mindee.parsing.v2.InferenceResponse;
56
import lombok.Getter;
67

78
/**
@@ -16,6 +17,12 @@ public class ClassificationClassifier {
1617
@JsonProperty("document_type")
1718
private String documentType;
1819

20+
/**
21+
* The extraction response associated with the classification.
22+
*/
23+
@JsonProperty("extraction_response")
24+
private InferenceResponse extractionResponse;
25+
1926
@Override
2027
public String toString() {
2128
return "Document Type: " + documentType;

src/main/java/com/mindee/v2/product/crop/CropItem.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
44
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.mindee.parsing.v2.InferenceResponse;
56
import com.mindee.parsing.v2.field.FieldLocation;
67
import lombok.AllArgsConstructor;
78
import lombok.EqualsAndHashCode;
@@ -29,6 +30,12 @@ public class CropItem {
2930
@JsonProperty("location")
3031
private FieldLocation location;
3132

33+
/**
34+
* The extraction response associated with the crop.
35+
*/
36+
@JsonProperty("extraction_response")
37+
private InferenceResponse extractionResponse;
38+
3239
@Override
3340
public String toString() {
3441
return "* :Location: " + location + "\n :Object Type: " + objectType;

src/main/java/com/mindee/v2/product/split/SplitRange.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
44
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.mindee.parsing.v2.InferenceResponse;
56
import java.util.ArrayList;
67
import lombok.AllArgsConstructor;
78
import lombok.EqualsAndHashCode;
@@ -29,4 +30,10 @@ public class SplitRange {
2930
*/
3031
@JsonProperty("document_type")
3132
public String documentType;
33+
34+
/**
35+
* The extraction response associated with the split.
36+
*/
37+
@JsonProperty("extraction_response")
38+
private InferenceResponse extractionResponse;
3239
}

src/test/java/com/mindee/v2/product/ClassificationTest.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import static com.mindee.TestingUtilities.getV2ResourcePath;
44
import static org.junit.jupiter.api.Assertions.assertEquals;
55
import static org.junit.jupiter.api.Assertions.assertNotNull;
6+
import static org.junit.jupiter.api.Assertions.assertNull;
67

78
import com.mindee.input.LocalResponse;
9+
import com.mindee.parsing.v2.InferenceResponse;
810
import com.mindee.v2.product.classification.ClassificationResponse;
911
import java.io.IOException;
1012
import org.junit.jupiter.api.DisplayName;
@@ -22,16 +24,43 @@ private ClassificationResponse loadResponse(String filePath) throws IOException
2224
@DisplayName("Result with single value")
2325
class SinglePredictionTest {
2426
@Test
25-
@DisplayName("all properties must be valid")
26-
void mustHaveValidProperties() throws IOException {
27+
@DisplayName("classification properties must be valid")
28+
void singleMustHaveValidProperties() throws IOException {
29+
ClassificationResponse response = loadResponse("products/classification/default_sample.json");
30+
assertNotNull(response.getInference());
31+
assertEquals(
32+
"invoice",
33+
response.getInference().getResult().getClassification().getDocumentType()
34+
);
35+
assertNull(response.getInference().getResult().getClassification().getExtractionResponse());
36+
}
37+
38+
@Test
39+
@DisplayName("extraction properties must be valid")
40+
void singleExtractionMustHaveValidProperties() throws IOException {
2741
ClassificationResponse response = loadResponse(
28-
"products/classification/classification_single.json"
42+
"products/classification/default_sample_extraction.json"
2943
);
3044
assertNotNull(response.getInference());
3145
assertEquals(
3246
"invoice",
3347
response.getInference().getResult().getClassification().getDocumentType()
3448
);
49+
InferenceResponse extractionResponse = response
50+
.getInference()
51+
.getResult()
52+
.getClassification()
53+
.getExtractionResponse();
54+
assertNotNull(extractionResponse);
55+
assertEquals(
56+
"Jiro Doi",
57+
extractionResponse
58+
.getInference()
59+
.getResult()
60+
.getFields()
61+
.getSimpleField("customer_name")
62+
.getStringValue()
63+
);
3564
}
3665
}
3766
}

src/test/java/com/mindee/v2/product/CropTest.java

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static org.junit.jupiter.api.Assertions.assertNotNull;
77

88
import com.mindee.input.LocalResponse;
9+
import com.mindee.parsing.v2.InferenceResponse;
910
import com.mindee.v2.product.crop.CropItem;
1011
import com.mindee.v2.product.crop.CropResponse;
1112
import java.io.IOException;
@@ -25,18 +26,18 @@ private CropResponse loadResponse(String filePath) throws IOException {
2526
@DisplayName("Result with single value")
2627
class SinglePredictionTest {
2728
@Test
28-
@DisplayName("all properties must be valid")
29+
@DisplayName("crop properties must be valid")
2930
void mustHaveValidProperties() throws IOException {
3031
CropResponse response = loadResponse("products/crop/crop_single.json");
3132
assertNotNull(response.getInference());
3233

3334
ArrayList<CropItem> crops = response.getInference().getResult().getCrops();
3435
assertEquals(1, crops.size());
3536

36-
CropItem crop1 = crops.get(0);
37-
assertEquals("invoice", crop1.getObjectType());
38-
assertNotNull(crop1.getLocation().getPolygon());
39-
assertEquals(0, crop1.getLocation().getPage());
37+
CropItem crop0 = crops.get(0);
38+
assertEquals("invoice", crop0.getObjectType());
39+
assertNotNull(crop0.getLocation().getPolygon());
40+
assertEquals(0, crop0.getLocation().getPage());
4041
}
4142

4243
@Test
@@ -54,23 +55,23 @@ void mustHaveValidDisplay() throws IOException {
5455
@DisplayName("Result with multiple values")
5556
class MultiPredictionTest {
5657
@Test
57-
@DisplayName("all properties must be valid")
58+
@DisplayName("crop properties must be valid")
5859
void mustHaveValidProperties() throws IOException {
5960
CropResponse response = loadResponse("products/crop/crop_multiple.json");
6061
assertNotNull(response.getInference());
6162

6263
ArrayList<CropItem> crops = response.getInference().getResult().getCrops();
6364
assertEquals(2, crops.size());
6465

65-
CropItem crop1 = crops.get(0);
66-
assertEquals("invoice", crop1.getObjectType());
66+
CropItem crop0 = crops.get(0);
67+
assertEquals("invoice", crop0.getObjectType());
68+
assertNotNull(crop0.getLocation().getPolygon());
69+
assertEquals(0, crop0.getLocation().getPage());
70+
71+
CropItem crop1 = crops.get(1);
72+
assertEquals("receipt", crop1.getObjectType());
6773
assertNotNull(crop1.getLocation().getPolygon());
6874
assertEquals(0, crop1.getLocation().getPage());
69-
70-
CropItem crop2 = crops.get(1);
71-
assertEquals("receipt", crop2.getObjectType());
72-
assertNotNull(crop2.getLocation().getPolygon());
73-
assertEquals(0, crop2.getLocation().getPage());
7475
}
7576

7677
@Test
@@ -82,5 +83,47 @@ void mustHaveValidDisplay() throws IOException {
8283
getV2ResourcePath("products/crop/crop_multiple.rst")
8384
);
8485
}
86+
87+
@Test
88+
@DisplayName("extraction properties must be valid")
89+
void extractionMustHaveValidProperties() throws IOException {
90+
CropResponse response = loadResponse("products/crop/default_sample_extraction.json");
91+
assertNotNull(response.getInference());
92+
93+
ArrayList<CropItem> crops = response.getInference().getResult().getCrops();
94+
assertEquals(2, crops.size());
95+
96+
CropItem crop0 = crops.get(0);
97+
assertEquals("receipt", crop0.getObjectType());
98+
assertNotNull(crop0.getLocation().getPolygon());
99+
assertEquals(0, crop0.getLocation().getPage());
100+
InferenceResponse extractionResponse0 = crop0.getExtractionResponse();
101+
assertNotNull(extractionResponse0);
102+
assertEquals(
103+
"CHEZ ALAIN MIAM MIAM",
104+
extractionResponse0
105+
.getInference()
106+
.getResult()
107+
.getFields()
108+
.getSimpleField("supplier_name")
109+
.getValue()
110+
);
111+
112+
CropItem crop1 = crops.get(1);
113+
assertEquals("receipt", crop1.getObjectType());
114+
assertNotNull(crop1.getLocation().getPolygon());
115+
assertEquals(0, crop1.getLocation().getPage());
116+
InferenceResponse extractionResponse1 = crop1.getExtractionResponse();
117+
assertNotNull(extractionResponse1);
118+
assertEquals(
119+
"La cerise sur la pizza",
120+
extractionResponse1
121+
.getInference()
122+
.getResult()
123+
.getFields()
124+
.getSimpleField("supplier_name")
125+
.getValue()
126+
);
127+
}
85128
}
86129
}

src/test/java/com/mindee/v2/product/SplitTest.java

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.junit.jupiter.api.Assertions.assertNotNull;
66

77
import com.mindee.input.LocalResponse;
8+
import com.mindee.parsing.v2.InferenceResponse;
89
import com.mindee.v2.product.split.SplitRange;
910
import com.mindee.v2.product.split.SplitResponse;
1011
import java.io.IOException;
@@ -24,41 +25,82 @@ private SplitResponse loadResponse(String filePath) throws IOException {
2425
@DisplayName("Result with single value")
2526
class SinglePredictionTest {
2627
@Test
27-
@DisplayName("all properties must be valid")
28+
@DisplayName("split properties must be valid")
2829
void mustHaveValidProperties() throws IOException {
2930
SplitResponse response = loadResponse("products/split/split_single.json");
3031
assertNotNull(response.getInference());
3132

3233
ArrayList<SplitRange> splits = response.getInference().getResult().getSplits();
3334
assertEquals(1, splits.size());
34-
assertEquals("receipt", splits.get(0).getDocumentType());
35-
assertEquals(0, splits.get(0).getPageRange().get(0));
35+
SplitRange split0 = splits.get(0);
36+
assertEquals("receipt", split0.getDocumentType());
37+
assertEquals(0, split0.getPageRange().get(0));
3638
}
3739
}
3840

3941
@Nested
4042
@DisplayName("Result with multiple values")
4143
class MultiPredictionTest {
4244
@Test
43-
@DisplayName("all properties must be valid")
45+
@DisplayName("split properties must be valid")
4446
void mustHaveValidProperties() throws IOException {
4547
SplitResponse response = loadResponse("products/split/split_multiple.json");
4648
assertNotNull(response.getInference());
4749

4850
ArrayList<SplitRange> splits = response.getInference().getResult().getSplits();
4951
assertEquals(3, splits.size());
5052

51-
SplitRange split1 = splits.get(0);
52-
assertEquals("passport", split1.getDocumentType());
53-
assertEquals(0, split1.getPageRange().get(0));
53+
SplitRange split0 = splits.get(0);
54+
assertEquals("passport", split0.getDocumentType());
55+
assertEquals(0, split0.getPageRange().get(0));
5456

55-
SplitRange split2 = splits.get(1);
56-
assertEquals("invoice", split2.getDocumentType());
57-
assertEquals(1, split2.getPageRange().get(0));
57+
SplitRange split1 = splits.get(1);
58+
assertEquals("invoice", split1.getDocumentType());
59+
assertEquals(1, split1.getPageRange().get(0));
5860

59-
SplitRange split3 = splits.get(2);
60-
assertEquals("receipt", split3.getDocumentType());
61-
assertEquals(4, split3.getPageRange().get(0));
61+
SplitRange split2 = splits.get(2);
62+
assertEquals("receipt", split2.getDocumentType());
63+
assertEquals(4, split2.getPageRange().get(0));
64+
}
65+
66+
@Test
67+
@DisplayName("extraction properties must be valid")
68+
void extractionMustHaveValidProperties() throws IOException {
69+
SplitResponse response = loadResponse("products/split/default_sample_extraction.json");
70+
assertNotNull(response.getInference());
71+
72+
ArrayList<SplitRange> splits = response.getInference().getResult().getSplits();
73+
assertEquals(2, splits.size());
74+
75+
SplitRange split0 = splits.get(0);
76+
assertEquals("invoice", split0.getDocumentType());
77+
assertEquals(0, split0.getPageRange().get(0));
78+
InferenceResponse extractionResponse0 = split0.getExtractionResponse();
79+
assertNotNull(extractionResponse0);
80+
assertEquals(
81+
"05 05 44 44 90",
82+
extractionResponse0
83+
.getInference()
84+
.getResult()
85+
.getFields()
86+
.getSimpleField("supplier_phone_number")
87+
.getValue()
88+
);
89+
90+
SplitRange split1 = splits.get(1);
91+
assertEquals("invoice", split1.getDocumentType());
92+
assertEquals(1, split1.getPageRange().get(0));
93+
InferenceResponse extractionResponse1 = split1.getExtractionResponse();
94+
assertNotNull(extractionResponse1);
95+
assertEquals(
96+
"416-555-1212",
97+
extractionResponse1
98+
.getInference()
99+
.getResult()
100+
.getFields()
101+
.getSimpleField("supplier_phone_number")
102+
.getValue()
103+
);
62104
}
63105
}
64106
}

0 commit comments

Comments
 (0)