3

我正在制作自己的 jarvis 程序,当我说“搜索”+ 某物时,我想打开 Google 并搜索“某物”。这是我的代码...(我没有全部粘贴)

    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...

有任何想法吗?

4

5 回答 5

4

Google 有一个查询字符串,您可以使用它直接转到用户输入的搜索字符串。以以下为例:

https://www.google.com/#q=test+and+such

(感谢马特 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);
        ...

您必须先确保userInputURL 已编码

string userInput = System.Web.HttpUtility.UrlEncode(input);
于 2013-08-29T13:15:34.310 回答
1

如果 switch 参数文本在“搜索”之后有搜索项,则它与 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;
于 2013-08-29T13:15:46.023 回答
1

这将不起作用,因为您的“语法构建”无法识别未加载到“语法构建”中的单词。(我想)我也在尝试这个。

于 2014-09-11T10:21:33.897 回答
0

我有相同的源代码。您必须在默认 commands.txt 中添加搜索命令,并确保自定义 commands.cs 中没有与该短语相关的任何内容

于 2015-01-09T17:03:28.530 回答
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;

这将加载不在默认语法中的听写文本。我没有测试这段代码,但这应该可以。

于 2015-05-06T20:25:26.250 回答