Grammar sub-package

Fundamental element classes

Dragonfly grammars are built up out of a small set of fundamental building blocks. These building blocks are implemented by the following element classes:

  • ElementBase – the base class from which all other element classes are derived
  • Sequence – sequence of child elements which must all match in the order given
  • Alternative – list of possibilities of which only one will be matched
  • Optional – wrapper around a child element which makes the child element optional
  • Repetition – repetition of a child element
  • Literal – literal word which must be said exactly by the speaker as given
  • RuleRef – reference to a dragonfly.all.Rule object; this element allows a rule to include (i.e. reference) another rule
  • ListRef – reference to a dragonfly.all.List object

The following element classes are built up out of the fundamental classes listed above:

  • Dictation – free-form dictation; this element matches any words the speaker says, and includes facilities for formatting the spoken words with correct spacing and capitalization
  • DictListRef – reference to a dragonfly.all.DictList object; this element is similar to the dragonfly.all.ListRef element, except that it returns the value associated with the spoken words instead of the spoken words themselves

Class reference

class Alternative(children=(), name=None)

Element class representing several child elements of which only one will match.

Constructor arguments:
  • children (iterable, default: ()) – the child elements of this element
  • name (str, default: None) – the name of this element

For a recognition to match, at least one of the child elements must match the recognition. The first matching child is used. Child elements are searched in the order they are given in the children constructor argument.

value(node)
The value of an Alternative is the value of its child that matched the recognition.
class ElementBase(name=None)

Base class for all other element classes.

Constructor argument:
  • name (str, default: None) – the name of this element; can be used when interpreting complex recognition for retrieving elements by name.
children
Iterable of child elements. (Read-only)
decode(state)
Attempt to decode the recognition stored in the given state.
dependencies()

Returns an iterable containing the dependencies of this element and of this element’s children.

The dependencies are the objects that are necessary for this element. These include lists and other rules.

element_tree_string()
Returns a formatted multi-line string representing this element and its children.
gstring()

Returns a formatted grammar string of the contents of this element and its children.

The grammar string is of a format similar to that used by Natlink to define its grammars.

value(node)

Determine the semantic value of this element given the recognition results stored in the node.

Argument:
  • node – a dragonfly.grammar.state.Node instance representing this element within the recognition parse tree

The default behavior of this method is to return an iterable containing the recognized words matched by this element (i.e. node.words()).

class Optional(child, name=None)

Element class representing an optional child element.

Constructor arguments:
  • child (ElementBase) – the child element of this element
  • name (str, default: None) – the name of this element

Recognitions always match this element. If the child element does match the recognition, then that result is used. Otherwise, this element itself does match but the child is not processed.

value(node)
The value of a Optional is the value of its child, if the child did match the recognition. Otherwise the value is None.
class Repetition(child, min=1, max=None, name=None)

Element class representing a repetition of one child element.

Constructor arguments:
  • child (ElementBase) – the child element of this element
  • min (int, default: 1) – the minimum number of times that the child element must be recognized; may be 0
  • max (int, default: None) – the maximum number of times that the child element must be recognized; if None, the child element must be recognized exactly min times (i.e. max = min + 1)
  • name (str, default: None) – the name of this element

For a recognition to match, at least one of the child elements must match the recognition. The first matching child is used. Child elements are searched in the order they are given in the children constructor argument.

get_repetitions(node)

Returns a list containing the nodes associated with each repetition of this element’s child element.

Argument:
  • node (Node) – the parse tree node associated with this repetition element; necessary for searching for child elements within the parse tree
value(node)

The value of a Repetition is a list containing the values of its child.

The length of this list is equal to the number of times that the child element was recognized.

class Sequence(children=(), name=None)

Element class representing a sequence of child elements which must all match a recognition in the correct order.

Constructor arguments:
  • children (iterable, default: ()) – the child elements of this element
  • name (str, default: None) – the name of this element

For a recognition to match, all child elements must match the recognition in the order that they were given in the children constructor argument.

value(node)
The value of a Sequence is a list containing the values of each of its children.

Table Of Contents

Previous topic

Object model

Next topic

Engines sub-package

This Page

Quick search