internal static IEnumerable <string> EvaluateCommand(string response) { string temp = string.Empty; switch (response.ToLower()) { case "showparents": return(KeywordFactory.GetParentNames()); case "showparents -c": return(KeywordFactory.GetParentNames(true)); case "getdefault": return((!string.IsNullOrWhiteSpace(KeywordFactory.DefaultParent)) ? new string[] { KeywordFactory.DefaultParent } : new string[] { "Not Set" }); case "removedefault": return(new string[] { KeywordFactory.RemoveParent() }); default: return(new string[] { "Command not found." }); } }
private static void OnSpeechRecognized(object sender, SpeechRecognizedEventArgs e) { List <ChildKeyword> children = null; int childIndex = -1; ParentKeyword parent = null; bool defaultParentIsUsed = !(KeywordFactory.GetParentNames().Any(s => s == e.Result.Words[0].Text)); if (!string.IsNullOrWhiteSpace(KeywordFactory.DefaultParent) && defaultParentIsUsed) { parent = KeywordFactory.GetParent(KeywordFactory.DefaultParent); } else { parent = KeywordFactory.GetParent(e.Result.Words[0].Text); } if (parent != null) { children = KeywordFactory.GetChildren(parent).ToList(); string wordToCheck = GetChildKeywordToCheck(e, !string.IsNullOrEmpty(KeywordFactory.DefaultParent) && defaultParentIsUsed); childIndex = children.FindIndex(c => c.Keyword == wordToCheck); } if (children != null && childIndex > -1) { string sequence = children[childIndex].KeySequence; int result = 0; if (int.TryParse(e.Result.Words[e.Result.Words.Count - 1].Text, out result)) { int index = sequence.IndexOf('}'); sequence = sequence.Insert(index, " " + result.ToString()); } SendKeys.SendWait(sequence); } }
private static void LoadGrammar() { if (sre != null && sre.Grammars.Count > 0) { sre.RecognizeAsyncStop(); sre.UnloadAllGrammars(); } string setDefaultWord = KeywordFactory.DefaultParent; string[] defaultSubChoices = null; string[] rootChoices = new string[KeywordFactory.Parents.Count]; GrammarBuilder[] gbAll = new GrammarBuilder[KeywordFactory.Parents.Count]; Choices choices = null; for (int p = 0; p < KeywordFactory.Parents.Count; p++) { string[] subChoices = null; Choices choicesSub = null; bool defaultExists = false; rootChoices[p] = KeywordFactory.Parents[p].Keyword; if (rootChoices[p] == setDefaultWord) { defaultExists = true; defaultSubChoices = KeywordFactory.GetChildrenNames( KeywordFactory.Parents .Where(parent => parent.Keyword == setDefaultWord) .Select(parent => parent) .Single()) .ToArray(); } else { choices = new Choices(rootChoices[p]); subChoices = new string[KeywordFactory.Parents[p].Children.Count]; for (int c = 0; c < KeywordFactory.Parents[p].Children.Count; c++) { subChoices[c] = KeywordFactory.Parents[p].Children[c].Keyword; } choicesSub = new Choices(subChoices); } // Create grammar builder if (!defaultExists) { gbAll[p] = new GrammarBuilder(rootChoices[p]) { Culture = new System.Globalization.CultureInfo("en-GB") }; gbAll[p].Append(choicesSub); } else { gbAll[p] = new GrammarBuilder(new Choices(defaultSubChoices)) { Culture = new System.Globalization.CultureInfo("en-GB") }; } // Check for quantity string[] quantifiers = KeywordFactory.GetQuantifiers(); Choices quantiferChoices = new Choices(quantifiers); gbAll[p].Append(quantiferChoices, 0, 1); } Choices allChoices = new Choices(); allChoices.Add(gbAll); Grammar gram = new Grammar(allChoices) { Name = "Actions", Enabled = true }; try { sre = new SpeechRecognitionEngine(); sre.SetInputToDefaultAudioDevice(); sre.LoadGrammar(gram); gram.SpeechRecognized += OnSpeechRecognized; sre.RecognizeAsync(RecognizeMode.Multiple); Console.WriteLine("*** Recognition Engine Ready! ***\n"); } catch (Exception e) { Console.WriteLine($"Uh oh:\n\n {e.Message}"); } }