diff --git a/eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml b/eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml index 11048593d743..e7baf627fd47 100755 --- a/eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml +++ b/eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml @@ -417,6 +417,8 @@ + + diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md b/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md index 162ff520b38f..3cb44caee64f 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md +++ b/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md @@ -2,6 +2,9 @@ ## 1.0.0-beta.4 (Unreleased) ### Breaking Changes +- `beginRecognizeReceipt` APIs now return a `RecognizedForm` model instead of a `RecognizedReceipt`. See +[this](https://github.com/Azure/azure-sdk-for-java/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/StronglyTypedRecognizedFormUSReceipt.java) +suggested approach for extracting information from receipts. - Methods returning `textContent` have been renamed to `fieldElements` on `FieldData` and `FormTableCell` - Renamed `FormContent` to `FormElement` - Renamed `FieldText` to `FieldData` diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/README.md b/sdk/formrecognizer/azure-ai-formrecognizer/README.md index 1ba2bd54919b..6fef4de94f21 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/README.md +++ b/sdk/formrecognizer/azure-ai-formrecognizer/README.md @@ -177,7 +177,7 @@ followed by polling the service at intervals to determine whether the operation succeeded, to get the result. Methods that train models or recognize values from forms are modeled as long-running operations. The client exposes -a `begin` method that returns a `SyncPoller` or `PollerFlux` instance. +a `begin` method that returns a `SyncPoller` or `PollerFlux` instance. Callers should wait for the operation to completed by calling `getFinalResult()` on the returned operation from the `begin` method. Sample code snippets are provided to illustrate using long-running operations [below](#Examples). @@ -195,7 +195,7 @@ The following section provides several code snippets covering some of the most c ### Recognize Forms Using a Custom Model Recognize name/value pairs and table data from forms. These models are trained with your own data, so they're tailored to your forms. You should only recognize forms of the same form type that the custom model was trained on. - + ```java String formUrl = "{form_url}"; String modelId = "{custom_trained_model_id}"; @@ -206,20 +206,19 @@ List recognizedForms = recognizeFormPoller.getFinalResult(); for (int i = 0; i < recognizedForms.size(); i++) { RecognizedForm form = recognizedForms.get(i); - System.out.printf("----------- Recognized Form %d-----------%n", i); + System.out.printf("----------- Recognized Form %d -----------%n", i); System.out.printf("Form type: %s%n", form.getFormType()); - form.getFields().forEach((label, formField) -> { + form.getFields().forEach((label, formField) -> System.out.printf("Field %s has value %s with confidence score of %f.%n", label, formField.getValueData().getText(), - formField.getConfidence()); - }); - System.out.print("-----------------------------------"); + formField.getConfidence()) + ); } ``` ### Recognize Content Recognize text and table structures, along with their bounding box coordinates, from documents. - + ```java // recognize form content using file input stream File form = new File("local/file_path/filename.png"); @@ -233,7 +232,7 @@ List contentPageResults = recognizeContentPoller.getFinalResult(); for (int i = 0; i < contentPageResults.size(); i++) { FormPage formPage = contentPageResults.get(i); - System.out.printf("----Recognizing content for page %d----%n", i); + System.out.printf("----Recognizing content for page %d ----%n", i); // Table information System.out.printf("Has width: %f and height: %f, measured with unit: %s.%n", formPage.getWidth(), formPage.getHeight(), @@ -241,64 +240,75 @@ for (int i = 0; i < contentPageResults.size(); i++) { formPage.getTables().forEach(formTable -> { System.out.printf("Table has %d rows and %d columns.%n", formTable.getRowCount(), formTable.getColumnCount()); - formTable.getCells().forEach(formTableCell -> { - System.out.printf("Cell has text %s.%n", formTableCell.getText()); - }); - System.out.println(); + formTable.getCells().forEach(formTableCell -> + System.out.printf("Cell has text %s.%n", formTableCell.getText())); }); } ``` ### Recognize receipts -Recognize data from a USA sales receipts using a prebuilt model. [Here][service_recognize_receipt] are the fields the -service returns for a recognized receipt. - +Recognize data from a USA sales receipts using a prebuilt model. [Here][service_recognize_receipt] are the fields the service returns for a recognized receipt. See [StronglyTypedRecognizedForm.java][strongly_typed_sample] for a suggested approach to extract +information from receipts. + + ```java String receiptUrl = "https://docs.microsoft.com/en-us/azure/cognitive-services/form-recognizer/media" + "/contoso-allinone.jpg"; -SyncPoller> syncPoller = +SyncPoller> syncPoller = formRecognizerClient.beginRecognizeReceiptsFromUrl(receiptUrl); -List receiptPageResults = syncPoller.getFinalResult(); +List receiptPageResults = syncPoller.getFinalResult(); for (int i = 0; i < receiptPageResults.size(); i++) { - RecognizedReceipt recognizedReceipt = receiptPageResults.get(i); - Map recognizedFields = recognizedReceipt.getRecognizedForm().getFields(); + RecognizedForm recognizedForm = receiptPageResults.get(i); + Map> recognizedFields = recognizedForm.getFields(); System.out.printf("----------- Recognized Receipt page %d -----------%n", i); - FormField merchantNameField = recognizedFields.get("MerchantName"); - if (merchantNameField.getFieldValue().getType() == FieldValueType.STRING) { - System.out.printf("Merchant Name: %s, confidence: %.2f%n", - merchantNameField.getFieldValue().asString(), - merchantNameField.getConfidence()); + FormField merchantNameField = recognizedFields.get("MerchantName"); + if (merchantNameField != null) { + if (FieldValueType.STRING.equals(merchantNameField.getValueType())) { + String merchantName = FieldValueType.STRING.cast(merchantNameField); + System.out.printf("Merchant Name: %s, confidence: %.2f%n", + merchantName, merchantNameField.getConfidence()); + } } - FormField transactionDateField = recognizedFields.get("TransactionDate"); - if (transactionDateField.getFieldValue().getType() == FieldValueType.DATE) { - System.out.printf("Transaction Date: %s, confidence: %.2f%n", - transactionDateField.getFieldValue().asDate(), - transactionDateField.getConfidence()); + + FormField merchantPhoneNumberField = recognizedFields.get("MerchantPhoneNumber"); + if (merchantPhoneNumberField != null) { + if (FieldValueType.PHONE_NUMBER.equals(merchantNameField.getValueType())) { + String merchantAddress = FieldValueType.PHONE_NUMBER.cast(merchantPhoneNumberField); + System.out.printf("Merchant Phone number: %s, confidence: %.2f%n", + merchantAddress, merchantPhoneNumberField.getConfidence()); + } } - FormField receiptItemsField = recognizedFields.get("Items"); - System.out.printf("Receipt Items: %n"); - if (receiptItemsField.getFieldValue().getType() == FieldValueType.LIST) { - List receiptItems = receiptItemsField.getFieldValue().asList(); - receiptItems.forEach(receiptItem -> { - if (receiptItem.getFieldValue().getType() == FieldValueType.MAP) { - receiptItem.getFieldValue().asMap().forEach((key, formField) -> { - if (key.equals("Name")) { - if (formField.getFieldValue().getType() == FieldValueType.STRING) { - System.out.printf("Name: %s, confidence: %.2fs%n", - formField.getFieldValue().asString(), - formField.getConfidence()); - } - } - if (key.equals("Quantity")) { - if (formField.getFieldValue().getType() == FieldValueType.INTEGER) { - System.out.printf("Quantity: %d, confidence: %.2f%n", - formField.getFieldValue().asInteger(), formField.getConfidence()); + + FormField transactionDateField = recognizedFields.get("TransactionDate"); + if (transactionDateField != null) { + if (FieldValueType.DATE.equals(transactionDateField.getValueType())) { + LocalDate transactionDate = FieldValueType.DATE.cast(transactionDateField); + System.out.printf("Transaction Date: %s, confidence: %.2f%n", + transactionDate, transactionDateField.getConfidence()); + } + } + + FormField receiptItemsField = recognizedFields.get("Items"); + if (receiptItemsField != null) { + System.out.printf("Receipt Items: %n"); + if (FieldValueType.LIST.equals(receiptItemsField.getValueType())) { + List> receiptItems = FieldValueType.LIST.cast(receiptItemsField); + receiptItems.forEach(receiptItem -> { + if (FieldValueType.MAP.equals(receiptItem.getValueType())) { + Map> formFieldMap = FieldValueType.MAP.cast(receiptItem); + formFieldMap.forEach((key, formField) -> { + if ("Quantity".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float quantity = FieldValueType.DOUBLE.cast(formField); + System.out.printf("Quantity: %f, confidence: %.2f%n", + quantity, formField.getConfidence()); + } } - } - }); - } - }); + }); + } + }); + } } } ``` @@ -307,7 +317,7 @@ for (int i = 0; i < receiptPageResults.size(); i++) { Train a machine-learned model on your own form type. The resulting model will be able to recognize values from the types of forms it was trained on. Provide a container SAS url to your Azure Storage Blob container where you're storing the training documents. See details on setting this up in the [service quickstart documentation][quickstart_training]. - + ```java String trainingFilesUrl = "{SAS-URL-of-your-container-in-blob-storage}"; SyncPoller trainingPoller = @@ -334,7 +344,7 @@ customFormModel.getSubmodels().forEach(customFormSubmodel -> { ### Manage your models Manage the custom models attached to your account. - + ```java AtomicReference modelId = new AtomicReference<>(); // First, we see how many custom models we have, and what our limit is @@ -376,7 +386,7 @@ to provide an invalid file source URL an `HttpResponseException` would be raised In the following code snippet, the error is handled gracefully by catching the exception and display the additional information about the error. - + ```java try { formRecognizerClient.beginRecognizeContentFromUrl("invalidSourceUrl"); @@ -414,7 +424,7 @@ These code samples show common scenario operations with the Azure Form Recognize #### Async APIs All the examples shown so far have been using synchronous APIs, but we provide full support for async APIs as well. You'll need to use `FormRecognizerAsyncClient` - + ```java FormRecognizerAsyncClient formRecognizerAsyncClient = new FormRecognizerClientBuilder() .credential(new AzureKeyCredential("{key}")) @@ -498,7 +508,8 @@ This project has adopted the [Microsoft Open Source Code of Conduct][coc]. For m [service_access]: https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account?tabs=multiservice%2Cwindows [service_doc_train_unlabeled]: https://docs.microsoft.com/azure/cognitive-services/form-recognizer/overview#train-without-labels [service_doc_train_labeled]: https://docs.microsoft.com/azure/cognitive-services/form-recognizer/overview#train-with-labels -[service_recognize_receipt]: https://westus2.dev.cognitive.microsoft.com/docs/services/form-recognizer-api-v2-preview/operations/GetAnalyzeReceiptResult +[service_recognize_receipt]: https://aka.ms/azsdk/python/formrecognizer/receiptfields +[strongly_typed_sample]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/StronglyTypedRecognizedForm.java [source_code]: src [quickstart_training]: https://docs.microsoft.com/azure/cognitive-services/form-recognizer/quickstarts/curl-train-extract#train-a-form-recognizer-model [wiki_identity]: https://github.com/Azure/azure-sdk-for-java/wiki/Identity-and-Authentication diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClient.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClient.java index 6f35bf31af36..6e9dc5a0a962 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClient.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClient.java @@ -16,7 +16,6 @@ import com.azure.ai.formrecognizer.models.OperationResult; import com.azure.ai.formrecognizer.models.RecognizeOptions; import com.azure.ai.formrecognizer.models.RecognizedForm; -import com.azure.ai.formrecognizer.models.RecognizedReceipt; import com.azure.core.annotation.ReturnType; import com.azure.core.annotation.ServiceClient; import com.azure.core.annotation.ServiceMethod; @@ -38,7 +37,6 @@ import java.util.UUID; import java.util.function.Function; -import static com.azure.ai.formrecognizer.Transforms.toReceipt; import static com.azure.ai.formrecognizer.Transforms.toRecognizedForm; import static com.azure.ai.formrecognizer.Transforms.toRecognizedLayout; import static com.azure.ai.formrecognizer.implementation.Utility.detectContentType; @@ -339,13 +337,13 @@ public PollerFlux> beginRecognizeContent(Flux> + public PollerFlux> beginRecognizeReceiptsFromUrl(String receiptUrl) { return beginRecognizeReceiptsFromUrl(receiptUrl, null); } @@ -364,17 +362,17 @@ public PollerFlux> beginRecognizeContent(Flux> + public PollerFlux> beginRecognizeReceiptsFromUrl(String receiptUrl, RecognizeOptions recognizeOptions) { try { recognizeOptions = getRecognizeOptionsProperties(recognizeOptions); - return new PollerFlux>( + return new PollerFlux>( recognizeOptions.getPollInterval(), receiptAnalyzeActivationOperation(receiptUrl, recognizeOptions.isIncludeFieldElements()), extractReceiptPollOperation(), @@ -403,13 +401,13 @@ public PollerFlux> beginRecognizeContent(Flux> beginRecognizeReceipts( + public PollerFlux> beginRecognizeReceipts( Flux receipt, long length) { return beginRecognizeReceipts(receipt, length, null); } @@ -433,13 +431,13 @@ public PollerFlux> beginRecognizeReceip * analyzing a receipt. * * @return A {@link PollerFlux} that polls the recognize receipt operation until it has completed, has failed, - * or has been cancelled. The completed operation returns a List of {@link RecognizedReceipt}. + * or has been cancelled. The completed operation returns a List of {@link RecognizedForm}. * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code recognizeOptions} is {@code null}. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public PollerFlux> + public PollerFlux> beginRecognizeReceipts(Flux receipt, long length, RecognizeOptions recognizeOptions) { try { recognizeOptions = getRecognizeOptionsProperties(recognizeOptions); @@ -514,7 +512,7 @@ private Function, Mono> receipt }; } - private Function, Mono>> + private Function, Mono>> fetchExtractReceiptResult(boolean includeFieldElements) { return (pollingContext) -> { try { @@ -522,7 +520,8 @@ private Function, Mono> receipt return service.getAnalyzeReceiptResultWithResponseAsync(resultUid) .map(modelSimpleResponse -> { throwIfAnalyzeStatusInvalid(modelSimpleResponse.getValue()); - return toReceipt(modelSimpleResponse.getValue().getAnalyzeResult(), includeFieldElements); + return toRecognizedForm(modelSimpleResponse.getValue().getAnalyzeResult(), + includeFieldElements); }) .onErrorMap(Utility::mapToHttpResponseExceptionIfExist); } catch (RuntimeException ex) { diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerClient.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerClient.java index 2d0fcb6f8d42..be8c2332fc80 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerClient.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerClient.java @@ -11,7 +11,6 @@ import com.azure.ai.formrecognizer.models.OperationResult; import com.azure.ai.formrecognizer.models.RecognizeOptions; import com.azure.ai.formrecognizer.models.RecognizedForm; -import com.azure.ai.formrecognizer.models.RecognizedReceipt; import com.azure.core.annotation.ReturnType; import com.azure.core.annotation.ServiceClient; import com.azure.core.annotation.ServiceMethod; @@ -257,13 +256,13 @@ public SyncPoller> beginRecognizeContent(InputSt * @param receiptUrl The URL of the receipt to analyze. * * @return A {@link SyncPoller} to poll the progress of the recognize receipt operation until it has completed, - * has failed, or has been cancelled. The completed operation returns a List of {@link RecognizedReceipt}. + * has failed, or has been cancelled. The completed operation returns a List of {@link RecognizedForm}. * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code receiptUrl} is {@code null}. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public SyncPoller> beginRecognizeReceiptsFromUrl(String receiptUrl) { + public SyncPoller> beginRecognizeReceiptsFromUrl(String receiptUrl) { return beginRecognizeReceiptsFromUrl(receiptUrl, null); } @@ -281,13 +280,13 @@ public SyncPoller> beginRecognizeReceip * analyzing a receipt. Include text lines and element references in the result. * * @return A {@link SyncPoller} to poll the progress of the recognize receipt operation until it has completed, - * has failed, or has been cancelled. The completed operation returns a List of {@link RecognizedReceipt}. + * has failed, or has been cancelled. The completed operation returns a List of {@link RecognizedForm}. * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code receiptUrl} is {@code null}. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public SyncPoller> + public SyncPoller> beginRecognizeReceiptsFromUrl(String receiptUrl, RecognizeOptions recognizeOptions) { return client.beginRecognizeReceiptsFromUrl(receiptUrl, recognizeOptions).getSyncPoller(); } @@ -306,13 +305,13 @@ public SyncPoller> beginRecognizeReceip * @param length The exact length of the data. * * @return A {@link SyncPoller} that polls the recognize receipt operation until it has completed, - * has failed, or has been cancelled. The completed operation returns a List of {@link RecognizedReceipt}. + * has failed, or has been cancelled. The completed operation returns a List of {@link RecognizedForm}. * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code receipt} is {@code null}. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public SyncPoller> + public SyncPoller> beginRecognizeReceipts(InputStream receipt, long length) { return beginRecognizeReceipts(receipt, length, null); } @@ -333,13 +332,13 @@ public SyncPoller> beginRecognizeReceip * analyzing a receipt. * * @return A {@link SyncPoller} that polls the recognize receipt operation until it has completed, has failed, - * or has been cancelled. The completed operation returns a List of {@link RecognizedReceipt}. + * or has been cancelled. The completed operation returns a List of {@link RecognizedForm}. * @throws FormRecognizerException If recognize operation fails and the {@link AnalyzeOperationResult} returned with * an {@link OperationStatus#FAILED}. * @throws NullPointerException If {@code recognizeOptions} is {@code null}. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public SyncPoller> + public SyncPoller> beginRecognizeReceipts(InputStream receipt, long length, RecognizeOptions recognizeOptions) { Flux buffer = Utility.toFluxByteBuffer(receipt); return client.beginRecognizeReceipts(buffer, length, recognizeOptions).getSyncPoller(); diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/Transforms.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/Transforms.java index 156c0a8f28ab..9ddbefa914a1 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/Transforms.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/Transforms.java @@ -25,7 +25,6 @@ import com.azure.ai.formrecognizer.models.LengthUnit; import com.azure.ai.formrecognizer.models.Point; import com.azure.ai.formrecognizer.models.RecognizedForm; -import com.azure.ai.formrecognizer.models.RecognizedReceipt; import com.azure.core.util.CoreUtils; import com.azure.core.util.logging.ClientLogger; @@ -80,7 +79,7 @@ static List toRecognizedForm(AnalyzeResult analyzeResult, boolea formPageRange = new FormPageRange(1, 1); } - Map extractedFieldMap = getUnlabeledFieldMap(documentResultItem, readResults, + Map> extractedFieldMap = getUnlabeledFieldMap(documentResultItem, readResults, includeFieldElements); extractedFormList.add(new RecognizedForm( extractedFieldMap, @@ -97,7 +96,7 @@ static List toRecognizedForm(AnalyzeResult analyzeResult, boolea if (clusterId != null) { formType.append(clusterId); } - Map extractedFieldMap = getLabeledFieldMap(includeFieldElements, readResults, + Map> extractedFieldMap = getLabeledFieldMap(includeFieldElements, readResults, pageResultItem, pageNumber); extractedFormList.add(new RecognizedForm( @@ -110,21 +109,6 @@ static List toRecognizedForm(AnalyzeResult analyzeResult, boolea return extractedFormList; } - /** - * Helper method to transform the service returned {@link AnalyzeResult} to SDK model {@link RecognizedReceipt}. - * - * @param analyzeResult The service returned result for analyze receipts. - * @param includeFieldElements Boolean to indicate if to set reference elements data on fields. - * - * @return The List of {@code RecognizedReceipt}. - */ - static List toReceipt(AnalyzeResult analyzeResult, boolean includeFieldElements) { - return toRecognizedForm(analyzeResult, includeFieldElements) - .stream() - .map(recognizedForm -> new RecognizedReceipt(recognizedForm)) - .collect(Collectors.toList()); - } - /** * Helper method to transform the service returned {@link AnalyzeResult} to SDK model {@link FormPage}. * @@ -213,9 +197,9 @@ static List getReadResultFormLines(ReadResult readResultItem) { * * @return The {@code RecognizedForm#getFields}. */ - private static Map getUnlabeledFieldMap(DocumentResult documentResultItem, + private static Map> getUnlabeledFieldMap(DocumentResult documentResultItem, List readResults, boolean includeFieldElements) { - Map extractedFieldMap = new TreeMap<>(); + Map> extractedFieldMap = new TreeMap<>(); // add receipt fields if (!CoreUtils.isNullOrEmpty(documentResultItem.getFields())) { documentResultItem.getFields().forEach((key, fieldValue) -> { @@ -233,8 +217,8 @@ private static Map getUnlabeledFieldMap(DocumentResult docume readResults)); } else { FieldData labelText = new FieldData(key, null, null, null); - extractedFieldMap.put(key, new FormField(DEFAULT_CONFIDENCE_VALUE, labelText, - key, null, null)); + extractedFieldMap.put(key, new FormField<>(DEFAULT_CONFIDENCE_VALUE, labelText, + key, null, null, null)); } }); } @@ -254,52 +238,44 @@ private static Map getUnlabeledFieldMap(DocumentResult docume * * @return The strongly typed {@link FormField} for the field input. */ - private static FormField setFormField(FieldData labelText, String key, FieldValue fieldValue, + private static FormField setFormField(FieldData labelText, String key, FieldValue fieldValue, FieldData valueText, Integer pageNumber, List readResults) { - FormField value; + FormField value; switch (fieldValue.getType()) { case PHONE_NUMBER: - value = new FormField(setDefaultConfidenceValue(fieldValue.getConfidence()), labelText, - key, new com.azure.ai.formrecognizer.models.FieldValue(FieldValueType.PHONE_NUMBER). - setFormFieldPhoneNumber(fieldValue.getValuePhoneNumber()), valueText); + value = new FormField<>(setDefaultConfidenceValue(fieldValue.getConfidence()), labelText, + key, fieldValue.getValuePhoneNumber(), valueText, FieldValueType.PHONE_NUMBER); break; case STRING: - value = new FormField(setDefaultConfidenceValue(fieldValue.getConfidence()), labelText, - key, new com.azure.ai.formrecognizer.models.FieldValue(FieldValueType.STRING) - .setFormFieldString(fieldValue.getValueString()), valueText); + value = new FormField<>(setDefaultConfidenceValue(fieldValue.getConfidence()), labelText, + key, fieldValue.getValueString(), valueText, FieldValueType.STRING); break; case TIME: LocalTime fieldTime = fieldValue.getValueTime() == null ? null : LocalTime .parse(fieldValue.getValueTime(), DateTimeFormatter.ofPattern("HH:mm:ss")); - value = new FormField(setDefaultConfidenceValue(fieldValue.getConfidence()), labelText, - key, new com.azure.ai.formrecognizer.models.FieldValue(FieldValueType.TIME) - .setFormFieldTime(fieldTime), valueText); + value = new FormField<>(setDefaultConfidenceValue(fieldValue.getConfidence()), labelText, + key, fieldTime, valueText, FieldValueType.TIME); break; case DATE: - value = new FormField(setDefaultConfidenceValue(fieldValue.getConfidence()), labelText, - key, new com.azure.ai.formrecognizer.models.FieldValue(FieldValueType.DATE) - .setFormFieldDate(fieldValue.getValueDate()), valueText); + value = new FormField<>(setDefaultConfidenceValue(fieldValue.getConfidence()), labelText, + key, fieldValue.getValueDate(), valueText, FieldValueType.DATE); break; case INTEGER: - value = new FormField(setDefaultConfidenceValue(fieldValue.getConfidence()), labelText, - key, new com.azure.ai.formrecognizer.models.FieldValue(FieldValueType.INTEGER) - .setFormFieldInteger(fieldValue.getValueInteger()), valueText); + value = new FormField<>(setDefaultConfidenceValue(fieldValue.getConfidence()), labelText, + key, fieldValue.getValueInteger(), valueText, FieldValueType.LONG); break; case NUMBER: - value = new FormField(setDefaultConfidenceValue(fieldValue.getConfidence()), labelText, - key, new com.azure.ai.formrecognizer.models.FieldValue(FieldValueType.FLOAT) - .setFormFieldFloat(fieldValue.getValueNumber()), valueText); + value = new FormField<>(setDefaultConfidenceValue(fieldValue.getConfidence()), labelText, + key, fieldValue.getValueNumber(), valueText, FieldValueType.DOUBLE); break; case ARRAY: - value = new FormField(setDefaultConfidenceValue(fieldValue.getConfidence()), null, key, - new com.azure.ai.formrecognizer.models.FieldValue(FieldValueType.LIST) - .setFormFieldList(toFormFieldArray(fieldValue.getValueArray(), readResults)), null); + value = new FormField<>(setDefaultConfidenceValue(fieldValue.getConfidence()), null, key, + toFormFieldArray(fieldValue.getValueArray(), readResults), null, FieldValueType.LIST); break; case OBJECT: - value = new FormField(setDefaultConfidenceValue(fieldValue.getConfidence()), labelText, - key, new com.azure.ai.formrecognizer.models.FieldValue(FieldValueType.MAP) - .setFormFieldMap(toFormFieldObject(fieldValue.getValueObject(), pageNumber, readResults)), valueText - ); + value = new FormField<>(setDefaultConfidenceValue(fieldValue.getConfidence()), labelText, + key, toFormFieldObject(fieldValue.getValueObject(), pageNumber, readResults), valueText, + FieldValueType.MAP); break; default: throw LOGGER.logExceptionAsError(new RuntimeException("FieldValue Type not supported")); @@ -327,9 +303,9 @@ private static float setDefaultConfidenceValue(Float confidence) { * * @return The Map of {@link FormField}. */ - private static Map toFormFieldObject(Map valueObject, + private static Map> toFormFieldObject(Map valueObject, Integer pageNumber, List readResults) { - Map fieldValueObjectMap = new TreeMap<>(); + Map> fieldValueObjectMap = new TreeMap<>(); valueObject.forEach((key, fieldValue) -> fieldValueObjectMap.put(key, setFormField(null, key, fieldValue, new FieldData(fieldValue.getText(), @@ -350,7 +326,7 @@ private static Map toFormFieldObject(Map * * @return The List of {@link FormField}. */ - private static List toFormFieldArray(List valueArray, List readResults) { + private static List> toFormFieldArray(List valueArray, List readResults) { return valueArray.stream() .map(fieldValue -> setFormField(null, null, fieldValue, null, fieldValue.getPage(), readResults)) .collect(Collectors.toList()); @@ -388,10 +364,10 @@ private static FormPage getFormPage(ReadResult readResultItem, List p * * @return The fields populated on {@link RecognizedForm#getFields() fields}. */ - private static Map getLabeledFieldMap(boolean includeFieldElements, + private static Map> getLabeledFieldMap(boolean includeFieldElements, List readResults, PageResult pageResultItem, Integer pageNumber) { - Map formFieldMap = new TreeMap<>(); + Map> formFieldMap = new TreeMap<>(); List keyValuePairs = pageResultItem.getKeyValuePairs(); forEachWithIndex(keyValuePairs, ((index, keyValuePair) -> { List formKeyContentList = null; @@ -407,10 +383,8 @@ private static Map getLabeledFieldMap(boolean includeFieldEle toBoundingBox(keyValuePair.getValue().getBoundingBox()), pageNumber, formValueContentList); String fieldName = "field-" + index; - FormField formField = new FormField(setDefaultConfidenceValue(keyValuePair.getConfidence()), - labelFieldData, fieldName, - new com.azure.ai.formrecognizer.models.FieldValue(FieldValueType.STRING) - .setFormFieldString(keyValuePair.getValue().getText()), valueText); + FormField formField = new FormField<>(setDefaultConfidenceValue(keyValuePair.getConfidence()), + labelFieldData, fieldName, keyValuePair.getValue().getText(), valueText, FieldValueType.STRING); formFieldMap.put(fieldName, formField); })); return formFieldMap; diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FieldValue.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FieldValue.java deleted file mode 100644 index 688930ce2439..000000000000 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FieldValue.java +++ /dev/null @@ -1,215 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -package com.azure.ai.formrecognizer.models; - -import com.azure.core.annotation.Fluent; - -import java.time.LocalDate; -import java.time.LocalTime; -import java.util.Collections; -import java.util.List; -import java.util.Map; - -/** - * The FieldValue model. - */ -@Fluent -public final class FieldValue { - private final FieldValueType type; - private Map formFieldMap; - private List formFieldList; - private Float formFieldFloat; - private Integer formFieldInteger; - private LocalDate formFieldDate; - private LocalTime formFieldTime; - private String formFieldString; - private String formFieldPhoneNumber; - - /** - * Constructs a FieldValue object - * - * @param type The type of the field. - */ - public FieldValue(final FieldValueType type) { - this.type = type; - } - - /** - * Set the map value of the field. - * - * @param formFieldMap the map value of the field. - * - * @return the FieldValue object itself. - */ - public FieldValue setFormFieldMap(final Map formFieldMap) { - this.formFieldMap = formFieldMap == null ? null : Collections.unmodifiableMap(formFieldMap); - return this; - } - - /** - * Set the list value of the field. - * - * @param formFieldList the list of the field. - * - * @return the FieldValue object itself. - */ - public FieldValue setFormFieldList(final List formFieldList) { - this.formFieldList = formFieldList == null ? null - : Collections.unmodifiableList(formFieldList); - return this; - } - - /** - * Set the float value of the field. - * - * @param formFieldFloat the float value of the field. - * - * @return the FieldValue object itself. - */ - public FieldValue setFormFieldFloat(final Float formFieldFloat) { - this.formFieldFloat = formFieldFloat; - return this; - } - - /** - * Set the integer value of the field. - * - * @param formFieldInteger the integer value of the field. - * - * @return the FieldValue object itself. - */ - public FieldValue setFormFieldInteger(final Integer formFieldInteger) { - this.formFieldInteger = formFieldInteger; - return this; - } - - /** - * Set the date value of the field. - * - * @param formFieldDate the date value of the field. - * - * @return the FieldValue object itself. - */ - public FieldValue setFormFieldDate(final LocalDate formFieldDate) { - this.formFieldDate = formFieldDate; - return this; - } - - /** - * Set the time value of the field. - * - * @param formFieldTime the time value of the field. - * - * @return the FieldValue object itself. - */ - public FieldValue setFormFieldTime(final LocalTime formFieldTime) { - this.formFieldTime = formFieldTime; - return this; - } - - /** - * Set the string value of the field. - * - * @param formFieldString the string value of the field. - * - * @return the FieldValue object itself. - */ - public FieldValue setFormFieldString(final String formFieldString) { - this.formFieldString = formFieldString; - return this; - } - - /** - * Set the phone number value of the field. - * - * @param formFieldPhoneNumber the phone number value of the field. - * - * @return the FieldValue object itself. - */ - public FieldValue setFormFieldPhoneNumber(final String formFieldPhoneNumber) { - this.formFieldPhoneNumber = formFieldPhoneNumber; - return this; - } - - /** - * Gets the type of the value of the field. - * - * @return the {@link FieldValueType type} of the field. - */ - public FieldValueType getType() { - return type; - } - - /** - * Gets the value of the field as a {@link String}. - * - * @return the value of the field as a {@link String}. - */ - public String asString() { - return this.formFieldString; - } - - /** - * Gets the value of the field as a {@link Integer}. - * - * @return the value of the field as a {@link Integer}. - */ - public Integer asInteger() { - return this.formFieldInteger; - } - - /** - * Gets the value of the field as a {@link Float}. - * - * @return the value of the field as a {@link Float}. - */ - public Float asFloat() { - return this.formFieldFloat; - } - - /** - * Gets the value of the field as a {@link LocalDate}. - * - * @return the value of the field as a {@link LocalDate}. - */ - public LocalDate asDate() { - return this.formFieldDate; - } - - /** - * Gets the value of the field as a {@link LocalTime}. - * - * @return the value of the field as a {@link LocalTime}. - */ - public LocalTime asTime() { - return this.formFieldTime; - } - - /** - * Gets the value of the field as a phone number. - * - * @return the value of the field as a phone number. - */ - public String asPhoneNumber() { - return this.formFieldPhoneNumber; - } - - /** - * Gets the value of the field as a {@link List}. - * - * @return the value of the field as an unmodifiable {@link List}. - */ - public List asList() { - return this.formFieldList; - } - - /** - * Gets the value of the field as a {@link Map}. - * - * @return the value of the field as an unmodifiable {@link Map}. - */ - public Map asMap() { - return this.formFieldMap; - } -} diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FieldValueType.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FieldValueType.java index 93e5f7dda8b0..975c61ae89c4 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FieldValueType.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FieldValueType.java @@ -3,62 +3,179 @@ package com.azure.ai.formrecognizer.models; -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonValue; +import com.azure.core.util.logging.ClientLogger; -/** Defines values for FieldValueType. */ +import java.time.LocalDate; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; +import java.util.Objects; + +/** + * Define enum values for FieldValue types. + */ +@SuppressWarnings("unchecked") public enum FieldValueType { - /** Enum value string. */ - STRING("string"), + /** + * Static value string for FieldValueType. + */ + STRING { + @Override + public T cast(FormField formField) { + if (isFieldValueNull(formField)) { + return null; + } + return (T) String.valueOf(formField.getValue()); + } + }, - /** Enum value date. */ - DATE("date"), + /** + * Static value date for FieldValueType. + */ + DATE { + @Override + public T cast(FormField formField) { + if (isFieldValueNull(formField)) { + return null; + } + if (this == formField.getValueType()) { + return (T) formField.getValue(); + } else if (STRING == formField.getValueType()) { + return (T) LocalDate.parse(formField.getValue().toString(), DateTimeFormatter.ofPattern("yyyy/MM/dd")); + } else { + throw LOGGER.logExceptionAsError(new UnsupportedOperationException(String.format("Cannot cast from " + + "field value of type %s to type %s", formField.getValueType(), DATE))); + } + } + }, - /** Enum value time. */ - TIME("time"), + /** + * Static value time for FieldValueType. + */ + TIME { + @Override + public T cast(FormField formField) { + if (isFieldValueNull(formField)) { + return null; + } + if (this == formField.getValueType()) { + return (T) formField.getValue(); + } else if (STRING == formField.getValueType()) { + return (T) LocalTime.parse(formField.getValue().toString(), DateTimeFormatter.ofPattern("HH:mm:ss")); + } else { + throw LOGGER.logExceptionAsError(new UnsupportedOperationException(String.format("Cannot cast from " + + "field value of type %s to type %s", formField.getValueType(), TIME))); + } + } + }, - /** Enum value phoneNumber. */ - PHONE_NUMBER("phoneNumber"), + /** + * Static value phone number for FieldValueType. + */ + PHONE_NUMBER { + @Override + public T cast(FormField formField) { + if (isFieldValueNull(formField)) { + return null; + } + if (this == formField.getValueType()) { + return (T) formField.getValue(); + } else if (STRING == formField.getValueType()) { + return (T) formField.getValue(); + } else { + throw LOGGER.logExceptionAsError(new UnsupportedOperationException(String.format("Cannot cast from " + + "field value of type %s to type %s", formField.getValueType(), PHONE_NUMBER))); + } + } + }, - /** Enum value number. */ - FLOAT("float"), + /** + * Static value double for FieldValueType. + */ + DOUBLE { + @Override + public T cast(FormField formField) { + if (isFieldValueNull(formField)) { + return null; + } + if (this == formField.getValueType()) { + return (T) formField.getValue(); + } else if (STRING == formField.getValueType()) { + return (T) Double.valueOf(formField.getValue().toString()); + } else { + throw LOGGER.logExceptionAsError(new UnsupportedOperationException(String.format("Cannot cast from " + + "field value of type %s to type %s", formField.getValueType(), DOUBLE))); + } + } + }, - /** Enum value integer. */ - INTEGER("integer"), + /** + * Static value long for FieldValueType. + */ + LONG { + @Override + public T cast(FormField formField) { + if (isFieldValueNull(formField)) { + return null; + } + if (this == formField.getValueType()) { + return (T) formField.getValue(); + } else if (STRING == formField.getValueType()) { + return (T) Long.valueOf(formField.getValue().toString()); + } else { + throw LOGGER.logExceptionAsError(new UnsupportedOperationException(String.format("Cannot cast from " + + "field value of type %s to type %s", formField.getValueType(), LONG))); + } + } + }, - /** Enum value array. */ - LIST("list"), + /** + * Static value list for FieldValueType. + */ + LIST { + @Override + public T cast(FormField formField) { + return getCollectionTypeCast(formField); + } + }, - /** Enum value object. */ - MAP("map"); + /** + * Static value map for FieldValueType. + */ + MAP { + @Override + public T cast(FormField formField) { + return getCollectionTypeCast(formField); + } + }; - /** The actual serialized value for a FieldValueType instance. */ - private final String value; + static boolean isFieldValueNull(FormField formField) { + Objects.requireNonNull(formField, "'formField' cannot be null"); + return formField.getValue() == null; + } - FieldValueType(String value) { - this.value = value; + T getCollectionTypeCast(FormField formField) { + if (isFieldValueNull(formField)) { + return null; + } + if (this == formField.getValueType()) { + return (T) formField.getValue(); + } else { + throw LOGGER.logExceptionAsError(new UnsupportedOperationException(String.format("Cannot cast from " + + "field value of type %s to type %s", formField.getValueType(), this))); + } } + private static final ClientLogger LOGGER = new ClientLogger(FieldValueType.class); + /** - * Parses a serialized value to a FieldValueType instance. + * Converts the form field value to a specific enum type. + * + * @param formField The recognized field value that needs to be converted. + * @param the class of the field. * - * @param value the serialized value to parse. - * @return the parsed FieldValueType object, or null if unable to parse. + * @return the converted value of the recognized field. + * @throws UnsupportedOperationException if the {@code formField} type does not match the casting value type. + * @throws NullPointerException if {@code formField} is {@code null} */ - @JsonCreator - public static FieldValueType fromString(String value) { - FieldValueType[] items = FieldValueType.values(); - for (FieldValueType item : items) { - if (item.toString().equalsIgnoreCase(value)) { - return item; - } - } - return null; - } - - @JsonValue - @Override - public String toString() { - return this.value; - } + public abstract T cast(FormField formField); } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormField.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormField.java index 036bfbef2db2..6eef6b191256 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormField.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormField.java @@ -9,13 +9,14 @@ * The FormField model. */ @Immutable -public final class FormField { +public final class FormField { private final float confidence; private final FieldData labelData; private final String name; - private final FieldValue fieldValue; private final FieldData valueData; + private final T value; + private final FieldValueType valueType; /** * Constructs a FormField object. @@ -23,16 +24,18 @@ public final class FormField { * @param confidence The confidence of the recognized field. * @param labelData The text, bounding box, and field elements for the field label. * @param name The name the field or label. - * @param fieldValue The value of the recognized field. + * @param value The value of the recognized field. * @param valueData The text, bounding box, and field elements for the field value. + * @param valueType The type of the value of the recognized field. */ - public FormField(final float confidence, final FieldData labelData, final String name, final FieldValue fieldValue, - final FieldData valueData) { + public FormField(final float confidence, final FieldData labelData, final String name, final T value, + final FieldData valueData, FieldValueType valueType) { this.confidence = confidence; this.labelData = labelData; this.name = name; - this.fieldValue = fieldValue; + this.value = value; this.valueData = valueData; + this.valueType = valueType; } /** @@ -65,14 +68,25 @@ public String getName() { /** * Get the value of the recognized field. * - * @return Value of the recognized field. + * @return the value of the recognized field. */ - public FieldValue getFieldValue() { - return this.fieldValue; + public T getValue() { + return this.value; } /** - * Get the text, bounding box, and field elements for the field value. + * The type of the value of the recognized field. + * Possible types include: 'String', + * 'LocalDate', 'LocalTime', 'Long', 'Double', 'Map', or 'List'. + * + * @return the type of the value of the field. + */ + public FieldValueType getValueType() { + return valueType; + } + + /** + * Get the text, bounding box, and text content of the field value. * * @return the text, bounding box, and field elements for the field value. */ diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/RecognizedForm.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/RecognizedForm.java index 5a8c11011ca7..9b775763a9f4 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/RecognizedForm.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/RecognizedForm.java @@ -20,7 +20,7 @@ public final class RecognizedForm { * For models trained with labels, this is the training-time label of the field. For models trained with forms * only, a unique name is generated for each field. */ - private final Map fields; + private final Map> fields; /* * Form type. @@ -45,7 +45,7 @@ public final class RecognizedForm { * @param formPageRange First and last page number where the document is found. * @param pages List of extracted pages from the form. */ - public RecognizedForm(final Map fields, final String formType, + public RecognizedForm(final Map> fields, final String formType, final FormPageRange formPageRange, final List pages) { this.fields = fields == null ? null : Collections.unmodifiableMap(fields); this.formType = formType; @@ -60,7 +60,7 @@ public RecognizedForm(final Map fields, final String formType * * @return the unmodifiable map of recognized fields. */ - public Map getFields() { + public Map> getFields() { return this.fields; } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/RecognizedReceipt.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/RecognizedReceipt.java deleted file mode 100644 index 2f16596dfaca..000000000000 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/RecognizedReceipt.java +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -package com.azure.ai.formrecognizer.models; - -import com.azure.core.annotation.Immutable; - -/** - * The RecognizedReceipt model. - */ -@Immutable -public class RecognizedReceipt { - - /** - * The recognized form. - */ - private final RecognizedForm recognizedForm; - - /** - * Constructs a RecognizedReceipt object. - * - * @param recognizedForm The recognized form. - */ - public RecognizedReceipt(final RecognizedForm recognizedForm) { - this.recognizedForm = recognizedForm; - } - - /** - * Get the extracted field information form for the provided document. - * - * @return The extracted field information form for the provided document. - */ - public RecognizedForm getRecognizedForm() { - return this.recognizedForm; - } -} diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/AdvancedDiffLabeledUnlabeledData.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/AdvancedDiffLabeledUnlabeledData.java index e380fce535a9..b3a96dd6c895 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/AdvancedDiffLabeledUnlabeledData.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/AdvancedDiffLabeledUnlabeledData.java @@ -78,7 +78,7 @@ public static void main(String[] args) throws IOException { } System.out.printf("Field %s has value %s based on %s within bounding box %s with a confidence score " + "of %.2f.%n", - label, formField.getFieldValue(), formField.getValueData().getText(), boundingBoxStr, + label, formField.getValue(), formField.getValueData().getText(), boundingBoxStr, formField.getConfidence()); // Find the value of a specific labeled field. @@ -120,7 +120,7 @@ public static void main(String[] args) throws IOException { System.out.printf("Field %s has value %s based on %s within bounding box %s with a confidence score " + "of %.2f.%n", - label, formField.getFieldValue(), formField.getValueData().getText(), boundingBoxStr, + label, formField.getValue(), formField.getValueData().getText(), boundingBoxStr, formField.getConfidence()); // Find the value of a specific unlabeled field. The specific key "Vendor Name:" provided in the example diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/AdvancedDiffLabeledUnlabeledDataAsync.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/AdvancedDiffLabeledUnlabeledDataAsync.java index f5e14b616707..24b783a515d1 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/AdvancedDiffLabeledUnlabeledDataAsync.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/AdvancedDiffLabeledUnlabeledDataAsync.java @@ -108,7 +108,7 @@ public static void main(String[] args) throws IOException { } System.out.printf("Field %s has value %s based on %s within bounding box %s with a confidence score " + "of %.2f.%n", - label, formField.getFieldValue(), formField.getValueData().getText(), boundingBoxStr, + label, formField.getValue(), formField.getValueData().getText(), boundingBoxStr, formField.getConfidence()); // Find the value of a specific labeled field. @@ -157,7 +157,7 @@ public static void main(String[] args) throws IOException { System.out.printf("Field %s has value %s based on %s within bounding box %s with a confidence " + "score of %.2f.%n", - label, formField.getFieldValue(), formField.getValueData().getText(), boundingBoxStr, + label, formField.getValue(), formField.getValueData().getText(), boundingBoxStr, formField.getConfidence()); // Find the value of a specific unlabeled field. The specific key "Vendor Name:" provided in the diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/Authentication.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/Authentication.java index a8ddc6396270..121fa1b04037 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/Authentication.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/Authentication.java @@ -7,21 +7,21 @@ import com.azure.ai.formrecognizer.models.FieldValueType; import com.azure.ai.formrecognizer.models.FormField; import com.azure.ai.formrecognizer.models.OperationResult; -import com.azure.ai.formrecognizer.models.RecognizedReceipt; +import com.azure.ai.formrecognizer.models.RecognizedForm; import com.azure.ai.formrecognizer.training.FormTrainingClient; import com.azure.ai.formrecognizer.training.FormTrainingClientBuilder; import com.azure.core.credential.AzureKeyCredential; import com.azure.core.util.polling.SyncPoller; import com.azure.identity.DefaultAzureCredentialBuilder; -import java.io.IOException; +import java.time.LocalDate; import java.util.List; import java.util.Map; /** * Samples for two supported methods of authentication in Form Recognizer and Form Training clients: - * 1) Use a Form Recognizer API key with AzureKeyCredential from azure.core.credentials - * 2) Use a token credential from azure-identity to authenticate with Azure Active Directory + * 1) Use a Form Recognizer API key with AzureKeyCredential from azure.core.credentials + * 2) Use a token credential from azure-identity to authenticate with Azure Active Directory */ public class Authentication { /** @@ -29,16 +29,15 @@ public class Authentication { * * @param args Unused arguments to the program. * - * @throws IOException Exception thrown when there is an error in reading all the bytes from the File. */ public static void main(String[] args) { - /* - Set the environment variables with your own values before running the sample: + /** + * Set the environment variables with your own values before running the sample: + * AZURE_CLIENT_ID - the client ID of your active directory application. + * AZURE_TENANT_ID - the tenant ID of your active directory application. + * AZURE_CLIENT_SECRET - the secret of your active directory application. + */ - 1) AZURE_CLIENT_ID - the client ID of your active directory application. - 2) AZURE_TENANT_ID - the tenant ID of your active directory application. - 3) AZURE_CLIENT_SECRET - the secret of your active directory application. - */ // Form recognizer client: Key credential authenticationWithKeyCredentialFormRecognizerClient(); // Form recognizer client: Azure Active Directory @@ -82,52 +81,72 @@ private static void authenticationWithAzureActiveDirectoryFormTrainingClient() { } private static void beginRecognizeCustomFormsFromUrl(FormRecognizerClient formRecognizerClient) { - String receiptUrl = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/sample-forms/receipts/contoso-allinone.jpg"; + String receiptUrl = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/sdk/formrecognizer" + + "/azure-ai-formrecognizer/src/samples/java/sample-forms/receipts/contoso-allinone.jpg"; - SyncPoller> recognizeReceiptPoller = + SyncPoller> recognizeReceiptPoller = formRecognizerClient.beginRecognizeReceiptsFromUrl(receiptUrl); - List receiptPageResults = recognizeReceiptPoller.getFinalResult(); + List receiptPageResults = recognizeReceiptPoller.getFinalResult(); for (int i = 0; i < receiptPageResults.size(); i++) { - RecognizedReceipt recognizedReceipt = receiptPageResults.get(i); - Map recognizedFields = recognizedReceipt.getRecognizedForm().getFields(); + RecognizedForm recognizedForm = receiptPageResults.get(i); + Map> recognizedFields = recognizedForm.getFields(); System.out.printf("----------- Recognized Receipt page %d -----------%n", i); - FormField merchantNameField = recognizedFields.get("MerchantName"); - if (merchantNameField.getFieldValue().getType() == FieldValueType.STRING) { - System.out.printf("Merchant Name: %s, confidence: %.2f%n", - merchantNameField.getFieldValue().asString(), - merchantNameField.getConfidence()); + FormField merchantNameField = recognizedFields.get("MerchantName"); + if (merchantNameField != null) { + if (FieldValueType.STRING.equals(merchantNameField.getValueType())) { + String merchantName = FieldValueType.STRING.cast(merchantNameField); + System.out.printf("Merchant Name: %s, confidence: %.2f%n", + merchantName, merchantNameField.getConfidence()); + } } - FormField transactionDateField = recognizedFields.get("TransactionDate"); - if (transactionDateField.getFieldValue().getType() == FieldValueType.DATE) { - System.out.printf("Transaction Date: %s, confidence: %.2f%n", - transactionDateField.getFieldValue().asDate(), - transactionDateField.getConfidence()); + + FormField merchantPhoneNumberField = recognizedFields.get("MerchantPhoneNumber"); + if (merchantPhoneNumberField != null) { + if (FieldValueType.PHONE_NUMBER.equals(merchantNameField.getValueType())) { + String merchantAddress = FieldValueType.PHONE_NUMBER.cast(merchantPhoneNumberField); + System.out.printf("Merchant Phone number: %s, confidence: %.2f%n", + merchantAddress, merchantPhoneNumberField.getConfidence()); + } + } + + FormField transactionDateField = recognizedFields.get("TransactionDate"); + if (transactionDateField != null) { + if (FieldValueType.DATE.equals(transactionDateField.getValueType())) { + LocalDate transactionDate = FieldValueType.DATE.cast(transactionDateField); + System.out.printf("Transaction Date: %s, confidence: %.2f%n", + transactionDate, transactionDateField.getConfidence()); + } } - FormField receiptItemsField = recognizedFields.get("Items"); - System.out.printf("Receipt Items: %n"); - if (receiptItemsField.getFieldValue().getType() == FieldValueType.LIST) { - List receiptItems = receiptItemsField.getFieldValue().asList(); - receiptItems.forEach(receiptItem -> { - if (receiptItem.getFieldValue().getType() == FieldValueType.MAP) { - receiptItem.getFieldValue().asMap().forEach((key, formField) -> { - if (key.equals("Name")) { - if (formField.getFieldValue().getType() == FieldValueType.STRING) { - System.out.printf("Name: %s, confidence: %.2fs%n", - formField.getFieldValue().asString(), - formField.getConfidence()); + + FormField receiptItemsField = recognizedFields.get("Items"); + if (receiptItemsField != null) { + System.out.printf("Receipt Items: %n"); + if (FieldValueType.LIST.equals(receiptItemsField.getValueType())) { + List> receiptItems = FieldValueType.LIST.cast(receiptItemsField); + receiptItems.forEach(receiptItem -> { + if (FieldValueType.MAP.equals(receiptItem.getValueType())) { + Map> formFieldMap = FieldValueType.MAP.cast(receiptItem); + formFieldMap.forEach((key, formField) -> { + if ("Name".equals(key)) { + if (FieldValueType.STRING.equals(formField.getValueType())) { + String name = FieldValueType.STRING.cast(formField); + System.out.printf("Name: %s, confidence: %.2fs%n", + name, formField.getConfidence()); + } } - } - if (key.equals("Quantity")) { - if (formField.getFieldValue().getType() == FieldValueType.INTEGER) { - System.out.printf("Quantity: %s, confidence: %.2f%n", - formField.getFieldValue().asInteger(), formField.getConfidence()); + if ("Quantity".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float quantity = FieldValueType.DOUBLE.cast(formField); + System.out.printf("Quantity: %f, confidence: %.2f%n", + quantity, formField.getConfidence()); + } } - } - }); - } - }); + }); + } + }); + } } System.out.print("-----------------------------------"); } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClientJavaDocCodeSnippets.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClientJavaDocCodeSnippets.java index ab23ef74d3c2..3ac36fa32243 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClientJavaDocCodeSnippets.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClientJavaDocCodeSnippets.java @@ -7,7 +7,7 @@ import com.azure.ai.formrecognizer.models.FormContentType; import com.azure.ai.formrecognizer.models.FormField; import com.azure.ai.formrecognizer.models.RecognizeOptions; -import com.azure.ai.formrecognizer.models.RecognizedReceipt; +import com.azure.ai.formrecognizer.models.RecognizedForm; import com.azure.core.credential.AzureKeyCredential; import com.azure.core.http.HttpPipeline; import com.azure.core.http.HttpPipelineBuilder; @@ -19,6 +19,7 @@ import java.nio.ByteBuffer; import java.nio.file.Files; import java.time.Duration; +import java.time.LocalDate; import java.util.List; import java.util.Map; @@ -76,7 +77,7 @@ public void beginRecognizeCustomFormsFromUrl() { recognizedForms.forEach(recognizedForm -> { recognizedForm.getFields().forEach((fieldText, fieldValue) -> { System.out.printf("Field text: %s%n", fieldText); - System.out.printf("Field value: %s%n", fieldValue.getFieldValue()); + System.out.printf("Field value: %s%n", fieldValue.getValue()); System.out.printf("Confidence score: %.2f%n", fieldValue.getConfidence()); }); })); @@ -103,7 +104,7 @@ public void beginRecognizeCustomFormsFromUrlWithOptions() { recognizedForms.forEach(recognizedForm -> { recognizedForm.getFields().forEach((fieldText, fieldValue) -> { System.out.printf("Field text: %s%n", fieldText); - System.out.printf("Field value: %s%n", fieldValue.getFieldValue()); + System.out.printf("Field value: %s%n", fieldValue.getValue()); System.out.printf("Confidence score: %.2f%n", fieldValue.getConfidence()); }); })); @@ -129,7 +130,7 @@ public void beginRecognizeCustomForms() throws IOException { recognizedForms.forEach(recognizedForm -> { recognizedForm.getFields().forEach((fieldText, fieldValue) -> { System.out.printf("Field text: %s%n", fieldText); - System.out.printf("Field value: %s%n", fieldValue.getFieldValue()); + System.out.printf("Field value: %s%n", fieldValue.getValue()); System.out.printf("Confidence score: %.2f%n", fieldValue.getConfidence()); }); })); @@ -162,7 +163,7 @@ public void beginRecognizeCustomFormsWithOptions() throws IOException { recognizedForms.forEach(recognizedForm -> { recognizedForm.getFields().forEach((fieldText, fieldValue) -> { System.out.printf("Field text: %s%n", fieldText); - System.out.printf("Field value: %s%n", fieldValue.getFieldValue()); + System.out.printf("Field value: %s%n", fieldValue.getValue()); System.out.printf("Confidence score: %.2f%n", fieldValue.getConfidence()); }); })); @@ -289,37 +290,57 @@ public void beginRecognizeReceiptsFromUrl() { recognizePollingOperation.getFinalResult()) .subscribe(recognizedReceipts -> { for (int i = 0; i < recognizedReceipts.size(); i++) { - RecognizedReceipt recognizedReceipt = recognizedReceipts.get(i); - Map recognizedFields = recognizedReceipt.getRecognizedForm().getFields(); + RecognizedForm recognizedForm = recognizedReceipts.get(i); + Map> recognizedFields = recognizedForm.getFields(); System.out.printf("----------- Recognized Receipt page %d -----------%n", i); - FormField merchantNameField = recognizedFields.get("MerchantName"); - if (merchantNameField.getFieldValue().getType() == FieldValueType.STRING) { - System.out.printf("Merchant Name: %s, confidence: %.2f%n", - merchantNameField.getFieldValue().asString(), - merchantNameField.getConfidence()); + FormField merchantNameField = recognizedFields.get("MerchantName"); + if (merchantNameField != null) { + if (FieldValueType.STRING.equals(merchantNameField.getValueType())) { + String merchantName = FieldValueType.STRING.cast(merchantNameField); + System.out.printf("Merchant Name: %s, confidence: %.2f%n", + merchantName, merchantNameField.getConfidence()); + } } - FormField transactionDateField = recognizedFields.get("TransactionDate"); - if (transactionDateField.getFieldValue().getType() == FieldValueType.DATE) { - System.out.printf("Transaction Date: %s, confidence: %.2f%n", - transactionDateField.getFieldValue().asDate(), - transactionDateField.getConfidence()); + + FormField merchantPhoneNumberField = recognizedFields.get("MerchantPhoneNumber"); + if (merchantPhoneNumberField != null) { + if (FieldValueType.PHONE_NUMBER.equals(merchantNameField.getValueType())) { + String merchantAddress = FieldValueType.PHONE_NUMBER.cast(merchantPhoneNumberField); + System.out.printf("Merchant Phone number: %s, confidence: %.2f%n", + merchantAddress, merchantPhoneNumberField.getConfidence()); + } + } + + FormField transactionDateField = recognizedFields.get("TransactionDate"); + if (transactionDateField != null) { + if (FieldValueType.DATE.equals(transactionDateField.getValueType())) { + LocalDate transactionDate = FieldValueType.DATE.cast(transactionDateField); + System.out.printf("Transaction Date: %s, confidence: %.2f%n", + transactionDate, transactionDateField.getConfidence()); + } } - FormField receiptItemsField = recognizedFields.get("Items"); - System.out.printf("Receipt Items: %n"); - if (receiptItemsField.getFieldValue().getType() == FieldValueType.LIST) { - List receiptItems = receiptItemsField.getFieldValue().asList(); - receiptItems.forEach(receiptItem -> { - if (receiptItem.getFieldValue().getType() == FieldValueType.MAP) { - receiptItem.getFieldValue().asMap().forEach((key, formField) -> { - if (key.equals("Quantity")) { - if (formField.getFieldValue().getType() == FieldValueType.INTEGER) { - System.out.printf("Quantity: %s, confidence: %.2f%n", - formField.getFieldValue().asInteger(), formField.getConfidence()); + + FormField receiptItemsField = recognizedFields.get("Items"); + if (receiptItemsField != null) { + System.out.printf("Receipt Items: %n"); + if (FieldValueType.LIST.equals(receiptItemsField.getValueType())) { + List> receiptItems = FieldValueType.LIST.cast(receiptItemsField); + receiptItems.forEach(receiptItem -> { + if (FieldValueType.MAP.equals(receiptItem.getValueType())) { + + Map> formFieldMap = FieldValueType.MAP.cast(receiptItem); + formFieldMap.forEach((key, formField) -> { + if ("Quantity".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float quantity = FieldValueType.DOUBLE.cast(formField); + System.out.printf("Quantity: %f, confidence: %.2f%n", + quantity, formField.getConfidence()); + } } - } - }); - } - }); + }); + } + }); + } } } }); @@ -342,37 +363,57 @@ public void beginRecognizeReceiptsFromUrlWithOptions() { recognizePollingOperation.getFinalResult()) .subscribe(recognizedReceipts -> { for (int i = 0; i < recognizedReceipts.size(); i++) { - RecognizedReceipt recognizedReceipt = recognizedReceipts.get(i); - Map recognizedFields = recognizedReceipt.getRecognizedForm().getFields(); - System.out.printf("----------- Recognized Receipt page %s -----------%n", i); - FormField merchantNameField = recognizedFields.get("MerchantName"); - if (merchantNameField.getFieldValue().getType() == FieldValueType.STRING) { - System.out.printf("Merchant Name: %s, confidence: %.2f%n", - merchantNameField.getFieldValue().asString(), - merchantNameField.getConfidence()); + RecognizedForm recognizedReceipt = recognizedReceipts.get(i); + Map> recognizedFields = recognizedReceipt.getFields(); + System.out.printf("----------- Recognized Receipt page %d -----------%n", i); + FormField merchantNameField = recognizedFields.get("MerchantName"); + if (merchantNameField != null) { + if (FieldValueType.STRING.equals(merchantNameField.getValueType())) { + String merchantName = FieldValueType.STRING.cast(merchantNameField); + System.out.printf("Merchant Name: %s, confidence: %.2f%n", + merchantName, merchantNameField.getConfidence()); + } } - FormField transactionDateField = recognizedFields.get("TransactionDate"); - if (transactionDateField.getFieldValue().getType() == FieldValueType.DATE) { - System.out.printf("Transaction Date: %s, confidence: %.2f%n", - transactionDateField.getFieldValue().asDate(), - transactionDateField.getConfidence()); + + FormField merchantPhoneNumberField = recognizedFields.get("MerchantPhoneNumber"); + if (merchantPhoneNumberField != null) { + if (FieldValueType.PHONE_NUMBER.equals(merchantNameField.getValueType())) { + String merchantAddress = FieldValueType.PHONE_NUMBER.cast(merchantPhoneNumberField); + System.out.printf("Merchant Phone number: %s, confidence: %.2f%n", + merchantAddress, merchantPhoneNumberField.getConfidence()); + } } - FormField receiptItemsField = recognizedFields.get("Items"); - System.out.printf("Receipt Items: %n"); - if (receiptItemsField.getFieldValue().getType() == FieldValueType.LIST) { - List receiptItems = receiptItemsField.getFieldValue().asList(); - receiptItems.forEach(receiptItem -> { - if (receiptItem.getFieldValue().getType() == FieldValueType.MAP) { - receiptItem.getFieldValue().asMap().forEach((key, formField) -> { - if (key.equals("Quantity")) { - if (formField.getFieldValue().getType() == FieldValueType.INTEGER) { - System.out.printf("Quantity: %s, confidence: %.2f%n", - formField.getFieldValue().asInteger(), formField.getConfidence()); + + FormField transactionDateField = recognizedFields.get("TransactionDate"); + if (transactionDateField != null) { + if (FieldValueType.DATE.equals(transactionDateField.getValueType())) { + LocalDate transactionDate = FieldValueType.DATE.cast(transactionDateField); + System.out.printf("Transaction Date: %s, confidence: %.2f%n", + transactionDate, transactionDateField.getConfidence()); + } + } + + FormField receiptItemsField = recognizedFields.get("Items"); + if (receiptItemsField != null) { + System.out.printf("Receipt Items: %n"); + if (FieldValueType.LIST.equals(receiptItemsField.getValueType())) { + List> receiptItems = FieldValueType.LIST.cast(receiptItemsField); + receiptItems.forEach(receiptItem -> { + if (FieldValueType.MAP.equals(receiptItem.getValueType())) { + + Map> formFieldMap = FieldValueType.MAP.cast(receiptItem); + formFieldMap.forEach((key, formField) -> { + if ("Quantity".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float quantity = FieldValueType.DOUBLE.cast(formField); + System.out.printf("Quantity: %f, confidence: %.2f%n", + quantity, formField.getConfidence()); + } } - } - }); - } - }); + }); + } + }); + } } } }); @@ -389,45 +430,64 @@ public void beginRecognizeReceipts() throws IOException { File receipt = new File("{file_source_url}"); Flux buffer = toFluxByteBuffer(new ByteArrayInputStream(Files.readAllBytes(receipt.toPath()))); formRecognizerAsyncClient.beginRecognizeReceipts(buffer, receipt.length()) - .flatMap(recognizePollingOperation -> + .subscribe(recognizePollingOperation -> // if training polling operation completed, retrieve the final result. - recognizePollingOperation.getFinalResult()) - .subscribe(recognizedReceipts -> { - for (int i = 0; i < recognizedReceipts.size(); i++) { - RecognizedReceipt recognizedReceipt = recognizedReceipts.get(i); - Map recognizedFields = recognizedReceipt.getRecognizedForm().getFields(); - System.out.printf("----------- Recognized Receipt page %s -----------%n", i); - FormField merchantNameField = recognizedFields.get("MerchantName"); - if (merchantNameField.getFieldValue().getType() == FieldValueType.STRING) { - System.out.printf("Merchant Name: %s, confidence: %.2f%n", - merchantNameField.getFieldValue().asString(), - merchantNameField.getConfidence()); - } - FormField transactionDateField = recognizedFields.get("TransactionDate"); - if (transactionDateField.getFieldValue().getType() == FieldValueType.DATE) { - System.out.printf("Transaction Date: %s, confidence: %.2f%n", - transactionDateField.getFieldValue().asDate(), - transactionDateField.getConfidence()); - } - FormField receiptItemsField = recognizedFields.get("Items"); - System.out.printf("Receipt Items: %n"); - if (receiptItemsField.getFieldValue().getType() == FieldValueType.LIST) { - List receiptItems = receiptItemsField.getFieldValue().asList(); - receiptItems.forEach(receiptItem -> { - if (receiptItem.getFieldValue().getType() == FieldValueType.MAP) { - receiptItem.getFieldValue().asMap().forEach((key, formField) -> { - if (key.equals("Quantity")) { - if (formField.getFieldValue().getType() == FieldValueType.INTEGER) { - System.out.printf("Quantity: %s, confidence: %.2f%n", - formField.getFieldValue().asInteger(), formField.getConfidence()); - } + recognizePollingOperation.getFinalResult().subscribe(recognizedReceipts -> { + for (int i = 0; i < recognizedReceipts.size(); i++) { + RecognizedForm recognizedForm = recognizedReceipts.get(i); + Map> recognizedFields = recognizedForm.getFields(); + System.out.printf("----------- Recognized Receipt page %d -----------%n", i); + FormField merchantNameField = recognizedFields.get("MerchantName"); + if (merchantNameField != null) { + if (FieldValueType.STRING.equals(merchantNameField.getValueType())) { + String merchantName = FieldValueType.STRING.cast(merchantNameField); + System.out.printf("Merchant Name: %s, confidence: %.2f%n", + merchantName, merchantNameField.getConfidence()); + } + } + + FormField merchantPhoneNumberField = recognizedFields.get("MerchantPhoneNumber"); + if (merchantPhoneNumberField != null) { + if (FieldValueType.PHONE_NUMBER.equals(merchantNameField.getValueType())) { + String merchantAddress = FieldValueType.PHONE_NUMBER.cast(merchantPhoneNumberField); + System.out.printf("Merchant Phone number: %s, confidence: %.2f%n", + merchantAddress, merchantPhoneNumberField.getConfidence()); + } + } + + FormField transactionDateField = recognizedFields.get("TransactionDate"); + if (transactionDateField != null) { + if (FieldValueType.DATE.equals(transactionDateField.getValueType())) { + LocalDate transactionDate = FieldValueType.DATE.cast(transactionDateField); + System.out.printf("Transaction Date: %s, confidence: %.2f%n", + transactionDate, transactionDateField.getConfidence()); + } + } + + FormField receiptItemsField = recognizedFields.get("Items"); + if (receiptItemsField != null) { + System.out.printf("Receipt Items: %n"); + if (FieldValueType.LIST.equals(receiptItemsField.getValueType())) { + List> receiptItems = FieldValueType.LIST.cast(receiptItemsField); + receiptItems.forEach(receiptItem -> { + if (FieldValueType.MAP.equals(receiptItem.getValueType())) { + + Map> formFieldMap = FieldValueType.MAP.cast(receiptItem); + formFieldMap.forEach((key, formField) -> { + if ("Quantity".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float quantity = FieldValueType.DOUBLE.cast(formField); + System.out.printf("Quantity: %f, confidence: %.2f%n", + quantity, formField.getConfidence()); + } + } + }); } }); } - }); + } } - } - }); + })); // END: com.azure.ai.formrecognizer.FormRecognizerAsyncClient.beginRecognizeReceipts#Flux-long } @@ -448,45 +508,65 @@ public void beginRecognizeReceiptsWithOptions() throws IOException { .setContentType(FormContentType.IMAGE_JPEG) .setIncludeFieldElements(includeFieldElements) .setPollInterval(Duration.ofSeconds(5))) - .flatMap(recognizePollingOperation -> + .subscribe(recognizePollingOperation -> // if training polling operation completed, retrieve the final result. - recognizePollingOperation.getFinalResult()) - .subscribe(recognizedReceipts -> { - for (int i = 0; i < recognizedReceipts.size(); i++) { - RecognizedReceipt recognizedReceipt = recognizedReceipts.get(i); - Map recognizedFields = recognizedReceipt.getRecognizedForm().getFields(); - System.out.printf("----------- Recognized Receipt page %s -----------%n", i); - FormField merchantNameField = recognizedFields.get("MerchantName"); - if (merchantNameField.getFieldValue().getType() == FieldValueType.STRING) { - System.out.printf("Merchant Name: %s, confidence: %.2f%n", - merchantNameField.getFieldValue().asString(), - merchantNameField.getConfidence()); - } - FormField transactionDateField = recognizedFields.get("TransactionDate"); - if (transactionDateField.getFieldValue().getType() == FieldValueType.DATE) { - System.out.printf("Transaction Date: %s, confidence: %.2f%n", - transactionDateField.getFieldValue().asDate(), - transactionDateField.getConfidence()); - } - FormField receiptItemsField = recognizedFields.get("Items"); - System.out.printf("Receipt Items: %n"); - if (receiptItemsField.getFieldValue().getType() == FieldValueType.LIST) { - List receiptItems = receiptItemsField.getFieldValue().asList(); - receiptItems.forEach(receiptItem -> { - if (receiptItem.getFieldValue().getType() == FieldValueType.MAP) { - receiptItem.getFieldValue().asMap().forEach((key, formField) -> { - if (key.equals("Quantity")) { - if (formField.getFieldValue().getType() == FieldValueType.INTEGER) { - System.out.printf("Quantity: %s, confidence: %.2f%n", - formField.getFieldValue().asInteger(), formField.getConfidence()); - } + recognizePollingOperation.getFinalResult().subscribe(recognizedReceipts -> { + for (int i = 0; i < recognizedReceipts.size(); i++) { + RecognizedForm recognizedForm = recognizedReceipts.get(i); + Map> recognizedFields = recognizedForm.getFields(); + System.out.printf("----------- Recognized Receipt page %d -----------%n", i); + FormField merchantNameField = recognizedFields.get("MerchantName"); + if (merchantNameField != null) { + if (FieldValueType.STRING.equals(merchantNameField.getValueType())) { + String merchantName = FieldValueType.STRING.cast(merchantNameField); + System.out.printf("Merchant Name: %s, confidence: %.2f%n", + merchantName, merchantNameField.getConfidence()); + } + } + + FormField merchantPhoneNumberField = recognizedFields.get("MerchantPhoneNumber"); + if (merchantPhoneNumberField != null) { + if (FieldValueType.PHONE_NUMBER.equals(merchantNameField.getValueType())) { + String merchantAddress = FieldValueType.PHONE_NUMBER.cast(merchantPhoneNumberField); + System.out.printf("Merchant Phone number: %s, confidence: %.2f%n", + merchantAddress, merchantPhoneNumberField.getConfidence()); + } + } + + FormField transactionDateField = recognizedFields.get("TransactionDate"); + if (transactionDateField != null) { + if (FieldValueType.DATE.equals(transactionDateField.getValueType())) { + LocalDate transactionDate = FieldValueType.DATE.cast(transactionDateField); + System.out.printf("Transaction Date: %s, confidence: %.2f%n", + transactionDate, transactionDateField.getConfidence()); + } + } + + FormField receiptItemsField = recognizedFields.get("Items"); + if (receiptItemsField != null) { + System.out.printf("Receipt Items: %n"); + if (FieldValueType.LIST.equals(receiptItemsField.getValueType())) { + List> receiptItems = FieldValueType.LIST.cast(receiptItemsField); + receiptItems.forEach(receiptItem -> { + if (FieldValueType.MAP.equals(receiptItem.getValueType())) { + + Map> formFieldMap = FieldValueType.MAP.cast(receiptItem); + formFieldMap.forEach((key, formField) -> { + if ("Quantity".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float quantity = FieldValueType.DOUBLE.cast(formField); + System.out.printf("Quantity: %f, confidence: %.2f%n", + quantity, formField.getConfidence()); + } + } + }); } }); } - }); + } } - } - }); + })); + // END: com.azure.ai.formrecognizer.FormRecognizerAsyncClient.beginRecognizeReceipts#Flux-long-recognizeOptions } } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/FormRecognizerClientJavaDocCodeSnippets.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/FormRecognizerClientJavaDocCodeSnippets.java index f1aa02207094..3e88ff23d154 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/FormRecognizerClientJavaDocCodeSnippets.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/FormRecognizerClientJavaDocCodeSnippets.java @@ -17,6 +17,7 @@ import java.io.InputStream; import java.nio.file.Files; import java.time.Duration; +import java.time.LocalDate; import java.util.List; import java.util.Map; @@ -67,13 +68,12 @@ public void beginRecognizeCustomFormsFromUrl() { String modelId = "{custom_trained_model_id}"; formRecognizerClient.beginRecognizeCustomFormsFromUrl(formUrl, modelId).getFinalResult() - .forEach(recognizedForm -> { + .forEach(recognizedForm -> recognizedForm.getFields().forEach((fieldText, fieldValue) -> { System.out.printf("Field text: %s%n", fieldText); - System.out.printf("Field value: %s%n", fieldValue.getFieldValue()); + System.out.printf("Field value: %s%n", fieldValue.getValue()); System.out.printf("Confidence score: %.2f%n", fieldValue.getConfidence()); - }); - }); + })); // END: com.azure.ai.formrecognizer.FormRecognizerClient.beginRecognizeCustomFormsFromUrl#string-string } @@ -94,7 +94,7 @@ public void beginRecognizeCustomFormsFromUrlWithOptions() { .getFinalResult() .forEach(recognizedForm -> recognizedForm.getFields().forEach((fieldText, fieldValue) -> { System.out.printf("Field text: %s%n", fieldText); - System.out.printf("Field value: %s%n", fieldValue.getFieldValue()); + System.out.printf("Field value: %s%n", fieldValue.getValue()); System.out.printf("Confidence score: %.2f%n", fieldValue.getConfidence()); })); // END: com.azure.ai.formrecognizer.FormRecognizerClient.beginRecognizeCustomFormsFromUrl#string-string-recognizeOptions @@ -116,7 +116,7 @@ public void beginRecognizeCustomForms() throws IOException { formRecognizerClient.beginRecognizeCustomForms(targetStream, form.length(), modelId).getFinalResult() .forEach(recognizedForm -> recognizedForm.getFields().forEach((fieldText, fieldValue) -> { System.out.printf("Field text: %s%n", fieldText); - System.out.printf("Field value: %s%n", fieldValue.getFieldValue()); + System.out.printf("Field value: %s%n", fieldValue.getValue()); System.out.printf("Confidence score: %.2f%n", fieldValue.getConfidence()); })); // END: com.azure.ai.formrecognizer.FormRecognizerClient.beginRecognizeCustomForms#InputStream-long-string @@ -145,7 +145,7 @@ public void beginRecognizeCustomFormsWithOptions() throws IOException { .getFinalResult() .forEach(recognizedForm -> recognizedForm.getFields().forEach((fieldText, fieldValue) -> { System.out.printf("Field text: %s%n", fieldText); - System.out.printf("Field value: %s%n", fieldValue.getFieldValue()); + System.out.printf("Field value: %s%n", fieldValue.getValue()); System.out.printf("Confidence score: %.2f%n", fieldValue.getConfidence()); })); // END: com.azure.ai.formrecognizer.FormRecognizerClient.beginRecognizeCustomForms#InputStream-long-string-recognizeOptions @@ -250,35 +250,54 @@ public void beginRecognizeReceiptsFromUrl() { String receiptUrl = "{file_source_url}"; formRecognizerClient.beginRecognizeReceiptsFromUrl(receiptUrl).getFinalResult() .forEach(recognizedReceipt -> { - Map recognizedFields = recognizedReceipt.getRecognizedForm().getFields(); - FormField merchantNameField = recognizedFields.get("MerchantName"); - if (merchantNameField.getFieldValue().getType() == FieldValueType.STRING) { - System.out.printf("Merchant Name: %s, confidence: %.2f%n", - merchantNameField.getFieldValue().asString(), - merchantNameField.getConfidence()); + Map> recognizedFields = recognizedReceipt.getFields(); + FormField merchantNameField = recognizedFields.get("MerchantName"); + if (merchantNameField != null) { + if (FieldValueType.STRING.equals(merchantNameField.getValueType())) { + String merchantName = FieldValueType.STRING.cast(merchantNameField); + System.out.printf("Merchant Name: %s, confidence: %.2f%n", + merchantName, merchantNameField.getConfidence()); + } } - FormField transactionDateField = recognizedFields.get("TransactionDate"); - if (transactionDateField.getFieldValue().getType() == FieldValueType.DATE) { - System.out.printf("Transaction Date: %s, confidence: %.2f%n", - transactionDateField.getFieldValue().asDate(), - transactionDateField.getConfidence()); + + FormField merchantPhoneNumberField = recognizedFields.get("MerchantPhoneNumber"); + if (merchantPhoneNumberField != null) { + if (FieldValueType.PHONE_NUMBER.equals(merchantNameField.getValueType())) { + String merchantAddress = FieldValueType.PHONE_NUMBER.cast(merchantPhoneNumberField); + System.out.printf("Merchant Phone number: %s, confidence: %.2f%n", + merchantAddress, merchantPhoneNumberField.getConfidence()); + } + } + + FormField transactionDateField = recognizedFields.get("TransactionDate"); + if (transactionDateField != null) { + if (FieldValueType.DATE.equals(transactionDateField.getValueType())) { + LocalDate transactionDate = FieldValueType.DATE.cast(transactionDateField); + System.out.printf("Transaction Date: %s, confidence: %.2f%n", + transactionDate, transactionDateField.getConfidence()); + } } - FormField receiptItemsField = recognizedFields.get("Items"); - System.out.printf("Receipt Items: %n"); - if (receiptItemsField.getFieldValue().getType() == FieldValueType.LIST) { - List receiptItems = receiptItemsField.getFieldValue().asList(); - receiptItems.forEach(receiptItem -> { - if (receiptItem.getFieldValue().getType() == FieldValueType.MAP) { - receiptItem.getFieldValue().asMap().forEach((key, formField) -> { - if (key.equals("Quantity")) { - if (formField.getFieldValue().getType() == FieldValueType.INTEGER) { - System.out.printf("Quantity: %s, confidence: %.2f%n", - formField.getFieldValue().asInteger(), formField.getConfidence()); + + FormField receiptItemsField = recognizedFields.get("Items"); + if (receiptItemsField != null) { + System.out.printf("Receipt Items: %n"); + if (FieldValueType.LIST.equals(receiptItemsField.getValueType())) { + List> receiptItems = FieldValueType.LIST.cast(receiptItemsField); + receiptItems.forEach(receiptItem -> { + if (FieldValueType.MAP.equals(receiptItem.getValueType())) { + Map> formFieldMap = FieldValueType.MAP.cast(receiptItem); + formFieldMap.forEach((key, formField) -> { + if ("Quantity".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float quantity = FieldValueType.DOUBLE.cast(formField); + System.out.printf("Quantity: %d, confidence: %.2f%n", + quantity, formField.getConfidence()); + } } - } - }); - } - }); + }); + } + }); + } } }); // END: com.azure.ai.formrecognizer.FormRecognizerClient.beginRecognizeReceiptsFromUrl#string @@ -289,38 +308,57 @@ public void beginRecognizeReceiptsFromUrl() { */ public void beginRecognizeReceiptsFromUrlWithOptions() { // BEGIN: com.azure.ai.formrecognizer.FormRecognizerClient.beginRecognizeReceiptsFromUrl#string-recognizeOptions - String receiptUrl = "{file_source_url}"; + String receiptUrl = "{receipt_url}"; formRecognizerClient.beginRecognizeReceiptsFromUrl(receiptUrl).getFinalResult() .forEach(recognizedReceipt -> { - Map recognizedFields = recognizedReceipt.getRecognizedForm().getFields(); - FormField merchantNameField = recognizedFields.get("MerchantName"); - if (merchantNameField.getFieldValue().getType() == FieldValueType.STRING) { - System.out.printf("Merchant Name: %s, confidence: %.2f%n", - merchantNameField.getFieldValue().asString(), - merchantNameField.getConfidence()); + Map> recognizedFields = recognizedReceipt.getFields(); + FormField merchantNameField = recognizedFields.get("MerchantName"); + if (merchantNameField != null) { + if (FieldValueType.STRING.equals(merchantNameField.getValueType())) { + String merchantName = FieldValueType.STRING.cast(merchantNameField); + System.out.printf("Merchant Name: %s, confidence: %.2f%n", + merchantName, merchantNameField.getConfidence()); + } } - FormField transactionDateField = recognizedFields.get("TransactionDate"); - if (transactionDateField.getFieldValue().getType() == FieldValueType.DATE) { - System.out.printf("Transaction Date: %s, confidence: %.2f%n", - transactionDateField.getFieldValue().asDate(), - transactionDateField.getConfidence()); + + FormField merchantPhoneNumberField = recognizedFields.get("MerchantPhoneNumber"); + if (merchantPhoneNumberField != null) { + if (FieldValueType.PHONE_NUMBER.equals(merchantNameField.getValueType())) { + String merchantAddress = FieldValueType.PHONE_NUMBER.cast(merchantPhoneNumberField); + System.out.printf("Merchant Phone number: %s, confidence: %.2f%n", + merchantAddress, merchantPhoneNumberField.getConfidence()); + } + } + + FormField transactionDateField = recognizedFields.get("TransactionDate"); + if (transactionDateField != null) { + if (FieldValueType.DATE.equals(transactionDateField.getValueType())) { + LocalDate transactionDate = FieldValueType.DATE.cast(transactionDateField); + System.out.printf("Transaction Date: %s, confidence: %.2f%n", + transactionDate, transactionDateField.getConfidence()); + } } - FormField receiptItemsField = recognizedFields.get("Items"); - System.out.printf("Receipt Items: %n"); - if (receiptItemsField.getFieldValue().getType() == FieldValueType.LIST) { - List receiptItems = receiptItemsField.getFieldValue().asList(); - receiptItems.forEach(receiptItem -> { - if (receiptItem.getFieldValue().getType() == FieldValueType.MAP) { - receiptItem.getFieldValue().asMap().forEach((key, formField) -> { - if (key.equals("Quantity")) { - if (formField.getFieldValue().getType() == FieldValueType.INTEGER) { - System.out.printf("Quantity: %s, confidence: %.2f%n", - formField.getFieldValue().asInteger(), formField.getConfidence()); + + FormField receiptItemsField = recognizedFields.get("Items"); + if (receiptItemsField != null) { + System.out.printf("Receipt Items: %n"); + if (FieldValueType.LIST.equals(receiptItemsField.getValueType())) { + List> receiptItems = FieldValueType.LIST.cast(receiptItemsField); + receiptItems.forEach(receiptItem -> { + if (FieldValueType.MAP.equals(receiptItem.getValueType())) { + Map> formFieldMap = FieldValueType.MAP.cast(receiptItem); + formFieldMap.forEach((key, formField) -> { + if ("Quantity".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float quantity = FieldValueType.DOUBLE.cast(formField); + System.out.printf("Quantity: %d, confidence: %.2f%n", + quantity, formField.getConfidence()); + } } - } - }); - } - }); + }); + } + }); + } } }); // END: com.azure.ai.formrecognizer.FormRecognizerClient.beginRecognizeReceiptsFromUrl#string-recognizeOptions @@ -333,41 +371,60 @@ public void beginRecognizeReceiptsFromUrlWithOptions() { */ public void beginRecognizeReceipts() throws IOException { // BEGIN: com.azure.ai.formrecognizer.FormRecognizerClient.beginRecognizeReceipts#InputStream-long - File receipt = new File("{file_source_url}"); + File receipt = new File("{receipt_url}"); byte[] fileContent = Files.readAllBytes(receipt.toPath()); InputStream targetStream = new ByteArrayInputStream(fileContent); formRecognizerClient.beginRecognizeReceipts(targetStream, receipt.length()) .getFinalResult().forEach(recognizedReceipt -> { - Map recognizedFields = recognizedReceipt.getRecognizedForm().getFields(); - FormField merchantNameField = recognizedFields.get("MerchantName"); - if (merchantNameField.getFieldValue().getType() == FieldValueType.STRING) { - System.out.printf("Merchant Name: %s, confidence: %.2f%n", - merchantNameField.getFieldValue().asString(), - merchantNameField.getConfidence()); + Map> recognizedFields = recognizedReceipt.getFields(); + FormField merchantNameField = recognizedFields.get("MerchantName"); + if (merchantNameField != null) { + if (FieldValueType.STRING.equals(merchantNameField.getValueType())) { + String merchantName = FieldValueType.STRING.cast(merchantNameField); + System.out.printf("Merchant Name: %s, confidence: %.2f%n", + merchantName, merchantNameField.getConfidence()); + } } - FormField transactionDateField = recognizedFields.get("TransactionDate"); - if (transactionDateField.getFieldValue().getType() == FieldValueType.DATE) { - System.out.printf("Transaction Date: %s, confidence: %.2f%n", - transactionDateField.getFieldValue().asDate(), - transactionDateField.getConfidence()); + + FormField merchantPhoneNumberField = recognizedFields.get("MerchantPhoneNumber"); + if (merchantPhoneNumberField != null) { + if (FieldValueType.PHONE_NUMBER.equals(merchantNameField.getValueType())) { + String merchantAddress = FieldValueType.PHONE_NUMBER.cast(merchantPhoneNumberField); + System.out.printf("Merchant Phone number: %s, confidence: %.2f%n", + merchantAddress, merchantPhoneNumberField.getConfidence()); + } } - FormField receiptItemsField = recognizedFields.get("Items"); - System.out.printf("Receipt Items: %n"); - if (receiptItemsField.getFieldValue().getType() == FieldValueType.LIST) { - List receiptItems = receiptItemsField.getFieldValue().asList(); - receiptItems.forEach(receiptItem -> { - if (receiptItem.getFieldValue().getType() == FieldValueType.MAP) { - receiptItem.getFieldValue().asMap().forEach((key, formField) -> { - if (key.equals("Quantity")) { - if (formField.getFieldValue().getType() == FieldValueType.INTEGER) { - System.out.printf("Quantity: %d, confidence: %.2f%n", - formField.getFieldValue().asInteger(), formField.getConfidence()); + + FormField transactionDateField = recognizedFields.get("TransactionDate"); + if (transactionDateField != null) { + if (FieldValueType.DATE.equals(transactionDateField.getValueType())) { + LocalDate transactionDate = FieldValueType.DATE.cast(transactionDateField); + System.out.printf("Transaction Date: %s, confidence: %.2f%n", + transactionDate, transactionDateField.getConfidence()); + } + } + + FormField receiptItemsField = recognizedFields.get("Items"); + if (receiptItemsField != null) { + System.out.printf("Receipt Items: %n"); + if (FieldValueType.LIST.equals(receiptItemsField.getValueType())) { + List> receiptItems = FieldValueType.LIST.cast(receiptItemsField); + receiptItems.forEach(receiptItem -> { + if (FieldValueType.MAP.equals(receiptItem.getValueType())) { + Map> formFieldMap = FieldValueType.MAP.cast(receiptItem); + formFieldMap.forEach((key, formField) -> { + if ("Quantity".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float quantity = FieldValueType.DOUBLE.cast(formField); + System.out.printf("Quantity: %d, confidence: %.2f%n", + quantity, formField.getConfidence()); + } } - } - }); - } - }); + }); + } + }); + } } }); // END: com.azure.ai.formrecognizer.FormRecognizerClient.beginRecognizeReceipts#InputStream-long @@ -392,35 +449,54 @@ public void beginRecognizeReceiptsWithOptions() throws IOException { .setIncludeFieldElements(includeFieldElements) .setPollInterval(Duration.ofSeconds(5))) .getFinalResult().forEach(recognizedReceipt -> { - Map recognizedFields = recognizedReceipt.getRecognizedForm().getFields(); - FormField merchantNameField = recognizedFields.get("MerchantName"); - if (merchantNameField.getFieldValue().getType() == FieldValueType.STRING) { - System.out.printf("Merchant Name: %s, confidence: %.2f%n", - merchantNameField.getFieldValue().asString(), - merchantNameField.getConfidence()); + Map> recognizedFields = recognizedReceipt.getFields(); + FormField merchantNameField = recognizedFields.get("MerchantName"); + if (merchantNameField != null) { + if (FieldValueType.STRING.equals(merchantNameField.getValueType())) { + String merchantName = FieldValueType.STRING.cast(merchantNameField); + System.out.printf("Merchant Name: %s, confidence: %.2f%n", + merchantName, merchantNameField.getConfidence()); + } } - FormField transactionDateField = recognizedFields.get("TransactionDate"); - if (transactionDateField.getFieldValue().getType() == FieldValueType.DATE) { - System.out.printf("Transaction Date: %s, confidence: %.2f%n", - transactionDateField.getFieldValue().asDate(), - transactionDateField.getConfidence()); + + FormField merchantPhoneNumberField = recognizedFields.get("MerchantPhoneNumber"); + if (merchantPhoneNumberField != null) { + if (FieldValueType.PHONE_NUMBER.equals(merchantNameField.getValueType())) { + String merchantAddress = FieldValueType.PHONE_NUMBER.cast(merchantPhoneNumberField); + System.out.printf("Merchant Phone number: %s, confidence: %.2f%n", + merchantAddress, merchantPhoneNumberField.getConfidence()); + } + } + + FormField transactionDateField = recognizedFields.get("TransactionDate"); + if (transactionDateField != null) { + if (FieldValueType.DATE.equals(transactionDateField.getValueType())) { + LocalDate transactionDate = FieldValueType.DATE.cast(transactionDateField); + System.out.printf("Transaction Date: %s, confidence: %.2f%n", + transactionDate, transactionDateField.getConfidence()); + } } - FormField receiptItemsField = recognizedFields.get("Items"); - System.out.printf("Receipt Items: %n"); - if (receiptItemsField.getFieldValue().getType() == FieldValueType.LIST) { - List receiptItems = receiptItemsField.getFieldValue().asList(); - receiptItems.forEach(receiptItem -> { - if (receiptItem.getFieldValue().getType() == FieldValueType.MAP) { - receiptItem.getFieldValue().asMap().forEach((key, formField) -> { - if (key.equals("Quantity")) { - if (formField.getFieldValue().getType() == FieldValueType.INTEGER) { - System.out.printf("Quantity: %d, confidence: %.2f%n", - formField.getFieldValue().asInteger(), formField.getConfidence()); + + FormField receiptItemsField = recognizedFields.get("Items"); + if (receiptItemsField != null) { + System.out.printf("Receipt Items: %n"); + if (FieldValueType.LIST.equals(receiptItemsField.getValueType())) { + List> receiptItems = FieldValueType.LIST.cast(receiptItemsField); + receiptItems.forEach(receiptItem -> { + if (FieldValueType.MAP.equals(receiptItem.getValueType())) { + Map> formFieldMap = FieldValueType.MAP.cast(receiptItem); + formFieldMap.forEach((key, formField) -> { + if ("Quantity".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float quantity = FieldValueType.DOUBLE.cast(formField); + System.out.printf("Quantity: %d, confidence: %.2f%n", + quantity, formField.getConfidence()); + } } - } - }); - } - }); + }); + } + }); + } } }); // END: com.azure.ai.formrecognizer.FormRecognizerClient.beginRecognizeReceipts#InputStream-long-recognizeOptions diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/GetBoundingBoxes.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/GetBoundingBoxes.java index ed281654cad8..f6f72e2d9c95 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/GetBoundingBoxes.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/GetBoundingBoxes.java @@ -50,7 +50,7 @@ public static void main(String[] args) { recognizedForm.getFields().forEach((fieldText, fieldValue) -> System.out.printf("Field %s has value %s " + "based on %s with a confidence score " + "of %.2f.%n", - fieldText, fieldValue.getFieldValue(), fieldValue.getValueData().getText(), + fieldText, fieldValue.getValue(), fieldValue.getValueData().getText(), fieldValue.getConfidence())); // Page Information diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/GetBoundingBoxesAsync.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/GetBoundingBoxesAsync.java index c8931c16ab2a..ebfad4bdc35f 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/GetBoundingBoxesAsync.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/GetBoundingBoxesAsync.java @@ -63,7 +63,7 @@ public static void main(String[] args) { recognizedForm.getFields().forEach((fieldText, fieldValue) -> { System.out.printf("Field %s has value %s based on %s with a confidence score " + "of %.2f.%n", - fieldText, fieldValue.getFieldValue(), fieldValue.getValueData().getText(), + fieldText, fieldValue.getValue(), fieldValue.getValueData().getText(), fieldValue.getConfidence()); }); diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java index d58e884434b3..e1851afe0cd4 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java @@ -11,7 +11,6 @@ import com.azure.ai.formrecognizer.models.FormPage; import com.azure.ai.formrecognizer.models.OperationResult; import com.azure.ai.formrecognizer.models.RecognizedForm; -import com.azure.ai.formrecognizer.models.RecognizedReceipt; import com.azure.ai.formrecognizer.training.FormTrainingClient; import com.azure.ai.formrecognizer.training.FormTrainingClientBuilder; import com.azure.core.credential.AzureKeyCredential; @@ -26,6 +25,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; +import java.time.LocalDate; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicReference; @@ -95,14 +95,13 @@ public void recognizeCustomForm() { for (int i = 0; i < recognizedForms.size(); i++) { RecognizedForm form = recognizedForms.get(i); - System.out.printf("----------- Recognized Form %d-----------%n", i); + System.out.printf("----------- Recognized Form %d -----------%n", i); System.out.printf("Form type: %s%n", form.getFormType()); - form.getFields().forEach((label, formField) -> { + form.getFields().forEach((label, formField) -> System.out.printf("Field %s has value %s with confidence score of %f.%n", label, formField.getValueData().getText(), - formField.getConfidence()); - }); - System.out.print("-----------------------------------"); + formField.getConfidence()) + ); } } @@ -124,7 +123,7 @@ public void recognizeContent() throws IOException { for (int i = 0; i < contentPageResults.size(); i++) { FormPage formPage = contentPageResults.get(i); - System.out.printf("----Recognizing content for page %d----%n", i); + System.out.printf("----Recognizing content for page %d ----%n", i); // Table information System.out.printf("Has width: %f and height: %f, measured with unit: %s.%n", formPage.getWidth(), formPage.getHeight(), @@ -132,10 +131,8 @@ public void recognizeContent() throws IOException { formPage.getTables().forEach(formTable -> { System.out.printf("Table has %d rows and %d columns.%n", formTable.getRowCount(), formTable.getColumnCount()); - formTable.getCells().forEach(formTableCell -> { - System.out.printf("Cell has text %s.%n", formTableCell.getText()); - }); - System.out.println(); + formTable.getCells().forEach(formTableCell -> + System.out.printf("Cell has text %s.%n", formTableCell.getText())); }); } } @@ -143,49 +140,61 @@ public void recognizeContent() throws IOException { public void recognizeReceipt() { String receiptUrl = "https://docs.microsoft.com/en-us/azure/cognitive-services/form-recognizer/media" + "/contoso-allinone.jpg"; - SyncPoller> syncPoller = + SyncPoller> syncPoller = formRecognizerClient.beginRecognizeReceiptsFromUrl(receiptUrl); - List receiptPageResults = syncPoller.getFinalResult(); + List receiptPageResults = syncPoller.getFinalResult(); for (int i = 0; i < receiptPageResults.size(); i++) { - RecognizedReceipt recognizedReceipt = receiptPageResults.get(i); - Map recognizedFields = recognizedReceipt.getRecognizedForm().getFields(); + RecognizedForm recognizedForm = receiptPageResults.get(i); + Map> recognizedFields = recognizedForm.getFields(); System.out.printf("----------- Recognized Receipt page %d -----------%n", i); - FormField merchantNameField = recognizedFields.get("MerchantName"); - if (merchantNameField.getFieldValue().getType() == FieldValueType.STRING) { - System.out.printf("Merchant Name: %s, confidence: %.2f%n", - merchantNameField.getFieldValue().asString(), - merchantNameField.getConfidence()); + FormField merchantNameField = recognizedFields.get("MerchantName"); + if (merchantNameField != null) { + if (FieldValueType.STRING.equals(merchantNameField.getValueType())) { + String merchantName = FieldValueType.STRING.cast(merchantNameField); + System.out.printf("Merchant Name: %s, confidence: %.2f%n", + merchantName, merchantNameField.getConfidence()); + } } - FormField transactionDateField = recognizedFields.get("TransactionDate"); - if (transactionDateField.getFieldValue().getType() == FieldValueType.DATE) { - System.out.printf("Transaction Date: %s, confidence: %.2f%n", - transactionDateField.getFieldValue().asDate(), - transactionDateField.getConfidence()); + + FormField merchantPhoneNumberField = recognizedFields.get("MerchantPhoneNumber"); + if (merchantPhoneNumberField != null) { + if (FieldValueType.PHONE_NUMBER.equals(merchantNameField.getValueType())) { + String merchantAddress = FieldValueType.PHONE_NUMBER.cast(merchantPhoneNumberField); + System.out.printf("Merchant Phone number: %s, confidence: %.2f%n", + merchantAddress, merchantPhoneNumberField.getConfidence()); + } } - FormField receiptItemsField = recognizedFields.get("Items"); - System.out.printf("Receipt Items: %n"); - if (receiptItemsField.getFieldValue().getType() == FieldValueType.LIST) { - List receiptItems = receiptItemsField.getFieldValue().asList(); - receiptItems.forEach(receiptItem -> { - if (receiptItem.getFieldValue().getType() == FieldValueType.MAP) { - receiptItem.getFieldValue().asMap().forEach((key, formField) -> { - if (key.equals("Name")) { - if (formField.getFieldValue().getType() == FieldValueType.STRING) { - System.out.printf("Name: %s, confidence: %.2fs%n", - formField.getFieldValue().asString(), - formField.getConfidence()); - } - } - if (key.equals("Quantity")) { - if (formField.getFieldValue().getType() == FieldValueType.INTEGER) { - System.out.printf("Quantity: %d, confidence: %.2f%n", - formField.getFieldValue().asInteger(), formField.getConfidence()); + + FormField transactionDateField = recognizedFields.get("TransactionDate"); + if (transactionDateField != null) { + if (FieldValueType.DATE.equals(transactionDateField.getValueType())) { + LocalDate transactionDate = FieldValueType.DATE.cast(transactionDateField); + System.out.printf("Transaction Date: %s, confidence: %.2f%n", + transactionDate, transactionDateField.getConfidence()); + } + } + + FormField receiptItemsField = recognizedFields.get("Items"); + if (receiptItemsField != null) { + System.out.printf("Receipt Items: %n"); + if (FieldValueType.LIST.equals(receiptItemsField.getValueType())) { + List> receiptItems = FieldValueType.LIST.cast(receiptItemsField); + receiptItems.forEach(receiptItem -> { + if (FieldValueType.MAP.equals(receiptItem.getValueType())) { + Map> formFieldMap = FieldValueType.MAP.cast(receiptItem); + formFieldMap.forEach((key, formField) -> { + if ("Quantity".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float quantity = FieldValueType.DOUBLE.cast(formField); + System.out.printf("Quantity: %f, confidence: %.2f%n", + quantity, formField.getConfidence()); + } } - } - }); - } - }); + }); + } + }); + } } } } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/Receipt.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/Receipt.java new file mode 100644 index 000000000000..f174d8f2ff62 --- /dev/null +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/Receipt.java @@ -0,0 +1,360 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.ai.formrecognizer; + +import com.azure.ai.formrecognizer.models.FormField; +import com.azure.ai.formrecognizer.models.RecognizedForm; +import com.azure.core.annotation.Immutable; + +import java.time.LocalDate; +import java.time.LocalTime; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +/** + * The Receipt model. + */ +public final class Receipt { + + /** + * List of recognized field items. + */ + private List receiptItems; + + /** + * Recognized receipt type information. + */ + private ReceiptType receiptType; + + /** + * Recognized field merchant name. + */ + private FormField merchantName; + + /** + * Recognized field merchant address. + */ + private FormField merchantAddress; + + /** + * Recognized field merchant phone number. + */ + private FormField merchantPhoneNumber; + + /** + * Recognized field subtotal. + */ + private FormField subtotal; + + /** + * Recognized field tax. + */ + private FormField tax; + + /** + * Recognized field tip. + */ + private FormField tip; + + /** + * Recognized field total. + */ + private FormField total; + + /** + * Recognized field transaction date. + */ + private FormField transactionDate; + + /** + * Recognized field transaction time. + */ + private FormField transactionTime; + + @SuppressWarnings("unchecked") + public Receipt(RecognizedForm recognizedForm) { + for (Map.Entry> entry : recognizedForm.getFields().entrySet()) { + String key = entry.getKey(); + FormField fieldValue = entry.getValue(); + switch (key) { + case "ReceiptType": + receiptType = new ReceiptType(((FormField) fieldValue).getValue(), + fieldValue.getConfidence()); + break; + case "MerchantName": + merchantName = (FormField) fieldValue; + break; + case "MerchantAddress": + merchantAddress = (FormField) fieldValue; + break; + case "MerchantPhoneNumber": + merchantPhoneNumber = (FormField) fieldValue; + break; + case "Subtotal": + subtotal = (FormField) fieldValue; + break; + case "Tax": + tax = (FormField) fieldValue; + break; + case "Tip": + tip = (FormField) fieldValue; + break; + case "Total": + total = (FormField) fieldValue; + break; + case "TransactionDate": + transactionDate = (FormField) fieldValue; + break; + case "TransactionTime": + transactionTime = (FormField) fieldValue; + break; + case "Items": + receiptItems = Collections.unmodifiableList(toReceiptItems(fieldValue)); + break; + default: + break; + } + } + } + + /** + * Get the itemized fields in the Recognized Receipt. + * + * @return the unmodifiable list of itemized fields on the receipt. + */ + public List getReceiptItems() { + return this.receiptItems; + } + + /** + * Get the type of Recognized Receipt. + * + * @return the type of Recognized Receipt. + */ + public ReceiptType getReceiptType() { + return this.receiptType; + } + + /** + * Get the merchant name field. + * + * @return the merchantName value. + */ + public FormField getMerchantName() { + return this.merchantName; + } + + /** + * Get the merchant address field. + * + * @return the merchantAddress value. + */ + public FormField getMerchantAddress() { + return this.merchantAddress; + } + + /** + * Get the merchant Phone number field. + * + * @return the merchantPhoneNumber value. + */ + public FormField getMerchantPhoneNumber() { + return this.merchantPhoneNumber; + } + + /** + * Get the subtotal field. + * + * @return the subtotal value. + */ + public FormField getSubtotal() { + return this.subtotal; + } + + /** + * Get the tax field. + * + * @return the tax value. + */ + public FormField getTax() { + return this.tax; + } + + /** + * Get the tip field. + * + * @return the tip value. + */ + public FormField getTip() { + return this.tip; + } + + /** + * Get the Total field. + * + * @return the total value. + */ + public FormField getTotal() { + return this.total; + } + + /** + * Get the Transaction date field. + * + * @return the transactionDate value. + */ + public FormField getTransactionDate() { + return this.transactionDate; + } + + /** + * Get the transaction time field. + * + * @return the transactionTime value. + */ + public FormField getTransactionTime() { + return this.transactionTime; + } + + /** + * Helper method to convert the service level + * {@link com.azure.ai.formrecognizer.implementation.models.FieldValue#getValueArray() value items} + * to SDK level {@link ReceiptItem receipt items}. + * + * @param fieldValueItems The strongly typed field values. + * b + * + * @return An unmodifiable list of {@link ReceiptItem}. + */ + @SuppressWarnings("unchecked") + private static List toReceiptItems(FormField fieldValueItems) { + List> fieldValueArray = (List>) fieldValueItems.getValue(); + List receiptItemList = new ArrayList<>(); + + for (FormField eachFieldValue : fieldValueArray) { + Map> objectValue = ((Map>) (eachFieldValue.getValue())); + FormField name = null; + FormField quantity = null; + FormField price = null; + FormField totalPrice = null; + for (Map.Entry> entry : objectValue.entrySet()) { + String key = entry.getKey(); + if ("Quantity".equals(key)) { + quantity = (FormField) entry.getValue(); + } else if ("Name".equals(key)) { + name = (FormField) entry.getValue(); + } else if ("Price".equals(key)) { + price = (FormField) entry.getValue(); + } else if ("Total Price".equals(key)) { + totalPrice = (FormField) entry.getValue(); + } + } + receiptItemList.add(new ReceiptItem(name, quantity, price, totalPrice)); + } + return Collections.unmodifiableList(receiptItemList); + } + + /** + * The USReceiptType model. + */ + @Immutable + public static final class ReceiptType { + private final String type; + private final float confidence; + + /** + * Constructs a Receipt Type. + * + * @param type The type of the receipt. + * @param confidence The confidence score. + */ + public ReceiptType(final String type, final float confidence) { + this.type = type; + this.confidence = confidence; + } + + /** + * Gets the type of the receipt. + * + * @return The type of the receipt. + */ + public String getType() { + return this.type; + } + + /** + * Gets the confidence score of the detected type of the receipt. + * + * @return The confidence score of the detected type of the receipt. + */ + public float getConfidence() { + return this.confidence; + } + } + + /** + * The ReceiptItem model. + */ + @Immutable + public static final class ReceiptItem { + private final FormField name; + private final FormField quantity; + private final FormField price; + private final FormField totalPrice; + + /** + * Constructs a ReceiptItem object. + * + * @param name Name of the field value. + * @param quantity quantity of the field value. + * @param price price of the field value. + * @param totalPrice Total price of the field value. + */ + public ReceiptItem(final FormField name, final FormField quantity, final FormField price, + final FormField totalPrice) { + this.name = name; + this.quantity = quantity; + this.price = price; + this.totalPrice = totalPrice; + } + + /** + * Gets the name of the field value. + * + * @return The name of the field value. + */ + public FormField getName() { + return name; + } + + /** + * Gets the quantity of the Receipt Item. + * + * @return the quantity of Receipt Item. + */ + public FormField getQuantity() { + return quantity; + } + + /** + * Gets the price of the Receipt Item. + * + * @return The total Price. + */ + public FormField getPrice() { + return price; + } + + /** + * Gets the total price of the Receipt Item. + * + * @return The total Price. + */ + public FormField getTotalPrice() { + return totalPrice; + } + } +} diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceipts.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceipts.java index da6871ca95b8..cd612e70d4a6 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceipts.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceipts.java @@ -6,7 +6,7 @@ import com.azure.ai.formrecognizer.models.FieldValueType; import com.azure.ai.formrecognizer.models.FormField; import com.azure.ai.formrecognizer.models.OperationResult; -import com.azure.ai.formrecognizer.models.RecognizedReceipt; +import com.azure.ai.formrecognizer.models.RecognizedForm; import com.azure.core.credential.AzureKeyCredential; import com.azure.core.util.polling.SyncPoller; @@ -15,11 +15,15 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; +import java.time.LocalDate; import java.util.List; import java.util.Map; /** - * Sample for recognizing US receipt information using file source URL. + * Sample for recognizing commonly found US receipt fields from a local file input stream. + * For a suggested approach to extracting information from receipts, see StronglyTypedRecognizedForm.java. + * See fields found on a receipt here: + * https://aka.ms/azsdk/python/formrecognizer/receiptfields */ public class RecognizeReceipts { @@ -42,72 +46,86 @@ public static void main(final String[] args) throws IOException { byte[] fileContent = Files.readAllBytes(sourceFile.toPath()); InputStream targetStream = new ByteArrayInputStream(fileContent); - SyncPoller> analyzeReceiptPoller = + SyncPoller> analyzeReceiptPoller = client.beginRecognizeReceipts(targetStream, sourceFile.length()); - List receiptPageResults = analyzeReceiptPoller.getFinalResult(); + List receiptPageResults = analyzeReceiptPoller.getFinalResult(); for (int i = 0; i < receiptPageResults.size(); i++) { - RecognizedReceipt recognizedReceipt = receiptPageResults.get(i); - Map recognizedFields = recognizedReceipt.getRecognizedForm().getFields(); + RecognizedForm recognizedForm = receiptPageResults.get(i); + Map> recognizedFields = recognizedForm.getFields(); System.out.printf("----------- Recognized Receipt page %d -----------%n", i); - FormField merchantNameField = recognizedFields.get("MerchantName"); + FormField merchantNameField = recognizedFields.get("MerchantName"); if (merchantNameField != null) { - if (merchantNameField.getFieldValue().getType() == FieldValueType.STRING) { + if (FieldValueType.STRING.equals(merchantNameField.getValueType())) { + String merchantName = FieldValueType.STRING.cast(merchantNameField); System.out.printf("Merchant Name: %s, confidence: %.2f%n", - merchantNameField.getFieldValue().asString(), - merchantNameField.getConfidence()); + merchantName, merchantNameField.getConfidence()); } } - FormField merchantAddressField = recognizedFields.get("MerchantAddress"); + + FormField merchantPhoneNumberField = recognizedFields.get("MerchantPhoneNumber"); + if (merchantPhoneNumberField != null) { + if (FieldValueType.PHONE_NUMBER.equals(merchantNameField.getValueType())) { + String merchantAddress = FieldValueType.PHONE_NUMBER.cast(merchantPhoneNumberField); + System.out.printf("Merchant Phone number: %s, confidence: %.2f%n", + merchantAddress, merchantPhoneNumberField.getConfidence()); + } + } + + FormField merchantAddressField = recognizedFields.get("MerchantAddress"); if (merchantAddressField != null) { - if (merchantAddressField.getFieldValue().getType() == FieldValueType.STRING) { + if (FieldValueType.STRING.equals(merchantNameField.getValueType())) { + String merchantAddress = FieldValueType.STRING.cast(merchantAddressField); System.out.printf("Merchant Address: %s, confidence: %.2f%n", - merchantAddressField.getFieldValue().asString(), - merchantAddressField.getConfidence()); + merchantAddress, merchantAddressField.getConfidence()); } } - FormField transactionDateField = recognizedFields.get("TransactionDate"); + + FormField transactionDateField = recognizedFields.get("TransactionDate"); if (transactionDateField != null) { - if (transactionDateField.getFieldValue().getType() == FieldValueType.DATE) { + if (FieldValueType.DATE.equals(transactionDateField.getValueType())) { + LocalDate transactionDate = FieldValueType.DATE.cast(transactionDateField); System.out.printf("Transaction Date: %s, confidence: %.2f%n", - transactionDateField.getFieldValue().asDate(), - transactionDateField.getConfidence()); + transactionDate, transactionDateField.getConfidence()); } } - FormField receiptItemsField = recognizedFields.get("Items"); + + FormField receiptItemsField = recognizedFields.get("Items"); if (receiptItemsField != null) { System.out.printf("Receipt Items: %n"); - if (receiptItemsField.getFieldValue().getType() == FieldValueType.LIST) { - List receiptItems = receiptItemsField.getFieldValue().asList(); + if (FieldValueType.LIST.equals(receiptItemsField.getValueType())) { + List> receiptItems = FieldValueType.LIST.cast(receiptItemsField); receiptItems.forEach(receiptItem -> { - if (receiptItem.getFieldValue().getType() == FieldValueType.MAP) { - receiptItem.getFieldValue().asMap().forEach((key, formField) -> { - if (key.equals("Name")) { - if (formField.getFieldValue().getType() == FieldValueType.STRING) { + if (FieldValueType.MAP.equals(receiptItem.getValueType())) { + Map> formFieldMap = FieldValueType.MAP.cast(receiptItem); + formFieldMap.forEach((key, formField) -> { + if ("Name".equals(key)) { + if (FieldValueType.STRING.equals(formField.getValueType())) { + String name = FieldValueType.STRING.cast(formField); System.out.printf("Name: %s, confidence: %.2fs%n", - formField.getFieldValue().asString(), - formField.getConfidence()); + name, formField.getConfidence()); } } - if (key.equals("Quantity")) { - if (formField.getFieldValue().getType() == FieldValueType.INTEGER) { - System.out.printf("Quantity: %d, confidence: %.2f%n", - formField.getFieldValue().asInteger(), formField.getConfidence()); + if ("Quantity".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float quantity = FieldValueType.DOUBLE.cast(formField); + System.out.printf("Quantity: %f, confidence: %.2f%n", + quantity, formField.getConfidence()); } } - if (key.equals("Price")) { - if (formField.getFieldValue().getType() == FieldValueType.FLOAT) { + if ("Price".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float price = FieldValueType.DOUBLE.cast(formField); System.out.printf("Price: %f, confidence: %.2f%n", - formField.getFieldValue().asFloat(), - formField.getConfidence()); + price, formField.getConfidence()); } } - if (key.equals("TotalPrice")) { - if (formField.getFieldValue().getType() == FieldValueType.FLOAT) { + if ("TotalPrice".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float totalPrice = FieldValueType.DOUBLE.cast(formField); System.out.printf("Total Price: %f, confidence: %.2f%n", - formField.getFieldValue().asFloat(), - formField.getConfidence()); + totalPrice, formField.getConfidence()); } } }); diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsAsync.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsAsync.java index f59aeb97cb60..d9c1b3849187 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsAsync.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsAsync.java @@ -6,7 +6,7 @@ import com.azure.ai.formrecognizer.models.FieldValueType; import com.azure.ai.formrecognizer.models.FormField; import com.azure.ai.formrecognizer.models.OperationResult; -import com.azure.ai.formrecognizer.models.RecognizedReceipt; +import com.azure.ai.formrecognizer.models.RecognizedForm; import com.azure.core.credential.AzureKeyCredential; import com.azure.core.util.polling.PollerFlux; import reactor.core.publisher.Mono; @@ -16,6 +16,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; +import java.time.LocalDate; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; @@ -23,7 +24,10 @@ import static com.azure.ai.formrecognizer.implementation.Utility.toFluxByteBuffer; /** - * Async sample for recognizing US receipt information from a local file. + * Async sample for recognizing commonly found US receipt fields from a local file input stream. + * For a suggested approach to extracting information from receipts, see StronglyTypedRecognizedForm.java. + * See fields found on a receipt here: + * https://aka.ms/azsdk/python/formrecognizer/receiptfields */ public class RecognizeReceiptsAsync { @@ -31,6 +35,7 @@ public class RecognizeReceiptsAsync { * Main method to invoke this demo. * * @param args Unused. Arguments to the program. + * * @throws IOException Exception thrown when there is an error in reading all the bytes from the File. */ public static void main(final String[] args) throws IOException { @@ -45,11 +50,11 @@ public static void main(final String[] args) throws IOException { byte[] fileContent = Files.readAllBytes(sourceFile.toPath()); InputStream targetStream = new ByteArrayInputStream(fileContent); - PollerFlux> analyzeReceiptPoller = + PollerFlux> analyzeReceiptPoller = client.beginRecognizeReceipts(toFluxByteBuffer(targetStream), sourceFile.length()); - Mono> receiptPageResultsMono = analyzeReceiptPoller + Mono> receiptPageResultsMono = analyzeReceiptPoller .last() .flatMap(recognizeReceiptPollOperation -> { if (recognizeReceiptPollOperation.getStatus().isComplete()) { @@ -64,66 +69,80 @@ public static void main(final String[] args) throws IOException { receiptPageResultsMono.subscribe(receiptPageResults -> { for (int i = 0; i < receiptPageResults.size(); i++) { - RecognizedReceipt recognizedReceipt = receiptPageResults.get(i); - Map recognizedFields = recognizedReceipt.getRecognizedForm().getFields(); + RecognizedForm recognizedForm = receiptPageResults.get(i); + Map> recognizedFields = recognizedForm.getFields(); System.out.printf("----------- Recognized Receipt page %d -----------%n", i); - FormField merchantNameField = recognizedFields.get("MerchantName"); + FormField merchantNameField = recognizedFields.get("MerchantName"); if (merchantNameField != null) { - if (merchantNameField.getFieldValue().getType() == FieldValueType.STRING) { + if (FieldValueType.STRING.equals(merchantNameField.getValueType())) { + String merchantName = FieldValueType.STRING.cast(merchantNameField); System.out.printf("Merchant Name: %s, confidence: %.2f%n", - merchantNameField.getFieldValue().asString(), - merchantNameField.getConfidence()); + merchantName, merchantNameField.getConfidence()); + } + } + + FormField merchantPhoneNumberField = recognizedFields.get("MerchantPhoneNumber"); + if (merchantPhoneNumberField != null) { + if (FieldValueType.PHONE_NUMBER.equals(merchantNameField.getValueType())) { + String merchantAddress = FieldValueType.PHONE_NUMBER.cast(merchantPhoneNumberField); + System.out.printf("Merchant Phone number: %s, confidence: %.2f%n", + merchantAddress, merchantPhoneNumberField.getConfidence()); } } - FormField merchantAddressField = recognizedFields.get("MerchantAddress"); + + FormField merchantAddressField = recognizedFields.get("MerchantAddress"); if (merchantAddressField != null) { - if (merchantAddressField.getFieldValue().getType() == FieldValueType.STRING) { + if (FieldValueType.STRING.equals(merchantNameField.getValueType())) { + String merchantAddress = FieldValueType.STRING.cast(merchantAddressField); System.out.printf("Merchant Address: %s, confidence: %.2f%n", - merchantAddressField.getFieldValue().asString(), - merchantAddressField.getConfidence()); + merchantAddress, merchantAddressField.getConfidence()); } } - FormField transactionDateField = recognizedFields.get("TransactionDate"); + + FormField transactionDateField = recognizedFields.get("TransactionDate"); if (transactionDateField != null) { - if (transactionDateField.getFieldValue().getType() == FieldValueType.DATE) { + if (FieldValueType.DATE.equals(transactionDateField.getValueType())) { + LocalDate transactionDate = FieldValueType.DATE.cast(transactionDateField); System.out.printf("Transaction Date: %s, confidence: %.2f%n", - transactionDateField.getFieldValue().asDate(), - transactionDateField.getConfidence()); + transactionDate, transactionDateField.getConfidence()); } } - FormField receiptItemsField = recognizedFields.get("Items"); + + FormField receiptItemsField = recognizedFields.get("Items"); if (receiptItemsField != null) { System.out.printf("Receipt Items: %n"); - if (receiptItemsField.getFieldValue().getType() == FieldValueType.LIST) { - List receiptItems = receiptItemsField.getFieldValue().asList(); + if (FieldValueType.LIST.equals(receiptItemsField.getValueType())) { + List> receiptItems = FieldValueType.LIST.cast(receiptItemsField); receiptItems.forEach(receiptItem -> { - if (receiptItem.getFieldValue().getType() == FieldValueType.MAP) { - receiptItem.getFieldValue().asMap().forEach((key, formField) -> { - if (key.equals("Name")) { - if (formField.getFieldValue().getType() == FieldValueType.STRING) { + if (FieldValueType.MAP.equals(receiptItem.getValueType())) { + Map> formFieldMap = FieldValueType.MAP.cast(receiptItem); + formFieldMap.forEach((key, formField) -> { + if ("Name".equals(key)) { + if (FieldValueType.STRING.equals(formField.getValueType())) { + String name = FieldValueType.STRING.cast(formField); System.out.printf("Name: %s, confidence: %.2fs%n", - formField.getFieldValue().asString(), - formField.getConfidence()); + name, formField.getConfidence()); } } - if (key.equals("Quantity")) { - if (formField.getFieldValue().getType() == FieldValueType.INTEGER) { - System.out.printf("Quantity: %d, confidence: %.2f%n", - formField.getFieldValue().asInteger(), formField.getConfidence()); + if ("Quantity".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float quantity = FieldValueType.DOUBLE.cast(formField); + System.out.printf("Quantity: %f, confidence: %.2f%n", + quantity, formField.getConfidence()); } } - if (key.equals("Price")) { - if (formField.getFieldValue().getType() == FieldValueType.FLOAT) { + if ("Price".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float price = FieldValueType.DOUBLE.cast(formField); System.out.printf("Price: %f, confidence: %.2f%n", - formField.getFieldValue().asFloat(), - formField.getConfidence()); + price, formField.getConfidence()); } } - if (key.equals("TotalPrice")) { - if (formField.getFieldValue().getType() == FieldValueType.FLOAT) { + if ("TotalPrice".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float totalPrice = FieldValueType.DOUBLE.cast(formField); System.out.printf("Total Price: %f, confidence: %.2f%n", - formField.getFieldValue().asFloat(), - formField.getConfidence()); + totalPrice, formField.getConfidence()); } } }); diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsFromUrl.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsFromUrl.java index 8e7239f00dc0..df9c21ca7d09 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsFromUrl.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsFromUrl.java @@ -6,15 +6,19 @@ import com.azure.ai.formrecognizer.models.FieldValueType; import com.azure.ai.formrecognizer.models.FormField; import com.azure.ai.formrecognizer.models.OperationResult; -import com.azure.ai.formrecognizer.models.RecognizedReceipt; +import com.azure.ai.formrecognizer.models.RecognizedForm; import com.azure.core.credential.AzureKeyCredential; import com.azure.core.util.polling.SyncPoller; +import java.time.LocalDate; import java.util.List; import java.util.Map; /** - * Sample for recognizing US receipt information using file source URL. + * Sample for recognizing commonly found US receipt fields from a file source URL. For a suggested approach to + * extracting information from receipts, see StronglyTypedRecognizedForm.java. + * See fields found on a receipt here: + * https://aka.ms/azsdk/python/formrecognizer/receiptfields */ public class RecognizeReceiptsFromUrl { @@ -30,73 +34,88 @@ public static void main(final String[] args) { .endpoint("https://{endpoint}.cognitiveservices.azure.com/") .buildClient(); - String receiptUrl = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/sample-forms/receipts/contoso-allinone.jpg"; - SyncPoller> recognizeReceiptPoller = + String receiptUrl = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/sdk/formrecognizer" + + "/azure-ai-formrecognizer/src/samples/java/sample-forms/receipts/contoso-allinone.jpg"; + SyncPoller> recognizeReceiptPoller = client.beginRecognizeReceiptsFromUrl(receiptUrl); - List receiptPageResults = recognizeReceiptPoller.getFinalResult(); + List receiptPageResults = recognizeReceiptPoller.getFinalResult(); for (int i = 0; i < receiptPageResults.size(); i++) { - RecognizedReceipt recognizedReceipt = receiptPageResults.get(i); - Map recognizedFields = recognizedReceipt.getRecognizedForm().getFields(); + RecognizedForm recognizedForm = receiptPageResults.get(i); + Map> recognizedFields = recognizedForm.getFields(); System.out.printf("----------- Recognized Receipt page %d -----------%n", i); - FormField merchantNameField = recognizedFields.get("MerchantName"); + FormField merchantNameField = recognizedFields.get("MerchantName"); if (merchantNameField != null) { - if (merchantNameField.getFieldValue().getType() == FieldValueType.STRING) { + if (FieldValueType.STRING.equals(merchantNameField.getValueType())) { + String merchantName = FieldValueType.STRING.cast(merchantNameField); System.out.printf("Merchant Name: %s, confidence: %.2f%n", - merchantNameField.getFieldValue().asString(), - merchantNameField.getConfidence()); + merchantName, merchantNameField.getConfidence()); } } - FormField merchantAddressField = recognizedFields.get("MerchantAddress"); + + FormField merchantAddressField = recognizedFields.get("MerchantAddress"); if (merchantAddressField != null) { - if (merchantAddressField.getFieldValue().getType() == FieldValueType.STRING) { + if (FieldValueType.STRING.equals(merchantNameField.getValueType())) { + String merchantAddress = FieldValueType.STRING.cast(merchantAddressField); System.out.printf("Merchant Address: %s, confidence: %.2f%n", - merchantAddressField.getFieldValue().asString(), - merchantAddressField.getConfidence()); + merchantAddress, merchantAddressField.getConfidence()); + } + } + + FormField merchantPhoneNumberField = recognizedFields.get("MerchantPhoneNumber"); + if (merchantPhoneNumberField != null) { + if (FieldValueType.PHONE_NUMBER.equals(merchantNameField.getValueType())) { + String merchantAddress = FieldValueType.PHONE_NUMBER.cast(merchantPhoneNumberField); + System.out.printf("Merchant Phone number: %s, confidence: %.2f%n", + merchantAddress, merchantPhoneNumberField.getConfidence()); } } - FormField transactionDateField = recognizedFields.get("TransactionDate"); + + FormField transactionDateField = recognizedFields.get("TransactionDate"); if (transactionDateField != null) { - if (transactionDateField.getFieldValue().getType() == FieldValueType.DATE) { + if (FieldValueType.DATE.equals(transactionDateField.getValueType())) { + LocalDate transactionDate = FieldValueType.DATE.cast(transactionDateField); System.out.printf("Transaction Date: %s, confidence: %.2f%n", - transactionDateField.getFieldValue().asDate(), - transactionDateField.getConfidence()); + transactionDate, transactionDateField.getConfidence()); } } - FormField receiptItemsField = recognizedFields.get("Items"); + + FormField receiptItemsField = recognizedFields.get("Items"); if (receiptItemsField != null) { System.out.printf("Receipt Items: %n"); - if (receiptItemsField.getFieldValue().getType() == FieldValueType.LIST) { - List receiptItems = receiptItemsField.getFieldValue().asList(); + if (FieldValueType.LIST.equals(receiptItemsField.getValueType())) { + List> receiptItems = FieldValueType.LIST.cast(receiptItemsField); receiptItems.forEach(receiptItem -> { - if (receiptItem.getFieldValue().getType() == FieldValueType.MAP) { - receiptItem.getFieldValue().asMap().forEach((key, formField) -> { - if (key.equals("Name")) { - if (formField.getFieldValue().getType() == FieldValueType.STRING) { + if (FieldValueType.MAP.equals(receiptItem.getValueType())) { + Map> formFieldMap = FieldValueType.MAP.cast(receiptItem); + formFieldMap.forEach((key, formField) -> { + if ("Name".equals(key)) { + if (FieldValueType.STRING.equals(formField.getValueType())) { + String name = FieldValueType.STRING.cast(formField); System.out.printf("Name: %s, confidence: %.2fs%n", - formField.getFieldValue().asString(), - formField.getConfidence()); + name, formField.getConfidence()); } } - if (key.equals("Quantity")) { - if (formField.getFieldValue().getType() == FieldValueType.INTEGER) { - System.out.printf("Quantity: %d, confidence: %.2f%n", - formField.getFieldValue().asInteger(), formField.getConfidence()); + if ("Quantity".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float quantity = FieldValueType.DOUBLE.cast(formField); + System.out.printf("Quantity: %f, confidence: %.2f%n", + quantity, formField.getConfidence()); } } - if (key.equals("Price")) { - if (formField.getFieldValue().getType() == FieldValueType.FLOAT) { + if ("Price".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float price = FieldValueType.DOUBLE.cast(formField); System.out.printf("Price: %f, confidence: %.2f%n", - formField.getFieldValue().asFloat(), - formField.getConfidence()); + price, formField.getConfidence()); } } - if (key.equals("TotalPrice")) { - if (formField.getFieldValue().getType() == FieldValueType.FLOAT) { + if ("TotalPrice".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float totalPrice = FieldValueType.DOUBLE.cast(formField); System.out.printf("Total Price: %f, confidence: %.2f%n", - formField.getFieldValue().asFloat(), - formField.getConfidence()); + totalPrice, formField.getConfidence()); } } }); diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsFromUrlAsync.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsFromUrlAsync.java index e6148c58664a..4df763056b81 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsFromUrlAsync.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsFromUrlAsync.java @@ -6,17 +6,21 @@ import com.azure.ai.formrecognizer.models.FieldValueType; import com.azure.ai.formrecognizer.models.FormField; import com.azure.ai.formrecognizer.models.OperationResult; -import com.azure.ai.formrecognizer.models.RecognizedReceipt; +import com.azure.ai.formrecognizer.models.RecognizedForm; import com.azure.core.credential.AzureKeyCredential; import com.azure.core.util.polling.PollerFlux; import reactor.core.publisher.Mono; +import java.time.LocalDate; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; /** - * Async sample for recognizing US receipt information using file source URL. + * Async sample for recognizing commonly found US receipt fields from a file source URL. For a suggested approach to + * extracting information from receipts, see StronglyTypedRecognizedForm.java. + * See fields found on a receipt here: + * https://aka.ms/azsdk/python/formrecognizer/receiptfields */ public class RecognizeReceiptsFromUrlAsync { @@ -32,11 +36,12 @@ public static void main(final String[] args) { .endpoint("https://{endpoint}.cognitiveservices.azure.com/") .buildAsyncClient(); - String receiptUrl = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/sample-forms/receipts/contoso-allinone.jpg"; - PollerFlux> recognizeReceiptPoller = + String receiptUrl = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/sdk/formrecognizer" + + "/azure-ai-formrecognizer/src/samples/java/sample-forms/receipts/contoso-allinone.jpg"; + PollerFlux> recognizeReceiptPoller = client.beginRecognizeReceiptsFromUrl(receiptUrl); - Mono> receiptPageResults = recognizeReceiptPoller + Mono> receiptPageResults = recognizeReceiptPoller .last() .flatMap(trainingOperationResponse -> { if (trainingOperationResponse.getStatus().isComplete()) { @@ -51,66 +56,81 @@ public static void main(final String[] args) { receiptPageResults.subscribe(recognizedReceipts -> { for (int i = 0; i < recognizedReceipts.size(); i++) { - RecognizedReceipt recognizedReceipt = recognizedReceipts.get(i); - Map recognizedFields = recognizedReceipt.getRecognizedForm().getFields(); + RecognizedForm recognizedForm = recognizedReceipts.get(i); + Map> recognizedFields = recognizedForm.getFields(); System.out.printf("----------- Recognized Receipt page %d -----------%n", i); - FormField merchantNameField = recognizedFields.get("MerchantName"); + FormField merchantNameField = recognizedFields.get("MerchantName"); if (merchantNameField != null) { - if (merchantNameField.getFieldValue().getType() == FieldValueType.STRING) { + if (FieldValueType.STRING.equals(merchantNameField.getValueType())) { + String merchantName = FieldValueType.STRING.cast(merchantNameField); System.out.printf("Merchant Name: %s, confidence: %.2f%n", - merchantNameField.getFieldValue().asString(), - merchantNameField.getConfidence()); + merchantName, merchantNameField.getConfidence()); } } - FormField merchantAddressField = recognizedFields.get("MerchantAddress"); + + FormField merchantPhoneNumberField = recognizedFields.get("MerchantPhoneNumber"); + if (merchantPhoneNumberField != null) { + if (FieldValueType.PHONE_NUMBER.equals(merchantNameField.getValueType())) { + String merchantAddress = FieldValueType.PHONE_NUMBER.cast(merchantPhoneNumberField); + System.out.printf("Merchant Address: %s, confidence: %.2f%n", + merchantAddress, merchantPhoneNumberField.getConfidence()); + } + } + + FormField merchantAddressField = recognizedFields.get("MerchantAddress"); if (merchantAddressField != null) { - if (merchantAddressField.getFieldValue().getType() == FieldValueType.STRING) { + if (FieldValueType.STRING.equals(merchantNameField.getValueType())) { + String merchantAddress = FieldValueType.STRING.cast(merchantAddressField); System.out.printf("Merchant Address: %s, confidence: %.2f%n", - merchantAddressField.getFieldValue().asString(), - merchantAddressField.getConfidence()); + merchantAddress, merchantAddressField.getConfidence()); } } - FormField transactionDateField = recognizedFields.get("TransactionDate"); + + FormField transactionDateField = recognizedFields.get("TransactionDate"); if (transactionDateField != null) { - if (transactionDateField.getFieldValue().getType() == FieldValueType.DATE) { + if (FieldValueType.DATE.equals(transactionDateField.getValueType())) { + LocalDate transactionDate = FieldValueType.DATE.cast(transactionDateField); System.out.printf("Transaction Date: %s, confidence: %.2f%n", - transactionDateField.getFieldValue().asDate(), - transactionDateField.getConfidence()); + transactionDate, transactionDateField.getConfidence()); } } - FormField receiptItemsField = recognizedFields.get("Items"); + + FormField receiptItemsField = recognizedFields.get("Items"); if (receiptItemsField != null) { System.out.printf("Receipt Items: %n"); - if (receiptItemsField.getFieldValue().getType() == FieldValueType.LIST) { - List receiptItems = receiptItemsField.getFieldValue().asList(); + if (FieldValueType.LIST.equals(receiptItemsField.getValueType())) { + List> receiptItems = FieldValueType.LIST.cast(receiptItemsField); receiptItems.forEach(receiptItem -> { - if (receiptItem.getFieldValue().getType() == FieldValueType.MAP) { - receiptItem.getFieldValue().asMap().forEach((key, formField) -> { - if (key.equals("Name")) { - if (formField.getFieldValue().getType() == FieldValueType.STRING) { + if (FieldValueType.MAP.equals(receiptItem.getValueType())) { + // we still have to cast or assign + Map> formFieldMap = FieldValueType.MAP.cast(receiptItem); + formFieldMap.forEach((key, formField) -> { + if ("Name".equals(key)) { + if (FieldValueType.STRING.equals(formField.getValueType())) { + String name = FieldValueType.STRING.cast(formField); System.out.printf("Name: %s, confidence: %.2fs%n", - formField.getFieldValue().asString(), - formField.getConfidence()); + name, formField.getConfidence()); } } - if (key.equals("Quantity")) { - if (formField.getFieldValue().getType() == FieldValueType.INTEGER) { + if ("Quantity".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float quantity = FieldValueType.DOUBLE.cast(formField); System.out.printf("Quantity: %f, confidence: %.2f%n", - formField.getFieldValue().asInteger(), formField.getConfidence()); + quantity, formField.getConfidence()); } } - if (key.equals("Price")) { - if (formField.getFieldValue().getType() == FieldValueType.FLOAT) { + if ("Price".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float price = FieldValueType.DOUBLE.cast(formField); System.out.printf("Price: %f, confidence: %.2f%n", - formField.getFieldValue().asFloat(), - formField.getConfidence()); + price, formField.getConfidence()); } } - if (key.equals("TotalPrice")) { - if (formField.getFieldValue().getType() == FieldValueType.FLOAT) { + if ("TotalPrice".equals(key)) { + if (FieldValueType.DOUBLE.equals(formField.getValueType())) { + Float totalPrice = FieldValueType.DOUBLE.cast(formField); System.out.printf("Total Price: %f, confidence: %.2f%n", - formField.getFieldValue().asFloat(), - formField.getConfidence()); + totalPrice, formField.getConfidence()); } } }); diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/StronglyTypedRecognizedForm.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/StronglyTypedRecognizedForm.java new file mode 100644 index 000000000000..f0144fdc9a56 --- /dev/null +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/StronglyTypedRecognizedForm.java @@ -0,0 +1,79 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +package com.azure.ai.formrecognizer; + +import com.azure.ai.formrecognizer.models.OperationResult; +import com.azure.ai.formrecognizer.models.RecognizedForm; +import com.azure.core.credential.AzureKeyCredential; +import com.azure.core.util.polling.SyncPoller; + +import java.util.List; + +/** + * Sample demonstrating converting recognized form fields to strongly typed US receipt field values. + * See + * + * for information on the strongly typed fields returned by service when recognizing receipts. + */ +public class StronglyTypedRecognizedForm { + + /** + * Main method to invoke this demo. + * + * @param args Unused. Arguments to the program. + */ + public static void main(final String[] args) { + // Instantiate a client that will be used to call the service. + FormRecognizerClient client = new FormRecognizerClientBuilder() + .credential(new AzureKeyCredential("{api_Key}")) + .endpoint("https://{endpoint}.cognitiveservices.azure.com/") + .buildClient(); + + String receiptUrl = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/sdk/formrecognizer" + + "/azure-ai-formrecognizer/src/samples/java/sample-forms/receipts/contoso-allinone.jpg"; + SyncPoller> recognizeReceiptPoller = + client.beginRecognizeReceiptsFromUrl(receiptUrl); + + List receiptPageResults = recognizeReceiptPoller.getFinalResult(); + + for (int i = 0; i < receiptPageResults.size(); i++) { + final RecognizedForm recognizedForm = receiptPageResults.get(i); + System.out.printf("----------- Recognized Receipt page %d -----------%n", i); + // Use Receipt model transform the recognized form to strongly typed US receipt fields + Receipt usReceipt = new Receipt(recognizedForm); + System.out.printf("Merchant Name: %s, confidence: %.2f%n", usReceipt.getMerchantName().getValue(), + usReceipt.getMerchantName().getConfidence()); + System.out.printf("Merchant Address: %s, confidence: %.2f%n", + usReceipt.getMerchantAddress().getValue(), + usReceipt.getMerchantAddress().getConfidence()); + System.out.printf("Merchant Phone Number %s, confidence: %.2f%n", + usReceipt.getMerchantPhoneNumber().getValue(), usReceipt.getMerchantPhoneNumber().getConfidence()); + System.out.printf("Total: %s confidence: %.2f%n", usReceipt.getTotal().getValue(), + usReceipt.getTotal().getConfidence()); + System.out.printf("Transaction Date: %s, confidence: %.2f%n", + usReceipt.getTransactionDate().getValue(), usReceipt.getTransactionDate().getConfidence()); + System.out.printf("Transaction Time: %s, confidence: %.2f%n", + usReceipt.getTransactionTime().getValue(), usReceipt.getTransactionTime().getConfidence()); + System.out.printf("Receipt Items: %n"); + usReceipt.getReceiptItems().forEach(receiptItem -> { + if (receiptItem.getName() != null) { + System.out.printf("Name: %s, confidence: %.2f%n", receiptItem.getName().getValue(), + receiptItem.getName().getConfidence()); + } + if (receiptItem.getQuantity() != null) { + System.out.printf("Quantity: %f, confidence: %.2f%n", receiptItem.getQuantity().getValue(), + receiptItem.getQuantity().getConfidence()); + } + if (receiptItem.getPrice() != null) { + System.out.printf("Price: %f, confidence: %.2f%n", receiptItem.getPrice().getValue(), + receiptItem.getPrice().getConfidence()); + } + if (receiptItem.getTotalPrice() != null) { + System.out.printf("Total Price: %f, confidence: %.2f%n", + receiptItem.getTotalPrice().getValue(), receiptItem.getTotalPrice().getConfidence()); + } + }); + System.out.print("-----------------------------------"); + } + } +} diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FieldValueTypeCastTest.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FieldValueTypeCastTest.java new file mode 100644 index 000000000000..1c74acbfcb93 --- /dev/null +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FieldValueTypeCastTest.java @@ -0,0 +1,429 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.ai.formrecognizer; + +import com.azure.ai.formrecognizer.models.FieldValueType; +import com.azure.ai.formrecognizer.models.FormField; +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class FieldValueTypeCastTest { + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to Date. + */ + @Test + public void toDateFromDate() { + LocalDate inputDate = LocalDate.of(2006, 6, 6); + LocalDate actualDate = FieldValueType.DATE.cast(new FormField<>(0, null, null, + inputDate, null, FieldValueType.DATE)); + assertEquals(inputDate, actualDate); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to Date from String. + */ + @Test + public void toDateFromString() { + String inputDateString = "2006/06/06"; + LocalDate inputDate = LocalDate.parse(inputDateString, DateTimeFormatter.ofPattern("yyyy/MM/dd")); + LocalDate actualDate = FieldValueType.DATE.cast(new FormField<>(0, null, null, + inputDateString, null, FieldValueType.STRING)); + assertEquals(inputDate, actualDate); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to Date from null field value. + */ + @Test + public void toDateFromNull() { + assertNull(FieldValueType.DATE.cast(new FormField<>(0, null, null, + null, null, FieldValueType.STRING))); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to Date from any other + * FieldValueType except for String. + */ + @Test + public void toDateFromPhoneNumber() { + final UnsupportedOperationException unsupportedOperationException = + assertThrows(UnsupportedOperationException.class, () -> + FieldValueType.DATE.cast(new FormField<>(0, null, null, + "19876543210", null, FieldValueType.PHONE_NUMBER))); + assertEquals(unsupportedOperationException.getMessage(), "Cannot cast from field value of " + + "type PHONE_NUMBER to type DATE"); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to TIME. + */ + @Test + public void toTimeFromTime() { + LocalTime inputTime = LocalTime.parse("13:59:00", DateTimeFormatter.ofPattern("HH:mm:ss")); + LocalTime actualTime = FieldValueType.TIME.cast(new FormField<>(0, null, null, + inputTime, null, FieldValueType.TIME)); + assertEquals(inputTime, actualTime); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to TIME from String. + */ + @Test + public void toTimeFromString() { + String inputTimeString = "13:59:00"; + LocalTime inputTime = LocalTime.parse(inputTimeString, DateTimeFormatter.ofPattern("HH:mm:ss")); + LocalTime actualTime = FieldValueType.TIME.cast(new FormField<>(0, null, null, + inputTimeString, null, FieldValueType.STRING)); + assertEquals(inputTime, actualTime); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to TIME from null field value. + */ + @Test + public void toTimeFromNull() { + assertNull(FieldValueType.TIME.cast(new FormField<>(0, null, null, + null, null, FieldValueType.TIME))); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to TIME from any other + * FieldValueType except for String. + */ + @Test + public void toTimeFromPhoneNumber() { + final UnsupportedOperationException unsupportedOperationException = + assertThrows(UnsupportedOperationException.class, () -> + FieldValueType.TIME.cast(new FormField<>(0, null, null, + "19876543210", null, FieldValueType.PHONE_NUMBER))); + assertEquals(unsupportedOperationException.getMessage(), "Cannot cast from field value of " + + "type PHONE_NUMBER to type TIME"); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to list. + */ + @Test + public void toListFromList() { + List inputList = new ArrayList<>(Arrays.asList("1")); + List actualList = FieldValueType.LIST.cast(new FormField<>(0, null, null, + inputList, null, FieldValueType.LIST)); + assertEquals(inputList, actualList); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to list from String. + */ + @Test + public void toListFromString() { + String listString = "1"; + final UnsupportedOperationException unsupportedOperationException = + assertThrows(UnsupportedOperationException.class, () -> + FieldValueType.LIST.cast(new FormField<>(0, null, null, + listString, null, FieldValueType.STRING))); + assertEquals(unsupportedOperationException.getMessage(), "Cannot cast from field value of " + + "type STRING to type LIST"); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to list from null field value. + */ + @Test + public void toListFromNull() { + assertNull(FieldValueType.LIST.cast(new FormField<>(0, null, null, + null, null, FieldValueType.LIST))); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to list from any other + * FieldValueType except for String. + */ + @Test + public void toListFromTime() { + LocalTime inputTime = LocalTime.parse("13:59:00", DateTimeFormatter.ofPattern("HH:mm:ss")); + final UnsupportedOperationException unsupportedOperationException = + assertThrows(UnsupportedOperationException.class, () -> + FieldValueType.LIST.cast(new FormField<>(0, null, null, + inputTime, null, FieldValueType.TIME))); + assertEquals(unsupportedOperationException.getMessage(), "Cannot cast from field value of " + + "type TIME to type LIST"); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to phone number. + */ + @Test + public void toPhoneNumberFromPhoneNumber() { + String phoneNumber = "19876543210"; + String actualPhoneNumber = FieldValueType.PHONE_NUMBER.cast(new FormField<>(0, null, null, + phoneNumber, null, FieldValueType.PHONE_NUMBER)); + assertEquals(phoneNumber, actualPhoneNumber); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to phone number from String. + */ + @Test + public void toPhoneNumberFromString() { + String phoneNumber = "19876543210"; + String actualPhoneNumber = FieldValueType.PHONE_NUMBER.cast(new FormField<>(0, null, null, + phoneNumber, null, FieldValueType.STRING)); + assertEquals(phoneNumber, actualPhoneNumber); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to phone number from null field value. + */ + @Test + public void toPhoneNumberFromNull() { + assertNull(FieldValueType.PHONE_NUMBER.cast(new FormField<>(0, null, null, + null, null, FieldValueType.PHONE_NUMBER))); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to phone number from any other + * FieldValueType except for String. + */ + @Test + public void toPhoneNumberFromTime() { + LocalTime inputTime = LocalTime.parse("13:59:00", DateTimeFormatter.ofPattern("HH:mm:ss")); + final UnsupportedOperationException unsupportedOperationException = + assertThrows(UnsupportedOperationException.class, () -> + FieldValueType.PHONE_NUMBER.cast(new FormField<>(0, null, null, + inputTime, null, FieldValueType.TIME))); + assertEquals(unsupportedOperationException.getMessage(), "Cannot cast from field value of " + + "type TIME to type PHONE_NUMBER"); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to map. + */ + @Test + public void toMapFromMap() { + Map inputMap = Collections.singletonMap("key", "value"); + Map actualList = FieldValueType.MAP.cast(new FormField<>(0, null, null, + inputMap, null, FieldValueType.MAP)); + assertEquals(inputMap, actualList); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to map from String. + */ + @Test + public void toMapFromString() { + String listString = "1"; + final UnsupportedOperationException unsupportedOperationException = + assertThrows(UnsupportedOperationException.class, () -> + FieldValueType.MAP.cast(new FormField<>(0, null, null, + listString, null, FieldValueType.STRING))); + assertEquals(unsupportedOperationException.getMessage(), "Cannot cast from field value of " + + "type STRING to type MAP"); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to map from null field value. + */ + @Test + public void toMapFromNull() { + assertNull(FieldValueType.MAP.cast(new FormField<>(0, null, null, + null, null, FieldValueType.MAP))); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to map from any other + * FieldValueType except for String. + */ + @Test + public void toMapFromTime() { + LocalTime inputTime = LocalTime.parse("13:59:00", DateTimeFormatter.ofPattern("HH:mm:ss")); + final UnsupportedOperationException unsupportedOperationException = + assertThrows(UnsupportedOperationException.class, () -> + FieldValueType.MAP.cast(new FormField<>(0, null, null, + inputTime, null, FieldValueType.TIME))); + assertEquals(unsupportedOperationException.getMessage(), "Cannot cast from field value of " + + "type TIME to type MAP"); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to double. + */ + @Test + public void toDoubleFromDouble() { + Double inputDouble = 2.2; + Double actualDoubleValue = FieldValueType.DOUBLE.cast(new FormField<>(0, null, null, + inputDouble, null, FieldValueType.DOUBLE)); + assertEquals(inputDouble, actualDoubleValue); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to double from String. + */ + @Test + public void toDoubleFromString() { + String doubleString = "2.2"; + Double actualDouble = FieldValueType.DOUBLE.cast(new FormField<>(0, null, null, + doubleString, null, FieldValueType.STRING)); + assertEquals(Double.valueOf(doubleString), actualDouble); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to double from null field value. + */ + @Test + public void toDoubleFromNull() { + assertNull(FieldValueType.DOUBLE.cast(new FormField<>(0, null, null, + null, null, FieldValueType.DOUBLE))); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to double from any other + * FieldValueType except for String. + */ + @Test + public void toDoubleFromTime() { + LocalTime inputTime = LocalTime.parse("13:59:00", DateTimeFormatter.ofPattern("HH:mm:ss")); + final UnsupportedOperationException unsupportedOperationException = + assertThrows(UnsupportedOperationException.class, () -> + FieldValueType.DOUBLE.cast(new FormField<>(0, null, null, + inputTime, null, FieldValueType.TIME))); + assertEquals(unsupportedOperationException.getMessage(), "Cannot cast from field value of " + + "type TIME to type DOUBLE"); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to long. + */ + @Test + public void toLongFromLong() { + long inputDouble = 22; + Long actualLongValue = FieldValueType.LONG.cast(new FormField<>(0, null, null, + inputDouble, null, FieldValueType.LONG)); + assertEquals(inputDouble, actualLongValue); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to long from String. + */ + @Test + public void toLongFromString() { + String inputDoubleString = "22"; + Long actualLongValue = FieldValueType.LONG.cast(new FormField<>(0, null, null, + inputDoubleString, null, FieldValueType.STRING)); + assertEquals(Long.valueOf(inputDoubleString), actualLongValue); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to long from null field value. + */ + @Test + public void toLongFromNull() { + assertNull(FieldValueType.LONG.cast(new FormField<>(0, null, null, + null, null, FieldValueType.LONG))); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to long from any other + * FieldValueType except for String. + */ + @Test + public void toLongFromTime() { + LocalTime inputTime = LocalTime.parse("13:59:00", DateTimeFormatter.ofPattern("HH:mm:ss")); + final UnsupportedOperationException unsupportedOperationException = + assertThrows(UnsupportedOperationException.class, () -> + FieldValueType.LONG.cast(new FormField<>(0, null, null, + inputTime, null, FieldValueType.TIME))); + assertEquals(unsupportedOperationException.getMessage(), "Cannot cast from field value of " + + "type TIME to type LONG"); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to String from TIME. + */ + @Test + public void toStringFromTime() { + LocalTime inputTime = LocalTime.parse("13:59:00", DateTimeFormatter.ofPattern("HH:mm:ss")); + String localTimeString = FieldValueType.STRING.cast(new FormField<>(0, null, null, + inputTime, null, FieldValueType.TIME)); + assertEquals(inputTime.toString(), localTimeString); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to String from String. + */ + @Test + public void toStringFromString() { + String stringValue = "String value"; + String actualStringValue = FieldValueType.STRING.cast(new FormField<>(0, null, null, + stringValue, null, FieldValueType.STRING)); + assertEquals(stringValue, actualStringValue); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to String from double. + */ + @Test + public void toStringFromDouble() { + Double doubleValue = 2.2; + String actualDouble = FieldValueType.STRING.cast(new FormField<>(0, null, null, + doubleValue, null, FieldValueType.DOUBLE)); + assertEquals(String.valueOf(doubleValue), actualDouble); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to long from String. + */ + @Test + public void toStringFromLong() { + Long inputLong = 22L; + String actualLongValue = FieldValueType.STRING.cast(new FormField<>(0, null, null, + inputLong, null, FieldValueType.LONG)); + assertEquals(String.valueOf(inputLong), actualLongValue); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to String from Map. + */ + @Test + public void toStringFromMap() { + Map inputMap = Collections.singletonMap("key", "value"); + String stringMap = FieldValueType.STRING.cast(new FormField<>(0, null, null, + inputMap, null, FieldValueType.MAP)); + assertEquals(inputMap.toString(), stringMap); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to String from Phone number. + */ + @Test + public void toStringFromPhoneNumber() { + String phoneNumber = "19876543210"; + String actualPhoneNumber = FieldValueType.STRING.cast(new FormField<>(0, null, null, + phoneNumber, null, FieldValueType.PHONE_NUMBER)); + assertEquals(phoneNumber, actualPhoneNumber); + } + + /** + * Test for {@link com.azure.ai.formrecognizer.models.FieldValueType#cast(FormField)} to String from List. + */ + @Test + public void toStringFromList() { + List inputList = Collections.singletonList("1"); + String actualStringList = FieldValueType.STRING.cast(new FormField<>(0, null, null, + inputList, null, FieldValueType.LIST)); + assertEquals(inputList.toString(), actualStringList); + } +} diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClientTest.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClientTest.java index 4a63846663ef..5aa35327f249 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClientTest.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClientTest.java @@ -11,7 +11,6 @@ import com.azure.ai.formrecognizer.models.OperationResult; import com.azure.ai.formrecognizer.models.RecognizeOptions; import com.azure.ai.formrecognizer.models.RecognizedForm; -import com.azure.ai.formrecognizer.models.RecognizedReceipt; import com.azure.ai.formrecognizer.training.FormTrainingAsyncClient; import com.azure.core.exception.HttpResponseException; import com.azure.core.http.HttpClient; @@ -78,7 +77,7 @@ private FormTrainingAsyncClient getFormTrainingAsyncClient(HttpClient httpClient public void recognizeReceiptData(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerAsyncClient(httpClient, serviceVersion); receiptDataRunner((data, dataLength) -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeReceipts(toFluxByteBuffer(data), dataLength, new RecognizeOptions() .setContentType(FormContentType.IMAGE_JPEG).setPollInterval(durationTestMode)) .getSyncPoller(); @@ -108,7 +107,7 @@ public void recognizeReceiptDataWithContentTypeAutoDetection(HttpClient httpClie FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerAsyncClient(httpClient, serviceVersion); - SyncPoller> syncPoller = client.beginRecognizeReceipts( + SyncPoller> syncPoller = client.beginRecognizeReceipts( getReplayableBufferData(RECEIPT_LOCAL_URL), RECEIPT_FILE_LENGTH, new RecognizeOptions() .setPollInterval(durationTestMode)).getSyncPoller(); @@ -124,7 +123,7 @@ public void recognizeReceiptDataWithContentTypeAutoDetection(HttpClient httpClie public void recognizeReceiptDataIncludeFieldElements(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerAsyncClient(httpClient, serviceVersion); receiptDataRunnerFieldElements((data, includeFieldElements) -> { - SyncPoller> syncPoller = client.beginRecognizeReceipts( + SyncPoller> syncPoller = client.beginRecognizeReceipts( toFluxByteBuffer(data), RECEIPT_FILE_LENGTH, new RecognizeOptions() .setContentType(FormContentType.IMAGE_JPEG).setIncludeFieldElements(includeFieldElements) .setPollInterval(durationTestMode)).getSyncPoller(); @@ -142,7 +141,7 @@ public void recognizeReceiptDataWithPngFile(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerAsyncClient(httpClient, serviceVersion); receiptPngDataRunnerFieldElements((data, includeFieldElements) -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeReceipts(toFluxByteBuffer(data), RECEIPT_PNG_FILE_LENGTH, new RecognizeOptions() .setContentType(FormContentType.IMAGE_PNG).setIncludeFieldElements(includeFieldElements) .setPollInterval(durationTestMode)).getSyncPoller(); @@ -160,7 +159,7 @@ public void recognizeReceiptDataWithBlankPdf(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerAsyncClient(httpClient, serviceVersion); blankPdfDataRunner((data, dataLength) -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeReceipts(toFluxByteBuffer(data), dataLength, new RecognizeOptions() .setContentType(FormContentType.APPLICATION_PDF).setPollInterval(durationTestMode)) .getSyncPoller(); @@ -174,7 +173,7 @@ public void recognizeReceiptDataWithBlankPdf(HttpClient httpClient, public void recognizeReceiptFromDataMultiPage(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerAsyncClient(httpClient, serviceVersion); multipageFromDataRunner((data, dataLength) -> { - SyncPoller> syncPoller = client.beginRecognizeReceipts( + SyncPoller> syncPoller = client.beginRecognizeReceipts( toFluxByteBuffer(data), dataLength, new RecognizeOptions() .setContentType(FormContentType.APPLICATION_PDF).setPollInterval(durationTestMode)) .getSyncPoller(); @@ -193,7 +192,7 @@ public void recognizeReceiptFromDataMultiPage(HttpClient httpClient, FormRecogni public void recognizeReceiptSourceUrl(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerAsyncClient(httpClient, serviceVersion); receiptSourceUrlRunner(sourceUrl -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeReceiptsFromUrl(sourceUrl).getSyncPoller(); syncPoller.waitForCompletion(); validateReceiptResultData(syncPoller.getFinalResult(), false); @@ -227,7 +226,7 @@ public void recognizeReceiptFromUrlIncludeFieldElements(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerAsyncClient(httpClient, serviceVersion); receiptSourceUrlRunnerFieldElements((sourceUrl, includeFieldElements) -> { - SyncPoller> syncPoller = client.beginRecognizeReceiptsFromUrl( + SyncPoller> syncPoller = client.beginRecognizeReceiptsFromUrl( sourceUrl, new RecognizeOptions().setIncludeFieldElements(includeFieldElements) .setPollInterval(durationTestMode)).getSyncPoller(); syncPoller.waitForCompletion(); @@ -245,7 +244,7 @@ public void recognizeReceiptSourceUrlWithPngFile(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerAsyncClient(httpClient, serviceVersion); receiptPngSourceUrlRunnerFieldElements((sourceUrl, includeFieldElements) -> { - SyncPoller> syncPoller = client.beginRecognizeReceiptsFromUrl( + SyncPoller> syncPoller = client.beginRecognizeReceiptsFromUrl( sourceUrl, new RecognizeOptions().setIncludeFieldElements(includeFieldElements) .setPollInterval(durationTestMode)) .getSyncPoller(); @@ -259,7 +258,7 @@ sourceUrl, new RecognizeOptions().setIncludeFieldElements(includeFieldElements) public void recognizeReceiptFromUrlMultiPage(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerAsyncClient(httpClient, serviceVersion); multipageFromUrlRunner(fileUrl -> { - SyncPoller> syncPoller = client.beginRecognizeReceiptsFromUrl( + SyncPoller> syncPoller = client.beginRecognizeReceiptsFromUrl( fileUrl, new RecognizeOptions().setPollInterval(durationTestMode)).getSyncPoller(); syncPoller.waitForCompletion(); validateMultipageReceiptData(syncPoller.getFinalResult()); diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTest.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTest.java index 860e06541abf..ca8741c86814 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTest.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTest.java @@ -11,7 +11,6 @@ import com.azure.ai.formrecognizer.models.OperationResult; import com.azure.ai.formrecognizer.models.RecognizeOptions; import com.azure.ai.formrecognizer.models.RecognizedForm; -import com.azure.ai.formrecognizer.models.RecognizedReceipt; import com.azure.ai.formrecognizer.training.FormTrainingClient; import com.azure.core.exception.HttpResponseException; import com.azure.core.http.HttpClient; @@ -63,7 +62,7 @@ private FormTrainingClient getFormTrainingClient(HttpClient httpClient, public void recognizeReceiptData(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerClient(httpClient, serviceVersion); receiptDataRunner((data, dataLength) -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeReceipts(data, dataLength, new RecognizeOptions() .setContentType(FormContentType.IMAGE_JPEG).setPollInterval(durationTestMode)); syncPoller.waitForCompletion(); @@ -91,7 +90,7 @@ public void recognizeReceiptDataNullData(HttpClient httpClient, public void recognizeReceiptDataWithContentTypeAutoDetection(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerClient(httpClient, serviceVersion); - SyncPoller> syncPoller = client.beginRecognizeReceipts( + SyncPoller> syncPoller = client.beginRecognizeReceipts( getContentDetectionFileData(RECEIPT_LOCAL_URL), RECEIPT_FILE_LENGTH, new RecognizeOptions() .setPollInterval(durationTestMode)); syncPoller.waitForCompletion(); @@ -107,7 +106,7 @@ public void recognizeReceiptDataWithContentTypeAutoDetection(HttpClient httpClie public void recognizeReceiptDataIncludeFieldElements(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerClient(httpClient, serviceVersion); receiptDataRunnerFieldElements((data, includeFieldElements) -> { - SyncPoller> syncPoller = client.beginRecognizeReceipts( + SyncPoller> syncPoller = client.beginRecognizeReceipts( data, RECEIPT_FILE_LENGTH, new RecognizeOptions().setContentType(FormContentType.IMAGE_JPEG) .setIncludeFieldElements(includeFieldElements).setPollInterval(durationTestMode)); syncPoller.waitForCompletion(); @@ -124,7 +123,7 @@ public void recognizeReceiptDataWithPngFile(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerClient(httpClient, serviceVersion); receiptPngDataRunnerFieldElements((data, includeFieldElements) -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeReceipts(data, RECEIPT_PNG_FILE_LENGTH, new RecognizeOptions().setContentType( FormContentType.IMAGE_PNG).setIncludeFieldElements(includeFieldElements) .setPollInterval(durationTestMode)); @@ -142,7 +141,7 @@ public void recognizeReceiptDataWithBlankPdf(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerClient(httpClient, serviceVersion); blankPdfDataRunner((data, dataLength) -> { - SyncPoller> syncPoller = client.beginRecognizeReceipts( + SyncPoller> syncPoller = client.beginRecognizeReceipts( data, dataLength, new RecognizeOptions().setContentType(FormContentType.APPLICATION_PDF) .setPollInterval(durationTestMode)); syncPoller.waitForCompletion(); @@ -155,7 +154,7 @@ data, dataLength, new RecognizeOptions().setContentType(FormContentType.APPLICAT public void recognizeReceiptFromDataMultiPage(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerClient(httpClient, serviceVersion); multipageFromDataRunner((data, dataLength) -> { - SyncPoller> syncPoller = client.beginRecognizeReceipts( + SyncPoller> syncPoller = client.beginRecognizeReceipts( data, dataLength, new RecognizeOptions().setContentType(FormContentType.APPLICATION_PDF) .setPollInterval(durationTestMode)); syncPoller.waitForCompletion(); @@ -173,7 +172,7 @@ data, dataLength, new RecognizeOptions().setContentType(FormContentType.APPLICAT public void recognizeReceiptSourceUrl(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerClient(httpClient, serviceVersion); receiptSourceUrlRunner((sourceUrl) -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeReceiptsFromUrl(sourceUrl); syncPoller.waitForCompletion(); validateReceiptResultData(syncPoller.getFinalResult(), false); @@ -202,7 +201,7 @@ public void recognizeReceiptFromUrlIncludeFieldElements(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerClient(httpClient, serviceVersion); receiptSourceUrlRunnerFieldElements((sourceUrl, includeFieldElements) -> { - SyncPoller> syncPoller = client.beginRecognizeReceiptsFromUrl( + SyncPoller> syncPoller = client.beginRecognizeReceiptsFromUrl( sourceUrl, new RecognizeOptions().setIncludeFieldElements(includeFieldElements) .setPollInterval(durationTestMode)); syncPoller.waitForCompletion(); @@ -220,7 +219,7 @@ public void recognizeReceiptSourceUrlWithPngFile(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerClient(httpClient, serviceVersion); receiptPngSourceUrlRunnerFieldElements((sourceUrl, includeFieldElements) -> { - SyncPoller> syncPoller = client.beginRecognizeReceiptsFromUrl( + SyncPoller> syncPoller = client.beginRecognizeReceiptsFromUrl( sourceUrl, new RecognizeOptions().setIncludeFieldElements(includeFieldElements) .setPollInterval(durationTestMode)); @@ -234,7 +233,7 @@ public void recognizeReceiptSourceUrlWithPngFile(HttpClient httpClient, public void recognizeReceiptFromUrlMultiPage(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerClient(httpClient, serviceVersion); multipageFromUrlRunner(receiptUrl -> { - SyncPoller> syncPoller = client.beginRecognizeReceiptsFromUrl( + SyncPoller> syncPoller = client.beginRecognizeReceiptsFromUrl( receiptUrl, new RecognizeOptions().setPollInterval(durationTestMode)); syncPoller.waitForCompletion(); validateMultipageReceiptData(syncPoller.getFinalResult()); @@ -411,7 +410,7 @@ public void recognizeCustomFormLabeledDataWithJpgContentType(HttpClient httpClie beginTrainingLabeledRunner((trainingFilesUrl, useTrainingLabels) -> { SyncPoller trainingPoller = getFormTrainingClient(httpClient, serviceVersion).beginTraining(trainingFilesUrl, useTrainingLabels, - null, durationTestMode); + null, durationTestMode); trainingPoller.waitForCompletion(); SyncPoller> syncPoller = client.beginRecognizeCustomForms( diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTestBase.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTestBase.java index 31c0cf01d633..4fa322aff744 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTestBase.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTestBase.java @@ -16,6 +16,7 @@ import com.azure.ai.formrecognizer.implementation.models.TextLine; import com.azure.ai.formrecognizer.implementation.models.TextWord; import com.azure.ai.formrecognizer.models.BoundingBox; +import com.azure.ai.formrecognizer.models.FieldValueType; import com.azure.ai.formrecognizer.models.FormElement; import com.azure.ai.formrecognizer.models.FormField; import com.azure.ai.formrecognizer.models.FormLine; @@ -26,7 +27,6 @@ import com.azure.ai.formrecognizer.models.FormWord; import com.azure.ai.formrecognizer.models.Point; import com.azure.ai.formrecognizer.models.RecognizedForm; -import com.azure.ai.formrecognizer.models.RecognizedReceipt; import com.azure.ai.formrecognizer.training.FormTrainingClientBuilder; import com.azure.core.credential.AzureKeyCredential; import com.azure.core.http.HttpClient; @@ -253,7 +253,8 @@ private static void validateBoundingBoxData(List expectedBoundingBox, Bou } } - private static void validateFieldValueTransforms(FieldValue expectedFieldValue, FormField actualFormField, + @SuppressWarnings("unchecked") + private static void validateFieldValueTransforms(FieldValue expectedFieldValue, FormField actualFormField, List readResults, boolean includeFieldElements) { if (expectedFieldValue != null) { if (expectedFieldValue.getBoundingBox() != null) { @@ -266,35 +267,35 @@ private static void validateFieldValueTransforms(FieldValue expectedFieldValue, } switch (expectedFieldValue.getType()) { case NUMBER: - assertEquals(expectedFieldValue.getValueNumber(), actualFormField.getFieldValue().asFloat()); + assertEquals(expectedFieldValue.getValueNumber(), FieldValueType.DOUBLE.cast(actualFormField)); break; case DATE: - assertEquals(expectedFieldValue.getValueDate(), actualFormField.getFieldValue().asDate()); + assertEquals(expectedFieldValue.getValueDate(), FieldValueType.DATE.cast(actualFormField)); break; case TIME: assertEquals(LocalTime.parse(expectedFieldValue.getValueTime(), - DateTimeFormatter.ofPattern("HH:mm:ss")), actualFormField.getFieldValue().asTime()); + DateTimeFormatter.ofPattern("HH:mm:ss")), FieldValueType.TIME.cast(actualFormField)); break; case STRING: - assertEquals(expectedFieldValue.getValueString(), actualFormField.getFieldValue().asString()); + assertEquals(expectedFieldValue.getValueString(), FieldValueType.STRING.cast(actualFormField)); break; case INTEGER: - assertEquals(expectedFieldValue.getValueInteger(), actualFormField.getFieldValue().asInteger()); + assertEquals(expectedFieldValue.getValueInteger(), FieldValueType.LONG.cast(actualFormField)); break; case PHONE_NUMBER: - assertEquals(expectedFieldValue.getValuePhoneNumber(), actualFormField.getFieldValue().asPhoneNumber()); + assertEquals(expectedFieldValue.getValuePhoneNumber(), FieldValueType.PHONE_NUMBER.cast(actualFormField)); break; case OBJECT: expectedFieldValue.getValueObject().forEach((key, fieldValue) -> { - FormField actualFormFieldValue = actualFormField.getFieldValue().asMap().get(key); + FormField actualFormFieldValue = ((Map>) actualFormField.getValue()).get(key); validateFieldValueTransforms(fieldValue, actualFormFieldValue, readResults, includeFieldElements); }); break; case ARRAY: - assertEquals(expectedFieldValue.getValueArray().size(), actualFormField.getFieldValue().asList().size()); + assertEquals(expectedFieldValue.getValueArray().size(), ((List>) actualFormField.getValue()).size()); for (int i = 0; i < expectedFieldValue.getValueArray().size(); i++) { FieldValue expectedReceiptItem = expectedFieldValue.getValueArray().get(i); - FormField actualReceiptItem = actualFormField.getFieldValue().asList().get(i); + FormField actualReceiptItem = ((List>) actualFormField.getValue()).get(i); validateFieldValueTransforms(expectedReceiptItem, actualReceiptItem, readResults, includeFieldElements); } break; @@ -492,14 +493,14 @@ abstract void recognizeCustomFormUrlMultiPageLabeled(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion); // Receipt - void validateReceiptDataFields(Map actualRecognizedReceiptFields, boolean includeFieldElements) { + void validateReceiptDataFields(Map> actualRecognizedReceiptFields, boolean includeFieldElements) { final AnalyzeResult analyzeResult = getAnalyzeRawResponse().getAnalyzeResult(); List readResults = analyzeResult.getReadResults(); DocumentResult documentResult = analyzeResult.getDocumentResults().get(0); Map expectedReceiptFields = documentResult.getFields(); assertEquals(expectedReceiptFields.get("ReceiptType").getValueString(), - actualRecognizedReceiptFields.get("ReceiptType").getFieldValue().asString()); + FieldValueType.STRING.cast(actualRecognizedReceiptFields.get("ReceiptType"))); assertEquals(expectedReceiptFields.get("ReceiptType").getConfidence(), actualRecognizedReceiptFields.get("ReceiptType").getConfidence()); validateFieldValueTransforms(expectedReceiptFields.get("MerchantName"), @@ -547,20 +548,20 @@ void validateContentResultData(List actualFormPageList, boolean includ } } - void validateReceiptResultData(List actualReceiptList, boolean includeFieldElements) { + void validateReceiptResultData(List actualReceiptList, boolean includeFieldElements) { final AnalyzeResult rawResponse = getAnalyzeRawResponse().getAnalyzeResult(); for (int i = 0; i < actualReceiptList.size(); i++) { - final RecognizedReceipt actualReceipt = actualReceiptList.get(i); - validateLabeledData(actualReceipt.getRecognizedForm(), includeFieldElements, rawResponse.getReadResults(), + final RecognizedForm actualReceipt = actualReceiptList.get(i); + validateLabeledData(actualReceipt, includeFieldElements, rawResponse.getReadResults(), rawResponse.getDocumentResults().get(i)); - validateReceiptDataFields(actualReceipt.getRecognizedForm().getFields(), includeFieldElements); + validateReceiptDataFields(actualReceipt.getFields(), includeFieldElements); } } - void validateBlankPdfResultData(List actualReceiptList) { + void validateBlankPdfResultData(List actualReceiptList) { assertEquals(1, actualReceiptList.size()); - final RecognizedReceipt actualReceipt = actualReceiptList.get(0); - assertTrue(actualReceipt.getRecognizedForm().getFields().isEmpty()); + final RecognizedForm actualReceipt = actualReceiptList.get(0); + assertTrue(actualReceipt.getFields().isEmpty()); } void validateRecognizedResult(List actualFormList, boolean includeFieldElements, @@ -697,7 +698,7 @@ private void validateUnLabeledResult(RecognizedForm actualForm, boolean includeF validatePageRangeData(expectedPage.getPage(), actualForm.getFormPageRange()); for (int i = 0; i < expectedPage.getKeyValuePairs().size(); i++) { final KeyValuePair expectedFormField = expectedPage.getKeyValuePairs().get(i); - final FormField actualFormField = actualForm.getFields().get("field-" + i); + final FormField actualFormField = actualForm.getFields().get("field-" + i); assertEquals(expectedFormField.getConfidence(), actualFormField.getConfidence()); assertEquals(expectedFormField.getKey().getText(), actualFormField.getLabelData().getText()); validateBoundingBoxData(expectedFormField.getKey().getBoundingBox(), @@ -720,7 +721,7 @@ private void validateLabeledData(RecognizedForm actualForm, boolean includeField assertEquals(documentResult.getPageRange().get(0), actualForm.getFormPageRange().getFirstPageNumber()); assertEquals(documentResult.getPageRange().get(1), actualForm.getFormPageRange().getLastPageNumber()); documentResult.getFields().forEach((label, expectedFieldValue) -> { - final FormField actualFormField = actualForm.getFields().get(label); + final FormField actualFormField = actualForm.getFields().get(label); assertEquals(label, actualFormField.getName()); if (expectedFieldValue != null) { if (expectedFieldValue.getConfidence() != null) { @@ -741,7 +742,7 @@ static void validateMultiPageDataLabeled(List actualRecognizedFo assertEquals(3, recognizedForm.getPages().size()); recognizedForm.getFields().forEach((label, formField) -> { assertNotNull(formField.getName()); - assertNotNull(formField.getFieldValue()); + assertNotNull(formField.getValue()); assertNotNull(formField.getValueData().getText()); assertNotNull(formField.getLabelData().getText()); }); @@ -751,34 +752,35 @@ static void validateMultiPageDataLabeled(List actualRecognizedFo static void validateMultiPageDataUnlabeled(List actualRecognizedFormsList) { actualRecognizedFormsList.forEach(recognizedForm -> { assertNotNull(recognizedForm.getFormType()); - assertEquals(1, recognizedForm.getPages().stream().count()); + assertEquals(1, (long) recognizedForm.getPages().size()); recognizedForm.getFields().forEach((label, formField) -> { assertNotNull(formField.getName()); - assertNotNull(formField.getFieldValue()); + assertNotNull(formField.getValue()); assertNotNull(formField.getValueData().getText()); assertNotNull(formField.getLabelData().getText()); + }); }); } - static void validateMultipageReceiptData(List recognizedReceipts) { + static void validateMultipageReceiptData(List recognizedReceipts) { assertEquals(3, recognizedReceipts.size()); - RecognizedForm receiptPage1 = recognizedReceipts.get(0).getRecognizedForm(); - RecognizedForm receiptPage2 = recognizedReceipts.get(1).getRecognizedForm(); - RecognizedForm receiptPage3 = recognizedReceipts.get(2).getRecognizedForm(); + RecognizedForm receiptPage1 = recognizedReceipts.get(0); + RecognizedForm receiptPage2 = recognizedReceipts.get(1); + RecognizedForm receiptPage3 = recognizedReceipts.get(2); assertEquals(1, receiptPage1.getFormPageRange().getFirstPageNumber()); assertEquals(1, receiptPage1.getFormPageRange().getLastPageNumber()); - Map receiptPage1Fields = receiptPage1.getFields(); + Map> receiptPage1Fields = receiptPage1.getFields(); assertEquals(EXPECTED_MULTIPAGE_ADDRESS_VALUE, receiptPage1Fields.get("MerchantAddress") - .getFieldValue().asString()); + .getValue()); assertEquals("Bilbo Baggins", receiptPage1Fields.get("MerchantName") - .getFieldValue().asString()); + .getValue()); assertEquals(EXPECTED_MULTIPAGE_PHONE_NUMBER_VALUE, receiptPage1Fields.get("MerchantPhoneNumber") - .getFieldValue().asPhoneNumber()); - assertNotNull(receiptPage1Fields.get("Total").getFieldValue().asFloat()); + .getValue()); + assertNotNull(receiptPage1Fields.get("Total").getValue()); assertNotNull(receiptPage1.getPages()); - assertEquals(ITEMIZED_RECEIPT_VALUE, receiptPage1Fields.get("ReceiptType").getFieldValue().asString()); + assertEquals(ITEMIZED_RECEIPT_VALUE, receiptPage1Fields.get("ReceiptType").getValue()); // Assert no fields, tables and lines on second page assertEquals(0, receiptPage2.getFields().size()); @@ -791,18 +793,15 @@ static void validateMultipageReceiptData(List recognizedRecei assertEquals(3, receiptPage3.getFormPageRange().getFirstPageNumber()); assertEquals(3, receiptPage3.getFormPageRange().getLastPageNumber()); - Map receiptPage3Fields = receiptPage3.getFields(); - assertEquals(EXPECTED_MULTIPAGE_ADDRESS_VALUE, receiptPage3Fields.get("MerchantAddress") - .getFieldValue().asString()); - assertEquals("Frodo Baggins", receiptPage3Fields.get("MerchantName") - .getFieldValue().asString()); - assertEquals(EXPECTED_MULTIPAGE_PHONE_NUMBER_VALUE, receiptPage3Fields.get("MerchantPhoneNumber") - .getFieldValue().asPhoneNumber()); - assertNotNull(receiptPage3Fields.get("Total").getFieldValue().asFloat()); + Map> receiptPage3Fields = receiptPage3.getFields(); + assertEquals(EXPECTED_MULTIPAGE_ADDRESS_VALUE, FieldValueType.STRING.cast(receiptPage3Fields.get("MerchantAddress"))); + assertEquals("Frodo Baggins", FieldValueType.STRING.cast(receiptPage3Fields.get("MerchantName"))); + assertEquals(EXPECTED_MULTIPAGE_PHONE_NUMBER_VALUE, FieldValueType.PHONE_NUMBER.cast(receiptPage3Fields.get("MerchantPhoneNumber"))); + assertNotNull(receiptPage3Fields.get("Total").getValue()); // why isn't tip returned by service? // total value 1000 returned by service but should be 4300, service bug - assertEquals(3000.0f, receiptPage3Fields.get("Subtotal").getFieldValue().asFloat()); - assertEquals(ITEMIZED_RECEIPT_VALUE, receiptPage3Fields.get("ReceiptType").getFieldValue().asString()); + assertEquals(3000.0f, (Float) receiptPage3Fields.get("Subtotal").getValue()); + assertEquals(ITEMIZED_RECEIPT_VALUE, FieldValueType.STRING.cast(receiptPage3Fields.get("ReceiptType"))); } protected String getEndpoint() { diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingAsyncClientTest.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingAsyncClientTest.java index 7d660cffb838..804537d907f1 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingAsyncClientTest.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingAsyncClientTest.java @@ -12,7 +12,7 @@ import com.azure.ai.formrecognizer.models.FormRecognizerException; import com.azure.ai.formrecognizer.models.OperationResult; import com.azure.ai.formrecognizer.models.RecognizeOptions; -import com.azure.ai.formrecognizer.models.RecognizedReceipt; +import com.azure.ai.formrecognizer.models.RecognizedForm; import com.azure.ai.formrecognizer.models.TrainingFileFilter; import com.azure.ai.formrecognizer.training.FormTrainingAsyncClient; import com.azure.core.exception.HttpResponseException; @@ -70,7 +70,7 @@ void getFormRecognizerClientAndValidate(HttpClient httpClient, FormRecognizerSer FormRecognizerAsyncClient formRecognizerClient = getFormTrainingAsyncClient(httpClient, serviceVersion) .getFormRecognizerAsyncClient(); blankPdfDataRunner(data -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = formRecognizerClient.beginRecognizeReceipts(toFluxByteBuffer(data), BLANK_FORM_FILE_LENGTH, new RecognizeOptions() .setContentType(FormContentType.APPLICATION_PDF) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingClientTest.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingClientTest.java index beb6aa4bd0fd..340889097b86 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingClientTest.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingClientTest.java @@ -13,7 +13,7 @@ import com.azure.ai.formrecognizer.models.FormRecognizerException; import com.azure.ai.formrecognizer.models.OperationResult; import com.azure.ai.formrecognizer.models.RecognizeOptions; -import com.azure.ai.formrecognizer.models.RecognizedReceipt; +import com.azure.ai.formrecognizer.models.RecognizedForm; import com.azure.ai.formrecognizer.models.TrainingFileFilter; import com.azure.ai.formrecognizer.training.FormTrainingClient; import com.azure.core.exception.HttpResponseException; @@ -55,7 +55,7 @@ public void getFormRecognizerClientAndValidate(HttpClient httpClient, FormRecogn FormRecognizerClient formRecognizerClient = getFormTrainingClient(httpClient, serviceVersion) .getFormRecognizerClient(); blankPdfDataRunner(data -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = formRecognizerClient.beginRecognizeReceipts(data, BLANK_FORM_FILE_LENGTH, new RecognizeOptions() .setContentType(FormContentType.APPLICATION_PDF).setPollInterval(durationTestMode)); syncPoller.waitForCompletion(); diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingClientTestBase.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingClientTestBase.java index cc3ed0e43fd8..3ce2fd70e2a5 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingClientTestBase.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingClientTestBase.java @@ -12,7 +12,7 @@ import com.azure.ai.formrecognizer.models.CustomFormSubmodel; import com.azure.ai.formrecognizer.models.ErrorInformation; import com.azure.ai.formrecognizer.models.FormRecognizerError; -import com.azure.ai.formrecognizer.models.RecognizedReceipt; +import com.azure.ai.formrecognizer.models.RecognizedForm; import com.azure.ai.formrecognizer.models.TrainingDocumentInfo; import com.azure.ai.formrecognizer.training.FormTrainingClientBuilder; import com.azure.core.credential.AzureKeyCredential; @@ -348,10 +348,10 @@ String getEndpoint() { : Configuration.getGlobalConfiguration().get(AZURE_FORM_RECOGNIZER_ENDPOINT); } - void validateBlankPdfResultData(List actualReceiptList) { + void validateBlankPdfResultData(List actualReceiptList) { assertEquals(1, actualReceiptList.size()); - final RecognizedReceipt actualReceipt = actualReceiptList.get(0); - assertTrue(actualReceipt.getRecognizedForm().getFields().isEmpty()); + final RecognizedForm actualReceipt = actualReceiptList.get(0); + assertTrue(actualReceipt.getFields().isEmpty()); } void blankPdfDataRunner(Consumer testRunner) {