private static GrammarBuilder CreateLightCommandGrammar(LightActionSpec lightActionSpec) { var allLightsIdentifierChoices = GrammarHelper.ConvertIdentyListToGrammarBuilder(lightActionSpec.LightIdentifiers.ToList()); var lightActions = lightActionSpec.GetAllActions(); var lightsLables = lightActionSpec.GetLightSubjectChoices(); var actionGrammarBuilders = new List <GrammarBuilder>(); foreach (var action in lightActions) { var actionChoices = action.ToChoices(); var actionGrammer1 = new GrammarBuilder(); actionGrammer1.AppendWildcard(); actionGrammer1.Append(new SemanticResultKey(LightActionSemanticKey, actionChoices)); actionGrammer1.AppendWildcard(); actionGrammer1.Append(new SemanticResultKey(LightIdentifierSemanticKey, allLightsIdentifierChoices)); actionGrammer1.AppendWildcard(); actionGrammer1.Append(new SemanticResultKey(CommandSubjectSemanticKey, lightsLables)); var actionGrammar2 = new GrammarBuilder(); actionGrammar2.AppendWildcard(); actionGrammar2.Append(new SemanticResultKey(LightActionSemanticKey, actionChoices)); actionGrammar2.AppendWildcard(); actionGrammar2.Append(new SemanticResultKey(CommandSubjectSemanticKey, lightsLables)); actionGrammar2.AppendWildcard(); actionGrammar2.Append(new SemanticResultKey(LightIdentifierSemanticKey, allLightsIdentifierChoices)); actionGrammarBuilders.Add(actionGrammer1); actionGrammarBuilders.Add(actionGrammar2); } return(new GrammarBuilder(new Choices(actionGrammarBuilders.ToArray()))); }
public HouseVoiceCommandHandler() { CommandInitiated = false; LastSpeechTime = DateTime.MinValue; LightActionSpec = new LightActionSpec(); }
private static GrammarBuilder CreateLightCommandGrammar(LightActionSpec lightActionSpec) { var allLightsIdentifierChoices = GrammarHelper.ConvertIdentyListToGrammarBuilder(lightActionSpec.LightIdentifiers.ToList()); var lightActions = lightActionSpec.GetAllActions(); var lightsLables = lightActionSpec.GetLightSubjectChoices(); var actionGrammarBuilders = new List<GrammarBuilder>(); foreach(var action in lightActions) { var actionChoices = action.ToChoices(); var actionGrammer1 = new GrammarBuilder(); actionGrammer1.AppendWildcard(); actionGrammer1.Append(new SemanticResultKey(LightActionSemanticKey, actionChoices)); actionGrammer1.AppendWildcard(); actionGrammer1.Append(new SemanticResultKey(LightIdentifierSemanticKey, allLightsIdentifierChoices)); actionGrammer1.AppendWildcard(); actionGrammer1.Append(new SemanticResultKey(CommandSubjectSemanticKey, lightsLables)); var actionGrammar2 = new GrammarBuilder(); actionGrammar2.AppendWildcard(); actionGrammar2.Append(new SemanticResultKey(LightActionSemanticKey, actionChoices)); actionGrammar2.AppendWildcard(); actionGrammar2.Append(new SemanticResultKey(CommandSubjectSemanticKey, lightsLables)); actionGrammar2.AppendWildcard(); actionGrammar2.Append(new SemanticResultKey(LightIdentifierSemanticKey, allLightsIdentifierChoices)); actionGrammarBuilders.Add(actionGrammer1); actionGrammarBuilders.Add(actionGrammar2); } return new GrammarBuilder(new Choices(actionGrammarBuilders.ToArray())); }
private void Engine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { // Handle special Action Start. if (string.Equals(e.Result.Text, InitiateCommandsPhrase, StringComparison.OrdinalIgnoreCase)) { Console.WriteLine("Acknowledged..."); ComputerFeedbackPlayer.PlayComputerInit(); CommandInitiated = true; } // Handle special action Cancel Override. else if (string.Equals(e.Result.Text, CancelProgramCommand, StringComparison.OrdinalIgnoreCase)) { Console.WriteLine("Program Quit Detected."); speechCancellationTokenSource.Cancel(); } else { if (!CommandInitiated) { // Ignore matching commands if Commands are not initiated by start command. return; } var semantics = e.Result.Semantics; if (!semantics.ContainsKey(CommandSubjectSemanticKey)) { Console.WriteLine("Grammar recognized but no subject was found on which to take an action."); //throw new Exception("Grammar recognized but no subject was found on which to take an action."); } var subject = e.Result.Semantics[CommandSubjectSemanticKey]; // TODO this should be configured not hardcoded for when there are more than just light actions. if (Equals(subject.Value, LightActionSpec.LightSubjectSemanticValue)) { ComputerFeedbackPlayer.PlayComputerAck(); if (!semantics.ContainsKey(LightIdentifierSemanticKey) || !semantics.ContainsKey(LightActionSemanticKey)) { Console.WriteLine("A command with the light subject must contain a light identifier and action semantic."); //throw new Exception("A command with the light subject must contain a light identifier and action semantic."); } // TODO consider also making this configured by the action spec itself. Something like expected Semantic Keys. var identifier = e.Result.Semantics[LightIdentifierSemanticKey]; var action = e.Result.Semantics[LightActionSemanticKey]; LightActionSpec.ExecuteAction((string)action.Value, (string)identifier.Value); Console.WriteLine("Grammer match: {0}", e.Result.Text); Console.WriteLine("subject:{0}, action:{1}, identifier:{2}", subject.Value, action.Value, identifier.Value); Console.WriteLine("With Confidence {0}", e.Result.Confidence); } else { Console.WriteLine("Subject value {0} was found but can not be bound to an action", subject.Value); //throw new Exception(string.Format("Subject value {0} was found but can not be bound to an action", subject.Value)); } } // Do not share a variable with the above code, this is to protect against long processing times. SetNewSpeechTime(DateTime.UtcNow); }
public HouseVoiceCommandHandler() { CommandInitiated = false; LastSpeechTime = DateTime.MinValue; LightActionSpec = new LightActionSpec(); }