【问题标题】:The effect of the grammar in the Web Speech APIWeb Speech API 中语法的效果
【发布时间】:2017-05-13 10:01:01
【问题描述】:

在 Web Speech API 的示例中,始终指定语法。比如MDN's colour change example中的语法是:

#JSGF V1.0;
grammar colors;
public <color> = aqua | azure | beige | bisque | black | blue | brown | chocolate | coral | crimson | cyan | fuchsia | ghostwhite | 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 ;

但是,在实际使用 API 时(在 Chrome 54.0.2840.71 上),result function

  1. 有时会返回不符合所提供语法的字符串
  2. 不提供描述语音的解析树

那么语法实际上做了什么?我怎样才能获得这些行为中的任何一种(仅限于语法并查看解析树)?

【问题讨论】:

标签: javascript html parsing speech-recognition webspeech-api


【解决方案1】:
【解决方案2】:

我知道这是一个老问题,但我正在经历一些与此类似的问题,因为这是我最近一直试图弄清楚的问题,并且我有一个解决方案。语法似乎不起作用,至少不可靠或不符合预期。

作为解决方案,我编写了一个函数来解决问题。为它提供来自SpeecRecognition.onresult 回调的event.results,并确保将maxAlternatives 设置为10 之类的值。同时提供一个短语列表。它将返回它找到的包含其中一个短语的第一个转录本,否则它只返回具有最高置信度的转录本。

function ExtractTranscript(phrases, results) {
  // Loop through the alternatives to check if any of our hot phrases are contained in them.
  for (let result in results[0]) {
    if (new RegExp(phrases.join("|")).test(results[0][result].transcript)) {
      return results[0][result].transcript; // Return them if they are
    }
  }
  return results[0][0].transcript; // Otherwise return the highest confidence
}

对于长转录本等,这种解决方案可能会有所改进,但它适用于我的情况,适用于短语等短命令。希望它也可以帮助其他人。

【讨论】:

    猜你喜欢
    • 2015-04-05
    • 2019-04-12
    • 2016-10-31
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 2014-02-26
    • 2013-05-28
    • 1970-01-01
    相关资源
    最近更新 更多