This site is no longer updated.Go to new Conversational Cloud docs

Advanced pattern elements


$pattern_name

$pattern-name references a named pattern.

Learn more about named patterns

How to use in a script

Pattern declaration:

patterns:
    $thanks = (thank* [$you]|thx|great|hurray)
    $ok = (okey|okay|o key|ok)
    $you = (you|u)

Pattern usage in a script:

state: Thanks
    q!: *  $thanks
    q!: * $ok *
    q!: * everything is clear *
    q!: i see [$thanks] *
    script:
        $reactions.answer("You are welcome.\nDo you have other questions?")

~lemma

~lemma checks all word forms.

For example, the ~make pattern will match the words: make, makes, making, and made.

The rule is applied to all word forms of all homonyms: words that are spelled and pronounced alike but different in meaning or grammatical forms.

For example, the ~base pattern will match the noun and verb word forms (base, bases and based, basing).

Using this element may lead to false positive matches. It happens because of language morphological diversity.

How to use in a script

state: Delivery
    q!: * {(order/deliver/delivery) * [food|~dinner] * [$cafe]} * $City *
    q: * $City *
    script:
        if (!$session.address) {
            $session.address = {};
        }
        $session.address.city = $parseTree.City[0].value.name;
    go!: ../../Delivery

$morph

$morph<part of speech and/or grammeme> checks grammatical properties of a word.

For example, for the $morph<NOUN sg> pattern: NOUN — a noun, sg — in the singular.

You can specify one or several properties.

Morphological parsing is accomplished with the АОТ library.

How to use in a script

theme: /Bank Information
    state: Bank Information
        q!: * $morph<NOUN> * [our/your/this] (bank) *
        q!: * (~call) [our/your/this] (bank) *
        go: /

It is not necessary to enclose one word in brackets ().


$regexp/regexp_i

$regexp/regexp_i<literals and metacharacters> is a regular expression — a pattern matching a set of strings. A pattern consists of literals and metacharacters — characters with special, not literal meaning. $regexp or $regexp_i catches all strings matching the pattern.

$regexp is sensitive to letter case, $regexp_i is case-insensitive.

The regular expression syntax is the same as the one used in Java.

How to use in a script

Declaration of a pattern to match any word in the request:

patterns:
    $anyWord = $regexp<.+>

Declaration of a pattern to match percentages:

patterns:
    $procent = $regexp<\b([0-9][0-9]?%?)\b|\b100%?\b>

$entity

$entity<named entity> converts a named entity into a pattern.

Learn more about named entities

How to use in a script

Named pattern declaration:

$roamingRegion = $entity<RoamingRegions> || converter = RoamingRegionTagConverter

How to use in a script:

state: Problems
    q!: * {$problems * roaming} *
    q!: * {$someProblems * $roamingRegion} *
    script:
        $temp.messageForAgent = 'The client has some problems with roaming';
    a: Please, wait a moment. I will call a specialist to answer your question.
    go!: /TransferToAgent

Here, RoamingRegions is a dictionary name, RoamingRegionTagConverter is a converter name.

When declaring a pattern in such a way, $parseTree gets the value property which is usually used to hold a dictionary identifier or value.

The $entity rule writes only the entity identifier into value. The list of associated values is stored in the dictionary.


$Pattern::Alias

$Pattern::Alias allows you to specify an alias for the token, under which the token will be placed in $parseTree.

How to use in a script

Consider the $Number::Hour example: the $Number pattern will be interpreted as Hour in parseTree.

Script:

q!: I’ll come at $Number::Hour

User request:

I’ll come at 7

Parse tree:

{
    "tag": "root",
    "pattern": "root",
    "text": "I’ll come at 7",
    "words": [
        "i",
        "’ll",
        "come",
        "at",
        "7"
    ],
    "Hour": [
        {
            "tag": "Hour",
            "pattern": "Number",
            "text": "7",
            "words": [
                "7"
            ]
         }
    ],
    "_Hour": "7",
}

The parse tree without using an alias for the q!: I’ll come at $Number script is:

{
    "tag": "root",
    "pattern": "root",
    "text": "I’ll come at 7",
    "words": [
        "i",
        "’ll",
        "come",
        "at",
        "7"
    ],
    "Number": [
        {
            "tag": "Number",
            "pattern": "Number",
            "text": "7",
            "words": [
                 "7"
            ]
        }
    ],
    "_Number": "7",
}

An example of $Pattern::Alias usage:

state:
    q!: $Number::minuend minus $Number::subtrahend
    q!: subtract $Number::subtrahend from $Number::minuend
    a: {{ $parseTree._minuend - $parseTree._subtrahend }}

Here, the meaning of $Number depends on the position in the query: either the first or the second number can be a subtrahend.


(one:1 | two:2 …​)

(one:1 | two:2 …​) is a mapping of different semantics. It allows you to set a value for a particular phrase. This value is written in the value property of $parseTree for the pattern that has been used to declare the mapping.

How to use in a script

Pattern declaration:

$price = ((free|zero|0):0|(seven|7):7|(two hundreds|200):200) [dollars]

Script:

q!: {activate service ([for] $price)}

User request:

Activate free service

Parse tree:

{
    "tag": "root",
    "pattern": "root",
    "text": "Activate free service",
    "words": [
        "activate",
        "free",
        "service"
    ],
    "price": [
        {
            "tag": "price",
            "pattern": "price",
            "text": "free",
            "words": [
                "free"
            ],
            "value": "0"
        }
    ]
}

$repeat

$repeat<named pattern> is a nested pattern that can be repeated in a text for an unlimited number of times.

You can use only named patterns. Otherwise, you will get the Repeat can contain only named pattern like $repeat<$Number> error.

How to use in a script

patterns:
    $color = (red/white/blue/green/yellow/black)

theme: /

    state: asd
        q!: my favorite colour* (is/are) $repeat<$color>
        if: $parseTree.color.length > 1
            a: Wow! You like {{ $parseTree.color.length }} colours
        else:
            a: Why {{ $parseTree._color }}?

$oneWord

$oneWord is any word, number, or character.

This named pattern is available in any project without declaration.

How to use in a script

state: Dialog
    q!: * $you * not ~understand * !
    q!: [$oneWord] is not what i [have] ~mean
    go!: /CatchAll/CatchALLState

$nonEmptyGarbage

$nonEmptyGarbage is arbitrary text.

It differs from the * pattern in that the text must contain at least one character and it cannot be a punctuation mark.

This named pattern is available in any project without declaration.

How to use in a script

$Text = * $nonEmptyGarbage * || converter = $converters.textConverter
state: Action
                q: $nonEmptyGarbage
                go!: /NextStep