好的。我一直在研究语音识别程序。当你说“在网上搜索猫的图像”时,我想这样做,它会打开浏览器并搜索猫的图像。我制作了一个文本框来接收用户语音的输入,但我很难找到如何从文本框中检索文本并将其发送到网络。其中 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;
}
}
}