Skip to main content

Converters

tip
Converters — scripts that are used to interpret the values ​​of the text in each token.

Converters allow you to convert data tokens placed in $parseTree.

Declaration

Converter declaration:

  • When declaring a pattern:

    patterns:
    $four = four|| converter = function() {return 4}
  • Under init tags:

    init:
    function amountConverter(pt) {
    var ret = pt.Number[0].value;
    return ret
    }
  • In the .js-library file, for example converters.js:

        function amountConverter(pt) {
    var ret = pt.Number[0].value;
    return ret
    }

Functions of the converters get $parseTree as the first argument.

You can use inside a converter the variable refering to $parseTree. To do this, set its name as the first argument of the function; the second argument is the context.

Usage

Get value ​​from the text

Script:

$Digit = $regexp<\d+> || converter = numberConverterDigit

.js-file:

function numberConverterDigit(parseTree) {
return parseInt(parseTree.text);
}

Convert value from mapping

Script:

$Numeral = (one:1|...) || converter = valueToNumberConverter

.js-file:

function valueToNumberConverter(parseTree) {
return parseInt(parseTree.value);
}

Get value from nested tokens

Script:

$Numeral = (one:1|...)
$Minutes = $Numeral || converter = minutesConverter

.js-file:

function minutesConverter(parseTree) {
return parseInt(parseTree.Numeral.value)
};

Get value ​​from dictionaries

Script:

$City = $entity<Cities> || converter = cityConverter
caution

If a pattern is created with $entity<> and converter, its name must not match the name of an NLU core entity. For example, if a project has an $Example pattern and an Example entity, it might cause errors during the script execution.

The $entity rule stores as a value only the entity identifier. The list of associated values ​​is contained in the dictionary. To write a value from the directory into value, specify in the .js-file:

function cityConverter(parseTree) {
var id = parseTree.City[0].value;
return Cities[id].value;
}