Documentation

Powered by Algolia

Patterns

Simple patterns

Pattern is a string representing possible user voice input or Alan voice response. They can be used in the intent, follow, question, play or reply functions.

intent('how is the weather today', p => {
    p.play('It is great!');
});

Patterns with alternatives

Patterns may contain alternatives. This means that any of those words and their combinations might be used by the user to give the command. Such functionality is achieved by separating the words or even whole phrases by the delimiter | and enclosing such sets of alternatives into the brackets ().

Let's use previous example and add some variety on what the user can ask and what he will receive in response.

intent('(how is|what is) the weather today', p => {
    p.play('(It is|Feels) great!');
});

Now the user can use commands how is the weather today and what is the weather today. In response he will hear It is great! or Feels great! picked at random.

Multiple alternative sets

There might be any number of alternatives in each set and any number of such sets in the single pattern. Let's add multiple alternative sets to the command defined in previous example and see how it will affect total number of possible user-given commands and received responses.

intent('(how is|what is) the (weather|climate) today', p => {
    p.play('(It is|Feels) (great|awesome)!');
});

Now the user can use commands how is the weather today, how is the climate today, what is the weather today and what is the climate today. In response he will hear It is great!, It is awesome!, Feels great! or Feels awesome! picked at random.

Optional alternatives

There is also a way to define alternative to be optional. For that you will be adding another delimeter | at the end of the alternative set. This will signal Alan that some particular command might be used both with and without the words from the given alternative set. Same logic applies to responses that Alan will give back to the user. Let's add optional alternatives to the command defined in previous example and see how it will affect total number of possible user-given commands and received responses.

intent('(how is|what is) the (weather|climate) (today|)', p => {
    p.play('(It is|Feels|) (great|awesome)!');
});

We've made word today optional, so now user can ask how is the weather or how is the weather today, both commands would be valid. Same goes for response, now the first set of alternatives is considered to be optional and you will hear the full responses like It is great! or Feels great! as well as short responses like great! or awesome!.

Multiple patterns

In Alan platform you have the ability to define multiple patterns for each command (intent, follow, question) or response (play, reply) function.

To do so you need to pass them to the desired function as comma separated strings. As you are adding more and more multiple alternative sets and optional alternatives you will run into a problem when some pattern combinations make no sense. Or you might want to allow users to give some commands in a different way/manner. In such case you will need to use multiple patterns:

intent("(how is|what is) the (weather|climate) (today|)", "today's forecast", p => {
    p.play("(It is|Feels|) (great|awesome)!","Rainy, windy, and cold. (A complete mess!|)");
});

Such multiple patterns also may be passed as an array. All of the command (intent, follow, question) and reply functions on Alan Platform support this functionality.

let weatherRequests = [
    "(how is|what is) the (weather|climate) (today|)",
    "today's forecast"
];

let weatherResponses = [
    "(It is|Feels|) (great|awesome)!",
    "Rainy, windy, and cold. (A complete mess!|)"
];

intent(weatherRequests,
    reply(weatherResponses));

Take a note that p.play can be used to send commands and therefore arrays of any kind are considered to be a command. Use comma separated patterns instead.

Patterns with slots

Patterns that are used in command (intent, follow, question) or response (play, reply) functions can contain a wide set of pre- and user-defined slots. For a breakdown of all available pre- and user-defined slots as well as a step-by-step guide on how to use them - refer to Slots section of Alan Platform documentation.

// User-defined slots
intent('Do you know anything about $(TOPIC physics|mathematics|chemistry)', p => {
    p.play('Yes. I do know ' + p.TOPIC.value + '. (Ask me anything about it!|)')
});

// Predefined slot
intent('How much is $(NUMBER) plus $(NUMBER)', p => {
    let result = p.NUMBERs[0].number + p.NUMBERs[1].number
    p.play(`It is ${result}`);
});

Wildcard for plural form

Patterns that are used in command (intent, follow, question) functions can contain special character _ at the end of nouns. This means the noun is expected in both single and plural form. It is used when it is not important either noun is in singular or plural form. Such nouns with the wildcard should be placed in brackets (). Slots can also be pluralized, in this case .value of the slot will contain the noun in the form that user has used when he gave a command.

intent('I want $(NUMBER) (cup_) of tea', p => {
    p.play('As you wish');
});

intent('Add $(NUMBER) $(S student_)', p => {
    p.play(`Done. Added ${p.NUMBER.value} ${p.S.value}`);
});