Created
February 24, 2009 02:24
-
-
Save tessro/69342 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Windows.Forms; | |
using System.Speech.Recognition; | |
using System.Net; | |
using System.Web; | |
using System.IO; | |
namespace ListenerApplication | |
{ | |
public partial class Form1 : Form | |
{ | |
protected SpeechRecognitionEngine _engine; | |
protected string _address = @"http://192.168.1.106:4242"; | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private void Post(string address, string body) | |
{ | |
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; | |
request.Method = @"POST"; | |
request.ContentType = @"application/x-www-form-urlencoded"; | |
byte[] byteData = UTF8Encoding.UTF8.GetBytes(body); | |
request.ContentLength = byteData.Length; | |
using (Stream postStream = request.GetRequestStream()) | |
{ | |
postStream.Write(byteData, 0, byteData.Length); | |
} | |
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) | |
{ | |
StreamReader reader = new StreamReader(response.GetResponseStream()); | |
Console.WriteLine(reader.ReadToEnd()); | |
} | |
} | |
private void SendCommand(RecognitionResult result) | |
{ | |
StringBuilder data = new StringBuilder(); | |
data.Append("text=" + result.Text); | |
System.Xml.XmlDocument doc = result.ConstructSmlFromSemantics() as System.Xml.XmlDocument; | |
StringWriter sw = new StringWriter(); | |
System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(sw); | |
doc.WriteTo(xw); | |
data.Append("&semantics=" + sw.ToString()); | |
try | |
{ | |
Post(_address, data.ToString()); | |
} | |
catch (WebException e) | |
{ | |
MessageBox.Show(String.Format("Error during POST:\n{0}\n", e.Message)); | |
} | |
} | |
private void ReloadGrammars() | |
{ | |
try | |
{ | |
Grammar g = new Grammar(@"..\..\grammar.xml"); | |
g.Name = "command grammar"; | |
g.Enabled = true; | |
_engine.UnloadAllGrammars(); | |
_engine.LoadGrammar(g); | |
} | |
catch (System.ArgumentException exp) | |
{ | |
MessageBox.Show(String.Format("Invalid constructor arguments:\n{0}\n", exp.Message)); | |
} | |
catch (Exception exp) | |
{ | |
MessageBox.Show(String.Format("Failed to load.\nError message:\n{0}", exp.Message)); | |
} | |
} | |
private void Form1_Load(object sender, EventArgs e) | |
{ | |
_engine = new SpeechRecognitionEngine(); | |
_engine.SpeechDetected += new EventHandler<SpeechDetectedEventArgs>(recognizer_SpeechDetected); | |
_engine.SpeechRecognitionRejected += new EventHandler<SpeechRecognitionRejectedEventArgs>(recognizer_SpeechRecognitionRejected); | |
_engine.SpeechHypothesized += new EventHandler<SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized); | |
_engine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized); | |
_engine.SetInputToDefaultAudioDevice(); | |
ReloadGrammars(); | |
_engine.RecognizeAsync(RecognizeMode.Multiple); | |
} | |
private void recognizer_SpeechDetected(object sender, SpeechDetectedEventArgs args) | |
{ | |
voiceText.Text = ""; | |
} | |
private void recognizer_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs args) | |
{ | |
voiceText.Font = new Font(voiceText.Font, FontStyle.Regular); | |
voiceText.Text = "(rejected)"; | |
} | |
private void recognizer_SpeechHypothesized(object sender, SpeechHypothesizedEventArgs args) | |
{ | |
voiceText.Font = new Font(voiceText.Font, FontStyle.Regular); | |
voiceText.Text = args.Result.Text; | |
} | |
private void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs args) | |
{ | |
voiceText.Font = new Font(voiceText.Font, FontStyle.Bold); | |
voiceText.Text = args.Result.Text; | |
if (args.Result.Semantics[@"agent"].Value.ToString() == @"system") | |
{ | |
SemanticValue details = args.Result.Semantics[@"details"]; | |
if (details.ContainsKey(@"command") == true && details[@"command"].Value != null) { | |
string command = details[@"command"].Value.ToString(); | |
if (command == @"reload-grammar") | |
{ | |
ReloadGrammars(); | |
voiceText.Text += " [OK]"; | |
Post(@"http://192.168.1.106:4567", ""); | |
} | |
} | |
} | |
else | |
{ | |
SendCommand(args.Result); | |
} | |
} | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
ReloadGrammars(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment