简体   繁体   English

Web 语音 API 语法

[英]Web speech API grammar

Can somebody please tell me what this有人可以告诉我这是什么吗

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

line means from the follwoing?线的意思是从以下?

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

If i want to incorporate my own grammar what changes to this line do I need to make?如果我想合并我自己的语法,我需要对这一行进行哪些更改?

The line is a string following a set of rules of the JSGF specification .该行是遵循JSGF 规范的一组规则的字符串。 There is a more "user-friendly" explanation at MDN on this topic as well. MDN上也有关于这个主题的更“用户友好”的解释。

Basically in this case we can break this down to:基本上在这种情况下,我们可以将其分解为:

  • #JSGF V1.0; a header from the specification, always necessary to know which version it should use.规范中的 header,始终需要知道它应该使用哪个版本。 Shouldn't change for you.不应该为你改变。
  • grammar colors; is a (custom) name of your grammar.是您的语法的(自定义)名称。
  • public <color> = creates a publicly accessible rule called color . public <color> =创建一个名为color的可公开访问的规则。 "Publicly accessible" means your grammar can be imported by someone else and this rule is accessible to them. “可公开访问”意味着您的语法可以由其他人导入,并且他们可以访问此规则。 color is the rule name. color是规则名称。 This is necessary when referring from another rule (example later)从另一个规则引用时这是必要的(稍后示例)
  • aqua | azure |... aqua | azure |... are the options that match this. aqua | azure |...是与此匹配的选项。 | means "or".意思是“或”。 So when it recognises one of aqua , azure , ... , it matches that <color> rule.因此,当它识别aquaazure...之一时,它与<color>规则匹配。

A bit more sophisticated example with referencing would be: #JSGF V1.0; grammar greeting; <greet> = hello | welcome; <name> = Alice | Bob; public <statement> = <greet> <name>;一个更复杂的引用示例是: #JSGF V1.0; grammar greeting; <greet> = hello | welcome; <name> = Alice | Bob; public <statement> = <greet> <name>; #JSGF V1.0; grammar greeting; <greet> = hello | welcome; <name> = Alice | Bob; public <statement> = <greet> <name>;

But now to the practicality of this: I've played around with the MDN Speech Recognition Demo a bit and I don't think that browsers are really using the grammar (yet).但现在来看一下它的实用性:我已经玩过MDN Speech Recognition Demo了,我认为浏览器还没有真正使用语法。 If you have a look into its source code , it will never call the recognition.onnomatch function, making the grammar feel a bit useless to me.如果你查看它的源代码,它永远不会调用recognition.onnomatch function,让我觉得语法有点没用。 It also doesn't show up in the results and in the end you only retrieve a transcript of the spoken text - at least in Chrome.它也不会出现在结果中,最后您只能检索语音文本的抄本 - 至少在 Chrome 中是这样。

My conclusion to this (mid 2020) is that you do not really need it right now.我对此的结论(2020 年中)是你现在并不真正需要它。 Maybe it could help in the future but as the Can I use... table (still) looks quite red, I doubt this will be the final way of doing things with speech.也许它将来会有所帮助,但由于我可以使用...表(仍然)看起来很红,我怀疑这将是用语音做事的最终方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM