> ## 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モデルの学習

ABBYY FlexiCapture 12 SDK は、NLP (自然言語処理) モデルを使用して非定型テキストから情報を抽出します。サンプル ドキュメントで学習させた NLP モデルが、ドキュメントから抽出すべきエンティティとセグメントを判断するため、必要な情報をより効率的に抽出できます。

ドキュメントの多様性や、抽出する情報の複雑さと量に応じて、1 つ以上の NLP モデルを作成して学習させることができます。セグメンテーション モデルはセクション全体のテキストを使用して必要な段落を検出し、抽出モデルはテキスト断片内のエンティティを検出します。

## 学習の準備

ABBYY FlexiCapture SDK では、NLP テクノロジーは Document Definition の一部として使用されます。そのため、新しい Document Definition を作成するか、コレクションから取得する必要があります。

1. [**Project**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/project.md) の [**DocumentDefinitions**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/documentdefinitions.md) コレクションから Document Definition を取得します。**DocumentDefinitions** オブジェクトの [**CheckOut**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/documentdefinitions-checkout.md) メソッドを呼び出して Document Definition を編集モードに切り替え、[**DocumentDefinition**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/documentdefinition.md) オブジェクトの [**Sections**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/documentdefinition.md#sections) プロパティを使用して Document Definition のセクションを取得します。
2. [**SectionDefinition**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/sectiondefinition.md) オブジェクトの [**NlpModels**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/sectiondefinition.md#nlpmodels) プロパティを使用して、NLP モデルのコレクションを取得します。
3. [**NlpModels**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/nlpmodels.md) オブジェクトの [**ExtractionModels**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/nlpmodels.md#extractionmodels) プロパティを使用して抽出モデルのコレクションを取得するか、[**NlpModels**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/nlpmodels.md) オブジェクトの [**SegmentationModelHandler**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/nlpmodels.md#segmentationmodelhandler) プロパティを使用してセグメンテーションモデルのハンドラーを取得します。既存の抽出モデルがある場合は、[**ExtractionModels**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/extractionmodels.md) オブジェクトの [**LoadFromFile**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/extractionmodels-loadfromfile.md) メソッドを使用してロードできます。
4. 新しいモデルを作成することもできます。
   1. エンティティを抽出するには、エンティティ抽出モデルが必要です。**ExtractionModels** オブジェクトの [**AddNew**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/extractionmodels-addnew.md) メソッドを呼び出します。
   2. セグメンテーションによってエンティティ抽出の精度と速度を向上させる場合は、**SegmentationModelHandler** オブジェクトの [**AddNewSegmentationModel**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/segmentationmodelhandler-addnewsegmentationmodel.md) メソッドを呼び出してセグメンテーションモデルを作成します。
5. 両方のモデルで、次の操作を行います。
   1. NLP モデルを学習させる場合は、学習フラグ (**AllowTraining** プロパティ) が TRUE に設定されていることを確認します。
   2. **Source** プロパティを使用して、エンティティの抽出元となる field を指定します。
      注: セグメンテーションモデルの場合、**Source** プロパティの値は、そのセグメンテーションモデルが属する特定の文書定義セクションです。
   3. **Language** プロパティを使用して、抽出対象のテキストの言語を指定します。
   4. **ResultFields** プロパティを使用して出力フィールドを指定します。出力フィールドには、モデルが文書から抽出した情報が格納されます。
6. **DocumentDefinitions** オブジェクトの [**CheckIn**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/documentdefinitions-checkin.md) メソッドを呼び出します。

> [accordion] C# コード
>
> ```csharp theme={null}
> // 編集用に文書定義を取得する
> project.DocumentDefinitions.CheckOut(documentDefinition);
>
> // NLP で十分な品質を得るには、
> // フラットな field 構造を持つセクションを文書定義内に 1 つだけ作成することを推奨します。
> // そのセクションを取得する
> ISectionDefinition sectionDefinition = documentDefinition.Sections[0];
>
> // セグメンテーションモデルを設定する
> ISegmentationModel segmentationModel = sectionDefinition.NlpModels.SegmentationModelHandler.AddNewSegmentationModel("Segmentator", language);
> IFieldDefinitionsCollection segmentationModelResultFieldDefinitions = engine.CreateFieldDefinitionsCollection();
>
> // セグメントに対応する field 定義を取得し、結果 field 定義のコレクションに追加する
> // これを各セグメントに対して行う
> segmentationModelResultFieldDefinitions.Add(segmentFieldDefinition);
>
> // セグメンテーションモデルの結果 field を設定する
> segmentationModel.ResultFields = segmentationModelResultFieldDefinitions;
>
> // 抽出モデルを設定する
> IExtractionModel extractionModel = sectionDefinition.NlpModels.ExtractionModels.AddNew("ModelName", segmentFieldDefinition, language);
> IFieldDefinitionsCollection extractionModelResultFieldDefinitions = engine.CreateFieldDefinitionsCollection();
>
> // 抽出対象の field に対応する field 定義を取得し、結果 field 定義のコレクションに追加する
> // 結果 field は特定のセグメント内に配置されている必要がある点に注意
> extractionModelResultFieldDefinitions.Add(fieldDefinition);
>
> // これを抽出対象の各 field に対して行う
> extractionModel.ResultFields = extractionModelResultFieldDefinitions;
> // すべての抽出モデルに対して繰り返す
>
> // 文書定義を保存する
> project.DocumentDefinitions.CheckIn(documentDefinition);
> ```

## NLPモデルの学習

NLPモデルをロードまたは作成したら、サンプルドキュメントを使ってモデルを学習させる必要があります。

1. [**FieldsExtractionTrainingBatches**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/fieldsextractiontrainingbatches.md) オブジェクトの [**AddNew**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/fieldsextractiontrainingbatches-addnew.md) メソッドを使用して、新しい学習用バッチを追加します。
2. [**FieldsExtractionTrainingBatch**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/fieldsextractiontrainingbatch.md) オブジェクトの [**IsNlpTrainingBatch**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/fieldsextractiontrainingbatch.md) プロパティの値を TRUE に設定し、学習用バッチを NLP 学習用のバッチとして定義します。
3. [NLPモデルの学習に使用する画像をバッチに追加します](https://docs.abbyy.com/ja/flexi-capture/sdk/guided-tour/engine-addingimages.md)。[**Batch**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/batch.md) オブジェクトの [**Recognize**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/batch-recognize.md) メソッドを呼び出し、Document Definition とのマッチングを行って、バッチ内のすべてのドキュメントを認識します。
4. 学習対象のフィールドのレイアウトをマークアップします。[**FieldRegions**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/fieldregions.md) オブジェクトの [**AddNew**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/fieldregions-addnew.md) メソッドを呼び出して、各フィールドに正しい領域を割り当てます。
5. フィールド抽出を学習させます。学習用バッチから [**FieldsExtractionTrainer**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/fieldsextractiontrainer.md) オブジェクトを取得し、その [**Train**](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/fieldsextractiontrainer-train.md) メソッドを呼び出します。

> [accordion] C# コード
>
> ```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();
> ```

これで、学習済みのNLPモデルを使用してドキュメントを処理できるようになりました。

> [!WARNING]
>
> NLP テクノロジーを使用してドキュメントを認識するには、NLPモジュールがインストールされている必要があります。NLP コンポーネントが見つからない場合、NLPモデルは使用できません。

## NLPモデルの学習に関する制限事項

1. 1つの文書定義セクションに設定できるセグメンテーションモデルは1つだけです
2. 学習に使用する fields (**ResultFields** プロパティの fields) のネストは1レベルまでです。つまり、単純な field、または field グループ内の field といったフラットな構造である必要があります。別のグループに含まれるグループ内の fields は学習できません。最初のネストレベルはソース field です。ソース field の値と fields の構造として指定できる組み合わせは次のとおりです。
   1. **Source** プロパティの値が文書定義セクションで、**ResultFields** プロパティの値が、繰り返し fields と非繰り返し fields、および非繰り返し fields のグループからなるフラットなリストである場合：

      ![](https://mintcdn.com/abbyy/DG4YBah30S0eFFo5/images/flexi-capture/sdk/NlpModelField1.png?fit=max\&auto=format\&n=DG4YBah30S0eFFo5\&q=85\&s=2dec19be97ed22c90b42c57b65b8713e)
   2. **Source** プロパティの値がセグメンテーションモデルの field、または flexible な記述によって取得された field で、**ResultFields** プロパティの値が、繰り返し fields と非繰り返し fields、および非繰り返し fields のグループからなるフラットなリストである場合：

      ![](https://mintcdn.com/abbyy/DG4YBah30S0eFFo5/images/flexi-capture/sdk/NlpModelField2.png?fit=max\&auto=format\&n=DG4YBah30S0eFFo5\&q=85\&s=33946bbe1f6d75df3698d05f7fc09ec8)
   3. **Source** プロパティの値が繰り返しグループ内の非繰り返し field で、**ResultFields** プロパティの値が、同じグループ内の繰り返し fields と非繰り返し fields、および非繰り返し fields のグループからなるフラットなリストである場合：

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

      この場合、各グループの非繰り返し fields は同じ文の中に含まれていることが推奨されます。
3. **Source** プロパティの値に繰り返し field を指定することはできません。代わりに、繰り返しグループ内の非繰り返し field を使用できます。

## サンプル

このシナリオの実装例については、コードサンプル「[NLPモデルの学習](https://docs.abbyy.com/ja/flexi-capture/sdk/guided-tour/samples.md#nlpmodeltraining)」を参照してください。

## 関連項目

[NLP学習オブジェクト](https://docs.abbyy.com/ja/flexi-capture/sdk/api-reference/nlp-overview.md)

[仕様: NLPモジュール](https://docs.abbyy.com/ja/flexi-capture/sdk/specifications/nlpmodule.md)
