/
AnalyzeBusinessCardFromUrlAsync.java
234 lines (215 loc) · 12.6 KB
/
AnalyzeBusinessCardFromUrlAsync.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.ai.formrecognizer;
import com.azure.ai.formrecognizer.models.AnalyzeDocumentOptions;
import com.azure.ai.formrecognizer.models.AnalyzeResult;
import com.azure.ai.formrecognizer.models.AnalyzedDocument;
import com.azure.ai.formrecognizer.models.DocumentField;
import com.azure.ai.formrecognizer.models.DocumentFieldType;
import com.azure.ai.formrecognizer.models.DocumentOperationResult;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.util.polling.LongRunningOperationStatus;
import com.azure.core.util.polling.PollerFlux;
import reactor.core.publisher.Mono;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* Asynchronous sample for analyzing business card information from a URL.
*/
public class AnalyzeBusinessCardFromUrlAsync {
/**
* Main method to invoke this demo.
*
* @param args Unused. Arguments to the program.
*/
public static void main(final String[] args) throws IOException {
// Instantiate a client that will be used to call the service.
DocumentAnalysisAsyncClient client = new DocumentAnalysisClientBuilder()
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildAsyncClient();
String businessCardUrl =
"https://raw.githubusercontent.com/Azure/azure-sdk-for-java/main/sdk/formrecognizer"
+ "/azure-ai-formrecognizer/src/samples/resources/sample-forms/businessCards/businessCard.jpg";
PollerFlux<DocumentOperationResult, AnalyzeResult> analyzeBusinessCardPoller
= client.beginAnalyzeDocumentFromUrl("prebuilt-businessCard",
businessCardUrl, new AnalyzeDocumentOptions().setPages(Arrays.asList("1")).setLocale("en-US"));
Mono<AnalyzeResult> businessCardPageResultsMono
= analyzeBusinessCardPoller
.last()
.flatMap(pollResponse -> {
if (LongRunningOperationStatus.SUCCESSFULLY_COMPLETED.equals(pollResponse.getStatus())) {
System.out.println("Polling completed successfully");
return pollResponse.getFinalResult();
} else {
return Mono.error(new RuntimeException("Polling completed unsuccessfully with status:"
+ pollResponse.getStatus()));
}
});
businessCardPageResultsMono.subscribe(businessCardPageResults -> {
for (int i = 0; i < businessCardPageResults.getDocuments().size(); i++) {
System.out.printf("--------Analyzing business card %d%n--------", i);
AnalyzedDocument analyzedBusinessCard = businessCardPageResults.getDocuments().get(i);
Map<String, DocumentField> businessCardFields = analyzedBusinessCard.getFields();
DocumentField contactNamesDocumentField = businessCardFields.get("ContactNames");
if (contactNamesDocumentField != null) {
if (DocumentFieldType.LIST == contactNamesDocumentField.getType()) {
List<DocumentField> contactNamesValueDocumentFields = contactNamesDocumentField.getValueList();
contactNamesValueDocumentFields.stream()
.filter(contactNamesValueDocumentField ->
DocumentFieldType.MAP == contactNamesValueDocumentField.getType())
.map(contactNamesValueDocumentField -> {
System.out
.printf("Contact name: %s%n", contactNamesValueDocumentField.getContent());
return contactNamesValueDocumentField.getValueMap();
})
.forEach(contactNamesDocumentFieldMap -> contactNamesDocumentFieldMap
.forEach((label, contactNameDocumentField) -> {
if ("FirstName".equals(label)) {
if (DocumentFieldType.STRING == contactNameDocumentField.getType()) {
String firstName = contactNameDocumentField.getValueString();
System.out.printf("\tFirst Name: %s, confidence: %.2f%n",
firstName, contactNameDocumentField.getConfidence());
}
}
if ("LastName".equals(label)) {
if (DocumentFieldType.STRING == contactNameDocumentField.getType()) {
String lastName = contactNameDocumentField.getValueString();
System.out.printf("\tLast Name: %s, confidence: %.2f%n",
lastName, contactNameDocumentField.getConfidence());
}
}
}));
}
}
DocumentField jobTitles = businessCardFields.get("JobTitles");
if (jobTitles != null) {
if (DocumentFieldType.LIST == jobTitles.getType()) {
List<DocumentField> jobTitlesItems = jobTitles.getValueList();
jobTitlesItems.forEach(jobTitlesItem -> {
if (DocumentFieldType.STRING == jobTitlesItem.getType()) {
String jobTitle = jobTitlesItem.getValueString();
System.out.printf("Job Title: %s, confidence: %.2f%n",
jobTitle, jobTitlesItem.getConfidence());
}
});
}
}
DocumentField departments = businessCardFields.get("Departments");
if (departments != null) {
if (DocumentFieldType.LIST == departments.getType()) {
List<DocumentField> departmentsItems = departments.getValueList();
departmentsItems.forEach(departmentsItem -> {
if (DocumentFieldType.STRING == departmentsItem.getType()) {
String department = departmentsItem.getValueString();
System.out.printf("Department: %s, confidence: %.2f%n",
department, departmentsItem.getConfidence());
}
});
}
}
DocumentField emails = businessCardFields.get("Emails");
if (emails != null) {
if (DocumentFieldType.LIST == emails.getType()) {
List<DocumentField> emailsItems = emails.getValueList();
emailsItems.forEach(emailsItem -> {
if (DocumentFieldType.STRING == emailsItem.getType()) {
String email = emailsItem.getValueString();
System.out.printf("Email: %s, confidence: %.2f%n", email, emailsItem.getConfidence());
}
});
}
}
DocumentField websites = businessCardFields.get("Websites");
if (websites != null) {
if (DocumentFieldType.LIST == websites.getType()) {
List<DocumentField> websitesItems = websites.getValueList();
websitesItems.forEach(websitesItem -> {
if (DocumentFieldType.STRING == websitesItem.getType()) {
String website = websitesItem.getValueString();
System.out.printf("Web site: %s, confidence: %.2f%n",
website, websitesItem.getConfidence());
}
});
}
}
DocumentField mobilePhones = businessCardFields.get("MobilePhones");
if (mobilePhones != null) {
if (DocumentFieldType.LIST == mobilePhones.getType()) {
List<DocumentField> mobilePhonesItems = mobilePhones.getValueList();
mobilePhonesItems.forEach(mobilePhonesItem -> {
if (DocumentFieldType.PHONE_NUMBER == mobilePhonesItem.getType()) {
String mobilePhoneNumber = mobilePhonesItem.getValuePhoneNumber();
System.out.printf("Mobile phone number: %s, confidence: %.2f%n",
mobilePhoneNumber, mobilePhonesItem.getConfidence());
}
});
}
}
DocumentField otherPhones = businessCardFields.get("OtherPhones");
if (otherPhones != null) {
if (DocumentFieldType.LIST == otherPhones.getType()) {
List<DocumentField> otherPhonesItems = otherPhones.getValueList();
otherPhonesItems.forEach(otherPhonesItem -> {
if (DocumentFieldType.PHONE_NUMBER == otherPhonesItem.getType()) {
String otherPhoneNumber = otherPhonesItem.getValuePhoneNumber();
System.out.printf("Other phone number: %s, confidence: %.2f%n",
otherPhoneNumber, otherPhonesItem.getConfidence());
}
});
}
}
DocumentField faxes = businessCardFields.get("Faxes");
if (faxes != null) {
if (DocumentFieldType.LIST == faxes.getType()) {
List<DocumentField> faxesItems = faxes.getValueList();
faxesItems.forEach(faxesItem -> {
if (DocumentFieldType.PHONE_NUMBER == faxesItem.getType()) {
String faxPhoneNumber = faxesItem.getValuePhoneNumber();
System.out.printf("Fax phone number: %s, confidence: %.2f%n",
faxPhoneNumber, faxesItem.getConfidence());
}
});
}
}
DocumentField addresses = businessCardFields.get("Addresses");
if (addresses != null) {
if (DocumentFieldType.LIST == addresses.getType()) {
List<DocumentField> addressesItems = addresses.getValueList();
addressesItems.forEach(addressesItem -> {
if (DocumentFieldType.STRING == addressesItem.getType()) {
String address = addressesItem.getValueString();
System.out
.printf("Address: %s, confidence: %.2f%n", address, addressesItem.getConfidence());
}
});
}
}
DocumentField companyName = businessCardFields.get("CompanyNames");
if (companyName != null) {
if (DocumentFieldType.LIST == companyName.getType()) {
List<DocumentField> companyNameItems = companyName.getValueList();
companyNameItems.forEach(companyNameItem -> {
if (DocumentFieldType.STRING == companyNameItem.getType()) {
String companyNameValue = companyNameItem.getValueString();
System.out.printf("Company name: %s, confidence: %.2f%n", companyNameValue,
companyNameItem.getConfidence());
}
});
}
}
}
});
// The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep
// the thread so the program does not end before the send operation is complete. Using .block() instead of
// .subscribe() will turn this into a synchronous call.
try {
TimeUnit.MINUTES.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}