samedi 30 mai 2015

C# Speech Recognition Searching Google

I've been working on an AI in C# for a while now and I finally figured out how to add part of the speech as a variable. However the problem I'm having now is it's running everything I say and searching google. I only want it to start the search if I start off by saying "Google". What should I be doing differently?

Here's a little bit of the code I have right now:

How can I change this so it only searches when I start off by saying Google and I'm still able to run my other commands from the text file??

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        //Load Grammar
        GrammarBuilder gBuilder = new GrammarBuilder();
        gBuilder.Append(new Choices(System.IO.File.ReadAllLines(@"C:\Users\ThatOneCodeNoob\Desktop\EVA 1.0\Commands.txt")));
        Grammar grammar = new Grammar(gBuilder);
        DictationGrammar dictation = new DictationGrammar();
        gBuilder.AppendDictation("google");

        recEngine.LoadGrammarAsync(grammar);
        recEngine.LoadGrammarAsync(dictation);
        recEngine.SetInputToDefaultAudioDevice();
        recEngine.RecognizeAsync(RecognizeMode.Multiple);
        recEngine.SpeechRecognized += recEngine_SpeechRecognized;
        sSynth.Speak("EVA, online");
    }

    //Speech Recognized
    void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        if (e.Result.Text.StartsWith("google")) ;
        {
            string query = e.Result.Text.Replace("google", "");
            System.Diagnostics.Process.Start("http://ift.tt/1pUWlSX" + query);
            return;
        }
        switch (e.Result.Text)
        {

               //Open Commands
            case "open facebook":
                sSynth.Speak("Opening facebook for you now");
                System.Diagnostics.Process.Start("http://ift.tt/g8FRpY");
                break;

            case "open pandora":
                sSynth.Speak("Opening Pandora for you");
                System.Diagnostics.Process.Start("http://www.pandora.com/");
                break;

            case "open youtube":
                sSynth.Speak("Loading YouTube now");
                System.Diagnostics.Process.Start("http://www.youtube.com/");
                break;

            case "open google chrome":
                System.Diagnostics.Process.Start(@"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe");
                sSynth.Speak("starting chrome for you");
                break;

            case "minimize":
                try
                {
                    sSynth.Speak("minimizing");
                    ActiveForm.WindowState = FormWindowState.Minimized;
                    break;
                }
                catch
                {
                    sSynth.Speak("I can't minimize this window sir");
                    break;
                }

                //Date/Time Commands

            case "whats the time":
            case "what time is it":
                sSynth.Speak(DateTime.Now.ToShortTimeString());
                break;

            case "what day is it":
                sSynth.Speak(DateTime.Now.ToString("dddd"));
                break;

            case "whats today":
                sSynth.Speak(DateTime.Now.ToLongDateString());
                break;

            case "whats todays date":
            case "whats the date":
                sSynth.Speak(DateTime.Now.ToString("dd/M/yyyy"));
                break;

                //Social Commands

            case "thanks":
            case "thank you":
                {
                    sSynth.Speak("You're welcome");
                    break;
                }

            case "goodbye":
                sSynth.Speak("goodbye for now sir");
                Application.Exit();
                break;

            case"eva":
                sSynth.Speak("yes sir");
                break;

                //Offline & Online

            case "go offline":
                sSynth.Speak("I will await further orders");
                GoOffline();
                break;

            case"wake up":
            case"come back":
            case "come online":
                ComeOnline();
                break;

        }

    }

Aucun commentaire:

Enregistrer un commentaire