javascriptwebkitspeechrecognition

Javascript: Value cannot pass through a condition although it is true


When I'm doing some stuff, I figure out a weird problem. I try to make a voice recognition. The first part of the condition works correctly but it does not work for the second part.

var v;

function test() {
  var ses = new webkitSpeechRecognition();
  ses.lang = "En"
  ses.continuous = true;
  ses.start();
  ses.onresult = function(event)
    if (event.results.length > 0) {
      sonuc = event.results[event.results.length - 1];
      document.querySelector("input").value = sonuc[0].transcript;
      v = sonuc[0].transcript;
      console.log(v + " 1");

      if (v == "good") {
        switchLED('on');
        console.log(v + "2");
        console.log("OK !");
        console.log("LED ok");
      }

      if (v == "bad") {
        switchLED('off');
        console.log(v + "2");
        console.log("OK !");
        console.log("LED off");
      }
    }
  }

For the first time, I said: good and the results were:

good 1 
good 2
OK ! 
LED OK

This was correct.

Then, for the second time if I say: bad the result is:

bad 1

and it did not go through the condition if(v == "bad").

It is only happened when bad is the second word I said. If I refresh the page and said "bad" directly on the first time, I will get all the correct results.


Solution

  • According to Mozzila's documentation:

    For continuous recognition, leading or trailing whitespace will be included where necessary so that concatenation of consecutive SpeechRecognitionResults produces a proper transcript of the session.

    This matches exactly what you described. Since your speech recognition session is continuous (as seen in ses.continuous = true) what happens is that when you say any word for the first time, the transcriptor will get the word correctly. But when it recognises the next word, a leading whitespace will be added. You probably miscopied your console output, which must've been like this:

    good 1 
    good 2
    OK ! 
    LED OK
    
     bad 1 
    ^ leading space here
    

    Indeed, " bad" is not equal to "bad". This is why the string didn't pass the condition. To solve this, just make sure to trim your string:

    var v;
    
    function test() {
      var ses = new webkitSpeechRecognition();
      ses.lang = "En"
      ses.continuous = true;
      ses.start();
      ses.onresult = function(event)
        if (event.results.length > 0) {
          sonuc = event.results[event.results.length - 1];
          document.querySelector("input").value = sonuc[0].transcript;
          v = sonuc[0].transcript.trim(); // <-- trim here
          console.log(v + " 1");
    
          if (v == "good") {
            switchLED('on');
            console.log(v + "2");
            console.log("OK !");
            console.log("LED ok");
          }
    
          if (v == "bad") {
            switchLED('off');
            console.log(v + "2");
            console.log("OK !");
            console.log("LED off");
          }
        }
      }
    

    Then your code should work as expected. Hope it helps!