Recognition results

Information about the most recent recognition process is contained in the application.lastresult$ variable. By default, the recognizer only returns a single result. However, this read-only variable is really an array of elements, where each element application.lastresult$[i] represents a possible recognition result. If no index is specified, the application.lastresult$ variable contains the properties of the 0th element in the array. In other words, application.lastresult$ is equivalent to application.lastresult$[0].

Use the maxnbest and maxinterpretations properties to increase the size of the array to return more than one result (see Enabling n-best for more information).

Each item in the application.lastresult$ array contains values for the field’s shadow variables. The Voice Browser service uses read-only shadow variables to return results from the execution of a field item other than the value stored under the name attribute.

The shadow variables available are:

  • lastresult$[i].confidence: The confidence level for this interpretation from 0.0-1.0. A value of 0.0 indicates minimum confidence, while 1.0 indicates maximum confidence.
  • lastresult$[i].utterance: The raw string of words recognized for this interpretation.
  • lastresult$[i].inputmode: For this interpretation, the mode in which user input was provided: dtmf or voice.
  • lastresult$[i].interpretation: an ECMAscript variable containing the interpretation.
  • lastresult$[i].markname: The name or evaluated nameexpr assigned to a mark that was in effect when the caller barged in. If no mark was in effect, this variable is undefined.
  • lastresult$[i].marktime: The time (in milliseconds) at which the caller barged in after the mark identified in lastresult$.markname was in effect. If no mark was in effect, this variable is undefined.
  • lastresult$[i].recording: The playable object that holds the recording of the user's last utterance from the preceding recognition. The value held in this variable can be played or submitted to another page. This variable only appears when the recorded utterance property (recordutterance) is set to true.
  • lastresult$[i].recordingsize: The size in bytes of the utterance recorded in the lastresult$[i].recording variable. This variable only appears when the recorded utterance property (recordutterance) is set to true.
  • lastresult$[i].recordingduration: The duration in milliseconds of the utterance recorded in the lastresult$[i].recording variable. This variable only appears when the recorded utterance property (recordutterance) is set to true.

Note: Each new recognition replaces the values assigned to these shadow variables. To preserve a value for use elsewhere in the application, you must assign it to a separate variable before the next recognition occurs.

When an application root document is loaded, application.lastresult$ is initiated to the value undefined. All the shadow variables are then set immediately after any recognition. The interpretations are ranked by the lastresult$[i].confidence score, from highest to lowest. Different elements in the array always differ in utterance, interpretation, or both.

For more information, see the application.lastresult$ variable. For information on using application.lastresult$ for n-best recognition processing, see Using n-best.

Since the application.lastresult$ variable is an array, it has the standard properties and methods available to ECMAScript arrays. For example, you can determine how many results appear in lastresult$ by checking the array length:

var numInterps = lastresult$.length; 

For information about the properties and methods of arrays in ECMAScript, see VoiceXML developers reference. See also the ECMA-262 standard documentation.:

Working with utterance recordings

When you enable the recordutterance property—that is, when you set it to true—the application.lastresult$ includes a recording of the user utterance, and the recordingsize and recordingduration shadow variables. The recording itself can be assigned to a variable and played back later, or posted to a server:

<!-- record-utterance enabled -->
<property name="recordutterance" value="true"/>
<!-- field that plays back the utterance and posts it to a server -->
<field name="answer1">
    …
    <filled>
        <var name="audio1" expr="answer1$.recording"/>
       <prompt>I heard <value expr="audio1"/>.</prompt>
        <data name="datapost"
            src="destination"
            namelist="audio1"
            method="post"
            syntax="e4x"
            enctype="multipart/form-data"/>
    </filled>
</field>

Remember that the variable value will change each time you record a new utterance.

For more information, seeVoiceXML developers reference. Also see the discussion of recording properties in the VoiceXML 2.1 W3C Recommendation.

Extracting the URI for an utterance recording

You can extract the URI of the recording by treating the lastresult$.recording as a string:

<!-- The URI can be extracted by treating the shadow as text; -->
<var name="uri1" expr="'' + application.lastresult$.recording"/>

Here, the string concatenation evaluated in the expr expression treats the recording as text, because of the null string ('') at the beginning of the expression.

If the diagnostic logging level is set to VD_INFO, the recording URI is written to the Voice Browser service diagnostic log. You can also write the URI to the call log explicitly:

<log expr="application.lastresult$.recording"/>

Here no casting is necessary, as the <log> element extracts the URI string automatically.

Again, be aware that the recording will not be available indefinitely if the cache cleaning mechanism is active. This means that the URI may not be useful after a certain period. If you need to preserve the recording for later use, have your application save it separately.