marniekayulistina/tubes-sismul-if8-2009-speechtotext — pull request #1 by asepsaepudin1 (2013-01-05 08:58:33+00:00)
Title: Update code

///

/// Loads the grammar and commands.
///

private void loadGrammarAndCommands()
{
try
{
Choices texts = new Choices();
string[] lines = File.ReadAllLines(Environment.CurrentDirectory + "\\example.txt");
foreach (string line in lines)
{
// skip commentblocks and empty lines..
if (line.StartsWith("--") || line == String.Empty) continue;

// split the line
var parts = line.Split(new char[] { '|' });

// add commandItem to the list for later lookup or execution
words.Add(new Word() { Text = parts[0], AttachedText = parts[1], IsShellCommand = (parts[2] == "true") });

// add the text to the known choices of speechengine
texts.Add(parts[0]);
}
Grammar wordsList = new Grammar(new GrammarBuilder(texts));
speechRecognitionEngine.LoadGrammar(wordsList);
}
catch (Exception ex)
{
throw ex;
}
}

///

/// Gets the known command.
///

/// The order.
///
private string getKnownTextOrExecute(string command)
{
try
{
var cmd = words.Where(c => c.Text == command).First();

if (cmd.IsShellCommand)
{
Process proc = new Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = cmd.AttachedText;
proc.Start();
return "you just started : " + cmd.AttachedText;
}
else
{
return cmd.AttachedText;
}
}
catch (Exception)
{
return command;
}
}