繁体   English   中英

谷歌通过c#中的语音搜索

[英]Google search via speech in c#

我正在制作自己的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...

有任何想法吗?

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

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

(感谢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是URL编码

string userInput = System.Web.HttpUtility.UrlEncode(input);

由于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;

这不起作用,因为你的“grammarbuild”无法识别未加载到“grammarbuild”中的单词。 (我想)我还在尝试这个。

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

正如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;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM