2

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.

TheNeosrb
  • 89
  • 1
  • 2
  • 10
  • 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 Mar 19 '14 at 15:23
  • FWIW.. you may be interested in an [answer I posted](http://stackoverflow.com/a/18605036/2589202) several months ago. – crthompson Mar 19 '14 at 15:26
  • 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 Mar 19 '14 at 15:26
  • @paqogomez I don't know how to implement this in my code. Could you help me somehow? – TheNeosrb Mar 19 '14 at 15:46
  • 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 Mar 19 '14 at 16:48

1 Answers1

1

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");
    }
//...
Community
  • 1
  • 1
crthompson
  • 15,653
  • 6
  • 58
  • 80
  • I will definitely implement this, this is very good. But this is not what I am looking for. Using your code, the method will be just open (the commands that executes an application). So, the user says open, computer recognizes it as a command and next he says is notepad, that's name of the application that needs to be changeable. So, using your code, I need to write a method for every single application I want to run with my app. Do you know what I want know? – TheNeosrb Mar 19 '14 at 16:57
  • @TheNeosrb Yes, I was going to comment on that. It does require that you map each command. I dont know how you would translate the name of a program into a path on the computer. That sounds iffy as well. Easy to say `delete c:`. You should know what programs your program is launching. – crthompson Mar 19 '14 at 17:15
  • @TheNeosrb If it makes it easier, you could read the possible files out of a config file and build up your MySpeechMethods class that way. But that is probably outside the bounds of this question. – crthompson Mar 19 '14 at 17:18
  • What about a different approach. Like, user says `google [query]` and then my app opens the `https://www.google.rs/search?q=[query]`. So computer only recognizes first word as a command and other words as search query? – TheNeosrb Mar 19 '14 at 17:19
  • @TheNeosrb I think you could expand the `GetSpeechMethod` in that case. Capture the first word, then pass the remaining in as a parameter. – crthompson Mar 19 '14 at 17:21
  • @TheNeosrb Well, learning is part of the process. I cant take that away from you by doing it. Start by changing the `GetSpeechMethod` to accept 2 parameters, then work through the errors. Post another question if you get stuck! Good luck! Dont forget to upvote and mark answers. :) – crthompson Mar 19 '14 at 18:37
  • Can you somehow give me your email so I can update you with my project? You helped me alot. :) – TheNeosrb Mar 19 '14 at 18:47