2013-07-14 60 views
1

好的。我一直在研究语音识别程序。我想说,当你说一些类似于“在网上搜索猫的图像”时,它会打开浏览器并搜索猫的图像。我创建了一个文本框来接收用户语音的输入,但我一直难以找到如何从文本框中检索文本并将其发送到网络。 Where textbox1.text = speech;是演讲转换为文本的地方。 继承人到目前为止我的代码:通过C#语音搜索网页

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; 
     } 
    } 
} 
+1

你的问题是什么? – DevT

回答

2

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

string speech = e.Result.Text; 

了吗?您只需对该字符串进行URLEncode(示例函数here)并打开browser window with the URL即可。

-1

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

+0

请详细说明您对问题的回答。 –

+0

嗨阿隆,这只是一个想法。我已经开发出我的Jarvis,能够阅读yweather和它的工作正常。我会尽力在这个星期尝试一下。 – Alfy

+0

嘿,如果它只有一个想法,评论而不是提出“答案”。 –