Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ isFieldElementsIncluded, new SourcePath().setSource(formUrl), context)
.andThen(after -> after.map(modelSimpleResponse ->
toRecognizedForm(modelSimpleResponse.getValue().getAnalyzeResult(),
isFieldElementsIncluded,
modelId))
modelId, false))
.onErrorMap(Utility::mapToHttpResponseExceptionIfExist)));
} catch (RuntimeException ex) {
return PollerFlux.error(ex);
Expand Down Expand Up @@ -246,7 +246,7 @@ isFieldElementsIncluded, new SourcePath().setSource(formUrl), context)
.andThen(after -> after.map(modelSimpleResponse ->
toRecognizedForm(modelSimpleResponse.getValue().getAnalyzeResult(),
isFieldElementsIncluded,
modelId))
modelId, false))
.onErrorMap(Utility::mapToHttpResponseExceptionIfExist)));
} catch (RuntimeException ex) {
return PollerFlux.error(ex);
Expand Down Expand Up @@ -478,7 +478,7 @@ PollerFlux<FormRecognizerOperationResult, List<FormPage>> beginRecognizeContent(
.andThen(after -> after.map(modelSimpleResponse ->
toRecognizedForm(modelSimpleResponse.getValue().getAnalyzeResult(),
isFieldElementsIncluded,
null))
null, false))
.onErrorMap(Utility::mapToHttpResponseExceptionIfExist)));
} catch (RuntimeException ex) {
return PollerFlux.error(ex);
Expand Down Expand Up @@ -573,7 +573,7 @@ public PollerFlux<FormRecognizerOperationResult, List<RecognizedForm>> beginReco
.andThen(after -> after.map(modelSimpleResponse ->
toRecognizedForm(modelSimpleResponse.getValue().getAnalyzeResult(),
isFieldElementsIncluded,
null))
null, false))
.onErrorMap(Utility::mapToHttpResponseExceptionIfExist)));
} catch (RuntimeException ex) {
return PollerFlux.error(ex);
Expand Down Expand Up @@ -655,7 +655,7 @@ PollerFlux<FormRecognizerOperationResult, List<RecognizedForm>> beginRecognizeBu
.andThen(after -> after.map(modelSimpleResponse -> toRecognizedForm(
modelSimpleResponse.getValue().getAnalyzeResult(),
isFieldElementsIncluded,
null))
null, true))
.onErrorMap(Utility::mapToHttpResponseExceptionIfExist)));
} catch (RuntimeException ex) {
return PollerFlux.error(ex);
Expand Down Expand Up @@ -748,7 +748,7 @@ PollerFlux<FormRecognizerOperationResult, List<RecognizedForm>> beginRecognizeBu
.andThen(after -> after.map(modelSimpleResponse -> toRecognizedForm(
modelSimpleResponse.getValue().getAnalyzeResult(),
isFieldElementsIncluded,
null))
null, true))
.onErrorMap(Utility::mapToHttpResponseExceptionIfExist)));
} catch (RuntimeException ex) {
return PollerFlux.error(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import static com.azure.ai.formrecognizer.implementation.Utility.forEachWithIndex;
import static com.azure.ai.formrecognizer.implementation.models.FieldValueType.ARRAY;
import static com.azure.ai.formrecognizer.implementation.models.FieldValueType.OBJECT;

/**
* Helper class to convert service level models to SDK exposed models.
Expand All @@ -63,10 +64,11 @@ private Transforms() {
* @param includeFieldElements Boolean to indicate if to set reference elements data on fields.
*
* @param modelId the unlabeled model Id used for recognition.
* @param isBusinessCard boolean indicating if its recognizing a business card.
* @return The List of {@code RecognizedForm}.
*/
static List<RecognizedForm> toRecognizedForm(AnalyzeResult analyzeResult, boolean includeFieldElements,
String modelId) {
String modelId, boolean isBusinessCard) {
List<ReadResult> readResults = analyzeResult.getReadResults();
List<DocumentResult> documentResults = analyzeResult.getDocumentResults();
List<PageResult> pageResults = analyzeResult.getPageResults();
Expand All @@ -85,7 +87,8 @@ static List<RecognizedForm> toRecognizedForm(AnalyzeResult analyzeResult, boolea
formPageRange = new FormPageRange(1, 1);
}

Map<String, FormField> extractedFieldMap = getLabeledFieldMap(documentResultItem, readResults);
Map<String, FormField> extractedFieldMap = getLabeledFieldMap(documentResultItem, readResults,
isBusinessCard);
final RecognizedForm recognizedForm = new RecognizedForm(
extractedFieldMap,
documentResultItem.getDocType(),
Expand Down Expand Up @@ -209,10 +212,11 @@ static List<FormLine> getReadResultFormLines(ReadResult readResultItem) {
*
* @param documentResultItem The extracted document level information.
* @param readResults The text extraction result returned by the service.
* @param isBusinessCard boolean indicating if its recognizing a business card.
* @return The {@link RecognizedForm#getFields}.
*/
private static Map<String, FormField> getLabeledFieldMap(DocumentResult documentResultItem,
List<ReadResult> readResults) {
List<ReadResult> readResults, boolean isBusinessCard) {
Map<String, FormField> recognizedFieldMap = new LinkedHashMap<>();
// add receipt fields
if (!CoreUtils.isNullOrEmpty(documentResultItem.getFields())) {
Expand All @@ -226,7 +230,7 @@ private static Map<String, FormField> getLabeledFieldMap(DocumentResult document
valueData = new FieldData(fieldValue.getText(), toBoundingBox(fieldValue.getBoundingBox()),
fieldValue.getPage(), formElementList);
}
recognizedFieldMap.put(key, setFormField(key, valueData, fieldValue, readResults));
recognizedFieldMap.put(key, setFormField(key, valueData, fieldValue, readResults, isBusinessCard));
} else {
recognizedFieldMap.put(key, new FormField(key, null, null, null,
DEFAULT_CONFIDENCE_VALUE));
Expand All @@ -241,14 +245,15 @@ private static Map<String, FormField> getLabeledFieldMap(DocumentResult document
* {@link FormField} with reference elements set when {@code includeFieldElements} is set to true.
*
* @param name The name of the field.
* @param fieldValue The named field values returned by the service.
* @param valueData The value text of the field.
* @param fieldValue The named field values returned by the service.
* @param readResults The text extraction result returned by the service.
*
* @param isBusinessCard boolean indicating if its recognizing a business card.
* @return The strongly typed {@link FormField} for the field input.
*/
private static FormField setFormField(String name, FieldData valueData, FieldValue fieldValue,
List<ReadResult> readResults) {
List<ReadResult> readResults, boolean isBusinessCard) {
com.azure.ai.formrecognizer.models.FieldValue value;
switch (fieldValue.getType()) {
case PHONE_NUMBER:
Expand Down Expand Up @@ -286,17 +291,18 @@ private static FormField setFormField(String name, FieldData valueData, FieldVal
break;
case ARRAY:
value = new com.azure.ai.formrecognizer.models.FieldValue(
toFieldValueArray(fieldValue.getValueArray(), readResults), FieldValueType.LIST);
toFieldValueArray(fieldValue.getValueArray(), readResults, isBusinessCard), FieldValueType.LIST);
break;
case OBJECT:
value = new com.azure.ai.formrecognizer.models.FieldValue(
toFieldValueObject(fieldValue.getValueObject(), readResults), FieldValueType.MAP);
toFieldValueObject(fieldValue.getValueObject(), readResults, isBusinessCard), FieldValueType.MAP);
break;
default:
throw LOGGER.logExceptionAsError(new RuntimeException("FieldValue Type not supported"));
}

return new FormField(name, null, valueData, value, setDefaultConfidenceValue(fieldValue.getConfidence()));
return new FormField(name, null, valueData, value,
setDefaultConfidenceValue(fieldValue.getConfidence()));
}

/**
Expand All @@ -317,10 +323,11 @@ private static float setDefaultConfidenceValue(Float confidence) {
*
* @param valueObject The array of field values returned by the service in {@link FieldValue#getValueObject()}.
*
* @param isBusinessCard boolean indicating if its recognizing a business card.
* @return The Map of {@link FormField}.
*/
private static Map<String, FormField> toFieldValueObject(Map<String, FieldValue> valueObject,
List<ReadResult> readResults) {
List<ReadResult> readResults, boolean isBusinessCard) {
Map<String, FormField> fieldValueObjectMap = new TreeMap<>();
valueObject.forEach((key, fieldValue) ->
fieldValueObjectMap.put(key,
Expand All @@ -330,7 +337,7 @@ private static Map<String, FormField> toFieldValueObject(Map<String, FieldValue>
fieldValue.getPage(),
setReferenceElements(fieldValue.getElements(), readResults)),
fieldValue,
readResults)
readResults, isBusinessCard)
));
return fieldValueObjectMap;
}
Expand All @@ -342,20 +349,32 @@ private static Map<String, FormField> toFieldValueObject(Map<String, FieldValue>
*
* @param valueArray The array of field values returned by the service in {@link FieldValue#getValueArray()}.
* @param readResults The text extraction result returned by the service.
*
* @param isBusinessCard boolean indicating if its recognizing a business card.
* @return The List of {@link FormField}.
*/
private static List<FormField> toFieldValueArray(List<FieldValue> valueArray, List<ReadResult> readResults) {
private static List<FormField> toFieldValueArray(List<FieldValue> valueArray, List<ReadResult> readResults,
boolean isBusinessCard) {
return valueArray.stream()
.map(fieldValue -> {
FieldData valueData = null;
// ARRAY has ho value data, such as bounding box.
if (ARRAY != fieldValue.getType()) {
if (ARRAY != fieldValue.getType() && OBJECT != fieldValue.getType()) {
valueData = new FieldData(fieldValue.getText(), toBoundingBox(fieldValue.getBoundingBox()),
fieldValue.getPage() == null ? 1 : fieldValue.getPage(),
fieldValue.getPage(),
setReferenceElements(fieldValue.getElements(), readResults));
} else if (isBusinessCard && OBJECT.equals(fieldValue.getType())) {
// TODO: (savaity) Service bug, update after fixed.
if (fieldValue.getValueObject().get("FirstName") != null
&& fieldValue.getValueObject().get("LastName") != null) {
if (fieldValue.getValueObject().get("FirstName").getPage()
== fieldValue.getValueObject().get("LastName").getPage()) {
valueData = new FieldData(fieldValue.getText(), toBoundingBox(fieldValue.getBoundingBox()),
fieldValue.getValueObject().get("FirstName").getPage(),
setReferenceElements(fieldValue.getElements(), readResults));
}
}
}
return setFormField(null, valueData, fieldValue, readResults);
return setFormField(null, valueData, fieldValue, readResults, isBusinessCard);
})
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public abstract class FormRecognizerClientTestBase extends TestBase {
static final List<String> BUSINESS_CARD_FIELDS = Arrays.asList("ContactNames", "JobTitles", "Departments",
"Emails", "Websites", "MobilePhones", "OtherPhones", "Faxes", "Addresses", "CompanyNames");

// Business Card fields
// Receipt fields
static final List<String> RECEIPT_FIELDS = Arrays.asList("MerchantName", "MerchantPhoneNumber", "MerchantAddress",
"Total", "Subtotal", "Tax", "TransactionDate", "TransactionDate", "TransactionTime", "Items");

Expand Down Expand Up @@ -794,6 +794,11 @@ static void validateMultipageBusinessData(List<RecognizedForm> recognizedBusines
assertEquals("+14257793479", phoneNumberList.get(0).getValue().asPhoneNumber());
assertNotNull(businessCard1.getPages());

// assert contact name page number
FormField contactNameField = businessCard1Fields.get("ContactNames").getValue().asList().get(0);
assertEquals(contactNameField.getValueData().getPageNumber(), 1);
assertEquals(contactNameField.getValueData().getText(), "JOHN SINGER");

assertEquals(2, businessCard2.getPageRange().getFirstPageNumber());
assertEquals(2, businessCard2.getPageRange().getLastPageNumber());
Map<String, FormField> businessCard2Fields = businessCard2.getFields();
Expand All @@ -802,6 +807,11 @@ static void validateMultipageBusinessData(List<RecognizedForm> recognizedBusines
List<FormField> phoneNumber2List = businessCard2Fields.get("OtherPhones").getValue().asList();
assertEquals("+44 (0) 20 9876 5432", phoneNumber2List.get(0).getValueData().getText());
assertNotNull(businessCard2.getPages());

// assert contact name page number
FormField contactName2Field = businessCard2Fields.get("ContactNames").getValue().asList().get(0);
assertEquals(contactName2Field.getValueData().getPageNumber(), 2);
assertEquals(contactName2Field.getValueData().getText(), "Dr. Avery Smith");
}

static void validateMultipageReceiptData(List<RecognizedForm> recognizedReceipts) {
Expand Down