Rule Expansions

Rule expansions are built by combining together small phrases with a number of grammar operations. The operations are:

Operation Example Description
Alternatives $rule = $A | $B; match either rule A or rule B
Optional Expansion $rule = $A [$B]; match rule A, optionally followed by rule B
Repetition $rule = $A <7>; match rule A seven times

Any two rule references not separated by an operator are treated as a logical "and." So $rule = $A $B; is matched if rule $A is matched followed by rule $B..

Rule Alternatives

As we saw in the previous yes/no grammar, the Speech Engine can be told to accept one or more possibilities by using the rule alternative operator, the pipe symbol (the | character).

$toppings = pepperoni | sausage | green peppers;

The above rule is matched by one of the phrases "pepperoni," "sausage," or "green peppers."

Note that the rule alternative operator is greedy. It collects "peppers" with "green" to form the alternative "green peppers." If you wish to scope the effects of the rule alternative operator, you can use parentheses:

$pizza = (pepperoni | sausage) pizza;

This rule matches "pepperoni pizza" or "sausage pizza." Without the parentheses, it would match "pepperoni" or "sausage pizza."

Optional Expansion

If you wish to make a portion of a rule expansion optional, you can wrap that portion of the expansion in the optional operator, square brackets (the [ ] characters).

$yes = yes [please];

This rule matches "yes" or "yes please."

Any of the ABNF operators may be wrapped inside each other, or used in sequence, to create more and more expressive sentences.

$yes = yes [please | thank you];

This rule matches "yes," "yes please," or "yes thank you."

Repetition

If you wish to allow a portion of a rule expansion to be repeated a number of times, you can use the repeat operator via angle brackets (the < > characters). Inside the angle brackets, enter the number of times you would like to repeat the preceding portion of a rule. The repeat operator can be used to specify a fixed number of repetitions, or a range of repetitions.

Example

$digit = one | two | three | four | five | six | seven | eight | nine | zero;
$seven_digits = $digit <7>;
$seven_to_ten_digits = $digit <7-10>;
$one_or_more_digit = $digit <1->;

The $seven_digits rule allows any seven digit combination to be recognized. The $seven_to_ten_digits rule allows any seven to ten digit combination to be recognized. By specifying the range 1- (the equivalent of writing 1-n times), the $one_or_more_digit rule allows one or more digits to be recognized.

The repeat operator is tightly binding; it only applies to whatever immediately precedes it. Use parentheses to control how much of a rule expansion it applies to.

Example

$oh_boy1 = oh boy <3>;
$oh_boy2 = (oh boy)<3>;

The rule $oh_boy1 matches "oh boy boy boy." $oh_boy2 matches "oh boy oh boy oh boy."

You should be very careful when using the repeat operator, especially when allowing for an infinite number of repetitions in combination with rule references. It is possible to build recursive grammars that have very large numbers of valid parses, and when the Engine attempts to decode utterances against these kinds of grammars it can wreak havoc on your applications.

Once you are comfortable with rule expansions, move on to Rule References to continue the tutorial.

© 2012 LumenVox LLC. All rights reserved.