Built-in entities

If the entities you want to extract are among the 31 predefined entities of the Botfuel Entity Extraction service, use the WsExtractor class.

Here is an example of a color extractor that extracts the color standard entity:

const { WsExtractor } = require('botfuel-dialog');

class ColorExtractor extends WsExtractor {}

ColorExtractor.params = {
  dimensions: ['color'],
};

module.exports = ColorExtractor;

This file must be located at src/extractors/color-extractor.js. The file name and the class name don’t have any importance, so you can name them however you like as long as the file name ends with -extractor.js

This will give your PromptDialogs the ability to extract color entities:

const { PromptDialog } = require('botfuel-dialog');

class CarDialog extends PromptDialog {}

CarDialog.params = {
  namespace: 'car',
  entities: {
    color: {
      dim: 'color',
    },
  },
};

module.exports = CarDialog;

Note that one extractor file can define multiple entities:

const { WsExtractor } = require('botfuel-dialog');

class MultiExtractor extends WsExtractor {}

MultiExtractor.params = {
  dimensions: ['color', 'number', 'city'],
};

module.exports = MultiExtractor;

Boolean extractor

We also provide a built in way to extract booleans in Botfuel Dialog using the system:boolean dimension as follows:

const { PromptDialog } = require('botfuel-dialog');

class CarDialog extends PromptDialog {}

CarDialog.params = {
  namespace: 'car',
  entities: {
    answer: {
      dim: 'system:boolean',
    },
  },
};

module.exports = CarDialog;

Here is an example of a view displaying different bot responses depending on the user’s answer:

const { PromptView } = require('botfuel-dialog');

class CarView extends PromptView {
  renderEntities(matchedEntities) {
    const hasAnswer = !!matchedEntities.answer;
    const positiveAnswer = hasAnswer && matchedEntities.answer.values[0].value;
    const negativeAnswer = hasAnswer && !positiveAnswer;

    if (positiveAnswer) {
      return [new BotTextMessage('Great, so you said yes!')];
    }

    if (negativeAnswer) {
      return [new BotTextMessage('You said no :(')];
    }

    return [new BotTextMessage('Please answer by yes or no.')];
  }
}

module.exports = CarView;

This boolean extractor uses the following corpora:

EN:

0,false,no,ko,incorrect
1,true,yes,ok,correct

FR:

0,faux,non,ko,incorrect
1,vrai,oui,ok,correct

ES:

0,falso,no,ko,incorrecto
1,verdadero,si,ok,correcto

Should you want to use your own corpora, you will need to implement your own custom extractor. See custom entities and BooleanExtractor for reference.