通过Web Speech API使用语法

时间:2018-11-09 08:47:12

标签: javascript grammar webspeech-api

是的,有类似这样的问题,但是它们太老了……chrome.link现在完全支持语法:https://developer.mozilla.org/en-US/docs/Web/API/SpeechRecognition/grammars

我正在使用 Web Speech API 来实现语音识别。 SpeechRecognition接口的grammars属性返回并设置了SpeechGrammar对象的集合,这些对象表示将由当前SpeechRecognition理解的语法。我正在使用chrome的最新更新,并且“他们”说chrome支持语法。

在大多数情况下,我说“你是谁”时,网络语音将其识别为“你好吗”。例如,如何将“您是谁”这句话设置为可能的输入内容?

我尝试了几个小时,没有成功。

这是代码:

  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');
  }

2 个答案:

答案 0 :(得分:0)

有一种方法可以向语法列表中添加另一个语法,并为其添加更多的权重(重要性):

const grammar = '#JSGF V1.0; grammar colors; public <color> = aqua | azure | beige | bisque | black | blue | brown | chocolate | coral | crimson | cyan | fuchsia | ghost | white | gold | goldenrod | gray | green | indigo | ivory | khaki | lavender | lime | linen | magenta | maroon | moccasin | navy | olive | orange | orchid | peru | pink | plum | purple | red | salmon | sienna | silver | snow | tan | teal | thistle | tomato | turquoise | violet | white | yellow ;'
const recognition = new SpeechRecognition()
const speechRecognitionList = new SpeechGrammarList()
speechRecognitionList.addFromString(grammar, 1)
recognition.grammars = speechRecognitionList

签出speechGrammarListInstance.addFromString

对于您的用例来说,它可能不是完美的解决方案。在控制语法方面,Web Speech API似乎不太灵活。

答案 1 :(得分:0)

仅供参考(截至2020年9月),语法似乎在Chrome中根本不起作用。 由于对外部第三方资源的依赖以及通过网络将用户数据传输到未知方的原因,对SpeechGrammarList的讨论没有得到充分说明,并且存在隐私问题。

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

还有

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