Export (0) Print
Expand All

Lesson 5 -- Alternatives to Inline Grammars

In this tutorial, we have been writing our own inline grammars, and we will continue to do so. When you are writing your own applications in the future, however, there are ways you can avoid the need to write your own grammars.

This lesson explores three alternatives to the inline grammars that we have been using. In addition, a method for testing these alternatives will be presented.

When the grammar you need for a VoiceXML application is very simple, you can elect to not use a grammar at all, but to use <option> elements instead.

When the grammar you need for an application is complex, you can save a lot of development time by using a grammar that has been written, tested, and debugged by someone else. One way you can do this is to use a grammar that has been built into the VoiceXML platform itself. Another way is to use a grammar written by Tellme that can be referenced by the general public.

This lesson includes the following topics:

  • The <option> element

  • Built-in grammars

  • Tellme public grammars

  • A VoiceXML application for testing grammars

You can replace very simple and unambiguous grammars with a set of <option> elements, as is discussed in http://msdn.microsoft.com/en-us/library/ff929038.aspx. When you do this, you do not need to write a grammar at all. For example, if you are asking the caller to choose one of three California destinations, you could use these three statements where you would otherwise have placed a grammar (you can test this by putting these three lines in the test application where it says "your grammar goes here").


   <option value="lax"> Los Angeles </option>
   <option value="san"> San Diego </option>
   <option value="sfo"> San Francisco </option>

The string in the value attribute is assigned to the field variable when a recognition match occurs. To see this, insert this code in the test application below and run the application. If the caller says "San Francisco", sfo is placed in the result variable.

Note Note

If the value attribute is absent the content of the <option> element (e.g., Los Angeles), stripped of leading and trailing white space, is assigned to the field variable.

DTMF can also be used (with or without voice response):


   <option dtmf="1" value="lax"> Los Angeles </option>
   <option dtmf="2" value="san"> San Diego </option>
   <option dtmf="3" value="sfo"> San Francisco </option>

Now, to return sfo, the caller can either say "San Francisco" or press 3.

It is also possible to use DTMF alone:


   <option dtmf="1" value="lax"/> 
   <option dtmf="2" value="san"/> 
   <option dtmf="3" value="sfo"/> 

Here, lax, san, or sfo is assigned to the field variable when 1, 2, or 3 is pressed.

Another variation is:

   <option dtmf="1"/> 
   <option dtmf="2"/> 
   <option dtmf="3"> 

Here, 1, 2, or 3 is assigned to the field variable.

Note Note

An accept attribute and the <enumerate> element can be used with <option> elements in the same way they are used with <choice> elements, as was discussed in Lesson 3 - Menus.

The VoiceXML 2.x specifications require that the platform provides grammars for a variety of very common situations. There are seven such "builtin" grammars: boolean, date, digits, currency, number, phone, and time. These grammars are activated by including a type attribute for the <field> element. For example,

<field name="result" type="date">

See http://msdn.microsoft.com/en-us/library/ff929046.aspx for further details.

When you use a builtin grammar, you do not need to include an inline or referenced grammar in your form.

Builtin Grammar type Attribute

Expected Utterance

Return String

boolean

Expects an affirmative or negative. DTMF digits 1 and 2 are supported for yes and no.

The result is true or false.

date

Expects a date spoken as month, day, and year by the caller. The caller can also input the date using DTMF in the following format: yyyymmdd.

The result is a fixed-length date string formatted as yyyymmdd. For example, if the caller utters June 18, 1970, the Platform returns "19700618". If a part of the date was not uttered by the caller, it is returned as question marks (?). For example, if the caller utters "June 1970", the date returned is "197006??".

digits

Expects one or more individual digits (0-9) from the caller.

The result is a string of concatenated digits. For example, if the caller utters "1" "5" "0", the Platform returns "150".

currency

Expects a currency. For DTMF input, the star key (*) acts as a decimal point. Amounts are limited to ten million dollars.

The result is a concatenated string prefixed by the standard currency indicator (http://www.id3.org/ISO_4217) if spoken by the caller. For example, if the caller utters "150 US Dollars and 95 cents", the Platform returns "USD150.95".

number

Expects a number. For example, the caller can say "one hundred and fifty", "thirty-nine point five", or "negative twenty-three". For DTMF input, the star key (*) acts as a decimal point.

The result is a string of concatenated digits, optionally including a decimal point.

phone

Valid spoken inputs include phrases that specify a phone number. For DTMF input, the star key (*) acts as an "x" and indicates an extension.

The result is a string containing a telephone number consisting of a string of digits and optionally containing the character "x" to indicate a phone number with an extension. For North America, a result could be "8005551234x789". If the field is subsequently used in <say-as> with the interpret-as value "vxml:phone", it will be spoken as a phone number appropriate to the current language.

time

Expects the time, including hours and optionally minutes, and AM or PM. The caller can input the time using DTMF in the following format: hhmm. DTMF input does not support AM or PM.

The result is a time string formatted as hhmmx where x is "a", "p", "h", or "?" to indicate AM, PM, military, or an ambiguous time. DTMF input does not support AM or PM. If, for example, the caller says "one-thirty p m", the result is "0130p." If the caller types "1300", the result is "1300h." If the caller says "one", the result is "0100".

Important note Important

You should use the test application below to explore a builtin grammar before you use it so that you can determine what it accepts for input and what it returns for each caller utterance. Only then will you know how to write an effective prompt and how to deal with the returned string. For example, if you plan to use type="date" (don't forget to change the <field> element to read <field name="result" type="date">), try various utterances and make a table like this:

Utterance

Returned String

March 15th, 1962

"19620315"

March, 1962

"19620319" (wrong!-interprets as March 19, '62)

3-15-62

"19620315"

March 15th

"????0315"

March

"????03??"

3-15

"????0315"

1962

no result or various nonsense results

You can see from the test that the builtin date grammar is quite flexible. However, it does not work for the year alone or the month and year alone, so you would not want to use it to ask a caller for the month and year (or just the year) of their birth. On the other hand, you can ask for the month and day of birth or the full birth date.

Note Note

The builtin grammars, which are in compliance with the W3C specifications for VoiceXML 2.x, are somewhat limited. The Tellme public grammars, described below, are more flexible.

Tellme has written and tested a number of frequently needed grammars that can be referenced by the general public.

Name

Description

URI

Confirm

Recognizes responses to "is that correct?"

http://grammar.svc.tellme.com/yesno/amss/v2/confirm.grxml

Confirm (DTMF)

Press 1 for yes, 2 for no.

http://grammar.svc.tellme.com/yesno/confirm-dtmf.grxml

WouldYou

Recognizes responses to "would you .....?" Example: "Would you like to make a reservation now?"

http://grammar.svc.tellme.com/yesno/mss/v1/wouldyou.grxml

AreYou

Recognizes responses to "are you.......?" Example: "Are you the named insured?"

http://grammar.svc.tellme.com/yesno/mss/v1/areyou.grxml

Dunno

Recognizes various forms of "I don’t know."

http://grammar.svc.tellme.com/dunno/mss/v1/dunno.grxml

Date

Recognizes a variety of date formats.

http://grammar.svc.tellme.com/date/mss/v1/date.grxml

Date (DTMF)

Recognizes a variety of date formats.

http://grammar.svc.tellme.com/date/date-dtmf.grxml

Birthdate

Recognizes month, day, and year.

http://grammar.svc.tellme.com/birthdate/mss/v1/birthdate.grxml

Birthdate, month and day only

Recognizes month and day only.

http://grammar.svc.tellme.com/birthdate/mss/v1/monthday.grxml

Natural numbers through 9

Recognizes spoken natural numbers 0 through 9. Other grammars that require numbers (e.g. credit card grammars) use this grammar.

http://grammar.svc.tellme.com/naturalnumber/mss/v1/natural_numbers_thru_9.grxml

Natural numbers through 99

Recognizes spoken natural numbers 0 through 99. Other grammars that require numbers (e.g. currency grammars) use this grammar.

http://grammar.svc.tellme.com/naturalnumber/mss/v1/natural_numbers_thru_99.grxml

Natural numbers through 999

Recognizes spoken natural numbers 0 through 999.

http://grammar.svc.tellme.com/naturalnumber/mss/v1/natural_numbers_thru_999.grxml

Natural numbers through 9999

Recognizes spoken natural numbers 0 through 9999.

http://grammar.svc.tellme.com/naturalnumber/mss/v1/natural_numbers_thru_9999.grxml

SSN

Recognizes a nine-digit social security number.

http://grammar.svc.tellme.com/ssn/mss/v1/ssn.grxml

SSN (DTMF)

Recognizes a nine-digit social security number.

http://grammar.svc.tellme.com/ssn/ssn-dtmf.grxml

Percentage

Recognizes integers or floating point numbers with two decimal places from zero to one hundred.

http://grammar.svc.tellme.com/percentage/mss/v1/percentage.grxml

Time

Recognizes a numeric time, am or pm, time zone, and relative time (morning, afternoon, or evening).

http://grammar.svc.tellme.com/time/mss/v1/time.grxml

Military time (24 hour clock)

Recognizes military time. Passes an h to the am_pm slot to differentiate from regular time.

http://grammar.svc.tellme.com/time/mss/v1/military_time.grxml

Phone number

Recognizes seven or ten digit U.S. telephone numbers.

http://grammar.svc.tellme.com/phonenumber/mss/v2/phonenumber.grxml

-or-

http://grammar.svc.tellme.com/phonenumber/mss/v2/phonenumber.grxml#SevenOrTenDigits

Phone number (DTMF)

Recognizes seven or ten digit U.S. telephone numbers.

http://grammar.svc.tellme.com/phonenumber/phonenumber-dtmf.grxml

Phone number, seven digits

Recognizes seven digit U.S. telephone numbers.

http://grammar.svc.tellme.com/phonenumber/mss/v2/phonenumber.grxml#SevenDigits

Phone number, seven digits (DTMF)

Recognizes seven digit U.S. telephone numbers.

http://grammar.svc.tellme.com/phonenumber/phonenumber-dtmf.grxml#SevenDigits

Phone number, ten digits

Recognizes ten digit U.S. telephone numbers.

http://grammar.svc.tellme.com/phonenumber/mss/v2/phonenumber.grxml#TenDigits

Phone number, ten digits (DTMF)

Recognizes ten digit U.S. telephone numbers.

http://grammar.svc.tellme.com/phonenumber/phonenumber-dtmf.grxml#TenDigits

Street number

Recognizes address numbers assigned to buildings.

http://grammar.svc.tellme.com/streetnumber/mss/v1/streetnumber.grxml

Apartment number

Recognizes additional identifiers, such as apartment, suite, unit, or floor numbers.

http://grammar.svc.tellme.com/apartmentnumber/mss/v1/apartmentnumber.grxml

PO Box number

Recognizes a U.S. post office box number with optional letter identifier.

http://grammar.svc.tellme.com/poboxnumber/mss/v1/poboxnumber.grxml

Zip code

Recognizes a 5-digit U.S. zip code.

http://grammar.svc.tellme.com/zipcode/mss/v1/zipcode.grxml

Zip code (DTMF)

Recognizes a 5-digit U.S. zip code.

http://grammar.svc.tellme.com/zipcode/zipcode-dtmf.grxml

Postal code (Canada)

Recognizes a 6-string alphanumeric Canadian zip code.

http://grammar.svc.tellme.com/postalcode/mss/v1/canadian_postalcode.grxml

Countries

Recognizes all countries. Recognizes both United Kingdom and Great Britain

http://grammar.svc.tellme.com/countries/mss/v1/countries.grxml

Letters in sequence

Recognizes letters of the alphabet, spoken in sequence.

Root rule recognizes an indeterminate number of letters.

Has individual rules for one, two, ......, ten letters.

Also recognizes responses such as "a as in apple, b as in boy, and so forth."

http://grammar.svc.tellme.com/lettersequence/mss/v1/manyletters.grxml

http://grammar.svc.tellme.com/lettersequence/mss/v1/manyletters.grxml#one_letter

http://grammar.svc.tellme.com/lettersequence/mss/v1/manyletters.grxml#two_letters

http://grammar.svc.tellme.com/lettersequence/mss/v1/manyletters.grxml#three_letters

http://grammar.svc.tellme.com/lettersequence/mss/v1/manyletters.grxml#four_letters

http://grammar.svc.tellme.com/lettersequence/mss/v1/manyletters.grxml#five_letters

http://grammar.svc.tellme.com/lettersequence/mss/v1/manyletters.grxml#six_letters

http://grammar.svc.tellme.com/lettersequence/mss/v1/manyletters.grxml#seven_letters

http://grammar.svc.tellme.com/lettersequence/mss/v1/manyletters.grxml#eight_letters

http://grammar.svc.tellme.com/lettersequence/mss/v1/manyletters.grxml#nine_letters

http://grammar.svc.tellme.com/lettersequence/mss/v1/manyletters.grxml#ten_letters

The following table lists typical utterances for the Tellme grammars and the strings that the grammars return for those utterances.

Name

Typical Utterance

Returned String

Confirm

sure

wrong

"yes"

"no"

Confirm (DTMF)

1

2

"yes"

"no"

WouldYou

yes, I would

no, I wouldn't

"yes"

"no"

AreYou

sure

that's not right

"yes"

"no"

Dunno

I don't know

"dunno"

Date

April 15, 2010

"day=^date=15^month=04^year=2010^special_date="

day means day of week

special_date can be last, next, tomorrow, or yesterday

Date (DTMF)

2010 4 15

"year=2010^month=04^date=15"

Birthdate

June third nineteen sixty seven

"06-03-1967"

Birthdate, month and day only

June third

"06-03"

Natural numbers through 9

zero

two

"0"

"2"

Natural numbers through 99

forty two

ninety

"42"

"90"

Natural numbers through 999

two hundred and ten

five hundred

"210"

"500"

Natural numbers through 9999

five thousand, four hundred and twelve

"5412"

SSN

five five five four four three two three two

555443232

SSN (DTMF)

5 5 5 4 4 3 2 3 2

555443232

Percentage

twelve

twelve percent

four point six six

four point nine

four point nine percent

"12"

"12"

"4.66"

"4.9"

"4.9"

Time

two fifteen pm, Pacific Standard Time

nine in the morning

"hour=2^minute=15^am_pm=pm^zone=pacific"

"hour=9^minute=00^special=morning"

Military time (24 hour clock)

fifteen hundred

nineteen hundred ten

"hour=15^minute=00^am_pm=h"

"hour=19^minute=10^am_pm=h"

Phone number

seven oh seven two two four one one seven one

two two four one one seven one

"7072241171"

"2241171"

Phone number (DTMF)

7 0 7 2 2 4 1 1 7 1

2 2 4 1 1 7 1

"7072241171"

"2241171"

Phone number, seven digits

two two four one one seven one

"2241171"

Phone number, seven digits (DTMF)

2 2 4 1 1 7 1

"2241171"

Phone number, ten digits

seven oh seven two two four one one seven one

"7072241171"

Phone number, ten digits (DTMF)

7 0 7 2 2 4 1 1 7 1

"7072241171"

Street number

thirty two forty one

four seven seven eight

"3241"

"4778"

Apartment number

apartment three A

floor two

"apartment:3A"

"floor:2"

PO Box number

thirty two A

one one four B

3496

"32A"

"114B"

"3496"

Zip code

nine four zero four one

oh one oh two five

"94041"

"01025"

Zip code (DTMF)

9 4 0 4 1

0 1 0 2 5

"94041"

"01025"

Postal code (Canada)

K one A zero B one

"K1A0B1"

Countries

Great Britain

United Kingdom

Spain

Trinidad and Tobago

great_britain

united_kingdom

spain

trinidad_and_tobago

Letters in sequence

C-l-a-i-r-e

C as in cat, l as in lemon, a as in apple, I as in igloo r as in rabbit, e as in elephant

"claire"

Important note Important

As with the builtin grammars, you should use the test application to try various utterances for grammars you plan to use, so that you can see what works and what the return values are. If you were testing the date grammar or the birth date grammar, for example, you would find that the recognizer tries to interpret your utterance as a month and day if you say only a year. For utterances of only a year, you need to use another grammar such as "natural numbers through 9999".

Here is a simple VoiceXML application that we can use to test grammars. It is especially useful with builtin and Tellme public grammars where we may not know:

  • exactly what utterances will produce a match.

  • exactly what the return values will be when there is a match.

For example, both the builtin and Tellme public grammars include a date grammar. But what must a caller say to get a match? Here are some possibilities:

  • March 27 1989

  • March 27 89

  • 3 27 1989

  • 3 27 89

  • twenty seventh of march

  • 1989

It is important to know which of these utterances will produce a match. The VoiceXML test application below will allow us to try all of these different utterances (and more) to see which ones work.

It is also important to know what the grammar returns.

The builtin date grammar returns the string "19890327".

The Tellme date grammar returns "day=^date=27^month=03^year=1989^special_date=". Here, day is the day of the week and special_date can be yesterday, tomorrow, last (as in last Wednesday), or next (as in next Tuesday).

The test application is designed to show these return values.

The test application


<?xml version="1.0"?>
<vxml version="2.1" revision="4"
xmlns="http://www.w3.org/2001/06/grammar"
xml:lang="en-US">
<link event="event.onquit">
    <grammar version="1.0" root="top">
      <rule id="top">
        <item>quit</item>
      </rule>
    </grammar>
  </link>
  <catch event="event.onquit">
    <exit/>
  </catch>
<form id="mainDialog">
<field name="result">
<prompt>please say something <break/> or say <break size="small"/>
              quit <break size="small"/> to exit.
      </prompt>
      <!-- your grammar goes here -->

      <catch event="noinput nomatch">
         Sorry. Didn't get that. Please try again.
         <reprompt/>
      </catch>

      <filled>
   <log>raw data = <value expr="result"/></log>
         Thank you.
         <clear namelist="result"/>
      </filled>
      <noinput> I didnt hear you </noinput>
      <nomatch> I didnt get that </nomatch>
</field>
</form>
</vxml>

<option> elements can be inserted where the code says "your grammar goes here."

For builtin grammars—you add a type attribute to the result field. For example: type="date" or type="currency".

Links to external Tellme grammars can be inserted where the code says "your grammar goes here." For example:

<grammar src="http://grammar.svc.tellme.com/date/mss/v1/date.grxml"/>

This short VoiceXML application has some features that have not been discussed previously:

  • <clear namelist="result"/> in the <filled> element causes the form interpretation algorithm (FIA) to revisit the result field again. Every time the result field is revisited, the application will prompt for input again. This means that the application will ask for input again after each match or each failed match, so that you can test a variety of different utterances during one phone call. The FIA and the <clear> element will be explained in Lesson 7.

  • The <link> element and the following <catch> element at the beginning of the application allow the caller to end the connection at any time by saying "quit." This is helpful because the application would continue reprompting otherwise. Of course you can also exit the application by hanging up. The <link> and <catch> elements will be covered in Lesson 10.

  • The <noinput> and <no match> elements allow the application to keep reprompting if there is either no input or there is input that does not produce a match. Both of these elements will be covered in Lesson 10.

  • The <log> element is used to write the match result (which is placed in the variable result) to the debug log so that you can see exactly what was returned for each different utterance you test.

In this lesson, we have introduced you to three alternatives to using inline grammars. Two of these alternatives (builtin and Tellme grammars) involve grammars that were created by someone other than yourself.

Lesson 6, which follows, covers some important odds and ends that you need to know about. The following subjects are included:

  • Form items

  • Properties

  • Confirmation prompting

Many of the builtin and Tellme grammars return strings with concatenated sets of digits. You often need to use JavaScript to retrieve the information you want from these strings. If you are not familiar with JavaScript, and regular expressions in particular, you may want to read Appendix B -- Processing Grammar Returns.

Appendix B shows you how to extract the information you want from the strings that are returned from builtin and Tellme grammars. For example, a date grammar might return the string "19960428" representing April 28, 1996. How do you isolate the year, month, and day from this string? Appendix B will show you how to do it.

Community Additions

ADD
Show:
© 2014 Microsoft