【发布时间】: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