< Summary

Class:Azure.AI.FormRecognizer.Models.RecognizedForm
Assembly:Azure.AI.FormRecognizer
File(s):C:\Git\azure-sdk-for-net\sdk\formrecognizer\Azure.AI.FormRecognizer\src\RecognizedForm.cs
Covered lines:36
Uncovered lines:0
Coverable lines:36
Total lines:119
Line coverage:100% (36 of 36)
Covered branches:17
Total branches:18
Branch coverage:94.4% (17 of 18)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
.ctor(...)-100%50%
get_FormType()-100%100%
get_PageRange()-100%100%
get_Fields()-100%100%
get_Pages()-100%100%
ConvertUnsupervisedFields(...)-100%100%
ConvertSupervisedFields(...)-100%100%
ConvertSupervisedPages(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\formrecognizer\Azure.AI.FormRecognizer\src\RecognizedForm.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System.Collections.Generic;
 5using System.Linq;
 6
 7namespace Azure.AI.FormRecognizer.Models
 8{
 9    /// <summary>
 10    /// Represents a form that has been recognized by a trained model.
 11    /// </summary>
 12    public class RecognizedForm
 13    {
 4414        internal RecognizedForm(PageResult pageResult, IReadOnlyList<ReadResult> readResults, int pageIndex)
 15        {
 16            // Recognized form from a model trained without labels.
 4417            FormType = $"form-{pageResult.ClusterId}";
 4418            PageRange = new FormPageRange(pageResult.Page, pageResult.Page);
 4419            Fields = ConvertUnsupervisedFields(pageResult.Page, pageResult.KeyValuePairs, readResults);
 20
 21            // For models trained without labels, the service treats every page as a separate form, so
 22            // we end up with a single page per form.
 23
 4424            Pages = new List<FormPage> { new FormPage(pageResult, readResults, pageIndex) };
 4425        }
 26
 9227        internal RecognizedForm(DocumentResult documentResult, IReadOnlyList<PageResult> pageResults, IReadOnlyList<Read
 28        {
 29            // Recognized form from a model trained with labels.
 9230            FormType = documentResult.DocType;
 31
 32            // TODO: validate that PageRange.Length == 2.
 33            // https://github.com/Azure/azure-sdk-for-net/issues/10547
 34
 9235            PageRange = new FormPageRange(documentResult.PageRange[0], documentResult.PageRange[1]);
 36
 37            // documentResult.Fields is required and not null, according to the swagger file, but it's not
 38            // present when a blank receipt is submitted for recognition.
 39
 9240            Fields = documentResult.Fields == null
 9241                ? new Dictionary<string, FormField>()
 9242                : ConvertSupervisedFields(documentResult.Fields, readResults);
 9243            Pages = ConvertSupervisedPages(pageResults, readResults);
 9244        }
 45
 46        /// <summary>
 47        /// The type of form the model identified the submitted form to be.
 48        /// </summary>
 49        // Convert clusterId to a string (ex. "FormType1").
 15650        public string FormType { get; }
 51
 52        /// <summary>
 53        /// The range of pages this form spans.
 54        /// </summary>
 54055        public FormPageRange PageRange { get; }
 56
 57        /// <summary>
 58        /// A dictionary of the fields recognized from the input document. The key is
 59        /// the <see cref="FormField.Name"/> of the field. For models trained with labels,
 60        /// this is the training-time label of the field. For models trained with forms
 61        /// only, a unique name is generated for each field.
 62        /// </summary>
 80463        public IReadOnlyDictionary<string, FormField> Fields { get; }
 64
 65        /// <summary>
 66        /// A list of pages describing the recognized form elements present in the input
 67        /// document.
 68        /// </summary>
 87269        public IReadOnlyList<FormPage> Pages { get; }
 70
 71        private static IReadOnlyDictionary<string, FormField> ConvertUnsupervisedFields(int pageNumber, IReadOnlyList<Ke
 72        {
 4473            Dictionary<string, FormField> fieldDictionary = new Dictionary<string, FormField>();
 74
 4475            int i = 0;
 124076            foreach (var keyValuePair in keyValuePairs)
 77            {
 57678                var fieldName = keyValuePair.Label ?? $"field-{i++}";
 57679                fieldDictionary[fieldName] = new FormField(fieldName, pageNumber, keyValuePair, readResults);
 80            }
 81
 4482            return fieldDictionary;
 83        }
 84
 85        private static IReadOnlyDictionary<string, FormField> ConvertSupervisedFields(IReadOnlyDictionary<string, FieldV
 86        {
 9287            Dictionary<string, FormField> fieldDictionary = new Dictionary<string, FormField>();
 88
 175289            foreach (var field in fields)
 90            {
 78491                fieldDictionary[field.Key] = field.Value == null
 78492                    ? null
 78493                    : new FormField(field.Key, field.Value, readResults);
 94            }
 95
 9296            return fieldDictionary;
 97        }
 98
 99        private IReadOnlyList<FormPage> ConvertSupervisedPages(IReadOnlyList<PageResult> pageResults, IReadOnlyList<Read
 100        {
 92101            List<FormPage> pages = new List<FormPage>();
 102
 496103            for (int i = 0; i < readResults.Count; i++)
 104            {
 105                // Check range here so only pages that are part of this form are added. Avoid "pageNumber = i + 1"
 106                // because it is not safe to assume the pages will always be in order.
 107
 156108                var pageNumber = readResults[i].Page;
 109
 156110                if (pageNumber >= PageRange.FirstPageNumber && pageNumber <= PageRange.LastPageNumber)
 111                {
 116112                    pages.Add(new FormPage(pageResults.Any() ? pageResults[i] : null, readResults, i));
 113                }
 114            }
 115
 92116            return pages;
 117        }
 118    }
 119}