Skip to content

Commit 224a08c

Browse files
authored
🎨 a few minor improvements to using Generated products (#163)
1 parent 8a437ef commit 224a08c

File tree

2 files changed

+77
-9
lines changed

2 files changed

+77
-9
lines changed

src/main/java/com/mindee/parsing/generated/GeneratedObject.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public class GeneratedObject extends HashMap<String, Object> {
2121
public StringField asStringField() {
2222
return new StringField(
2323
(String) this.get("value"),
24-
(String) this.get("raw_value"),
24+
this.get("raw_value") != null ? (String) this.get("raw_value") : null,
2525
this.getConfidence(),
26-
this.getAsPolygon("polygon"),
26+
this.getPolygon(),
2727
this.getPageId()
2828
);
2929
}
@@ -35,7 +35,7 @@ public AmountField asAmountField() {
3535
return new AmountField(
3636
(Double) this.get("value"),
3737
this.getConfidence(),
38-
this.getAsPolygon("polygon"),
38+
this.getPolygon(),
3939
this.getPageId()
4040
);
4141
}
@@ -47,7 +47,7 @@ public DateField asDateField() {
4747
return new DateField(
4848
LocalDate.parse((String) this.get("value")),
4949
this.getConfidence(),
50-
this.getAsPolygon("polygon"),
50+
this.getPolygon(),
5151
this.getPageId()
5252
);
5353
}
@@ -61,15 +61,34 @@ public ClassificationField asClassificationField() {
6161
);
6262
}
6363

64+
/**
65+
* Get the polygon, if present.
66+
*/
67+
public Polygon getPolygon() {
68+
return this.getAsPolygon("polygon");
69+
}
70+
71+
/**
72+
* Get the specified key as a {@link Polygon} object.
73+
*/
6474
public Polygon getAsPolygon(String key) {
65-
return PolygonUtils.getFrom((List<List<Double>>) this.get(key));
75+
if (this.containsKey(key)) {
76+
return PolygonUtils.getFrom((List<List<Double>>) this.get(key));
77+
}
78+
return null;
6679
}
6780

81+
/**
82+
* Get the page ID, if present.
83+
*/
6884
public Integer getPageId() {
69-
return (Integer) this.get("page_id");
85+
return this.get("page_id") != null ? (Integer) this.get("page_id") : null;
7086
}
7187

88+
/**
89+
* Get the confidence score, if present.
90+
*/
7291
public Double getConfidence() {
73-
return (Double) this.get("confidence");
92+
return this.get("confidence") != null ? (Double) this.get("confidence") : null;
7493
}
7594
}

src/test/java/com/mindee/product/generated/GeneratedV1Test.java

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ void whenAsyncCompleteDeserialized_mustHaveValidProperties() throws IOException
5454

5555
Map<String, GeneratedFeature> features = docPrediction.getFields();
5656

57+
// Direct access to the hashmap
58+
5759
Assertions.assertFalse(features.get("address").isList());
5860
Assertions.assertEquals("AVDA DE MADRID S-N MADRID MADRID", features.get("address").get(0).get("value"));
5961

@@ -75,6 +77,24 @@ void whenAsyncCompleteDeserialized_mustHaveValidProperties() throws IOException
7577
Assertions.assertTrue(features.get("surnames").isList());
7678
Assertions.assertEquals("ESPAÑOLA", features.get("surnames").get(0).get("value"));
7779
Assertions.assertEquals("ESPAÑOLA", features.get("surnames").get(1).get("value"));
80+
81+
// Access as a StringField without raw_value
82+
StringField addressField = features.get("address").asStringField();
83+
Assertions.assertEquals(
84+
"AVDA DE MADRID S-N MADRID MADRID",
85+
addressField.getValue()
86+
);
87+
Assertions.assertNull(addressField.getRawValue());
88+
Assertions.assertNull(addressField.getConfidence());
89+
Assertions.assertNull(addressField.getPageId());
90+
Assertions.assertNull(addressField.getPolygon());
91+
92+
// Access as a DateField
93+
DateField expiryDateField = features.get("expiry_date").asDateField();
94+
Assertions.assertEquals(
95+
LocalDate.parse("2025-01-01"),
96+
expiryDateField.getValue()
97+
);
7898
}
7999

80100
@Test
@@ -120,13 +140,12 @@ void whenSyncCompleteDeserialized_mustHaveValidProperties() throws IOException {
120140
1,
121141
customerName.get(0).get("page_id")
122142
);
123-
System.out.println(customerName.get(0).get("polygon").getClass());
124143
Assertions.assertEquals(
125144
"Polygon with 4 points.",
126145
customerName.get(0).getAsPolygon("polygon").toString()
127146
);
128147

129-
// Access as a StringField
148+
// Access as a StringField with raw_value
130149
StringField customerNameField = customerName.asStringField();
131150
Assertions.assertEquals(
132151
"JIRO DOI",
@@ -140,6 +159,36 @@ void whenSyncCompleteDeserialized_mustHaveValidProperties() throws IOException {
140159
0.87,
141160
customerNameField.getConfidence()
142161
);
162+
Assertions.assertEquals(
163+
1,
164+
customerNameField.getPageId()
165+
);
166+
Assertions.assertEquals(
167+
"Polygon with 4 points.",
168+
customerNameField.getPolygon().toString()
169+
);
170+
171+
// Access as a StringField without raw_value
172+
StringField supplierAddressField = features.get("supplier_address").asStringField();
173+
Assertions.assertEquals(
174+
"156 University Ave, Toronto ON, Canada M5H 2H7",
175+
supplierAddressField.getValue()
176+
);
177+
Assertions.assertNull(
178+
supplierAddressField.getRawValue()
179+
);
180+
Assertions.assertEquals(
181+
0.53,
182+
supplierAddressField.getConfidence()
183+
);
184+
Assertions.assertEquals(
185+
1,
186+
supplierAddressField.getPageId()
187+
);
188+
Assertions.assertEquals(
189+
"Polygon with 4 points.",
190+
supplierAddressField.getPolygon().toString()
191+
);
143192

144193
// Access as an AmountField
145194
AmountField totalAmountField = features.get("total_amount").asAmountField();

0 commit comments

Comments
 (0)