I\'m building an application that recognizes multiple words from a user; thus putting together a sentence using the words recognized.
Here\'s what I have as of now:<
This answer may be a little late, but I have not found any other place with an actual answer to this problem. So to save others the the time and frustration, here is how I did it.
using System;
using System.Threading;
using System.Speech;
using System.Speech.Synthesis;
using System.Speech.Recognition;
namespace SpeachTest
{
public class GrammerTest
{
static void Main()
{
Choices choiceList = new Choices();
choiceList.Add(new string[]{"what", "is", "a", "car", "are", "you", "robot"} );
GrammarBuilder builder = new GrammarBuilder();
builder.Append(choiceList);
Grammar grammar = new Grammar(new GrammarBuilder(builder, 0, 4) ); //Will recognize a minimum of 0 choices, and a maximum of 4 choices
SpeechRecognizer speechReco = new SpeechRecognizer();
speechReco.LoadGrammar(grammar);
}
}
}
The key here is this line
new GrammarBuilder(builder, 0, 4)
This tells the speech recognizer to recognize, up to 4 repetitions of elements form builder
, and a minimum of zero.
So it will now recognize
"what is a car"
And if you want more than 4 repetitions, just change new GrammarBuilder(builder, 0, 4)
to
new GrammarBuilder(builder, 0 "the number of repetitions you want")
See this for more info GrammarBuilder(builder, minRepeat, maxRepeat)
It recognizes one word because you incorrectly constructed the grammar. Since you constructed the grammar consisting of choice of one of the words "what", "is", "a", "car" it exactly recognizes one of the words.
You probably want to read introduction into grammars and related documentation.
http://msdn.microsoft.com/en-us/library/hh378438(v=office.14).aspx
If you want to construct the grammar describing the phrase you can just use GrammarBuilder like this:
Grammar gr = new Grammar(new GrammarBuilder("what is a car"));
This grammar will recognize a phrase.
To understand how Choices work you can read documentation on Choices:
http://msdn.microsoft.com/en-us/library/microsoft.speech.recognition.choices(v=office.14).aspx