Skip to content

Commit 62a9870

Browse files
authored
✨ add support for us mail v3 (#216)
1 parent 1a65242 commit 62a9870

File tree

12 files changed

+496
-18
lines changed

12 files changed

+496
-18
lines changed
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import com.mindee.MindeeClient;
2+
import com.mindee.input.LocalInputSource;
3+
import com.mindee.parsing.common.AsyncPredictResponse;
4+
import com.mindee.product.us.usmail.UsMailV3;
5+
import java.io.File;
6+
import java.io.IOException;
7+
8+
public class SimpleMindeeClient {
9+
10+
public static void main(String[] args) throws IOException, InterruptedException {
11+
String apiKey = "my-api-key";
12+
String filePath = "/path/to/the/file.ext";
13+
14+
// Init a new client
15+
MindeeClient mindeeClient = new MindeeClient(apiKey);
16+
17+
// Load a file from disk
18+
LocalInputSource inputSource = new LocalInputSource(new File(filePath));
19+
20+
// Parse the file asynchronously
21+
AsyncPredictResponse<UsMailV3> response = mindeeClient.enqueueAndParse(
22+
UsMailV3.class,
23+
inputSource
24+
);
25+
26+
// Print a summary of the response
27+
System.out.println(response.toString());
28+
29+
// Print a summary of the predictions
30+
// System.out.println(response.getDocumentObj().toString());
31+
32+
// Print the document-level predictions
33+
// System.out.println(response.getDocumentObj().getInference().getPrediction().toString());
34+
35+
// Print the page-level predictions
36+
// response.getDocumentObj().getInference().getPages().forEach(
37+
// page -> System.out.println(page.toString())
38+
// );
39+
}
40+
41+
}

docs/us_mail_v2.md renamed to docs/us_mail_v3.md

+40-14
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Using the [sample below](https://github.com/mindee/client-lib-test-data/blob/mai
1414
import com.mindee.MindeeClient;
1515
import com.mindee.input.LocalInputSource;
1616
import com.mindee.parsing.common.AsyncPredictResponse;
17-
import com.mindee.product.us.usmail.UsMailV2;
17+
import com.mindee.product.us.usmail.UsMailV3;
1818
import java.io.File;
1919
import java.io.IOException;
2020

@@ -31,8 +31,8 @@ public class SimpleMindeeClient {
3131
LocalInputSource inputSource = new LocalInputSource(new File(filePath));
3232

3333
// Parse the file asynchronously
34-
AsyncPredictResponse<UsMailV2> response = mindeeClient.enqueueAndParse(
35-
UsMailV2.class,
34+
AsyncPredictResponse<UsMailV3> response = mindeeClient.enqueueAndParse(
35+
UsMailV3.class,
3636
inputSource
3737
);
3838

@@ -57,7 +57,20 @@ public class SimpleMindeeClient {
5757

5858
**Output (RST):**
5959
```rst
60-
:Sender Name: zed
60+
########
61+
Document
62+
########
63+
:Mindee ID: f9c36f59-977d-4ddc-9f2d-31c294c456ac
64+
:Filename: default_sample.jpg
65+
66+
Inference
67+
#########
68+
:Product: mindee/us_mail v3.0
69+
:Rotation applied: Yes
70+
71+
Prediction
72+
==========
73+
:Sender Name: company zed
6174
:Sender Address:
6275
:City: Dallas
6376
:Complete Address: 54321 Elm Street, Dallas, Texas 54321
@@ -66,11 +79,12 @@ public class SimpleMindeeClient {
6679
:Street: 54321 Elm Street
6780
:Recipient Names: Jane Doe
6881
:Recipient Addresses:
69-
+-----------------+-------------------------------------+-------------------+-------------+------------------------+-------+---------------------------+
70-
| City | Complete Address | Is Address Change | Postal Code | Private Mailbox Number | State | Street |
71-
+=================+=====================================+===================+=============+========================+=======+===========================+
72-
| Detroit | 1234 Market Street PMB 4321, Det... | | 12345 | 4321 | MI | 1234 Market Street |
73-
+-----------------+-------------------------------------+-------------------+-------------+------------------------+-------+---------------------------+
82+
+-----------------+-------------------------------------+-------------------+-------------+------------------------+-------+---------------------------+-----------------+
83+
| City | Complete Address | Is Address Change | Postal Code | Private Mailbox Number | State | Street | Unit |
84+
+=================+=====================================+===================+=============+========================+=======+===========================+=================+
85+
| Detroit | 1234 Market Street PMB 4321, Det... | False | 12345 | 4321 | MI | 1234 Market Street | |
86+
+-----------------+-------------------------------------+-------------------+-------------+------------------------+-------+---------------------------+-----------------+
87+
:Return to Sender: False
7488
```
7589

7690
# Field Types
@@ -96,13 +110,17 @@ The text field `StringField` extends `BaseField`, but also implements:
96110
* **value** (`String`): corresponds to the field value.
97111
* **rawValue** (`String`): corresponds to the raw value as it appears on the document.
98112

113+
### BooleanField
114+
The boolean field `BooleanField` extends BaseField, but also implements:
115+
* **value** (`Boolean`): corresponds to the value of the field.
116+
99117
## Specific Fields
100118
Fields which are specific to this product; they are not used in any other product.
101119

102120
### Recipient Addresses Field
103121
The addresses of the recipients.
104122

105-
A `UsMailV2RecipientAddress` implements the following attributes:
123+
A `UsMailV3RecipientAddress` implements the following attributes:
106124

107125
* **city** (`String`): The city of the recipient's address.
108126
* **complete** (`String`): The complete address of the recipient.
@@ -111,12 +129,13 @@ A `UsMailV2RecipientAddress` implements the following attributes:
111129
* **privateMailboxNumber** (`String`): The private mailbox number of the recipient's address.
112130
* **state** (`String`): Second part of the ISO 3166-2 code, consisting of two letters indicating the US State.
113131
* **street** (`String`): The street of the recipient's address.
132+
* **unit** (`String`): The unit number of the recipient's address.
114133
Fields which are specific to this product; they are not used in any other product.
115134

116135
### Sender Address Field
117136
The address of the sender.
118137

119-
A `UsMailV2SenderAddress` implements the following attributes:
138+
A `UsMailV3SenderAddress` implements the following attributes:
120139

121140
* **city** (`String`): The city of the sender's address.
122141
* **complete** (`String`): The complete address of the sender.
@@ -125,10 +144,17 @@ A `UsMailV2SenderAddress` implements the following attributes:
125144
* **street** (`String`): The street of the sender's address.
126145

127146
# Attributes
128-
The following fields are extracted for US Mail V2:
147+
The following fields are extracted for US Mail V3:
148+
149+
## Return to Sender
150+
**isReturnToSender**: Whether the mailing is marked as return to sender.
151+
152+
```java
153+
System.out.println(result.getDocument().getInference().getPrediction().getIsReturnToSender().value);
154+
```
129155

130156
## Recipient Addresses
131-
**recipientAddresses**(List<[UsMailV2RecipientAddress](#recipient-addresses-field)>): The addresses of the recipients.
157+
**recipientAddresses**(List<[UsMailV3RecipientAddress](#recipient-addresses-field)>): The addresses of the recipients.
132158

133159
```java
134160
for (recipientAddressesElem : result.getDocument().getInference().getPrediction().getRecipientAddresses())
@@ -148,7 +174,7 @@ for (recipientNamesElem : result.getDocument().getInference().getPrediction().ge
148174
```
149175

150176
## Sender Address
151-
**senderAddress**([UsMailV2SenderAddress](#sender-address-field)): The address of the sender.
177+
**senderAddress**([UsMailV3SenderAddress](#sender-address-field)): The address of the sender.
152178

153179
```java
154180
System.out.println(result.getDocument().getInference().getPrediction().getSenderAddress().value);

src/main/java/com/mindee/CommandLineInterface.java

+18
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
import com.mindee.parsing.common.Inference;
1010
import com.mindee.parsing.common.PredictResponse;
1111
import com.mindee.product.custom.CustomV1;
12+
import com.mindee.product.financialdocument.FinancialDocumentV1;
1213
import com.mindee.product.internationalid.InternationalIdV2;
1314
import com.mindee.product.invoice.InvoiceV4;
1415
import com.mindee.product.invoicesplitter.InvoiceSplitterV1;
1516
import com.mindee.product.multireceiptsdetector.MultiReceiptsDetectorV1;
1617
import com.mindee.product.passport.PassportV1;
1718
import com.mindee.product.receipt.ReceiptV4;
19+
import com.mindee.product.us.usmail.UsMailV3;
1820
import java.io.File;
1921
import java.io.IOException;
2022
import java.util.ArrayList;
@@ -105,6 +107,14 @@ void receiptMethod(
105107
System.out.println(standardProductOutput(ReceiptV4.class, file));
106108
}
107109

110+
@Command(name = "financial-document", description = "Parse using Financial Document")
111+
void financialDocumentMethod(
112+
@Parameters(index = "0", paramLabel = "<path>", scope = ScopeType.LOCAL)
113+
File file
114+
) throws IOException {
115+
System.out.println(standardProductOutput(FinancialDocumentV1.class, file));
116+
}
117+
108118
@Command(name = "multi-receipt-detector", description = "Parse using Multi Receipts Detector")
109119
void multiReceiptDetectorMethod(
110120
@Parameters(index = "0", paramLabel = "<path>", scope = ScopeType.LOCAL)
@@ -137,6 +147,14 @@ void internationalIdMethod(
137147
System.out.println(standardProductAsyncOutput(InternationalIdV2.class, file));
138148
}
139149

150+
@Command(name = "us-mail", description = "Parse using US Mail")
151+
void usMailMethod(
152+
@Parameters(index = "0", paramLabel = "<path>", scope = ScopeType.LOCAL)
153+
File file
154+
) throws IOException, InterruptedException {
155+
System.out.println(standardProductAsyncOutput(UsMailV3.class, file));
156+
}
157+
140158
@Command(name = "custom", description = "Invokes a Custom API")
141159
void customMethod(
142160
@Option(

src/main/java/com/mindee/parsing/SummaryHelper.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ public static String formatForDisplay(String inputValue, Integer maxColSize) {
5555
if (inputValue == null || inputValue.isEmpty()) {
5656
return "";
5757
}
58-
String outputValue = inputValue.replace("\n", "\\n").replace("\t", "\\t").replace("\r", "\\r");
58+
String outputValue = inputValue
59+
.replace("\n", "\\n")
60+
.replace("\t", "\\t")
61+
.replace("\r", "\\r");
5962
if (maxColSize == null || outputValue.length() <= maxColSize) {
6063
return outputValue;
6164
} else {

src/main/java/com/mindee/parsing/standard/BooleanField.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
66
import com.mindee.geometry.Polygon;
77
import com.mindee.geometry.PolygonDeserializer;
8+
import com.mindee.parsing.SummaryHelper;
89
import lombok.Getter;
910

1011

@@ -45,7 +46,7 @@ public boolean isEmpty() {
4546
*/
4647
@Override
4748
public String toString() {
48-
return this.value == null ? "" : value.toString();
49+
return SummaryHelper.formatForDisplay(this.value, null);
4950
}
5051

5152
}

src/main/java/com/mindee/parsing/standard/StringField.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
66
import com.mindee.geometry.Polygon;
77
import com.mindee.geometry.PolygonDeserializer;
8+
import com.mindee.parsing.SummaryHelper;
89
import lombok.Getter;
910

1011
/**
@@ -56,6 +57,6 @@ public boolean isEmpty() {
5657

5758
@Override
5859
public String toString() {
59-
return value == null ? "" : value;
60+
return SummaryHelper.formatString(this.value);
6061
}
6162
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.mindee.product.us.usmail;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.mindee.http.EndpointInfo;
5+
import com.mindee.parsing.common.Inference;
6+
import lombok.Getter;
7+
8+
/**
9+
* US Mail API version 3 inference prediction.
10+
*/
11+
@Getter
12+
@JsonIgnoreProperties(ignoreUnknown = true)
13+
@EndpointInfo(endpointName = "us_mail", version = "3")
14+
public class UsMailV3
15+
extends Inference<UsMailV3Document, UsMailV3Document> {
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.mindee.product.us.usmail;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.mindee.parsing.SummaryHelper;
6+
import com.mindee.parsing.common.Prediction;
7+
import com.mindee.parsing.standard.BooleanField;
8+
import com.mindee.parsing.standard.StringField;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import lombok.EqualsAndHashCode;
12+
import lombok.Getter;
13+
14+
/**
15+
* US Mail API version 3.0 document data.
16+
*/
17+
@Getter
18+
@EqualsAndHashCode(callSuper = false)
19+
@JsonIgnoreProperties(ignoreUnknown = true)
20+
public class UsMailV3Document extends Prediction {
21+
22+
/**
23+
* Whether the mailing is marked as return to sender.
24+
*/
25+
@JsonProperty("is_return_to_sender")
26+
protected BooleanField isReturnToSender;
27+
/**
28+
* The addresses of the recipients.
29+
*/
30+
@JsonProperty("recipient_addresses")
31+
protected List<UsMailV3RecipientAddress> recipientAddresses = new ArrayList<>();
32+
/**
33+
* The names of the recipients.
34+
*/
35+
@JsonProperty("recipient_names")
36+
protected List<StringField> recipientNames = new ArrayList<>();
37+
/**
38+
* The address of the sender.
39+
*/
40+
@JsonProperty("sender_address")
41+
protected UsMailV3SenderAddress senderAddress;
42+
/**
43+
* The name of the sender.
44+
*/
45+
@JsonProperty("sender_name")
46+
protected StringField senderName;
47+
48+
@Override
49+
public boolean isEmpty() {
50+
return (
51+
this.senderName == null
52+
&& this.senderAddress == null
53+
&& (this.recipientNames == null || this.recipientNames.isEmpty())
54+
&& (this.recipientAddresses == null || this.recipientAddresses.isEmpty())
55+
&& this.isReturnToSender == null
56+
);
57+
}
58+
59+
@Override
60+
public String toString() {
61+
StringBuilder outStr = new StringBuilder();
62+
outStr.append(
63+
String.format(":Sender Name: %s%n", this.getSenderName())
64+
);
65+
outStr.append(
66+
String.format(":Sender Address:%n%s", this.getSenderAddress().toFieldList())
67+
);
68+
String recipientNames = SummaryHelper.arrayToString(
69+
this.getRecipientNames(),
70+
"%n "
71+
);
72+
outStr.append(
73+
String.format(":Recipient Names: %s%n", recipientNames)
74+
);
75+
String recipientAddressesSummary = "";
76+
if (!this.getRecipientAddresses().isEmpty()) {
77+
int[] recipientAddressesColSizes = new int[]{17, 37, 19, 13, 24, 7, 27, 17};
78+
recipientAddressesSummary =
79+
String.format("%n%s%n ", SummaryHelper.lineSeparator(recipientAddressesColSizes, "-"))
80+
+ "| City "
81+
+ "| Complete Address "
82+
+ "| Is Address Change "
83+
+ "| Postal Code "
84+
+ "| Private Mailbox Number "
85+
+ "| State "
86+
+ "| Street "
87+
+ "| Unit "
88+
+ String.format("|%n%s%n ", SummaryHelper.lineSeparator(recipientAddressesColSizes, "="));
89+
recipientAddressesSummary += SummaryHelper.arrayToString(this.getRecipientAddresses(), recipientAddressesColSizes);
90+
recipientAddressesSummary += String.format("%n%s", SummaryHelper.lineSeparator(recipientAddressesColSizes, "-"));
91+
}
92+
outStr.append(
93+
String.format(":Recipient Addresses: %s%n", recipientAddressesSummary)
94+
);
95+
outStr.append(
96+
String.format(":Return to Sender: %s%n", this.getIsReturnToSender())
97+
);
98+
return SummaryHelper.cleanSummary(outStr.toString());
99+
}
100+
}

0 commit comments

Comments
 (0)