so I want to make some speech recognition (using system.speech.recognition) software with some command list that can be change. So for example at first you have 2 kind of command list : "Left,Right" which when you said left or right it will do some function. But I want in this software I can change the command list whenever I want it to change, for example at the command list above I want to change word "Left" to "Up", and when I said "Up" it will doing the function that before doing by the "Left" word. This is my code :
Choices commands = new Choices();
GrammarBuilder gBuilder = new GrammarBuilder();
public void Masokey_Load(object sender, EventArgs e)
{
// Choices commands = new Choices();
commands.Add(new string[] { Atext.Text, Dtext.Text});
// GrammarBuilder gBuilder = new GrammarBuilder();
gBuilder.Append(commands);
Grammar grammar = new Grammar(gBuilder);
recEngine.LoadGrammarAsync(grammar);
recEngine.SetInputToDefaultAudioDevice();
recEngine.SpeechRecognized += RecEngine_SpeechRecognized;
}
//save_btn
public void Savebtn_Click(object sender, EventArgs e)
{
commands.Add(new string[] { Atext.Text, Dtext.Text});
gBuilder.Append(commands);
}
public void RecEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if(e.Result.Text == Atext.Text)
{
PressKey(0x1E);
}
else if (e.Result.Text == Dtext.Text)
{
PressKey(0x20);
}
}
I want to change the value inside "command.Add(new string[] {Atext.Text, Dtext.Text)"
I put the command list into a textbox so when I run the software, when I want to change the command list I just need to change the word inside the textbox and when I click the save button, the command list will change. The problem is when I click the save button, the command list won't change.
Is it possible to change the value of Choices object and GrammarBuilder object?
You shouldn't modify that array (probably a list) after you fill it. I assume gBuilder
contains all commands that voice recognition accepts. If that is true, add all commands you will be using and in RecEngine_SpeechRecognized
event check if they are enabled before actually triggering actions based on them.
To give you a basic example:
public void RecEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
//mode could be a string or an enum variable
if(mode.Equals("LEFT_RIGHT") && e.Result.Text == Atext.Text)
{
PressKey(0x1E);
}
else if (mode.Equals("LEFT_RIGHT") && e.Result.Text == Dtext.Text)
{
PressKey(0x20);
}else if (mode.Equals("UP_DOWN") && e.Result.Text == Stext.Text)
{
PressKey(...);
}else if(mode.Equals("UP_DOWN") && e.Result.Text == Wtext.Text)
{
PressKey(....);
}
}
If your program is going to be more complex a much better approach would be creating a list of enabled commands. An example to check that implementation:
public void RecEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
//enabledCommands is a list of strings (or whatever can store Atext.Text and the others)
if(enabledCommands.Contains(e.Result.Text){ //First check if whatever command you received is enabled
if(e.Result.Text == Atext.Text) //then check what command it is and execute it
{
PressKey(0x1E);
}
}
}
If your program requires that you can go further and implement a list of objects, each of which contains a command and a Boolean indicating if that command is enabled, but that's a bunch more code and probably more complex than you need it to be.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.