ECMAScript

watermelonroachdaleInternet and Web Development

Jul 30, 2012 (5 years and 1 month ago)

264 views

VoiceXML:


Events, Errors, and ECMAScript

Acknowledgements


Prof. Mctear, Natural Language Processing,
http://www.infj.ulst.ac.uk/nlp/index.html
, University of
Ulster.

Events

1.
Plain events


happen normally, involve special
event handler

2.
Error events
-

unexpected situations, may cause
application to terminate

Plain events

Error events

cancel, help, exit (user)

error.badfetch

noinput, nomatch (user input)

error.semantic

connection.disconnect.hangup

error.noauthorization

maxspeechtimeout

error.unsupported.builtin

Note: how events are handled is platform
-
dependent

Default catch handlers

Event Type

Action

cancel

don’t reprompt

error

exit interpreter

exit

exit interpreter

help

reprompt

noinput

reprompt

nomatch

reprompt

maxspeechtimeout

reprompt

connection.disconnect

exit interpreter

all others

exit interpreter

noinput, nomatch

<noinput>


System does not detect any speech.

This event is thrown when the timeout interval is
exceeded (in IBM WebSphere Voice Toolkit the
default

timeout interval is 500 milliseconds).


<nomatch>

The user’s input does not match the recognition grammar.

The default behaviour in the IBM WebSphere Voice
Toolkit for this event is to output a message and then
re
-
prompt.


These (and other) default event handlers can be modified.

Properties such as “timeout” can also be modified.

<catch>

<catch event = "nomatch">






<prompt>

I did not understand what you said. Please
answer the question again. </prompt>

</catch>


<catch event = "nomatch noinput help">







<prompt>


I did not understand what you said. Please
answer the question again. </prompt>



<reprompt/>



<!
--


Present the original prompt message again to the
user


--
>

</catch>


<catch event = "nomatch"
count = "2"
>




<!
--


count different occurrences of an event


--
>

Shorthand <catch> elements

Some common events, such as noinput, nomatch, help, and
error have shorthand versions, e.g.



<nomatch>



I did not understand, please try again.

</nomatch>



<noinput>



I didn’t hear anything. Please speak a little louder.


<noinput>


With counters: <nomatch count = “1”>

<throw>

Used to define application
-
specific events e.g. user has
failed 3 times to enter valid id


<catch event = “nomatch" count = "1“>

That is not a valid i d

</catch>



<catch event = "nomatch" count = "4“>

<throw event = "error.invalid_password"/>

</catch>


<catch event = "error.invalid_password" >

<prompt> Please check your i d. Goodbye </prompt>


<exit/>

</catch>


Event handlers and scope


Event handlers can be defined at different scope levels


Scope levels form a hierarchy.



Event handlers defined in a higher scope level are
inherited at lower scope levels within the hierarchy.




A local event handler overrides or replaces an inherited
event handler.

If a local event handler has the same
name as an inherited event handler, then the local event
handler replaces the inherited event handler.


The VoiceXML interpreter examines the following
containers in the order listed to select an event handler:


1.
Field

2.
Dialog

3.
Document

4.
Application

5.
Session


Tutorial Exercises: Error handling

Aim: to explore events such as <noinput> and <nomatch> and
modify their default behaviours


1.
Run the file
‘studentsystem1b.vxml’
and test how it
behaves when a) You do not say anything; b) You say
something that is not in the specified recognition
vocabulary.

2.
Modify the default behaviours for <noinput> and
<nomatch> using counters for alternative behaviours.

3.
Define an application specific event using <throw> that
causes the application to terminate with some form of
explanation following an event such as repeated failure to
elicit valid input.


The event could be a third failure to elicit the user’s spoken
input, with an event thrown such as “error.no_input”. This
event can be caught, causing the system to transfer to a
human operator. Include a prompt that explains what is
happening. If transfer is not possible, then use <exit /> or
<disconnect/> to cause the application to terminate.

What is ECMAScript?


Used as the scripting language of VoiceXML for client
-
side processing, e.g. to validate user input (without
requiring access to the server), or to dynamically
generate values.


VoiceXML variables are completely equivalent to
ECMAScript variables.


The “expr” and the “cond” attributes reference
ECMAScript expressions.


Escaping characters to conform to the rules of XML e.g.

<if cond = “age &gt; 21” >


Or use the <script> element e.g.

<SCRIPT>

<![CDATA[

... ECMAScript code ...
]]>

</SCRIPT>

http://www.ecma
-
international.org/publications/standards/Ecma
-
262.htm


ECMAScript Example

<form id="form">

<block>

<var name="hours"/>

<var name="minutes"/>

<var name="seconds"/>


<script>

<![CDATA[

var now=new Date();

hours=now.getHours();

minutes=now.getMinutes();

seconds=now.getSeconds();

]]>

</script>


<prompt>

The current time is

<value expr="hours"/>

hours,

<value expr
="minutes"/> minutes,

and <value expr

="seconds"/> seconds.

</prompt>



</block>


</form>


More on the date object

The Date object stores a year, month, day, date, hour,
minute, and second.





Methods of the Date object include :



getSeconds( ) returns the seconds in Date


get Minutes( ) returns the minutes in Date


getHours( ) returns the hours in Date


getDay( ) returns the day of the week in Date beginning
at 0


getDate( ) returns the day of the month in Date


getMonth( ) returns the month in Date beginning at 0


getFullYear( ) returns the year as a 4 digit string


Example with <if>: Calculating variable interest

<form id="calculate_interest">

<var name = “interest"/>

<var name="interest_calculated" />

<field name="amount“ type=“number”>

<prompt>What is the amount in your account? </prompt>

<filled>

<script>


<![CDATA[


if (amount <= 1000) {interest = 0.02}


if (amount > 1000 && amount <= 10000) {interest = 0.03}


if (amount > 10000) {interest = 0.04}


interest_calculated = interest * amount;


]]>

</script>

<prompt> the interest on your balance is <value
expr="interest_calculated"/> </prompt>

</filled>

</field>

</form>

Tutorial Exercise: ECMAScript

Using the file ‘interest.vxml’ as a basis, create a file called
‘commission.vxml’ that elicits the selling price of a
property and then calculates the commission fee
based on the following rules:


1.

if (amount <= 90000) {fee = 0.05}

2.

if (amount > 90000 && amount <= 200000) {fee =
0.04}

3.

if (amount > 200000) {fee = 0.03}


Universal Commands and Navigation

Overview


Using a root document for global commands and
storing information


Navigation within and between documents


The <subdialog> element


Exercises:


Using a root document


Creating a subdialog


Root document


Simple applications may consist of one document.


Larger applications may consist of several documents,
one of which is the root document, and the others are
child documents of the root document.


The root document can contain
event handlers,
grammars, variables, scripts and other constructs in the
scope of the application which are inherited by the child
documents.


These elements are active throughout the entire
application, unless otherwise specified.


The child documents must reference the root document
using the “application” attribute of the <vxml> element,
e.g.

<
vxml version=“2.0"

application="root.vxml">

(assuming root document is called “root.vxml”: Note: the
root document can have any arbitrary name)

root.vxml

<!
--

header information
--
>


<var name=“user_pin"/>




<link next="main.vxml" >

<grammar type="application/srgs+xml" root="main" version="1.0"
mode="voice">

<rule id="main" scope="public">

<one
-
of>

<item> main menu </item>

<item> start over </item>

</one
-
of>

</rule>

</grammar>

</link>


</vxml>

Variable stored with application scope

-

can be referenced by leaf documents

Link with grammar of words available

throughout application and

specification of where to go next

logon.vxml

<field name="pin" type="digits" >

<prompt>what is your four digit pin</prompt>

</field>


<block>

<assign name="application.user_pin" expr="pin" />

<goto next="main.vxml" />

</block>


</form>

</vxml>

Assigns value to the

variable ‘user_pin’ which

Is stored with application

scope in ‘root.vxml’

Retrieving the value


The value stored as an application level variable can be
retrieved as follows:


<prompt version="1.0">

Your pin is

<say
-
as interpret
-
as="vxml:digits">

<value expr="application.user_pin"/> </say
-
as>



The prompt demonstrates that the value of ‘user
-
pin’ is
available to each leaf document that references
‘root.vxml’

More on Navigation

Transition to other forms etc when the FIA has processed
a form (dialog)



<choice>
-

Used within the
<menu>

element to select and
transition to another dialog.


<goto>
-

Used within
<block>, <catch>, <error>, <filled>,
<foreach>, <help>, <if>, <noinput>, <nomatch>,
and

<prompt>



<link>
-

Used within
<field>, <form>, <initial>
, and
<vxml>



<submit>
Used within
<block>, <catch>, <error>, <filled>,
<foreach>, <help>, <if>, <noinput>, <nomatch>,

and

<prompt>




Attributes for navigation

Specifying the target item:



next
-

A URI reference e.g.



next="http://myexamples.com/student_system“

(absolute
reference)


next=“students.vxml“

(relative e.g. in same directory)


next=“students.vxml #first_query
" (specifies dialog within
the document)


nextitem

-

The name of an input item within the same form e.g.
nextitem = “student_name”



expr
-

A

URI reference that is dynamically determined by
evaluating the ECMAScript expression e.g.


expr=“student_system' + '#' + ‘first_query'“


expritem

-

The name of an input item within the same form

<goto> <submit>


<goto>


transition to another form item within current form, another form
within current document, or another document


transitioning to another dialog or document using <goto> will
cause the old dialog’s variables to be lost. If the variables are to
persist across multiple documents, they should be stored with
application scope




<submit>


used to submit values to the document server via an HTTP GET
or POST request


“namelist” attribute specifies which values are being submitted
e.g.

<submit
next=“http:www.myserver.com/getstudentdetails.jsp”
namelist = “studentname coursename” />


Control will not necessarily return to the calling document. For
example, the script specified in the “next” attribute might
generate a VoiceXML document dynamically and execution will
continue with this document.

<exit>


<exit>


terminates all loaded documents and returns control
to the voice browser which determines what to do
next


Attributes:


expr

ECMAScript expression that is evaluated as
the return value


namelist

List of variable names to be returned


<exit> example


<filled>

<submit namelist = “source target amount" />


<exit/>


</filled>




<
disconnect>

Causes the voice browser to disconnect


<disconnect> example


<filled>

<submit namelist = “source target amount" />


<disconnect/>


</filled>





Note:
Before ending the session, developers can catch
the
<disconnect>

event to perform some post
-
disconnect processing such as submitting any
remaining data to a Web server.



<transfer> (1)


Enables the user to connect via the telephone to another
voice application on another speech server.


Also enables the user to connect to a person's
telephone. Frequently this will be an operator or help
agent that assists a user having trouble.


blind transfer


the caller can converse only with the party on the
other end of the connection.


bridge transfer


the caller may converse with another party and also
hear prompts and respond by speaking or pressing
the keys on a touchtone telephone.


This enables the
caller to proactively end the transferred call.



<transfer> (2)


When the transfer is complete, the connection is broken.




Once the transfer disconnects, the instructions in the
<filled>

element associated with the
<transfer>

element are
interpreted.





The input
-
form
-
item variable may contain values including
the following which indicate why the transfer was not
successful:


busy
-

The other person was busy.


noanswer
-

The other person did not answer.


network_busy
-

An intermediate network was not able to
transfer the call.



Various shadow variables contain information about a
successful transfer e.g..


duration of the call transfer in seconds.




mode used by the caller to terminate the transferred call
(voice or DTMF

<subdialog>


Enables frequently used code to be saved in a library
and re
-
used in several applications


When invoked, a subdialog executes in a new
execution environment; it does not inherit any
parameters from the calling dialog.


The subdialog
proceeds until it encounters:



<return> element

Ends the execution of a subdialog
and returns control and data to the calling dialog.

Data is
returned to the calling dialog as a namelist.

When the
subdialog returns, its execution environment is deleted.


<exit>

element

Execution stops


No form items remain eligible for the FIA to select
-

Execution stops

3.
<form id="main_menu">

4. <block>

5. Welcome to the Student
System Main Menu

6 </block>

7 <subdialog name="result"
src="
#validation
">

8 </subdialog>

9 <block>

10 <prompt>

11 hello <value expr =
"
result.username
" />

12 </prompt>

13 </block>

14 </form>

15 <!
--

subdialog
--
>

16 <form id="
validation
">

17 <field name="
username
">

18 <grammar>


19 liz | margaret | mike |
guest

20 </grammar>

21 <prompt> Please say
your user name.
</prompt>

22 </field>

23 <filled>

24 <return
namelist="
username
" />

25 </filled>

26 </form>


<subdialog> 1

Go to subdialog to elicit name which is returned

to main dialog for greeting

studentsystem3a.vxml

<form id="main_menu">

<block>Welcome </block>

<field name=“studentname“>

<grammar> john | david |
rosemary | jennifer
</grammar>

<prompt> Please say your
user name </prompt

</field>




<subdialog name="result"
src="#
validation
">


<param name = “
username
"
expr = “studentname" />

</subdialog>

</form>

<!
--

subdialog
--
>

<form id="
validation
">

<var name = “
username
" />


<block>

<prompt>

hello <value expr =

username
" />

</prompt>



<return />


</block>


</form>





<subdialog> 2

Pass value into subdialog for validation

studentsystem3b.vxml

<form id="main_menu">

<block>Welcome </block>

<field name=“studentname“>

… elicit name …


</field>

<subdialog name="result"
src="#validation">

<param name = “studentname"
expr = “username" />


</subdialog>

<block>

<prompt> hello <value expr =
"result.studentname" />

the student number is <value
expr = "result.studentid" />


</prompt></block>

</form>

<!
--

subdialog
--
>

<form id="validation">

<var name = “studentname" />

<var name = "studentid"
expr=“‘‘" />

<block>

<if cond= "name=='john'">


<assign name = "studentid"
expr = "'96050918'" />


</if>


<return namelist = "studentid"
/>

</block>


</form>


<subdialog> 3

Pass values in both directions: elicit value and pass to

subdialog for validation, return to main dialog with result

studentsystem3c.vxml

Tutorial Exercises: Using a root
document

1.
Assuming that you have separate documents for the
main elements of the Student System
-

student details,
course details, view student details, reports, and a
main menu document
-

create a root document with
links to these documents. Provide a link in the root
document that will enable the user to navigate to the
main menu document from any other document in the
application.

2.
Create a variable with application scope in the root
document to hold the user's pin. Elicit a value for the
user's pin in a file such as logon.vxml, store the value
in the root document and access it from some other
child document.

Tutorial Exercises: Subdialog (1)

3.
Passing values back from a subdialog following some
computation within the subdialog



Load and run the file studentsystem3a.vxml. You can
say one of the student names (john, david, rosemary or
jennifer) in response to the system prompt.


4.
Passing values into a subdialog to be referenced in that
subdialog



Load and run the file studentsystem3b.vxml. As before,
you can say one of the student names (John, David,
Rosemary, Jennifer) in response to the system prompt.

Tutorial Exercises: Subdialog (1)

5.
Passing values in both directions



Load and run the file studentsystem3c.vxml. This time you
have to say the student name 'John' for the conditional
statement in the subdialog to evaluate correctly.


6.

Add further conditions in the subdialog for the names 'David'
and 'Rosemary'.


Amend the code in the main dialog so that if the user says
the name 'Jennifer', the system responds 'Sorry that student
is not listed in the database'.



Note: Names that are not in the database will still have to be
included in the recognition vocabulary to be recognised and
passed on to the subdialog.