I am looking for a way to define what you expect the user to say in an <input>
tag
with the HTML 5 speech
attribute set.
I know that you can specify a specific grammar to use via the grammar
attribute,
like this:
<input type="text" speech grammar="grammar.grxml" />
( see http://lists.w3.org/Archives/Public/public-xg-htmlspeech/2011Feb/att-0020/api-draft.html )
but I was hoping for a way to make this dynamic, so that I can specify what I expect the user to say via javascript.
For example, if you had a dynamically generated list of items for a user to select from by speech, how would you specify that what they will say will most likely be one of those items?
P.S. I am testing this with Google Chrome, thus using the x-webkit-speech
attribute instead of speech
and likewise x-webkit-grammar
instead of grammar
.
I found a way to do it client-side, using a new html5 feature: blobs.
This makes a url out of the grammar string, and then sets that url for our
input
element.This way there is no need to make a server request, thus making it faster and less load on the server.
For more information on blobs, see this and this.
The grammar file could be dynamically generated using something like PHP, JSP, or your favorite web development language. The grammar file is fetched using HTTP so you could have something like this if you are using PHP:
The PHP would dynamically create the grammar based on information passed in a query string or through stored session information and return it to the speech engine.