Exemplo n.º 1
0
        private static IReadOnlyList <ExtractedPage> ConvertPages(DocumentResult_internal documentResult, ICollection <PageResult_internal> pageResults, ICollection <ReadResult_internal> readResults)
        {
            List <ExtractedPage> pages = new List <ExtractedPage>();

            Dictionary <int, List <ExtractedField> > fieldsByPage = new Dictionary <int, List <ExtractedField> >();

            foreach (var field in documentResult.Fields)
            {
                // TODO: We are currently setting the field page to 0 if field.Value.Page comes back as null.
                // https://github.com/Azure/azure-sdk-for-net/issues/10369

                // TODO: How should we handle the multiple values per field and the strongly-typed ones?
                // https://github.com/Azure/azure-sdk-for-net/issues/10333

                List <ExtractedField> list;
                if (!fieldsByPage.TryGetValue(field.Value.Page ?? 0, out list))
                {
                    fieldsByPage[field.Value.Page ?? 0] = new List <ExtractedField>();
                }

                fieldsByPage[field.Value.Page ?? 0].Add(new ExtractedField(field));
            }

            foreach (var pageFields in fieldsByPage)
            {
                int pageNumber = pageFields.Key;
                var page       = new ExtractedPage(pageNumber, pageFields.Value, pageResults.ElementAt(pageNumber - 1), readResults.ElementAt(pageNumber - 1));
                pages.Add(page);
            }

            return(pages);
        }
Exemplo n.º 2
0
        internal static DocumentResult_internal DeserializeDocumentResult_internal(JsonElement element)
        {
            DocumentResult_internal result = new DocumentResult_internal();

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("docType"))
                {
                    result.DocType = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("pageRange"))
                {
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        result.PageRange.Add(item.GetInt32());
                    }
                    continue;
                }
                if (property.NameEquals("fields"))
                {
                    foreach (var property0 in property.Value.EnumerateObject())
                    {
                        result.Fields.Add(property0.Name, FieldValue_internal.DeserializeFieldValue_internal(property0.Value));
                    }
                    continue;
                }
            }
            return(result);
        }
Exemplo n.º 3
0
 internal ExtractedForm(DocumentResult_internal documentResult, ICollection <PageResult_internal> pageResults, ICollection <ReadResult_internal> readResults)
 {
     // Supervised
     LearnedFormType = documentResult.DocType;
     StartPageNumber = documentResult.PageRange.First();
     EndPageNumber   = documentResult.PageRange.Last();
     Pages           = ConvertPages(documentResult, pageResults, readResults);
 }
Exemplo n.º 4
0
 internal RecognizedForm(DocumentResult_internal documentResult, IReadOnlyList <PageResult_internal> pageResults, IReadOnlyList <ReadResult_internal> readResults)
 {
     // Recognized form from a model trained with labels.
     FormType  = documentResult.DocType;
     PageRange = new FormPageRange(documentResult.PageRange);
     Fields    = PopulateFields(documentResult.Fields, readResults);
     Pages     = PopulatePages(pageResults, readResults);
 }
Exemplo n.º 5
0
        internal ExtractedReceipt(DocumentResult_internal documentResult, IList <ReadResult_internal> readResults)
        {
            StartPageNumber = documentResult.PageRange.First();
            EndPageNumber   = documentResult.PageRange.Last();

            SetReceiptValues(documentResult.Fields);

            if (readResults != null)
            {
                RawExtractedPage = ConvertRawPages(StartPageNumber, EndPageNumber, readResults);
            }
        }
        internal RecognizedForm(DocumentResult_internal documentResult, IReadOnlyList <PageResult_internal> pageResults, IReadOnlyList <ReadResult_internal> readResults)
        {
            // Recognized form from a model trained with labels.
            FormType = documentResult.DocType;

            // TODO: validate that PageRange.Length == 2.
            // https://github.com/Azure/azure-sdk-for-net/issues/10547

            PageRange = new FormPageRange(documentResult.PageRange[0], documentResult.PageRange[1]);
            Fields    = ConvertSupervisedFields(documentResult.Fields, readResults);
            Pages     = ConvertSupervisedPages(pageResults, readResults);
        }
Exemplo n.º 7
0
        internal ExtractedReceipt(DocumentResult_internal documentResult, IReadOnlyList <ReadResult_internal> readResults)
        {
            StartPageNumber = documentResult.PageRange[0];
            EndPageNumber   = documentResult.PageRange[documentResult.PageRange.Count - 1];

            SetReceiptValues(documentResult.Fields);

            if (readResults != null)
            {
                RawExtractedPage = ConvertRawPages(StartPageNumber, EndPageNumber, readResults);
            }
        }
Exemplo n.º 8
0
        internal ExtractedReceipt(DocumentResult_internal documentResult, ReadResult_internal readResult)
        {
            StartPageNumber = documentResult.PageRange.First();
            EndPageNumber   = documentResult.PageRange.Last();

            SetReceiptValues(documentResult.Fields);

            if (readResult != null)
            {
                RawExtractedPage = new RawExtractedPage(readResult);
            }
        }
#pragma warning disable CA1801 // Remove unused parameter
        internal RecognizedForm(DocumentResult_internal documentResult, IList <PageResult_internal> pageResults, IList <ReadResult_internal> readResults)
#pragma warning restore CA1801 // Remove unused parameter
        {
            // Supervised
            FormType = documentResult.DocType;
            //PageRange = new FormPageRange(documentResult.PageRange);

            //Fields = ConvertFields(documentResult.Fields, readResults);
            //Tables = ConvertLabeledTables(pageResults, readResults);

            //// TODO: Populate CheckBoxes

            //if (readResults != null)
            //{
            //    PageText = ConvertPageText(readResults);
            //}
        }
Exemplo n.º 10
0
        internal RecognizedForm(DocumentResult_internal documentResult, IReadOnlyList <PageResult_internal> pageResults, IReadOnlyList <ReadResult_internal> readResults)
        {
            // Recognized form from a model trained with labels.
            FormType = documentResult.DocType;

            // TODO: validate that PageRange.Length == 2.
            // https://github.com/Azure/azure-sdk-for-net/issues/10547

            PageRange = new FormPageRange(documentResult.PageRange[0], documentResult.PageRange[1]);

            // documentResult.Fields is required and not null, according to the swagger file, but it's not
            // present when a blank receipt is submitted for recognition.

            Fields = documentResult.Fields == null
                ? new Dictionary <string, FormField>()
                : ConvertSupervisedFields(documentResult.Fields, readResults);
            Pages = ConvertSupervisedPages(pageResults, readResults);
        }
Exemplo n.º 11
0
 internal RecognizedReceipt(DocumentResult_internal documentResult, IReadOnlyList <PageResult_internal> pageResults, IReadOnlyList <ReadResult_internal> readResults)
 {
     // Hard-coding locale for v2.0.
     ReceiptLocale  = "en-US";
     RecognizedForm = new RecognizedForm(documentResult, pageResults, readResults);
 }
Exemplo n.º 12
0
        internal static AnalyzeResult_internal DeserializeAnalyzeResult_internal(JsonElement element)
        {
            AnalyzeResult_internal result = new AnalyzeResult_internal();

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("version"))
                {
                    result.Version = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("readResults"))
                {
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        result.ReadResults.Add(ReadResult_internal.DeserializeReadResult_internal(item));
                    }
                    continue;
                }
                if (property.NameEquals("pageResults"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    result.PageResults = new List <PageResult_internal>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        result.PageResults.Add(PageResult_internal.DeserializePageResult_internal(item));
                    }
                    continue;
                }
                if (property.NameEquals("documentResults"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    result.DocumentResults = new List <DocumentResult_internal>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        result.DocumentResults.Add(DocumentResult_internal.DeserializeDocumentResult_internal(item));
                    }
                    continue;
                }
                if (property.NameEquals("errors"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    result.Errors = new List <FormRecognizerError>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        result.Errors.Add(FormRecognizerError.DeserializeFormRecognizerError(item));
                    }
                    continue;
                }
            }
            return(result);
        }
Exemplo n.º 13
0
        internal static AnalyzeResult_internal DeserializeAnalyzeResult_internal(JsonElement element)
        {
            string version = default;
            IReadOnlyList <ReadResult_internal>     readResults     = default;
            IReadOnlyList <PageResult_internal>     pageResults     = default;
            IReadOnlyList <DocumentResult_internal> documentResults = default;
            IReadOnlyList <FormRecognizerError>     errors          = default;

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("version"))
                {
                    version = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("readResults"))
                {
                    List <ReadResult_internal> array = new List <ReadResult_internal>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        if (item.ValueKind == JsonValueKind.Null)
                        {
                            array.Add(null);
                        }
                        else
                        {
                            array.Add(ReadResult_internal.DeserializeReadResult_internal(item));
                        }
                    }
                    readResults = array;
                    continue;
                }
                if (property.NameEquals("pageResults"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    List <PageResult_internal> array = new List <PageResult_internal>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        if (item.ValueKind == JsonValueKind.Null)
                        {
                            array.Add(null);
                        }
                        else
                        {
                            array.Add(PageResult_internal.DeserializePageResult_internal(item));
                        }
                    }
                    pageResults = array;
                    continue;
                }
                if (property.NameEquals("documentResults"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    List <DocumentResult_internal> array = new List <DocumentResult_internal>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        if (item.ValueKind == JsonValueKind.Null)
                        {
                            array.Add(null);
                        }
                        else
                        {
                            array.Add(DocumentResult_internal.DeserializeDocumentResult_internal(item));
                        }
                    }
                    documentResults = array;
                    continue;
                }
                if (property.NameEquals("errors"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    List <FormRecognizerError> array = new List <FormRecognizerError>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        if (item.ValueKind == JsonValueKind.Null)
                        {
                            array.Add(null);
                        }
                        else
                        {
                            array.Add(FormRecognizerError.DeserializeFormRecognizerError(item));
                        }
                    }
                    errors = array;
                    continue;
                }
            }
            return(new AnalyzeResult_internal(version, readResults, pageResults, documentResults, errors));
        }
 internal RecognizedReceipt(DocumentResult_internal documentResult, IReadOnlyList <PageResult_internal> pageResults, IReadOnlyList <ReadResult_internal> readResults)
 {
     RecognizedForm = new RecognizedForm(documentResult, pageResults, readResults);
 }