Skip to content

Commit

Permalink
remove bc workaround (#17025)
Browse files Browse the repository at this point in the history
  • Loading branch information
maririos authored Nov 17, 2020
1 parent a7ac34c commit 0a47283
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 44 deletions.
9 changes: 1 addition & 8 deletions sdk/formrecognizer/Azure.AI.FormRecognizer/src/FieldValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@ public readonly struct FieldValue
{
private readonly FieldValue_internal _fieldValue;
private readonly IReadOnlyList<ReadResult> _readResults;
private readonly bool _isBusinessCard;

internal FieldValue(FieldValue_internal fieldValue, IReadOnlyList<ReadResult> readResults)
: this(fieldValue, readResults, false) { }

internal FieldValue(FieldValue_internal fieldValue, IReadOnlyList<ReadResult> readResults, bool isBusinessCard)
: this()
{
ValueType = fieldValue.Type;
_fieldValue = fieldValue;
_readResults = readResults;
_isBusinessCard = isBusinessCard;
}

/// <summary>
Expand Down Expand Up @@ -339,9 +334,7 @@ public IReadOnlyList<FormField> AsList()
List<FormField> fieldList = new List<FormField>();
foreach (var fieldValue in _fieldValue.ValueArray)
{
// Business card has a special condition on how to calculate pages
// so we need to tell the FormField that it is from BusinessCards
fieldList.Add(new FormField(null, fieldValue, _readResults, _isBusinessCard));
fieldList.Add(new FormField(null, fieldValue, _readResults));
}

return fieldList;
Expand Down
32 changes: 3 additions & 29 deletions sdk/formrecognizer/Azure.AI.FormRecognizer/src/FormField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal FormField(string name, int pageNumber, KeyValuePair field, IReadOnlyLis
Value = new FieldValue(new FieldValue_internal(field.Value.Text), readResults);
}

internal FormField(string name, FieldValue_internal fieldValue, IReadOnlyList<ReadResult> readResults, bool isBusinessCard = default)
internal FormField(string name, FieldValue_internal fieldValue, IReadOnlyList<ReadResult> readResults)
{
Confidence = fieldValue.Confidence ?? Constants.DefaultConfidenceValue;
Name = name;
Expand All @@ -56,12 +56,10 @@ internal FormField(string name, FieldValue_internal fieldValue, IReadOnlyList<Re
// TODO: FormEnum<T> ?
FieldBoundingBox boundingBox = new FieldBoundingBox(fieldValue.BoundingBox);

int fieldPage = isBusinessCard ? CalculatePage(fieldValue) : fieldValue.Page.Value;

ValueData = new FieldData(boundingBox, fieldPage, fieldValue.Text, fieldElements);
ValueData = new FieldData(boundingBox, fieldValue.Page.Value, fieldValue.Text, fieldElements);
}

Value = new FieldValue(fieldValue, readResults, isBusinessCard);
Value = new FieldValue(fieldValue, readResults);
}

/// <summary>
Expand Down Expand Up @@ -162,29 +160,5 @@ private static FormElement ResolveTextReference(IReadOnlyList<ReadResult> readRe

throw new InvalidOperationException($"Failed to parse element reference: {reference}");
}

/// <summary>
/// Business Cards pre-built model doesn't return a page number for the `ContactNames` field.
/// This function looks into the FieldValue_internal to see if it corresponds to
/// `ContactNames` and verifies that the page value before returning it.
/// </summary>
/// <returns>Page value if the field is `ContactNames` for Business cards. If not, defaults to 1.</returns>
private static int CalculatePage(FieldValue_internal field)
{
int page = 1;
if (field.Type == FieldValueType.Dictionary)
{
IReadOnlyDictionary<string, FieldValue_internal> possibleContactNamesField = field.ValueObject;
if (possibleContactNamesField.Count == 2 && possibleContactNamesField.ContainsKey("FirstName")
&& possibleContactNamesField.ContainsKey("LastName"))
{
if (possibleContactNamesField["FirstName"].Page == possibleContactNamesField["LastName"].Page)
{
page = possibleContactNamesField["FirstName"].Page.Value;
}
}
}
return page;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private static RecognizedFormCollection ConvertToRecognizedForms(AnalyzeResult a
List<RecognizedForm> businessCards = new List<RecognizedForm>();
for (int i = 0; i < analyzeResult.DocumentResults.Count; i++)
{
businessCards.Add(new RecognizedForm(analyzeResult.DocumentResults[i], analyzeResult.PageResults, analyzeResult.ReadResults, default, isBusinessCards: true));
businessCards.Add(new RecognizedForm(analyzeResult.DocumentResults[i], analyzeResult.PageResults, analyzeResult.ReadResults, default));
}
return new RecognizedFormCollection(businessCards);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ internal RecognizedForm(PageResult pageResult, IReadOnlyList<ReadResult> readRes
}

internal RecognizedForm(DocumentResult documentResult, IReadOnlyList<PageResult> pageResults, IReadOnlyList<ReadResult> readResults, string modelId)
: this(documentResult, pageResults, readResults, modelId, false) { }

internal RecognizedForm(DocumentResult documentResult, IReadOnlyList<PageResult> pageResults, IReadOnlyList<ReadResult> readResults, string modelId, bool isBusinessCards)
{
// Recognized form from a model trained with labels.
FormType = documentResult.DocType;
Expand All @@ -45,7 +42,7 @@ internal RecognizedForm(DocumentResult documentResult, IReadOnlyList<PageResult>

Fields = documentResult.Fields == null
? new Dictionary<string, FormField>()
: ConvertSupervisedFields(documentResult.Fields, readResults, isBusinessCards);
: ConvertSupervisedFields(documentResult.Fields, readResults);
Pages = ConvertSupervisedPages(pageResults, readResults);
ModelId = documentResult.ModelId.HasValue ? documentResult.ModelId.Value.ToString() : modelId;
FormTypeConfidence = documentResult.DocTypeConfidence ?? Constants.DefaultConfidenceValue;
Expand Down Expand Up @@ -121,15 +118,15 @@ private static IReadOnlyDictionary<string, FormField> ConvertUnsupervisedFields(
return fieldDictionary;
}

private static IReadOnlyDictionary<string, FormField> ConvertSupervisedFields(IReadOnlyDictionary<string, FieldValue_internal> fields, IReadOnlyList<ReadResult> readResults, bool isBusinessCards)
private static IReadOnlyDictionary<string, FormField> ConvertSupervisedFields(IReadOnlyDictionary<string, FieldValue_internal> fields, IReadOnlyList<ReadResult> readResults)
{
Dictionary<string, FormField> fieldDictionary = new Dictionary<string, FormField>();

foreach (var field in fields)
{
fieldDictionary[field.Key] = field.Value == null
? null
: new FormField(field.Key, field.Value, readResults, isBusinessCards);
: new FormField(field.Key, field.Value, readResults);
}

return fieldDictionary;
Expand Down

0 comments on commit 0a47283

Please sign in to comment.