asr_vosk: Allow to handle keyword and command in one sentence
[ros_wild_thumper.git] / scripts / asr_vosk.rb
index bc73949..42ae515 100755 (executable)
@@ -19,17 +19,19 @@ CONFIG = {
 class Speak
        def initialize(node)
                @logger = Logger.new(STDOUT)
-               @jsgf_enabled = false
+               @commands_enabled = false
                @publisher = node.advertise('asr_result', Std_msgs::String)
 
+               # Websocket handling
                EM.run do
-                       Signal.trap("INT")  { send_eof }
+                       Signal.trap("INT") { send_eof }
                        @ws = WebSocket::EventMachine::Client.connect(:uri => 'ws://192.168.36.4:2700')
 
                        def send_eof
                                @ws.send '{"eof" : 1}'
                        end
 
+                       # Loop over all input data
                        def run
                                while true do
                                        data = ARGF.read(16000)
@@ -69,37 +71,42 @@ class Speak
                                @logger.debug "word=" + result["word"]
                        end
 
+                       # check for keywords first
                        text = msg["text"]
                        @logger.debug "text=" + msg["text"]
-                       if KEYWORDS.include? text
-                               keyword_detect(text)
-                               return
+                       KEYWORDS.each do |keyword|
+                               if text.include? keyword
+                                       keyword_detected(keyword)
+                                       text = text.gsub(keyword, "").strip
+                               end
                        end
-                       if @jsgf_enabled
-                               final_result(msg["text"])
+
+                       # not a keyword, handle command if enabled
+                       if @commands_enabled and text.length > 0
+                               final_result(text)
                        end
                end
        end
 
-       # Enables/Disables the jsgf pipeline branch
-       def enable_jsgf(bEnable)
-               @jsgf_enabled = bEnable
+       # Enables/Disables the speech command
+       def enable_commands(bEnable)
+               @commands_enabled = bEnable
        end
 
-       # Result of jsgf pipeline branch
+       # Resulting speech command
        def final_result(hyp)
                @logger.info "final: " + hyp
-               enable_jsgf(false)
+               enable_commands(false)
 
-               # Publish pocketsphinx result as ros message
+               # Publish vosk result as ros message
                msg = Std_msgs::String.new
                msg.data = hyp
                @publisher.publish(msg)
        end
 
-       def keyword_detect(hyp)
+       def keyword_detected(hyp)
                @logger.debug "Got keyword: " + hyp
-               enable_jsgf(true)
+               enable_commands(true)
        end
 end