语音识别结果处理程序始终运行单个操作

Speech recognition result handler always runs a single action

我已经在 C# 中研究 AI 一段时间了,我终于想出了如何将部分语音添加为变量。然而,我现在遇到的问题是 运行 把我说的每一句话都说了一遍然后搜索 google。我只希望它在我以 "Google" 开头时开始搜索。我应该怎么做?

这是我现在拥有的一些代码:

我如何更改此设置,以便它仅在我开​​始时说 Google 时进行搜索,并且我仍然能够 运行 来自文本文件的其他命令? ?

    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("https://www.google.com/?gws_rd=ssl#q=" + query);
            return;
        }
        switch (e.Result.Text)
        {

               //Open Commands
            case "open facebook":
                sSynth.Speak("Opening facebook for you now");
                System.Diagnostics.Process.Start("https://www.facebook.com/");
                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;

        }

    }

去掉“;”此行末尾的符号

        if (e.Result.Text.StartsWith("google")) ;

表示立即结束if case,所以if body为void,剩下的一直执行