| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using Azure.AI.FormRecognizer.Models; |
| | 7 | |
|
| | 8 | | namespace Azure.AI.FormRecognizer.Training |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Represents a model trained from custom forms. |
| | 12 | | /// </summary> |
| | 13 | | public class CustomFormModel |
| | 14 | | { |
| 144 | 15 | | internal CustomFormModel(Model model) |
| | 16 | | { |
| 144 | 17 | | ModelId = model.ModelInfo.ModelId; |
| 144 | 18 | | Status = model.ModelInfo.Status; |
| 144 | 19 | | TrainingStartedOn = model.ModelInfo.TrainingStartedOn; |
| 144 | 20 | | TrainingCompletedOn = model.ModelInfo.TrainingCompletedOn; |
| 144 | 21 | | Submodels = ConvertToSubmodels(model); |
| | 22 | |
|
| | 23 | | // TrainResult can be null if model is not ready yet. |
| | 24 | |
|
| 144 | 25 | | TrainingDocuments = model.TrainResult != null |
| 144 | 26 | | ? ConvertToTrainingDocuments(model.TrainResult) |
| 144 | 27 | | : new List<TrainingDocumentInfo>(); |
| | 28 | |
|
| 144 | 29 | | Errors = model.TrainResult?.Errors ?? new List<FormRecognizerError>(); |
| 144 | 30 | | } |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// The unique identifier of this model. |
| | 34 | | /// </summary> |
| 156 | 35 | | public string ModelId { get; } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// A status indicating this model's readiness for use. |
| | 39 | | /// </summary> |
| 148 | 40 | | public CustomFormModelStatus Status { get; } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// The date and time (UTC) when model training was started. |
| | 44 | | /// </summary> |
| 32 | 45 | | public DateTimeOffset TrainingStartedOn { get; } |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// The date and time (UTC) when model training completed. |
| | 49 | | /// </summary> |
| 32 | 50 | | public DateTimeOffset TrainingCompletedOn { get; } |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// A list of submodels that are part of this model, each of which can recognize and extract fields from a diffe |
| | 54 | | /// </summary> |
| 360 | 55 | | public IReadOnlyList<CustomFormSubmodel> Submodels { get; } |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// A list of meta-data about each of the documents used to train the model. |
| | 59 | | /// </summary> |
| 148 | 60 | | public IReadOnlyList<TrainingDocumentInfo> TrainingDocuments { get; } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// A list of errors ocurred during the training operation. |
| | 64 | | /// </summary> |
| 60 | 65 | | public IReadOnlyList<FormRecognizerError> Errors { get; } |
| | 66 | |
|
| | 67 | | private static IReadOnlyList<CustomFormSubmodel> ConvertToSubmodels(Model model) |
| | 68 | | { |
| 144 | 69 | | if (model.Keys != null) |
| 72 | 70 | | return ConvertFromUnlabeled(model.Keys); |
| | 71 | |
|
| 72 | 72 | | if (model.TrainResult != null) |
| 68 | 73 | | return ConvertFromLabeled(model); |
| | 74 | |
|
| 4 | 75 | | return null; |
| | 76 | | } |
| | 77 | |
|
| | 78 | | private static IReadOnlyList<CustomFormSubmodel> ConvertFromUnlabeled(KeysResult keys) |
| | 79 | | { |
| 72 | 80 | | var subModels = new List<CustomFormSubmodel>(); |
| | 81 | |
|
| 304 | 82 | | foreach (var cluster in keys.Clusters) |
| | 83 | | { |
| 80 | 84 | | var fieldMap = new Dictionary<string, CustomFormModelField>(); |
| 3280 | 85 | | for (int i = 0; i < cluster.Value.Count; i++) |
| | 86 | | { |
| 1560 | 87 | | string fieldName = "field-" + i; |
| 1560 | 88 | | string fieldLabel = cluster.Value[i]; |
| 1560 | 89 | | fieldMap.Add(fieldName, new CustomFormModelField(fieldName, fieldLabel, default)); |
| | 90 | | } |
| 80 | 91 | | subModels.Add(new CustomFormSubmodel( |
| 80 | 92 | | $"form-{cluster.Key}", |
| 80 | 93 | | default, |
| 80 | 94 | | fieldMap)); |
| | 95 | | } |
| 72 | 96 | | return subModels; |
| | 97 | | } |
| | 98 | |
|
| | 99 | | private static IReadOnlyList<CustomFormSubmodel> ConvertFromLabeled(Model model) |
| | 100 | | { |
| 68 | 101 | | var fieldMap = new Dictionary<string, CustomFormModelField>(); |
| | 102 | |
|
| 68 | 103 | | if (model.TrainResult.Fields != null) |
| | 104 | | { |
| 1984 | 105 | | foreach (var formFieldsReport in model.TrainResult.Fields) |
| | 106 | | { |
| 924 | 107 | | fieldMap.Add(formFieldsReport.Name, new CustomFormModelField(formFieldsReport.Name, null, formFields |
| | 108 | | } |
| | 109 | | } |
| | 110 | |
|
| 68 | 111 | | return new List<CustomFormSubmodel> { |
| 68 | 112 | | new CustomFormSubmodel( |
| 68 | 113 | | $"form-{model.ModelInfo.ModelId}", |
| 68 | 114 | | model.TrainResult.AverageModelAccuracy, |
| 68 | 115 | | fieldMap)}; |
| | 116 | | } |
| | 117 | |
|
| | 118 | | private static IReadOnlyList<TrainingDocumentInfo> ConvertToTrainingDocuments(TrainResult trainResult) |
| | 119 | | { |
| 140 | 120 | | var trainingDocs = new List<TrainingDocumentInfo>(); |
| 1648 | 121 | | foreach (var docs in trainResult.TrainingDocuments) |
| | 122 | | { |
| 684 | 123 | | trainingDocs.Add( |
| 684 | 124 | | new TrainingDocumentInfo( |
| 684 | 125 | | docs.Name, |
| 684 | 126 | | docs.PageCount, |
| 684 | 127 | | docs.Errors ?? new List<FormRecognizerError>(), |
| 684 | 128 | | docs.Status)); |
| | 129 | | } |
| 140 | 130 | | return trainingDocs; |
| | 131 | | } |
| | 132 | | } |
| | 133 | | } |