我已经在C#中使用AI了一段时间了,我终于想出了如何将部分语音添加为变量。然而,我现在遇到的问题是它正在运行我所说的一切并搜索谷歌。如果我开始说“谷歌”,我只希望它开始搜索。我应该做些什么?
以下是我现在的一些代码:
如何更改此内容,以便仅在我开始时通过说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;
}
}
答案 0 :(得分:0)
删除“;”这一行末尾的符号
if (e.Result.Text.StartsWith("google")) ;
它意味着立即结束if案例,因此if body是无效的,其余的总是被执行。