/// <summary> /// Creates a mode with the specified name. /// </summary> /// <param name="name">The name of the mode to create.</param> /// <returns>A new mode with the specified name.</returns> public CommandRecognizerMode AddMode(string name) { CommandRecognizerMode mode = new CommandRecognizerMode(name, m_grammar, m_firstRuleId, m_firstRuleId + MaxRulesPerMode - 1); m_modes.Add(mode); m_firstRuleId += MaxRulesPerMode; return mode; }
void CommandExecuted(CommandRecognizerMode.CommandHandler handler) { RestorePreviousMode(); }
public void AddCommand(string commandPhrase, CommandRecognizerMode.CommandHandler handler) { if (m_nextRuleId > m_lastRuleId) { throw new Exception("Maximum number of rules exceeded for Command Recognizer Mode '" + Name + "'."); } List<CommandPart> parts = GetParts(commandPhrase); int ruleId = m_nextRuleId; ++m_nextRuleId; m_handlers[ruleId] = handler; // Create a new rule in the grammar ISpeechGrammarRule rule = m_grammar.Rules.Add("", SpeechRuleAttributes.SRATopLevel, ruleId); // Add a state for each part of the phrase and connect them together with // appropriate transitions ISpeechGrammarRuleState from = rule.InitialState; for (int i = 0; i < parts.Count; ++i) { CommandPart part = parts[i]; ISpeechGrammarRuleState state = null; if (i != parts.Count - 1) { state = rule.AddState(); } object propertyValue = 0; if (part.isWordFromList) { // Add a transition through a list rule ISpeechGrammarRule listRule = m_grammar.Rules.FindRule(part.phrase); if (listRule == null) { listRule = m_grammar.Rules.Add(part.phrase, 0, 0); propertyValue = "placeholder"; listRule.InitialState.AddWordTransition(null, "placeholder", " ", SpeechGrammarWordType.SGLexical, part.phrase, 0, ref propertyValue, 1); propertyValue = 0; } from.AddRuleTransition(state, listRule, "", 0, ref propertyValue, 1); } else { // Add a simple rule transition from.AddWordTransition(state, part.phrase, " ", SpeechGrammarWordType.SGLexical, "", 0, ref propertyValue, 1); } from = state; } }
internal void AddToCommandRecognizer() { m_commandRecognizerMode = Manager.Recognizer.AddMode(Name); m_commandRecognizerMode.ActiveStateChanged += RaiseActiveStateChanged; m_commandRecognizerMode.CommandExecuted += CommandExecuted; m_commandRecognizerMode.CommandNotRecognized += CommandNotRecognized; foreach (PersonalityCommand command in Commands) { CommandRecognizerMode.AddCommand(command.Phrase, command.Run); } }