Skip to main content

$parseTree

This object contains the result of parsing the request in accordance with existing named patterns, as well as return values of converters.

$parseTree is a hierarchical structure, with each level of embedding corresponding to its token, beginning from the root one.

A single level of $parseTree looks like this:

{
tag:
pattern:
text:
words:
value:
TokenByName: <$parseTree> // embedded tokens
...
}

Here:

  • tag — is the name of the token as it appears in $parseTree;
  • pattern — is the name of the named pattern which has matched some text in the request;
  • text — is the string representation of the request;
  • words — is an array of tokens;
  • value — is an attribute present when using mappings, converters, and named entities;
  • TokenByName — is a named pattern.

How to use

  • Let us construct a parse tree for the following phrase: What is the weather like in New York?

    The following named patterns will be used:

    $Question = (what|which)
    $Weather = (weather|forecast)
    $City = (New York [City]|Seattle)
    q: * [$Question] * $Weather * $City *

    Then the parse tree will look as follows:

    {
    "tag": "root",
    "pattern": "root",
    "text": "What is the weather like in New York",
    "words": [
    "what",
    "is",
    "the",
    "weather",
    "like",
    "in",
    "new",
    "york"
    ],
    "Question": [
    {
    "tag": "Question",
    "pattern": "Question",
    "text": "What",
    "words": [
    "what"
    ]
    }
    ],
    "Weather": [
    {
    "tag": "Weather",
    "pattern": "Weather",
    "text": "weather",
    "words": [
    "weather"
    ]
    }
    ],
    "City": [
    {
    "tag": "City",
    "pattern": "City",
    "text": "New York",
    "words": [
    "new",
    "york"
    ]
    }
    ],
    "_Question": "What",
    "_Weather": "weather",
    "_City": "New York"
    }
  • Now let us construct a parse tree for the phrase two plus two. The phrase is fragmented in accordance with existing named patterns. Since the structure is recursive, the fragments may be embedded to an arbitrary level, as in the following example:

    $Digit = $regexp<\d+>
    $Numeral = (one:1|two:2)
    $Number = ($Digit|$Numeral)
    $Operation = (plus:+|minus:-)
    q: * $Number::Number1 $Operation $Number::Number2 *

    The parse tree will look like this:

    {
    "tag": "root",
    "pattern": "root",
    "text": "two plus two",
    "words": [
    "two",
    "plus",
    "two"
    ],
    "Number1": [
    {
    "tag": "Number1",
    "pattern": "Number",
    "text": "two",
    "words": [
    "two"
    ],
    "Numeral": [
    {
    "tag": "Numeral",
    "pattern": "Numeral",
    "text": "two",
    "words": [
    "two"
    ],
    "value": "2"
    }
    ]
    }
    ],
    "Operation": [
    {
    "tag": "Operation",
    "pattern": "Operation",
    "text": "plus",
    "words": [
    "plus"
    ],
    "value": "+"
    }
    ],
    "Number2": [
    {
    "tag": "Number2",
    "pattern": "Number",
    "text": "two",
    "words": [
    "two"
    ],
    "Numeral": [
    {
    "tag": "Numeral",
    "pattern": "Numeral",
    "text": "two",
    "words": [
    "two"
    ],
    "value": "2"
    }
    ]
    }
    ],
    "_Number1": "2",
    "_Operation": "+",
    "_Number2": "2"
    }