通过C#语音进行Google搜索 [英] Google search via speech in c#
问题描述
我正在制作自己的jarvis程序,当我说搜索"时,我想 打开Google并搜索内容".这是我的代码...(我没有全部粘贴)
I am making my own jarvis program and when I say "search for" + something I want to open Google and search for "something". Here my code...( I don't paste it all)
private void Form1_Load(object sender, EventArgs e)
{
_recognizer.SetInputToDefaultAudioDevice();
_recognizer.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"C:\Users\Cpyros\Desktop\lefteris\Commands.txt")))));
_recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized);
_recognizer.RecognizeAsync(RecognizeMode.Multiple);
}
void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
int ranNum = rnd.Next(1, 10);
string speech = e.Result.Text;
switch (speech)
{
//GREETINGS
case "hello":
case "hello jarvis":
if (ranNum < 6) { JARVIS.Speak("Hello sir"); }
else if (ranNum > 5) { JARVIS.Speak("Hi"); }
break;
case "goodbye":
case "goodbye jarvis":
case "close":
case "close jarvis":
JARVIS.Speak("Until next time");
Close();
break;
case "jarvis":
if (ranNum < 5) { QEvent = ""; JARVIS.Speak("Yes sir"); }
else if (ranNum > 4) { QEvent = ""; JARVIS.Speak("Yes?"); }
break;
//WEBSITES
case "open facebook":
System.Diagnostics.Process.Start("http://www.facebook.com");
break;
case "open google":
Process.Start("https://www.google.gr/?gws_rd=cr");
JARVIS.Speak("Okay sir");
System.Windows.Forms.SendKeys.SendWait("^%.");
break;
here i want to add a case like "search for" + the thing i want to search...
有什么想法吗?
推荐答案
Google提供了一个查询字符串,您可以使用该查询字符串直接找到用户输入的搜索字符串.以以下示例为例:
Google has a query string that you can use to go right to a user input search string. Take the following for example:
(感谢Matt R,我也知道还有https://www.google.com/search?q=test+and+such
)
(thanks to Matt R, I learned there is also https://www.google.com/search?q=test+and+such
)
然后,您可以使用以前的Google案例声明的修改形式:
You can then use a modifiction of your previous Google case statement:
default:
if (speech.Contains("search for")
{
Process.Start("https://www.google.com/#q=" + userInput);
...
您必须通过执行以下操作来确保首先对userInput
进行URL编码
You will have to make sure the userInput
is URL Encoded first by doing
string userInput = System.Web.HttpUtility.UrlEncode(input);
这篇关于通过C#语音进行Google搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!