Share via


SALT bind Element Example

  Microsoft Speech Technologies Homepage

The following example demonstrates binding a part of a recognition result to an HTML page element.

The example uses the multiple mode recognizer. Click Start to begin. To end the session, click Stop. For a successful recognition, say the sentence displayed on the screen, choosing one of three cities from each group. The results of speech are displayed in the text area. The bind element is used in the two text boxes labeled From City and To City. These values are retrieved from an utterance and placed automatically into the appropriate text box. The two bind elements within the listen element declare the appropriate bindings.

To run this example, copy the following sample, paste it into a document, and save it with a .htm file extension. Then create a grammar file. Information about how to create the grammar file is found immediately following the code sample. When both files are created, open the .htm file with a browser to see how it works.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns:salt="http://www.saltforum.org/2002/SALT">
<head>
  <title>bind Element Example</title>
</head>
<body>
  <!-- declare the object on the page-->
  <object id="SpeechTags" CLASSID="clsid:DCF68E5B-84A1-4047-98A4-0A72276D19CC" VIEWASTEXT></object>
  <?import namespace="salt" implementation="#SpeechTags" />

  <salt:listen id="listen1" mode="multiple" onreco="Handleonreco()" onnoreco="Handleonnoreco()" 
  onsilence="Handleonsilence()" onerror="Handleonerror()">
    <salt:grammar name="travelGrammar">
      <grammar xmlns:sapi="https://schemas.microsoft.com/Speech/2002/06/SRGSExtensions" lang="en-US"
      tag-format="semantics-ms/1.0" version="1.0" root="toplevel" mode="voice" xmlns="http://www.w3.org/2001/06/grammar">
        <rule id="toplevel" scope="private">
          <ruleref uri="#request" type="application/srgs+xml"/>
          <item>to</item>
          <ruleref uri="#travel" type="application/srgs+xml"/>
          <item>from</item>
          <ruleref uri="#fromCity" type="application/srgs+xml"/>
          <tag>$.fromCity=$$.value</tag>
          <item>to</item>
          <ruleref uri="#toCity" type="application/srgs+xml"/>
          <tag>$.toCity=$$.value</tag>
        </rule>
        <rule id="request" scope="public">
          <one-of>
            <item>I would like</item>
            <item>I'd like</item>
            <item>I want</item>
          </one-of>
        </rule>        
        <rule id="travel" scope="private">
          <one-of>
            <item>fly</item>
            <item>travel</item>
            <item>go</item>
          </one-of>
        </rule>
        <rule id="fromCity" scope="public">
          <one-of>
            <item>Boston</item>
            <item>Houston</item>
            <item>Dallas</item>
          </one-of>
          <tag>$.value = $recognized.text</tag>
        </rule>
        <rule id="toCity" scope="public">
          <one-of>
            <item>Seattle</item>
            <item>New York City</item>
            <item>Salt Lake City</item>
          </one-of>
          <tag>$.value = $recognized.text</tag>
        </rule>
       </grammar>
    </salt:grammar>  
    
  <salt:bind targetelement="boxFromCity" targetattribute="value" value="//fromCity"/>
  <salt:bind targetelement="boxToCity"  targetattribute="value" value="//toCity"/>
  </salt:listen>
    
  <h3>bind Element Example</h3>
    
  <p>This example uses a multimodal <span class="element">listen</span> element.
  Click <b>Start</b> to begin; <b>Stop</b> to finish the speaking session.</p>

  <p>Say "I'd like to travel from [Boston | Houston | Dallas] to [Seattle | New York | Salt Lake City]." 
  <p>You may select one city from each city group.</p>

  <table cellspacing="4" cols="2">
    <tr>
      <td>Reco Status:</td> 
      <td><input type="text" size="90" name="boxStatus" value="(Recognition status goes here)"
      id="boxStatus"/></td>
    </tr>
    <tr>
      <td>Reco Text:  </td><td><input type="text" size="90" name="boxReco" 
      value="(Recognized text goes here)"      id="boxReco"/></td> 
    </tr>
    <tr>
      <td>From City:</td> 
      <td><input type="text" size="90" name="boxFromCity" value="(Departure city goes here)"
      id="boxFromCity"/></td>
    </tr>
    <tr>
      <td>To City: </td>
      <td><input type="text" size="90" name="boxToCity" value="(Arrival city goes here)" id="boxToCity"/></td>
    </tr>
  </table>

  <br>
  Semantic Markup Language (SML) Result:<br>
  <TEXTAREA cols="80" rows="10" ID=txtComments>(Semantic Markup Language (SML) results are displayed here)
  </TEXTAREA></p>
    
  <input type="button" name="BtnStart" value="Start" onclick="Start()" id="BtnStart"/>

  <input type="button" name="BtnStart" value="Stop" onclick="Stop()" id="BtnStop"/>

  <script language="JScript">

    function Start() {
      boxStatus.value = "(Listening...)";
      boxReco.value = "(Waiting response)";
      txtComments.value = "(Waiting response)";
      listen1.Start();
    }

    function Stop() {
      listen1.Stop();
      boxStatus.value = listen1.status;
    }

    function Handleonreco() {
      boxStatus.value = "onreco fired " + listen1.status;
      boxReco.value = event.srcElement.text;
      txtComments.value = event.srcElement.recoresult.xml
    }   

    function Handleonnoreco() {
      boxStatus.value = "onnoreco fired " + listen1.status;
      boxReco.value = ""
      txtComments.value = ""
      boxFromCity.value = ""
      boxToCity.value = ""
    }

    function Handleonsilence() {
      boxStatus.value = "onsilence fired " + listen1.status;
      boxReco.value = ""
      txtComments.value = "";
      boxFromCity.value = ""
      boxToCity.value = ""
    }

    function Handleonerror() {
      boxStatus.value = "onerror fired " + listen1.status;
      boxReco.value = "";
      txtComments.value = "";
      boxFromCity.value = ""
      boxToCity.value = ""
    }
  </script>
</body>
</html>

If the sentence, "I'd like to travel from Boston to Seattle." is spoken after speech recognition has begun, the following Semantic Markup Language (SML) is generated.

<SML confidence="1.000" unknownConfidence="yes" text="I'd like to travel from Boston to Seattle"
utteranceConfidence="1.000">
  <fromCity confidence="1.000" unknownConfidence="yes">Boston</fromCity>
  <toCity confidence="1.000" unknownConfidence="yes">Seattle</toCity>
</SML>

There may be slight differences in the confidence level due to differences in individual speech.

See Also

bind Element