1
votes

I need help. I want to make an application that will recognize what I am saying and do stuff that I say. For example:

If I say open [notepad], where [notepad] can be any application name, it needs to open notepad.

I think I need to use both Grammar and DictationGrammar, but I don't know how. Please help me. Thanks.

My code now looks like this:

    string WelcomeSentence = "Hello sir, how are you today";
    SpeechSynthesizer sSynth = new SpeechSynthesizer();
    PromptBuilder pBuilder = new PromptBuilder();
    SpeechRecognitionEngine sRecognize = new SpeechRecognitionEngine();

    private void frmMain_Load(object sender, EventArgs e)
    {
        sSynth.SelectVoice("IVONA Amy");
        sSynth.SetOutputToDefaultAudioDevice();
        pBuilder.ClearContent();
        pBuilder.AppendText(WelcomeSentence);
        sSynth.Speak(pBuilder);

        Choices sList = new Choices();
        sList.Add(File.ReadAllLines(@"Commands.ekd"));
        Grammar gr = new Grammar(new GrammarBuilder(sList));
        DictationGrammar dgr = new DictationGrammar();
        try
        {
            sRecognize.RequestRecognizerUpdate();
            sRecognize.LoadGrammar(gr);
            sRecognize.SpeechRecognized += sRecognize_SpeechRecognized;
            sRecognize.SetInputToDefaultAudioDevice();
            sRecognize.RecognizeAsync(RecognizeMode.Multiple);
            sRecognize.Recognize(); 
        }
        catch { return; }
    }
    private void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        if (e.Result.Text == "open notepad")
        {
            System.Diagnostics.Process.Start(@"C:\Windows\System32\Notepad.exe");
        }
        else
        {
            pBuilder.ClearContent();
            pBuilder.AppendText(e.Result.Text);
            sSynth.Speak(pBuilder);
        }
    }

Please help.

1
On this question, I give you full marks for adding code and creating a readable question. I think however that it is too broad. You need to narrow down your problem to more specific language.crthompson
FWIW.. you may be interested in an answer I posted several months ago.crthompson
I don't know what more can I say. I just want the application to recognize open as a command and notepad as being an application that is not added in grammar.TheNeosrb
@paqogomez I don't know how to implement this in my code. Could you help me somehow?TheNeosrb
I made one big assumption. That this code above, compiles and works. You dont mention an error, just that you're having problems w/ Grammar and DictationGrammar. It would appear that you are loading them both properly, but I dont know the nuance of your speech engine to say.crthompson

1 Answers

1
votes

Following along with an answer I posted several months ago, I offer this suggestion.

Realize that I'm leaving out the SpeechFactory class and much of the MySpeechMethods class, please copy it from the other answer. Also, as noted in the other answer, you'll have to do some error handling. With that caveat, you would modify your own code this way.

private void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
    var methods = new MySpeechMethods();
    MethodInfo myMethod;
    myMethod = SpeechFactory.GetSpeechMethod(e.Result.Text);

    if(myMethod != null) return;        
    pBuilder.ClearContent();
    pBuilder.AppendText(e.Result.Text);
    sSynth.Speak(pBuilder);
}

Then in the MySpeechMethods you would have your commands.

public class MySpeechMethods
{
    [Speech("Open Notepad")]
    public void OpenNotepad()
    {
       System.Diagnostics.Process.Start(@"C:\Windows\System32\Notepad.exe");
    }
//...