Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Grammar with Web Speech API

Yes, There are questions like this one but they're too old...Now grammar is fully supported in chrome.link: https://developer.mozilla.org/en-US/docs/Web/API/SpeechRecognition/grammars

I'm using Web Speech API to implement Speech Recognition. The grammars property of the SpeechRecognition interface returns and sets a collection of SpeechGrammar objects that represent the grammars that will be understood by the current SpeechRecognition. I'm using the last update of chrome and 'they' say grammar is supported in chrome.

Most of the times when I say "Who are you" Web Speech Recognized it as "How are you". How I can set the sentence "Who are you", for example,to possible inputs?

I tried it a couple of hours, pulling my hair without any success....

Here is the code :

  var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
  var SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList;
  var SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent;

  var recognition = new SpeechRecognition();
  var speechRecognitionList = new SpeechGrammarList();
  recognition.grammars = speechRecognitionList;
  recognition.lang = 'en-GB';
  recognition.interimResults = false;
  recognition.maxAlternatives = 1;

  recognition.start();

  recognition.onresult = function(event) {

    var speechResult = event.results[0][0].transcript;
 // We get speechResult to do something with it here

    console.log('Confidence: ' + event.results[0][0].confidence);
  }

  recognition.onspeechend = function() {
    recognition.stop();
    console.log('Speech ended!');
  }

  recognition.onerror = function(event) {
 console.log('Error occurred in recognition: ' + event.error);
  }

  recognition.onaudiostart = function(event) {
      //Fired when the user agent has started to capture audio.
      console.log('SpeechRecognition.onaudiostart');
  }

  recognition.onaudioend = function(event) {
      //Fired when the user agent has finished capturing audio.
      console.log('SpeechRecognition.onaudioend');
  }

  recognition.onend = function(event) {
      //Fired when the speech recognition service has disconnected.
      console.log('SpeechRecognition.onend');
  }

  recognition.onnomatch = function(event) {
      //Fired when the speech recognition can't recognise speech
      console.log('SpeechRecognition.onnomatch');
  }

  recognition.onsoundstart = function(event) {
      //Fired when any sound — recognisable speech or not — has been detected.
      console.log('SpeechRecognition.onsoundstart');
  }

  recognition.onsoundend = function(event) {
      //Fired when no sound present
      console.log('SpeechRecognition.onsoundend');
  }

  recognition.onspeechstart = function (event) {
      //Fired when speech starts
      console.log('SpeechRecognition.onspeechstart');
  }
  recognition.onstart = function(event) {
      //Fired when the speech recognition service has begun listening
      console.log('SpeechRecognition.onstart');
  }

1 Answers

Just for reference as of 2020 September, grammars don't seem to work at all in Chrome. There is a discussion of speechGrammarList being underspecified and having privacy issue concerns due to the reliance on external 3rd party resources and transferring user data over network to unknown parties.

https://github.com/WICG/speech-api/pull/58

and also

https://github.com/WICG/speech-api/issues/57

like image 128
Csaba Garay Avatar answered Oct 21 '25 01:10

Csaba Garay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!