lib/punchblock/translator/input_component.rb in punchblock-1.9.4 vs lib/punchblock/translator/input_component.rb in punchblock-2.0.0.beta1

- old
+ new

@@ -3,19 +3,13 @@ module Punchblock module Translator module InputComponent def execute validate - - @recognizer = DTMFRecognizer.new self, - @component_node.grammar.value, - (@component_node.initial_timeout || -1), - (@component_node.inter_digit_timeout || -1) - + setup_dtmf_recognizer send_ref - - @dtmf_handler_id = register_dtmf_event_handler + start_timers rescue OptionError => e with_error 'option error', e.message end def process_dtmf(digit) @@ -30,12 +24,12 @@ else super end end - def match(mode, confidence, utterance, interpretation) - complete Punchblock::Component::Input::Complete::Success.new(:mode => mode, :confidence => confidence, :utterance => utterance, :interpretation => interpretation) + def match(match) + complete success_reason(match) end def nomatch complete Punchblock::Component::Input::Complete::NoMatch.new end @@ -44,17 +38,43 @@ complete Punchblock::Component::Input::Complete::NoInput.new end private + def input_node + @component_node + end + def validate - raise OptionError, 'A grammar document is required.' unless @component_node.grammar - raise OptionError, 'A mode value other than DTMF is unsupported.' unless @component_node.mode == :dtmf + raise OptionError, 'A grammar document is required.' unless input_node.grammars.first + raise OptionError, 'Only a single grammar is supported.' unless input_node.grammars.size == 1 + raise OptionError, 'A mode value other than DTMF is unsupported.' unless input_node.mode == :dtmf end + def setup_dtmf_recognizer + @recognizer = DTMFRecognizer.new self, + input_node.grammars.first.value, + (input_node.initial_timeout || -1), + (input_node.inter_digit_timeout || -1), + input_node.terminator + end + + def start_timers + @recognizer.start_timers + end + + def success_reason(match) + nlsml = RubySpeech::NLSML.draw do + interpretation confidence: match.confidence do + instance match.interpretation + input match.utterance, mode: match.mode + end + end + Punchblock::Component::Input::Complete::Match.new :nlsml => nlsml + end + def complete(reason) unregister_dtmf_event_handler - @recognizer.finalize if @recognizer send_complete_event reason end end end end