Skip to main content

Advanced pattern elements

$pattern_name

$pattern-name references a named pattern.

How to use

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. The word after the tilde should be in its dictionary form.

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).

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

How to use

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

The $morph<feature> element matches against a word with specific grammatical features. The list of supported features depends on the bot engine used by the project.

botEngine: v1

Deprecated

The first version of the bot engine uses the AOT parser for morphological analysis. Use the tags for English and Russian to denote grammatical features. They should be put inside angle brackets and separated with spaces.

In this version, $morph also allows matching against one or several grammatical features at once. For example, $morph<NOUN sg> (a singular noun) will match against words like cat, bank, or condition.

botEngine: v2

The second version of the bot engine only allows the $morph element to match against parts of speech. Other grammatical features are not supported.

The format for part-of-speech tags depends on the tokenizer engine used by the project. In most cases, tags intended for one engine will not work properly for another.

TokenizerLink to format description
kaznlpkaznlp
morphsruspymorphy2
mystemmystem
pinyinThis engine does not support part-of-speech tagging.
spacyUniversal Dependencies
udpipeUniversal Dependencies

How to use

Usually, the $morph element is employed to find matches against specific word combinations. They can be hard to pin down with a simple enumeration of all their constituent words or word stems.

patterns:
# A pattern for street names like “Oxford street”
$street = $morph<PROPN> (st/str/street)

theme: /Jokes

state: Address
q!: * I live on * $street *
a: Why, really! And I live in the Server Quarter.

$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.

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

How to use

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

patterns:
$anyWord = $regexp<.+>

Declaration of a pattern to match percentages:

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

$entity

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

How to use

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.

caution
When declaring a pattern in such a way, $parseTree gets the value property which is usually used to hold a dictionary identifier or value.
tip
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

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

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.

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

How to use

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

theme: /

state: asd
q!: my favorite color* (is/are) $repeat<$color>
if: $parseTree.color.length > 1
a: Wow! You like {{ $parseTree.color.length }} colors
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

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

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