> ## 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.

# Document classification script

> Set up a document classification script in ABBYY FlexiCapture to combine classifiers, cascade classification, and confidence-based class assignment.

An ABBYY FlexiCapture classifier processes submitted documents and determines their class. This lets you choose a document definition to use for field extraction.

Each classifier corresponds to a specific classification training batch.

To use several classifiers for a single batch, set up a classification script. This is useful when some documents need additional classification: the first classifier assigns each document a class, and then the script selects an additional classifier based on the result. For example, documents can be first classified into "invoices" and "contracts", and then the invoices can be further classified by company name.

<Note>
  This script can only be set up for an existing classification training batch. You cannot set it up when creating a new batch.
</Note>

## Set up a classification script

<Steps>
  <Step title="Open the project properties">
    Click **Project → Project Properties**.
  </Step>

  <Step title="Open the Recognition tab">
    In the project properties window, go to the **Recognition** tab.
  </Step>

  <Step title="Open the classification script editor">
    In the **Classification** section, click **Set** to the right of **Use script**.
  </Step>

  <Step title="Add a classifier">
    In the dialog box, click **Add** and choose a classification batch from the list.
  </Step>

  <Step title="Set an alias name">
    Optionally, change the classifier's **Alias Name**. The script refers to the name in the **Alias Name** column, so you do not have to rewrite the script if a classifier's name changes during processing.
  </Step>

  <Step title="Open the script editor">
    Click **Edit Script** to open the [Script editor window](/flexi-capture/interface/script-editor).
  </Step>
</Steps>

## Parameters

| Name                  | Type                                                                         | Access     | Description                                                                                                                                                               |
| --------------------- | ---------------------------------------------------------------------------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `BatchTypeClassifier` | [IBatchTypeClassifier](/flexi-capture/appendix/scripts/ibatchtypeclassifier) | Read-only  | A classifier in the batch type.                                                                                                                                           |
| `IsConfident`         | bool                                                                         | Read-only  | Classification confidence. Unlike automatic classifiers, when you use the script this parameter must be specified manually in page properties. See the following example. |
| `Page`                | [IPage](/flexi-capture/appendix/scripts/ipage)                               | Read/write | Document page.                                                                                                                                                            |
| `Processing`          | [IProcessingCallback](/flexi-capture/appendix/scripts/iprocessingcallback)   | Read/write | The object for logging information about processing.                                                                                                                      |

The following example specifies `IsConfident` in page properties:

```csharp theme={null}
IClassificationResult result = Classifiers[0].ClassifyPage( Page );
Page.ResultClassName = result.ClassName;
Page.IsResultClassConfident = result.IsConfident;
```

## Classify a page and set the class name

This script classifies pages and adds the name of a class to their properties:

```csharp theme={null}
IBatchTypeClassifierResult classifierResult = BatchTypeClassifier.ClassifyPage( Page, null );
Page.ResultClassName = classifierResult.ClassName;
```

## Get classification confidence

This script gets classification confidence levels:

```csharp theme={null}
IClassificationResult result = Classifiers[0].ClassifyPage( Page );
FCTools.ShowMessage( "Class: " + result.ClassName );
Page.ResultClassName = result.ClassName;
FCTools.ShowMessage( "IsConfident: " + result.IsConfident );
Page.IsResultClassConfident = result.IsConfident;
```

## Combine multiple classifiers for cascade classification

```csharp theme={null}
var res0 = Classifiers.Get ( "Batch" ).ClassifyPage( Page );
IClassConfidences classVars = res0.Classes;
foreach( IClassConfidence c in classVars )
{
    FCTools.ShowMessage( c.Name + " --> " + c.Confidence.ToString() );
}
FCTools.ShowMessage( "Class res0: " + res0.ClassName);
if (res0.ClassName == "Invoice")
{
    var res1 = Classifiers.Get ( "Batch2" ).ClassifyPage( Page );
    IClassConfidences classVars1 = res1.Classes;
    foreach( IClassConfidence d in classVars1 )
    {
        FCTools.ShowMessage( d.Name + " --> " + d.Confidence.ToString() );
    }
    FCTools.ShowMessage( "Class res1: " + res1.ClassName);
    Page.ResultClassName = res1.ClassName;
}
else
{
    Page.ResultClassName = res0.ClassName;
}
FCTools.ShowMessage( "Page.ResultClassName: " +Page.ResultClassName);
```

## Assign the Unknown class to low-confidence documents

Use this script if your scenario does not require documents with a low confidence level to be assigned a class, even if a class has already been specified for them. An operator should classify such documents manually.

<Note>
  For the script to work, create a new class beforehand — for example, "Unknown" — and link it to the document definition in the **Class Mapping** dialog. For more information, see [Mapping classes to Document Definition sections](/flexi-capture/classify-mapping-classes).
</Note>

```csharp theme={null}
using System;

IClassificationResult result = Classifiers.Get ( "ClassifierBatch" ).ClassifyPage(Page);
Page.ResultClassName = result.ClassName;
IClassConfidences classVars = result.Classes;
Page.Comment = "";

foreach( IClassConfidence c in classVars )
{
    //FCTools.ShowMessage( c.Name + " -- " + c.Confidence.ToString() ); // records the classification confidence value in the event log
    Page.Comment = Page.Comment + c.Name + "-" + c.Confidence.ToString() + "; "; // records the classification confidence value in the page comments
}

if (classVars.Count>0)
{
    if (classVars[0].Confidence<80) Page.IsResultClassConfident=false; // confidence threshold below which a document will be classified as uncertainly classified
    else Page.IsResultClassConfident=true;

    if (classVars[0].Confidence<20) Page.ResultClassName="Unknown"; // confidence threshold below which a document will be assigned the "Unknown" class
    if (classVars.Count>1) {
        int classesToShow = Math.Min(classVars.Count, 3);
        for(int i=0; i<classesToShow; i++)
        {
            Page.Comment = Page.Comment + classVars[i].Name + "-" + classVars[i].Confidence.ToString() + "; ";
        }
    }
}

FCTools.ShowMessage( "Class: " + Page.ResultClassName );
```

<Note>
  The confidence threshold values in this script are for demonstration purposes only. Modify them depending on your workflow.
</Note>
