IBM Cloud Docs
Example grammars

Example grammars

The following examples describe more complex uses for grammars with speech recognition. Most examples are provided in both ABNF (application/srgs) and XML (application/srgs+xml) formats. Use these examples to help you get started with grammars.

In a production rule for a grammar, including a \ (backslash) is a syntax error in ABNF and is interpreted as a literal value in XML.

For more information about the languages and models that support grammars and their level of support (generally available or beta), see Language support for customization.

Confirmation grammars

Confirmation grammars are useful for applications that expect a one-word answer in response to a question. The following grammars define a list of possible yes and no responses.

List grammars

List grammars are useful for applications that expect the user to select an option from a predefined set of strings. The set is sometimes called a flat list. It typically consists of a list of terminal elements and does not include a complicated rule structure.

  • The following grammars define a list of phrases for digits:

  • The following grammar defines a list of valid names from which the user can choose, perhaps to select an individual from a telephone directory:

Vehicle identification number grammars

Vehicle identification numbers (VINs) use a fixed alphanumeric format, as do credit card numbers, telephone numbers, and US social security numbers. A great advantage of grammars over classical n-grams is that grammars are very effective for specifying such formats.

The VIN format is well standardized and has a fixed number of characters. Classic n-grams perform poorly for these types of tasks. Unlike grammars, they cannot ensure that the required number of characters is provided.

The following grammars recognize Honda VIN codes. They are more complex than the previous examples but demonstrate the power of grammars nicely.

For more information about the VIN format, see Vehicle identification number.

Grammars with optional elements

By making certain elements of a response optional, you can make grammars more flexible by anticipating how users might respond. The following grammar adds square brackets around the element $optionalphrase to make it optional. The user can speak one of a few additional phrases before stating the social security number. For instance, the user can say "my social is xxx xx xxxx" or just "xxx xx xxxx".

For more information about optional expansions in grammars, see Section 2.5 Repeats of the Speech Recognition Grammar Specification.

Disambiguation grammars

Other possible example grammars include situations in which the confidence of the recognized response is not very high but the application needs to be certain of the user's response. In this case, the application can dynamically create a grammar that includes the most likely responses and ask the user to repeat the answer. For example, the application can create a grammar that includes the two most likely options and ask the user to select one of the two: Did you mean 'option 1' or 'option 9'?

Disambiguation grammars are programmatically generated. No examples are provided.