我正在制作自己的jarvis程序,当我说“搜索”+我想要的东西时 打开谷歌并搜索“东西”。这是我的代码......(我不会全部粘贴)
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...
任何想法?
答案 0 :(得分:4)
Google有一个查询字符串,您可以使用该字符串直接转到用户输入搜索字符串。以下面的例子为例:
(感谢Matt R,我知道还有https://www.google.com/search?q=test+and+such
)
然后,您可以使用先前Google案例陈述的修改:
default:
if (speech.Contains("search for")
{
Process.Start("https://www.google.com/#q=" + userInput);
...
您必须先确保userInput
首先进行网址编码
string userInput = System.Web.HttpUtility.UrlEncode(input);
答案 1 :(得分:1)
如果switch参数文本在'search for'后面有搜索项时与case语句不匹配,则可以将其作为default
语句:
default:
if (speech.ToLower().Contains("search for")) // See if the string contains the 'search for' string.
{
string query = speech.Replace("search for", ""); // Remove the 'search for' text.
// Old code (does not make the text fully URL safe)
// query = query.Replace(' ', '+');
query = System.Web.HttpUtility.UrlEncode(query);
string url = "https://www.google.com.au/search?q=" + query;
System.Diagnostics.Process.Start(url);
}
break;
答案 2 :(得分:1)
这不起作用,因为你的#34; grammarbuild&#34;无法识别未加载到&#34; grammarbuild&#34;中的单词。 (我想)我还在尝试这个。
答案 3 :(得分:0)
我有相同的源代码。你必须在默认的commands.txt中添加搜索命令,并确保你在自定义commands.cs中没有任何关于短语的内容
答案 4 :(得分:0)
正如Nikki O之前所说,由于你的GrammarBuilder中没有单词,所以这不起作用。您可以简单地加载多个语法,或者只是执行:
default:
if (speech.ToLower().Contains("search for")) // See if the string contains the 'search for' string.
{
var dictationGrammar= new DictationGrammar();
sre.LoadGrammarAsync(dictationGrammar);
string url = "https://www.google.com.au/search?q=" + dictationGrammar;
System.Diagnostics.Process.Start(url);
}
break;
这将加载您的默认语法中没有的听写文本。我没有测试过这段代码,但这应该可行。