1

好的。我一直在研究语音识别程序。当你说“在网上搜索猫的图像”时,我想这样做,它会打开浏览器并搜索猫的图像。我制作了一个文本框来接收用户语音的输入,但我很难找到如何从文本框中检索文本并将其发送到网络。其中 textbox1.text = 语音;是将语音转换为文本的地方。到目前为止,这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.IO;
using System.Xml;
using System.Threading;
using System.Net;

namespace Jarvis
{
    public partial class Form1 : Form
    {
        SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
        SpeechSynthesizer JARVIS = new SpeechSynthesizer();
        string QEvent;
        string ProcWindow;
        string BrowseDirectory;
        string Temperature;
        string Condition;
        string Humidity;
        string WindSpeed;
        string Town;
        string TFCond;
        string TFHigh;
        string TFLow;

        double timer = 10;
        int count = 1;
        Random rnd = new Random();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _recognizer.SetInputToDefaultAudioDevice();
            _recognizer.LoadGrammar(new DictationGrammar());
            _recognizer.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"C:\Users\Jeremy\Documents\Visual Studio 2012\Projects\Jarvis\Jarvis\commands.txt")))));
            _recognizer.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"C:\Users\Jeremy\Documents\Visual Studio 2012\Projects\Jarvis\Jarvis\greetings.txt")))));
            _recognizer.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"C:\Users\Jeremy\Documents\Visual Studio 2012\Projects\Jarvis\Jarvis\programs.txt")))));
            _recognizer.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"C:\Users\Jeremy\Documents\Visual Studio 2012\Projects\Jarvis\Jarvis\social.txt")))));
            _recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized);
            _recognizer.RecognizeAsync(RecognizeMode.Multiple);

        }
        void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
            {
            var game = 0;
            int ranNum = rnd.Next(1, 10);
            int ranNumm = rnd.Next(1, 15);
            bool wh = false;
            bool sleep = false;
            string speech = e.Result.Text;
            textBox1.Text = speech;
            if (textBox1.Text == url)
            { 

            }
            switch (speech)
            {
                //GREETINGS
                case "hello":
                case "hey":
                case "hey jarvis":
                    if (ranNum < 6) { JARVIS.Speak("Hello sir"); }
                    else if (ranNum > 5) { JARVIS.Speak("Hi"); }
                    break;

            }
        }

        private void ShutdownTimer_Tick(object sender, EventArgs e)
        {
            if (timer == 0)
            {
                lblTimer.Visible = false;
                ComputerTermination();
                ShutdownTimer.Enabled = false;
            }
            else if (QEvent == "abort")
            {
                timer = 10;
                lblTimer.Visible = false;
                ShutdownTimer.Enabled = false;
            }
            else
            {
                timer = timer - .01;
                lblTimer.Text = timer.ToString();
            }
        }
        private void ComputerTermination()
        {
            switch (QEvent)
            {
                case "shutdown":
                    System.Diagnostics.Process.Start("shutdown", "-s");
                    break;
                case "logoff":
                    System.Diagnostics.Process.Start("shutdown", "-l");
                    break;
                case "restart":
                    System.Diagnostics.Process.Start("shutdown", "-r");
                    break;
            }
        }
        private void StopWindow()
        {
            System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName(ProcWindow);
            foreach (System.Diagnostics.Process proc in procs)
            {
                proc.CloseMainWindow();
            }
        }
        private void LoadDirectory()
        {
            lstCommands.Items.Clear();
            switch (QEvent)
            {
                case "music directory":
                    string[] files = Directory.GetFiles(BrowseDirectory, ".mp3", SearchOption.AllDirectories);
                    foreach (string file in files) {lstCommands.Items.Add(file.Replace(BrowseDirectory, ""));}
                    break;
                case "video directory":
                    files = Directory.GetFiles(BrowseDirectory, "*", SearchOption.AllDirectories);
                    foreach (string file in files) {lstCommands.Items.Add(file.Replace(BrowseDirectory, ""));}
                    break;
                case "picture directory":
                    files = Directory.GetFiles(BrowseDirectory, "*", SearchOption.AllDirectories);
                    foreach (string file in files) {lstCommands.Items.Add(file.Replace(BrowseDirectory, ""));}
                    break;
                case "load directory":
                    files = Directory.GetFiles(BrowseDirectory, "*", SearchOption.AllDirectories);
                    foreach (string file in files) {lstCommands.Items.Add(file.Replace(BrowseDirectory + "\\", ""));}
                    break;
            }
        }

        private void lstCommands_SelectedIndexChanged(object sender, EventArgs e)
        {
            Object open = BrowseDirectory + lstCommands.SelectedItem;
            try
            { System.Diagnostics.Process.Start(open.ToString()); }
            catch { open = BrowseDirectory + "\\" + lstCommands.SelectedItem; System.Diagnostics.Process.Start(open.ToString()); }
        }

        private void GetWeather()
        {
            string query = String.Format("http://weather.yahooapis.com/forecastrss?w=2434216");
            XmlDocument wData = new XmlDocument();
            wData.Load(query);

            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;
        }
    }
}
4

2 回答 2

2

当你有文本时,为什么需要从文本框中检索文本

string speech = e.Result.Text;

已经?您需要做的就是对字符串进行 URLEncode(此处为示例函数)并使用 URL 打开浏览器窗口

于 2013-07-15T16:54:44.650 回答
-1

使用 Google 图片搜索 API 开发者,获取图片 url,声明 xml 文档,制作变量。 https://developers.google.com/image-search/v1/devguide

于 2013-08-01T00:00:17.967 回答