Skip to content

Create extra data

To create extra data available in the XXXX.txt.ctx.json.gz file in the gen folder in order to enrich your output, use the extraData object in the onFinalize function.

For example, with this template:

TEMPLATE(FOOD)
{
    @CHOCOLATE,
    @FRUIT,
    @MEAT,
    @FISH
}

and with this rule:

SCOPE SENTENCE
{
    IDENTIFY(FOOD)
    {
        @CHOCOLATE[LEMMA("chocolate bar")]
    }
}

applied to this text:

I ate a 50g. chocolate bar yesterday.

you will get this extraction output:

TEMPLATE: FOOD

Field Value
@CHOCOLATE chocolate bar

With this code:

function onFinalize(result) {
    result["extraData"] = {};
    result["extraData"]["nutritionalFacts"]={
    "calories per 50g.":"260",
    "fat per 50g.":"7g",
    "protein per 50g":"2g",
    "carbohydrate per 50g":"31g"
    }
    result["extraData"]["chocolateProperties"]={
    "chocolate type": "dark chocolate",
    "percentage": "99%"
    }
    return result;
}

more data will be added into the XXXX.txt.ctx.json.gz file in order to enrich your extraction output. This is what you will see:

"extraData": {
    "nutritionalFacts": {
      "calories per 50g.": "260",
      "fat per 50g.": "14g",
      "protein per 50g": "2g",
      "carbohydrate per 50g": "31g"
    },
    "chocolateProperties": {
      "chocolate type": "dark chocolate",
      "percentage": "99%"
    }
}