Condividi tramite


Recognition Mode: Single Example

  Microsoft Speech Technologies Homepage

The following example demonstrates a single-mode recognition. Click and hold the Start button and say any one of the cities listed. The recognition result status is listed in the first text box, named Recognition Status. The results of the recognition displays in the second text box, named Recognition Text. The Semantic Markup Language (SML) recognition result displays in the large text box. For best results, continue to hold the button for a second or two after speaking.

The following sample uses single-mode recognition. As a result, the Start and Stop methods must be called explicitly. This sample represents a tap-and-talk situation. The customer could be using a Pocket PC, which requires a speak button to be pressed for the duration of the speech. If any errors are reported, the Recognition Status textbox displays a value that indicates the type of error that occurred.

This example also uses an inline grammar to define the city names. Alternatively, an external XML grammar file could have been specified.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns:salt="http://www.saltforum.org/2002/SALT">
  <head>
    <object id="Speechtags" CLASSID="clsid:DCF68E5B-84A1-4047-98A4-0A72276D19CC" VIEWASTEXT></object>
  </head>
    
  <body>
    <?import namespace="salt" implementation="#Speechtags" />
    <salt:listen id="listen1" mode="single" reject=".65" babbletimeout="5000" 
    onreco="Handleonreco()" onnoreco="Handleonnoreco()">
      <salt:grammar name="cities"> 
        <grammar version="1.0" tag-format="semantics-ms/1.0" lang="en-US" 
        xmlns="http://www.w3.org/2001/06/grammar" root="cities">
          <rule id="cities">
            <one-of>
              <item>Seattle</item> 
              <item>Boston</item> 
              <item>Houston</item> 
              <item>New York</item> 
            </one-of>
          </rule>
        </grammar>
      </salt:grammar>
     </salt:listen>

     <input type="button" name="BtnListen" value="Start Listening" OnClick="StartListening()" id="StartListen"/>
     <input type="button" name="BtnStop" value="Stop" OnClick="StopListening()" id="StopListen"/>
    
     <br><br>
       This is an example of an single mode listen.<br>
       Click Start to begin; after speaking the name of one of the valid cities, click Stop to end listening.<br>
       Valid city names for recognition are Seattle, Boston, Houston, and New York.<br><br>

    Speech Action:<br>      
    <input type="text" name="Action" size = "80" value="Listening for test" id="Action"/><br><br>
      
    Recognition Status:<br> 
    <input type="text" size="80" name="RecoStatus" value="(recognition status goes here)" id="RecoStatus"/>
    
    <br><br>
    Recognition Text:<br> 
    <input type="text" size="80" name="RecoText" value="(recognition text goes here)" id="RecoText"/>
        
    <br><br>
    Semantic Markup Language (SML) Result:<br>
    <TEXTAREA cols="80" rows="10" ID=TextComments>(Semantic Markup Language (SML) results are displayed here)
    </TEXTAREA><br><br>

    
    <script language="JScript">
    <!-- 
      var recoFlag = 0;
      function StartListening() {
        Action.value  = "Starting to listen...";
        recoFlag = 0;
        RecoStatus.value = "";
        RecoText.value="";
        TextComments.value="";
        listen1.Start();
        Action.value  = "Listening...";
      }
      
      function StopListening() {
        Action.value = "Stopped listening";
        listen1.Stop();
      }
      
      function Handleonreco() {
        Action.value = "Received onreco.";
        recoFlag = 1;
        smlResult = event.srcElement.recoresult;
        RecoStatus.value = listen1.status;        
        RecoText.value = smlResult.text;
        TextComments.value= smlResult.xml
      }
      
      function Handleonnoreco() {
          Action.value = "Received onnoreco";
          RecoStatus.value = listen1.status;
          TextComments.value = "";          
          if (recoFlag == 1) {
            RecoText.value = "No further speech was recognized.";
          }
          else 
            RecoText.value = "No speech was recognized.";
      }
    -->
    </script>
  </body>

See Also

listen Element | Recognition Mode: Automatic Example | Recognition Mode: Multiple Example