簡體   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