VoiceXML 2.1 Development Guide Home  |  Frameset Home

  I: GSL Grammars  |  TOC  |  GSL Hints  

GSL Basics

Note: GSL syntax is not considered to be a W3C-compliant syntax for grammars, and the Nuance has discontinued support for GSL grammars in their most recent product offerings. Voxeo will continue to support GSL-specific markup for some time to come, but it is strongly suggested that new applications and their associated grammars leverage the SRGS + SISR grammar syntaxes instead of being reliant upon the deprecated GSL grammar format.

The Prophecy voice browser only supports SRGS/XML, GSL and JSGF grammar formats. Any other grammar types are not supported and will throw an  'error.unsupported.format'.  Please refer to the Nuance Grammar Guide to learn more. Grammars may either be defined in an external file, or inside the vxml code itself, although any non-parseable characters will need to be enclosed in a CDATA block in order for the application to run properly. A good point to keep in mind is that the Nuance interpreter has hard-coded limits of up to 30 seconds as well as 50 words per utterance. These values were chosen as realistic "real world" upper bounds for speech recognition applications.

Also be aware of the fact that Prophecy absolutely requires that the grammar 'type' attribute be set for all grammars, be they inline or externally located. The Prophecy browser accepts both the 'text/gsl', (Nuance format), and 'application/x-gsl', (Prophecy format), values. Therefore it is suggested that you follow these rules when constructing a GSL grammar on any of the Voxeo VoiceXML platforms to ensure that your code is as compatible as possible:

<grammar src="MyGrammarFile.gsl#MYRULENAME" type="text/gsl"/>


When constructing a grammar, also remember that utterances may not contain any capital letters, else a grammar error will occur. The grammar interpretation, however, may contain capital letters. A breakdown of a simple external grammar is as follows:



  MYRULE [
    [ cheddar ]  { <F_1  "Cheese"> }
  ]


Markup Explanation
MYRULE (Top Level Rulename) This is the sub-file that the grammar src references
[cheddar] (User Utterance) The expected phrase from the caller that a grammar will get a valid match upon
F_1 (Slot Name) The field or slot in the application that the return result will be sent to upon a match
"cheese" (Returned Result) A user-defined value to be returned upon a successful recognition



Grammar File Size

The maximum allowable size for a GSL grammar on the VoiceXML platforms is around 1 megabyte. However, grammars of this size will often have load-time and recognition issues which can be alleviated by constructing them as smaller individual files and/or by writing them as subgrammar constructs, or even precompiling them as Nuance Grammar Objects, where applicable.  In addition, any recognition problems encountered by such a large grammar file, can also use NBest post-processing to ensure that the correct user utterances are retained.


DTMF Grammars

DTMF grammars follow the same rules for spoken word grammars. The best practice for writing your application is to ignore the long-since deprecated <dtmf> element and define your DTMF grammars within the <grammar> element in order to be more cross-platform portable.


Rulenames and Rulesets

External grammars must always have a top level rulename defined both in the grammar file, and within the src attribute, as demonstrated below:

<grammar src="MyGrammarFile.gsl#MYRULENAME" type="text/gsl"/>

Inline subgrammars must also have a top level rulename and be referenced by this in the 'src' attribute, (see below). Rulenames must also start with a capital letter; while it is good coding practice to have your rulenames in all capitals, it is not absolutely necessary.


Capturing Digits

Grammars for but a few digits in length is usually pretty accurate, but as the string length gets larger, the margin for error is greater. This is particularly evident when using a 'phone number' type grammar, as most folks are used to inserting pauses when relaying such a value to anyone or anything. Therefore, you may also wish to increase the 'incompletetimeout' value, as detailed in the Voxeo Property Guide.


Commented Text

Comments within gsl grammars are inserted by prefixing the text with the semi-colon character ';'.

MYRULE

;Some Comment

  [
    ( doctor strangelove ) ]  { <F_1  "Kubrick"> }
  ]



Grammar Operators


Several GSL grammar operators exist that we can take advantage of when constructing a grammar file:



Note: Adding an 'or' operator within a disjunction will always result in a a compilation error:


  MYRULE [
    [ ?new car ] { <CarShopping "NewCar"> }
  ]



Recursive Grammars

Traditional GSL Grammar recursion is currently not supported on the Prophecy platform:


;GSL2.0
SENT
[
  ( see spot run  )
  ( SENT and SENT )
]



If your application requires the need for grammar recursion, it is recommended that you use the SRGS formatted grammars, which allow you to structure a grammar in this manner much easier, and cleaner.


Grammar Scoping

Grammars can be given explicit scoping instructions via the 'scope' attribute. A grammar declared as 'document' scope will be considered active throughout the document, while a grammar declared as 'dialog' will only be active while in that particular form.


Grammars that have been given a scope other than 'dialog' must have a slotname defined and a <filled> handler in order to process correctly. The best practice for using an application scoped grammar is to utilize the <link> tag in the application root document. Links, unlike grammars, cannot declare a scope, and are always considered to be document/application scoped.


Grammar Scoping Example


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE vxml PUBLIC "-//W3C//DTD VOICEXML 2.1//EN" "http://www.w3.org/TR/2007/REC-voicexml21-20070619/vxml.dtd">

<vxml version="2.1">
  <!-- Document level grammar result for the result form -->
  <var name="doc_result" />

  <form id="frm_main">
    <!-- this is a document level grammar -->
    <grammar type="text/gsl" scope="document">
      <![CDATA[
        [
          ( [one  dtmf-1] )  { <doc "one">  }
          ( [two  dtmf-2] )  { <doc "two">  }
          ( [exit dtmf-0] )  { <doc "term"> }
        ]
      ]]>
    </grammar>

    <!-- Our document level grammar has been filled
          send the user to the next form -->
    <filled namelist="doc">
      <assign name="doc_result" expr="doc" />
      <goto next="#frm_doc_filled" />
    </filled>

    <!-- Get some input for the local grammar field -->
    <field name="field1" >
      <!-- This is a local grammar -->
      <grammar type="text/gsl">
        <![CDATA[
          [
            ( [three dtmf-3] )  { <field1 "three"> }
            ( [four  dtmf-4] )  { <field1 "four">  }
          ]
        ]]>
      </grammar>

      <!-- This is the main menu prompt -->
      <prompt>
        This is field 1
        Press one or two for the document level grammar to answer
        Enter three or four for the dialog or local level grammar to answer
        If you press zero the document level grammar will say goodbye and hangup
      </prompt>
    </field>

    <!-- our local grammar has been filled -->
    <filled namelist="field1">
      <prompt>
        You said <value expr="field1" />
        <break time="1000ms" />
      </prompt>

      <!-- Loop Back to main -->
      <goto next="#frm_main" />
    </filled>

    <!-- Set up an empty field to grab the document level grammar data -->
    <field name="doc" />
  </form>


  <!-- Create a new form for the filled document scope grammar -->
  <form id="frm_doc_filled">
    <block>
      <prompt>
        This is the document level grammar answering
        You said <value expr="doc_result" />
      </prompt>

      <!-- Check to see if we got a 0 for hangup -->
      <if cond="doc_result == 'term'">
        <prompt>
          Goodbye
        </prompt>
        <!-- Exit the application -->
        <exit />
      <else />
        <prompt>
          Going back to the main form
        </prompt>
        <goto next="#frm_main" />
      </if>
    </block>
  </form>
</vxml>


Download the Code!

  Download the grammar scoping source here!



Inline GSL Grammars

A grammar file, as previously mentioned, does not need to be defined externally, as we also have the option of nesting it within the VoiceXML file itself. We can do a simple inline grammar which defines both the utterance and the return value like this:

MyInlineGrammar_1.vxml

<?xml version="1.0"?>

<vxml version="2.0">
  <form>
    <field name="F_1">
      [(cheddar)  (?monteray jack) (+stinky french cheese) ]

      <prompt>
        what is your favorite cheese?
      </prompt>

      <filled>
        <prompt>
          You said <value expr="F_1"/>
        </prompt>
      </filled>
    </field>

  </form>
</vxml>


But, if we want to define explicit slot values inline, we will need to escape any offensive characters in a CDATA block, so that our grammar compiles properly:


MyInlineGrammar_2.vxml

<?xml version="1.0"?>

<vxml version="2.0">
  <form>
    <field name="F_1">
      <grammar type="text/gsl">
        <![CDATA[
          MYRULE
          [
            ( cheddar              )  { <F_1  "cheddar cheese"> }
            ( ?monteray jack        )  { <F_1  "jack cheese">    }
            ( +stinky french cheese )  { <F_1  "stinky cheese">  }
          ]
        ]]>
      </grammar>


      <prompt>
        what is your favorite cheese?
      </prompt>

      <filled>
        <prompt>
          You said <value expr="F_1$.interpretation.F_1"/>
        </prompt>
      </filled>
    </field>

  </form>
</vxml>



More References

Oh, needing more information on GSL markup, are we? Look no further. The Nuance Grammar Guide contains all kinds of additional info that can help you out when you want more details on GSL.


External GSL Grammars


External GSL grammars are almost exactly the same as an inline GSL grammar.  The main difference is that an external grammar is in a different file!  As you will see, both inline and external grammars are syntactically identical except for the exclusion of the CDATA tag.

Here are a few benefits of using external grammars


An external GSL grammar file example

This is an example of how you could write your external grammar file:


; External GSL Grammar
; External GSL grammars are almost exactly the same as an inline grammar,
;  just in a different file and no "CDATA" tag! :-P


;Define our rule
MYRULE
[
  ; user says at least: "cheddar"
  ( cheddar ?cheese          ) { <type_cheese "cheddar cheese"> }

  ; user says at least: "jack"
  ( ?monteray jack ?cheese  ) { <type_cheese "jack cheese">    }

  ; user says at least: "stinky french"
  ;  and/or cheese
  ( +stinky french ?cheese  ) { <type_cheese "stinky cheese">  }
]



How to implement your external grammar

Once your external GSL grammar prototype is complete and you are ready to finally implement it into your code, you would once again use ye olde <grammar> tag.  The main difference between an inline <grammar> and an external <grammar> tag is that you must declare your top level rule name.  For example:

<grammar src="external_grammar.gsl#MYRULE" type="text/gsl" />

As you can see, the top level rule "MYRULE" from our external grammar is defined within our 'src' attribute.  Pretty easy eh?


External GSL Grammar source code in action

And finally, here is a complete Voice XML application using an external GSL grammar.  Notice how the code below is a lot cleaner without the grammar code in the VoiceXML source.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE vxml PUBLIC "-//W3C//DTD VOICEXML 2.1//EN" "http://www.w3.org/TR/2007/REC-voicexml21-20070619/vxml.dtd">

<vxml version="2.1"
  xmlns="http://www.w3.org/2001/vxml"
  xmlns:voxeo="http://community.voxeo.com/xmlns/vxml">

  <form id="frm_main">
    <field name="f_cheese" slot="type_cheese">
      <!-- Define our external grammar -->
      <grammar src="external_grammar.gsl" type="text/gsl" />

      <!-- prompt the user to say their favorite type of cheese -->
      <prompt>
        Hi there!
        i am curious
        What is your favorite type of cheese?
      </prompt>


      <!-- the user did not match one of the cheeses in the grammar -->
      <catch event="nomatch noinput">
        <prompt>
          i'm sorry, i don't know what kind of cheese that is
          <reprompt />
        </prompt>
      </catch>

      <!-- a cheese was recognized! -->
      <filled>
        <prompt>
          That's amazing!
          <value expr="lastresult$.interpretation.type_cheese" /> is my favorite type of cheese too!
          <break time="300ms" />
          i'll talk to you soon
          bye!
        </prompt>
        <exit />
      </filled>
    </field>
  </form>
</vxml>



Download the Code!

And for all of you that would like to download all of the source code, AND logs from this tutorial running on Prophecy, click the download link.

  Download The Source!





  ANNOTATIONS: EXISTING POSTS
eosmann
11/5/2004 1:46 PM (EST)
The link at the top of this page that says "Nuance Grammar Guide" and opens the page http://community.voxeo.com/docs/grammar/grammar-gsl.pdf doesn't work!  Did it move?
steve.sax
11/5/2004 2:01 PM (EST)
Hi Eric,

The GSL grammar guide didn't move, but it did take a short vacation. However, I have called it, and asked it to get back on the job. Wisecracks aside, if you try the link again, it should come up for you, assuming you have a PDF reader.

Steve Sax
dodgly
1/31/2005 11:09 AM (EST)
It seems the PDF link on the page that was fixed back in November is once again dead.

  http://community.voxeo.com/vxml/docs/nuance20/grammar.pdf
steve.sax
1/31/2005 1:36 PM (EST)
Sorry about that folks. The Nuance grammar guide link has been fixed, for your viewing enjoyment..


Steve Sax
jeckhouse
10/23/2005 9:49 PM (EDT)
Looks to be broken again.
MattHenry
10/24/2005 12:15 AM (EDT)


Hello Justin,

If you are referring to the link above at:

http://community.voxeo.com/vxml/docs/nuance20/grammar.pdf


I am not having any problems reaching this PDF guide....please advise.

~Matt

gregzh
5/11/2006 12:25 AM (EDT)
Hi,

I am little lost here about planing grammar, so post this basic question.

There is a target sentence, something like "Your room number is 5503", my program need to fetch two kind of things here, One is "room number", the other is "5503".

HOw to prepare the grammar.

Thanks.
greg




MattHenry
5/11/2006 1:05 PM (EDT)

Hello Greg,


This is a pretty broad question for me to address within the confines of a doc annotation, but I'll do my best. For any complex utterance values you wish to accept in your application, you will likely want to design a multi-rule subgrammar using either GSL or SRGS grammar formats.

As this is a complex task, I very strongly advise that you start off with the following:

- review our GSL subgrammar tutorial
- review all documentation in this section dealing with GSL subgrammars
- review all grXML/SRGS subgrammar documentation
- download and review the 'grammar.zip' file from the below URL, and use if for reference:

http://community.voxeo.com/library/grammar/library.jsp


Best of luck!

~Matthew Henry
hemanthvasa
6/23/2006 6:31 AM (EDT)
Hi
I am new to this grammars i don't have much experience on this can u please tell me how to build grammar for the following sentence
"Always ask my Pin"

Thanks
HemanthVasa
sidvoxeo
6/23/2006 10:38 AM (EDT)
Hi Hemanth,
There are a lot of ways to write grammars,both GSL and GRXML grammars.
If you are using inline GSL grammar it would be some thing like
<grammar type="text/gsl">[WHAT EVER GRAMMAR YOU WANT TO BUILT]</grammar>

If you are using external grammar it could be some thing like this
<grammar src="digits.gsl#MAIN"/> where digit.gsl is an external gsl grammar and MAIN is the toplevel of the grammar.

If you look through the documentation , there are lot of examples to help you with this.
Thanks
Sid
Marquet
10/30/2006 8:10 AM (EST)
Hi,
I'm a beginner in voicexml and one of the questions i have is:
I suppose that when using your browser, the speaker will speak the words in english. What do i have to do if I want the speaker to understand that  he has to speak spanish? Or even more, what do i have to do if I want to talk in spanish and be understood by the application?
I mean, is there any option in the Motorola browser that allows me to  work using other languages? Maybe it's impossible and I must use another one, do you know any of them?
Thanks and excuse my english, si'l vous plait.

Marc
MattHenry
10/30/2006 11:35 AM (EST)


Hi there,

If you want to recognize a foreign language utterance, then you will have to set the ASR to use a recognition engine to the appropriate locale. At present, our US customers only have the option for English, and Spanish, (so you are in luck!).

You can use the following code sample below, which is annotated for your convenience to start developing a Spanish application. Note that this is available only for the Voicecenter 5.5 release, and is *not* supported in the Prophecy/Voicecenter 7.0 platform:


<?xml version="1.0" encoding="UTF-8"?>

<!-- ****************************************** -->
<!-- xml:lang attribute invokes the Spanish ASR -->
<!-- ****************************************** -->
<vxml version="2.0" xmlns="http://www.w3.org/2001/vxml" xmlns:voxeo="http://community.voxeo.com/xmlns/vxml" xml:lang="es-MX">


<!-- ******************************* -->
<!-- sets the rhetorical spanish TTS -->
<!-- ******************************* -->
<property name="nuance.core.tts.ResourceName" value="es-MX.F023" />

  <form id="F1">

    <field name="F_1" type="digits?length=3">
      <prompt bargein="true">
        hola senor.
      </prompt>
      <filled>
        <value expr="F_1"/>
          <log expr="'**********INTERP*************' + F_1$.interpretation.F_1" />
          <log expr="'**********UTTERANCE*************' + F_1$.utterance" />
          <log expr="'**********CONFIDENCE*************' + F_1$.confidence" />
          <log expr="'**********INPUTMODE*************' + F_1$.inputmode" />
      </filled>
    </field>
  </form>


</vxml> 


Regards,

~Matthew henry
SSA_42Entertainment
12/14/2007 2:17 PM (EST)
Hi, I have a very simple inline grammar:

<grammar type="text/gsl">
        [ (one) (two) (three) (four) (five) (six) (seven) (eight) (nine) (ten) ]
      </grammar>

The goal is to have the user say a number between one and ten. However numbers like eleven, fourteen, etc. are matched as well. How come?
VoxeoDante
12/14/2007 4:28 PM (EST)
Hello 42Entertainment,

Do you have a field with a type="digits" in your application?

A field type="digits" will invoke the builtin digits grammar.  This builtin grammar will allow for a wide range of numbers, including anything up to 16 digits long.

If this is not the case, I would like to see the xml for the document exhibiting this behavior.  If you do not want to post the code here in a public forum, please feel free to open a private account ticket, and reference this thread in that ticket.

If there is something I am missing here, or if there is anything else I can do, please let me know.

All the best,
Dante Vitulano
Voxeo Corporation

login
  I: GSL Grammars  |  TOC  |  GSL Hints  

© 2013 Voxeo Corporation  |  Voxeo IVR  |  VoiceXML & CCXML IVR Developer Site