Skip to content

CTX

Introduction

The pre-defined CTX object offers the various features described below.

getOptions

The getOptions method returns an object whose properties represent the options affecting or reflecting the analysis.
When used in the Studio IDE, the options are those set in Studio project settings plus others related to the analysis. For example, the JSON corresponding to the returned object could be like this:

{
    "enhanced_output": "false",
    "debugger": "true",
    "debugger_port": "9091",
    "explain_rule": "-1",
    "all_categories": "true",
    "filename": "Doc185672.xml.txt"
}

where:

  • enhanced_output: the current value of the Enable Advanced Disambiguation Info configuration property.
  • debugger: if true, the Studio IDE debugger is on.
  • debugger_port: the current value of the Debugger Port configuration property.
  • all_categories: corresponds to the Show All Categories configuration property.
  • filename: the name of the file being analyzed.

When using the text intelligence engine via the Studio Local Deployment Agent API or when uploading a model to Platform and then publishing the model to NL Flow, putting it in a workflow and using it via NL Flow API, the options are those specified in the input JSON under the options key. For example:

"options": {
    "custom": {
        "doSomethingSpecial": true
    }
}

The custom key in the input JSON corresponds to the custom_options property of the object returned by the method.

The syntax is:

CTX.getOptions()

For example, this code can access the doSomethingSpecial option of the example above and takes it into account:

var options = CTX.getOptions();

if( options.hasOwnProperty('custom') &&
    options.custom.hasOwnProperty('doSomethingSpecial') ) {
    // Do something special
}

getTemplates

The getTemplates method returns an array containing the definition of the extraction templates with their fields and fields' attributes.

For example, the JSON corresponding to the array could be something like this:

[
    {
        "template": "TEMPLATE01",
        "fields": [
            {
                "field": "FIELD01",
                "cardinal": "false",
                "solitary": "false"
            },
            {
                "field": "FIELD02",
                "cardinal": "false",
                "solitary": "false"
            }
        ]
    }
]

where cardinal and solitary are field attributes.

The syntax is:

CTX.getTemplates()

getTaxonomy

The getTaxonomy method returns an array of objects containing the project taxonomy.

Each item of the array represent a node of the taxonomy—a domain—with its descendants and has this structure:

{
    "name": domainName,
    "label": domainLabel[,
    "domains": array]
}

where domainName is the name of the domain, domainLabel is the optional label of the domain and domains, which is present only for non-leaf nodes, is an array each item of which represents a child node and as the same structure as above.

The syntax is:

CTX.getTaxonomy()

getAttributeMatch

The getAttributeMatch method must be used in functions invoked via the SCRIPT attribute used in a plus (+) combination with another attribute, for example:

KEYWORD("an apple a day keeps the doctor away") + SCRIPT(function)

The method returns the range of tokens matched by the previous attribute.
For example, given a rule with a condition containing the operand above, if the analyzed text is:

As the saying goes, an apple a day keeps the doctor away.

and the function specified as an argument to the SCRIPT attribute is like this:

function testAttributeMatch(index, params) {
    var match = CTX.getAttributeMatch();
    ...

during the analysis the match variable is an object like this:

{
    "tokenBegin": "5",
    "tokenEnd": "12"
}

tokenBegin is the number of the first token of the range matched by the KEYWORD attribute (an), tokenEnd is the number of the last token of the range (away).

Depending on the values of tokenBegin and tokenEnd and other variables, the function code can return true (the SCRIPT attribute is matched) or false (the SCRIPT attribute is not matched), in order to make true or false the combination with the previous attribute, i.e. the entire operand of the condition.

The syntax of the method is:

CTX.getAttributeMatch()

startProfiler and stopProfiler

startProfiler and stopProfiler are used to measure the time taken to execute all the statements between the invocations of the two.
This is a useful feature for debugging in Studio, for example to identify the slowest parts of a script and possibly speed them up.

Method startProfiler starts a profiler while method stopProfiler stops it. The profiler is a sort of timer. The same profiler can be started and stopped multiple times, in which case the number of starts and stops and the cumulative time are tracked. Any number of profilers can be used.

For example, this code:

function onFinalize(result) {

    CTX.startProfiler("myProfiler")

    var count = result.match_info.rules.categorization.length;
    var category;

    for (i=0; i < count; i++)
    {
        category = result.match_info.rules.categorization[i];

        if(category.label == "FOOD")
        {
            category.score += 15;
        }
    }

    CTX.stopProfiler("myProfiler")

    return result;
}

tracks the time taken by the execution of the onFinalize function in the myProfiler profiler.

The syntax of the startProfiler method is:

CTX.startProfiler(profiler name)

The syntax of the stopProfiler method is:

CTX.stopProfiler(profiler name)

The methods work:

  • For single document analysis, if in Studio the Enable Debug Info for single file analysis configuration property has been set to true.
  • For multiple document analysis, if the Enable Debug Info for files analyses configuration property has been set to true.

At the end of the analysis, profiler data are recorded:

  • In the JSON file test file name.ctx.json.gz which is stored in the gen subfolder of the Studio project folder.
    The number of starts and stops (count) and the cumulative elasped time (elapsed) are written as an item of the time_stats array which is a property of the document object:

    {
        "document": {
            "time_stats": [
                ...
                {
                    "name": "profiler name",
                    "count": 2,
                    "elapsed": 968.85
                }
                ...
            ]
        }
    }

  • In the Time statistics tab of the Statistics tool window in Studio.