打开谷歌并搜索带语音识别的文本

时间:2015-12-29 10:40:12

标签: c# speech-recognition

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

<img ... />

在这里,我要求jarvis搜索&#34;某事&#34; 那么他应该使用以下代码打开第二个表单

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搜索&#34;某事&#34;第二个表单打开,我按下它搜索Form2的按钮而不是那个&#34;&#34;我问

2 个答案:

答案 0 :(得分:1)

set {searchWord = value;}

不是&#34; =文字;}

此外,您正在设置密码为r,这似乎是整个语音命令,而不仅仅是目标搜索对象(即。#=&#34;搜索某些内容&#34;而不只是&#34;某些& #34;。)

答案 1 :(得分:1)

至于你从短语解析搜索词的问题......我已经对你的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;
            }
        }

我注意到你第一次说&#34; jarvis搜索汽车,&#34;它停在&#34; jarvis搜索&#34;,但如果您让程序继续运行并再次说出,它将正确识别整个短语。我认为这是因为你在第一次迭代时添加语法的方式。