> ## Documentation Index
> Fetch the complete documentation index at: https://docs.abbyy.com/llms.txt
> Use this file to discover all available pages before exploring further.

# NLP Model Training

ABBYY FlexiCapture 12 SDK uses NLP (Natural Language Processing) models to extract information from unstructured texts. NLP models trained on your sample documents determine what entities and segments should be extracted from a document, which allows for the information that you need to be extracted more efficiently.

You can create and train one or more NLP models depending on the variety of your documents, as well as the complexity and amount of the information that you need to extract. A segmentation model uses the text of the entire section to find the required paragraphs, while an extraction model finds entities in text fragments.

## Preparing for training

NLP technology is used in ABBYY FlexiCapture SDK as a part of a Document Definition, so you need to create a new Document Definition or get it from the collection.

1. Get the Document Definition from the [**DocumentDefinitions**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/documentdefinitions.md) collection of your [**Project**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/project.md). Switch the Document Definition to editing mode by calling the [**CheckOut**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/documentdefinitions-checkout.md) method of the **DocumentDefinitions** object and get the sections of the Document Definition using the [**Sections**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/documentdefinition.md#sections) property of the [**DocumentDefinition**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/documentdefinition.md) object.
2. Obtain the NLP models collection using the [**NlpModels**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/sectiondefinition.md#nlpmodels) property of the [**SectionDefinition**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/sectiondefinition.md) object.
3. Get the collection of the extraction models using the [**ExtractionModels**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/nlpmodels.md#extractionmodels) property of the [**NlpModels**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/nlpmodels.md) object or get the handler of the segmentation model using [**SegmentationModelHandler**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/nlpmodels.md#segmentationmodelhandler) property of the [**NlpModels**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/nlpmodels.md) object. If you have an existing extraction model, you can load it using the [**LoadFromFile**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/extractionmodels-loadfromfile.md) method of the [**ExtractionModels**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/extractionmodels.md) object.
4. Or you can create a new one:
   1. To extract entities, you need an entity extraction model. Call the [**AddNew**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/extractionmodels-addnew.md) method of the **ExtractionModels** object.
   2. If you want to improve the accuracy and speed of entity extraction using segmentation, create a segmentation model by calling [**AddNewSegmentationModel**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/segmentationmodelhandler-addnewsegmentationmodel.md) method of the **SegmentationModelHandler** object.
5. For both models:
   1. Make sure the training flag (**AllowTraining** property) is set to TRUE if you want to train your NLP model.
   2. Specify the field from which to extract the entity using the **Source** property.\
      Note: For a segmentation model, the value of the **Source** property is a specific Document Definition section to which the segmentation model belongs.
   3. Specify the language of the text to be extracted using the **Language** property.
   4. Specify the output fields using the **ResultFields** property. The output fields will contain information extracted by the model from your documents.
6. Call the [**CheckIn**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/documentdefinitions-checkin.md) method of the **DocumentDefinitions** object.

> [accordion] C# code
>
> ```csharp theme={null}
> // Get a document definition for editing
> project.DocumentDefinitions.CheckOut(documentDefinition);
>
> // We recommend to create one section in the document definition
> // with a flat field structure to get the acceptable quality of using NLP.
> // Obtain this one
> ISectionDefinition sectionDefinition = documentDefinition.Sections[0];
>
> // Set up a segmentation model
> ISegmentationModel segmentationModel = sectionDefinition.NlpModels.SegmentationModelHandler.AddNewSegmentationModel("Segmentator", language);
> IFieldDefinitionsCollection segmentationModelResultFieldDefinitions = engine.CreateFieldDefinitionsCollection();
>
> // Obtain a field definition corresponding to the segment and add it into result field definitions collection
> // Do this for each segment
> segmentationModelResultFieldDefinitions.Add(segmentFieldDefinition);
>
> // Set a result field for the segmentation model
> segmentationModel.ResultFields = segmentationModelResultFieldDefinitions;
>
> // Set up an extraction model
> IExtractionModel extractionModel = sectionDefinition.NlpModels.ExtractionModels.AddNew("ModelName", segmentFieldDefinition, language);
> IFieldDefinitionsCollection extractionModelResultFieldDefinitions = engine.CreateFieldDefinitionsCollection();
>
> // Obtain the field definition corresponding to the field to be extracted and add it into result field definitions collection
> // Note that the result fields should be located inside the certain segment
> extractionModelResultFieldDefinitions.Add(fieldDefinition);
>
> // Do this for each field to be extracted
> extractionModel.ResultFields = extractionModelResultFieldDefinitions;
> // Repeat it for all extraction models
>
> // Save the document definition
> project.DocumentDefinitions.CheckIn(documentDefinition);
> ```

## Training NLP models

Once your NLP model has been loaded or created, you need to train it on some sample documents.

1. Add a new training batch using the [**AddNew**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fieldsextractiontrainingbatches-addnew.md) method of the [**FieldsExtractionTrainingBatches**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fieldsextractiontrainingbatches.md) object.
2. Set the value of the [**IsNlpTrainingBatch**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fieldsextractiontrainingbatch.md) property of the [**FieldsExtractionTrainingBatch**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fieldsextractiontrainingbatch.md) object to TRUE to define a training batch as a batch for NLP training.
3. [Add images for NLP model training to the batch](https://docs.abbyy.com/flexi-capture/sdk/guided-tour/engine-addingimages.md). Call the [**Recognize**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/batch-recognize.md) method of the [**Batch**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/batch.md) object to match the Document Definition and recognize all the documents in the batch.
4. Mark up the layout of the fields that will be trained. Assign the correct region to each field by calling the [**AddNew**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fieldregions-addnew.md) method of the [**FieldRegions**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fieldregions.md) object.
5. Train field extraction. Get the [**FieldsExtractionTrainer**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fieldsextractiontrainer.md) object from the training batch and call its [**Train**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fieldsextractiontrainer-train.md) method.

> [accordion] C# code
>
> ```csharp theme={null}
> // Create a field extraction training batch for a particular section definition
> IFieldExtractionTrainingBatch trainingBatch = project.FieldsExtractionTrainingBatches.AddNew(sectionDefinition, null);
>
> // Set the batch as a batch for NLP training
> trainingBatch.IsNlpTrainingBatch = true;
> trainingBatch.Batch.Open();
>
> // Add images by grouping pages as one document
> IImageGroupingParams imageGroupingParams = engine.CreateImageGroupingParams();
> imageGroupingParams.GroupingRule = PagesGroupingEnum.PG_GroupPagesByOriginFile;
> trainingBatch.AddImages(imagePathsCollection, -1, null, imageGroupingParams, null, docIds);
>
> // Recognize added images
> trainingBatch.Recognize(null, RecognitionModeEnum.RM_ReRecognizeAll, null);
>
> // Getting a feedback from the verificator
> // Based on verification data, mark up the layout of the fields that will be trained
> document.Open();
> IPage page = document.Pages[pageIndex];
> IRegion newRegion = Engine.CreateRegion();
> newRegion.AddRect(left, top, right, bottom);
>  page on which the region is located.
> // Do this for each f
> // For the particular field, assign a corresponding region
> // and specify theield to be marked in the document
> field.FieldRegions.AddNew(page, newRegion, true);
> document.Close();
> // Do this for each document in the training batch
>
> // Train the NLP models
> trainingBatch.FieldsExtractionTrainer.Train();
> trainingBatch.Batch.Close();
> ```

You can now use the trained NLP model to process your documents.

> [!WARNING]
>
> To recognize documents using NLP technology, the NLP module must be installed. If no NLP components are found, you cannot use the NLP models.

## NLP model training limitations

1. You can only have one segmentation model for each Document Definition section
2. Fields used in training (fields of the **ResultFields** property) cannot be nested more than one level deep, i.e. must have a flat structure: a simple field or a field inside a group of fields. Fields inside a group that is part of another group cannot be trained. The first nesting level is a source field, and here are the possible options of the source field values and fields structure:
   1. The value of the **Source** property is a Document Definition section, the value of the **ResultFields** property is a flat list of repetitive and non-repetitive fields and groups of non-repetitive fields:

      ![](https://mintcdn.com/abbyy/DG4YBah30S0eFFo5/images/flexi-capture/sdk/NlpModelField1.png?fit=max\&auto=format\&n=DG4YBah30S0eFFo5\&q=85\&s=2dec19be97ed22c90b42c57b65b8713e)
   2. The value of the **Source** property is a segmentation model field or a field retrieved by a flexible description, the value of the **ResultFields** property is a flat list of repetitive and non-repetitive fields and groups of non-repetitive fields:

      ![](https://mintcdn.com/abbyy/DG4YBah30S0eFFo5/images/flexi-capture/sdk/NlpModelField2.png?fit=max\&auto=format\&n=DG4YBah30S0eFFo5\&q=85\&s=33946bbe1f6d75df3698d05f7fc09ec8)
   3. The value of the **Source** property is a non-repetitive field in a repetitive group, the value of the **ResultFields** property is a flat list of repetitive and non-repetitive fields and groups of non-repetitive fields inside the same group:

      ![](https://mintcdn.com/abbyy/DG4YBah30S0eFFo5/images/flexi-capture/sdk/NlpModelField3.png?fit=max\&auto=format\&n=DG4YBah30S0eFFo5\&q=85\&s=e7d1b5ff674839d98166ce99a8932dad)

      In this case, it is recommended that the non-repetitive fields be within the same sentence for each group.
3. The value of the **Source** property cannot be a repeatable field. You can use a non-repetitive field in a repetitive group instead.

## Samples

See the [NLP Model Training](https://docs.abbyy.com/flexi-capture/sdk/guided-tour/samples.md#nlpmodeltraining) code sample for an implementation of this scenario.

## See also

[NLP Training Objects](https://docs.abbyy.com/flexi-capture/sdk/api-reference/nlp-overview.md)

[Specifications: NLP Module](https://docs.abbyy.com/flexi-capture/sdk/specifications/nlpmodule.md)
