【问题标题】:Open google and search for text with speech recognition打开 google 并使用语音识别搜索文本
【发布时间】:2016-04-03 05:35:56
【问题描述】:

我正在编写代码以通过语音识别在谷歌上搜索,我想以第二种形式打开结果。 这是我第一种形式的代码

namespace Jarvis
{
    public partial class Form1 : Form
    {
        string Temperature;
        string Condition;
        string Humidity;
        string WindSpeed;
        string Town;
        string TFCond;
        string TFHigh;
        string TFLow;

        Boolean wake = true;

        private void StopWindow()
        {
            System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName(ProcWindow);
            foreach (System.Diagnostics.Process proc in procs)
            {
                proc.CloseMainWindow();

            }
        }

        // Weather application from Yahoo
        // to change city go to woeid on google, on yahoo site, type city take the code and past it below behind http://weather.yahooapis.com/forecastrss?w=
        private void GetWeather()
        {
            string query = String.Format("http://weather.yahooapis.com/forecastrss?w=973688");
            XmlDocument wData = new XmlDocument();
            wData.Load(query);

            // go to the link http://weather.yahooapis.com/forecastrss?w= and copy the http on top of the page
            XmlNamespaceManager manager = new XmlNamespaceManager(wData.NameTable);
            manager.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");

            XmlNode channel = wData.SelectSingleNode("rss").SelectSingleNode("channel");
            XmlNodeList nodes = wData.SelectNodes("/rss/channel/item/yweather:forecast", manager);

            Temperature = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["temp"].Value;

            Condition = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["text"].Value;

            Humidity = channel.SelectSingleNode("yweather:atmosphere", manager).Attributes["humidity"].Value;

            WindSpeed = channel.SelectSingleNode("yweather:wind", manager).Attributes["speed"].Value;

            Town = channel.SelectSingleNode("yweather:location", manager).Attributes["city"].Value;

            TFCond = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["text"].Value;

            TFHigh = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["high"].Value;

            TFLow = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["low"].Value;
        }

        SpeechRecognitionEngine rec = new SpeechRecognitionEngine();
        SpeechSynthesizer s = new SpeechSynthesizer();
        int count = 1;
        Choices list = new Choices();
        string ProcWindow;

        public Form1()
        {
            InitializeComponent();

            ///  line under writes the program commands to the DIR 
            ///  all commands are without capital lettres


            list.Add(new string[] { "good morning jarvis", "good afternoon jarvis", "good evening jarvis" });

            list.Add(new string[] { "how are you", "how are you feeling", "how was your day" });

            list.Add(new string[] { "what time is it", "whats the time", "how late is it" });

            list.Add(new string[] { "what is today", "whats the date of today" });

            // web browser
            list.Add(new string[] { "open google", "open facebook", "open youtube", "open chrome", "open gmail", "open hotmail" });

            // programs
            list.Add(new string[] { "start wordpad", "close wordpad", "start word", "close word", "start excel", "close excel", "start visual studio", "close visual studio", "start inventor", "close inventor", "start solidworks", "close solidworks", "start word", "close word", "start matlab", "close matlab" });

            list.Add(new string[] { "jarvis mute", "jarvis wake", "jarvis search for" });

            // weather prediction

            list.Add(new string[] { "whats tommorrows forecast", "how is the weather" });

            Grammar gr = new Grammar(new GrammarBuilder(list));

            try
            {
                rec.RequestRecognizerUpdate();
                rec.LoadGrammar(gr);
                rec.SpeechRecognized += rec_SpeechRecognized;
                rec.SetInputToDefaultAudioDevice(); // sets to earplugs of speakers
                rec.RecognizeAsync(RecognizeMode.Multiple);
                // Set the speaking rate and volume
                s.Rate = -1;
                s.Volume = 100;
            }

            catch { return; }

            // Change voice Female, Male
            s.SelectVoiceByHints(VoiceGender.Neutral);

            s.Speak("I am active and online sir!");
        }

        public static void killProg(string s)
        {
            System.Diagnostics.Process[] Procs = null;

            try
            {
                Procs = Process.GetProcessesByName(s);
                Process prog = Procs[0];

                if (!prog.HasExited)
                {
                    prog.Kill();

                }
            }

            finally
            {
                if (Procs != null)
                {
                    foreach (Process p in Procs)
                    {
                        p.Dispose();
                    }
                }
            }
        }

        public void say(string h)
        {
            s.Speak(h);
        }

        private void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            string r = e.Result.Text;

            if (r == "jarvis wake") wake = true;
            if (r == "jarvis mute") wake = false;

            if (r.ToLower().Contains("jarvis search for")) // See if the string contains the 'search for' string.
            {
                var googleGrammar = new DictationGrammar();
                    rec.LoadGrammarAsync(googleGrammar);
                Google searching = new Google();

                searching.password = r;
                searching.ShowDialog();

                return;
            }

这里我让 jarvis 搜索“某物” 那么他应该使用以下代码打开第二个表单

public partial class Google : Form
    {
        private string searchWord;

        public string password
        {
            get { return searchWord; }
            set { searchWord = Text; }
        }

        public Google()
        {
            InitializeComponent();
        }

        private void Google_Load(object sender, EventArgs e)
        {
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            searchBox.Text = searchWord;

            string search = searchBox.Text;

            StringBuilder add = new StringBuilder("https://www.google.com/#q=");
            add.Append(searchWord);

            googleWebbrowser.Navigate(add.ToString());
        }
    }
}

问题是:

现在当我说 jarvis 搜索“某事”时,第二个表单打开,我按下按钮搜索 Form2 而不是我要问的“某事”

【问题讨论】:

    标签: c# speech-recognition


    【解决方案1】:
    set {searchWord = value;}
    

    不是 "= 文本;}

    另外,您将密码设置为 r,这似乎是整个语音命令,而不仅仅是目标搜索对象(即 r = “搜索某物”,而不仅仅是“某物”)。

    【讨论】:

    • 谢谢,一个问题解决了。事实上,任何想法如何解决这个问题?
    • 解决了哪个问题?哪个还需要解决?您是否更改了“set”的代码?
    • 哦抱歉,设置的代码已解决。知道当我说 jarvis 寻找水。贾维斯没有开始搜索。当我说只有 jarvis 搜索时,它开始搜索。我认为这是 if 语句的问题? if (r.ToLower().Contains("jarvis search for"))
    • 在 if 语句中添加断线,并在运行时检查 r 的值。
    • 另外,你能把你的代码贴在 if 语句之前吗?
    【解决方案2】:

    至于您从短语中解析搜索词的问题...我已经测试了对您的 SpeechRecongized 方法的修改。

            private void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
            {
                string r = e.Result.Text.ToLower();
    
                if (r == "jarvis wake") wake = true;
                if (r == "jarvis mute") wake = false;
    
                if (r.Contains("jarvis search for")) // See if the string contains the 'search for' string.
                {
                    var googleGrammar = new DictationGrammar();
                    rec.LoadGrammarAsync(googleGrammar);
                    Google searching = new Google();
                    int indexOfSearchWords = r.IndexOf("jarvis search for") + 17; // 17 is length of "jarvis search for" +1
                    r = r.Substring(indexOfSearchWords, r.Length - indexOfSearchWords); //sets r to everything in the phrase after "jarvis search for"
    
                    if (r!="")
                    {
                        searching.password = r;
                        searching.Google_ShowSearchWord();
                        searching.ShowDialog();
                    }
    
                    return;
                }
            }
    

    我注意到,当您第一次说“jarvis search for cars”时,它会在“jarvis search for”处停止,但如果您让程序运行并再次说,它会正确识别整个短语。我认为这是因为您在第一次迭代时添加语法的方式。

    【讨论】:

    • 嗨,感谢您的代码,但它仍然不适合我。现在我第一次遇到搜索错误。Google_ShowSearchWord(); “Google”不包含“Google_ShowSearchWord”的定义,并且找不到接受“Google”类型的第一个参数的扩展方法“Google_ShowSearchWord”(您是否缺少 using 指令或程序集引用?)当我删除它时(我认为代码中没有必要?)然后 Jarvis 不再响应 jarvis 搜索的问题。
    • 抱歉,我忘了我在 google 搜索表单上添加了一个名为 ShowSearchWord 的函数,它将 searchBox 文本设置为要搜索的阶段。
    • ok,但它仍然无法正常工作。程序一直运行到 if (r!="")。然后它就不再做任何事情了
    【解决方案3】:

    上面的答案会导致你的程序挂起。坦率地说,这是一个糟糕的解决方案,我很惊讶其他人从未提到过这一点。 我注意到您的 rec_SpeechRecognized Sub 并没有使用开关盒。这不是必须的,而是一种更聪明的方法,因为在大多数情况下它们比 If 语句

    更快

    注意:这取决于switch case的条件数量是否很大。我有一个您可以尝试的解决方案,您不需要向文本文件添加命令,但它确实需要添加文本文件。我使用 StringBuilderAppendLine 来编排我的演讲。它会不断更新您的文本文件,覆盖您上次的搜索,并且执行速度很快,无需重复。鉴于它的设计,最好在另一种形式上使用它,但主要用于搜索,但这是您的决定。当您不使用它时,您可以创建一个命令:"End now"SendKeys.Send("%{F4}"); 来终止表单或 Application.Exit这是一种混合方法,它可以一次又一次地轻松检测短语,您无需花费数小时来添加命令列表。这像你一样使用 DictationGrammar。我为您转换了代码,因为我的代码在 VB.Net 中。 以下是图像文件中的代码和我的 GitHub 存储库的链接

    【讨论】:

      【解决方案4】:

      只需点击图片即可将其放大。我已经转换了代码,但是这个网站大部分时间都很难发布代码,所以我坚持使用图片和链接。省得头疼……

      This is my code in VB.Net

      我的存储库在这里提供完整详细信息的代码:https://github.com/Rythorian77/Dictation-Hybrid/commit/7a9b5697b67b46d9018525ea33beb1bacad1f949

      您可以在以下位置进行转换:https://converter.telerik.com

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-14
        • 1970-01-01
        • 2010-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多