예제 #1
0
    def chop_word(self, command):
        """Removes a single word from a command.
        
        **INPUTS**
        
        *[STR]* command -- The command. It is a list of written\spoken words.
        

        **OUTPUTS**
        
        Returns a tuple *(chopped_word, consumed, rest)* where:

        *STR* chopped_word -- The spoken form of the first word

        *INT* consumed -- Number of words consumed (always 1, but
         return it anyway because want to keep same method signature
         as chop_CSC, chop_LSA and chop_symbol).

        *[STR]* rest -- Rest of the command after the word was chopped
        
        """
        
        chopped_word, dummy = sr_interface.spoken_written_form(command[0])
        consumed = 1
        rest = command[1:]
        return chopped_word, consumed, rest
예제 #2
0
    def massage_command(self, command):
        """Massages a command to prepare it for interpretation.

        Makes sure to substitute special characters (e.g. {Spacebar})
        in the written form of words in the command. Also, makes sure
        that the spoken forms are all lowercase, and contain no
        multiple, leading or trailing blanks.
        
        **INPUTS**
        
        *[STR]* command -- The command to be massaged. It's a list of
         written\spoken words.
        
        **OUTPUTS**
        
        *[STR] mod_command* -- The massaged command
        """
        
        mod_command = []
        for a_word in command:
            spoken, written = sr_interface.spoken_written_form(a_word)
            written = sr_interface.clean_written_form(written, clean_for='vc')
            spoken = re.sub('\s+', ' ', spoken)
            spoken = re.sub('(^\s+|\s+$)', '', spoken)
            mod_command = mod_command + [sr_interface.vocabulary_entry(spoken, written, clean_written=0)]
        return mod_command
예제 #3
0
 def utterance_spoken_forms(self, utterance, echo_cmd=0):
     spoken_forms = []
     for a_word in utterance:
         spoken, written = sr_interface.spoken_written_form(a_word,
                                                            clean_written=0,
                                                            clean_spoken=0)
         spoken_forms.append(spoken)
     return spoken_forms
예제 #4
0
    def chop_CSC(self, cmd):
        """Chops the start of a command if it starts with a CSC.
        
        **INPUTS**
        
        *[STR]* cmd -- The command. It is a list of written\spoken words.

        **OUTPUTS**

        Returns a tuple *(chopped_CSC, consumed, rest)* where:
        
        *STR* chopped_symbol -- The written form of the CSC that
        was chopped off. If *None*, it means *cmd* did
        not start with a known CSC.

        *INT* consumed* -- Number of words consumed by the CSC from
         the command

        *[STR]* rest -- is what was left of *cmd* after the CSC
         was chopped off.        
        """
        
#        print '-- CmdInterp.chop_CSC: cmd=%s' % cmd

        chopped_CSC, consumed, rest = None, 0, cmd

        #
        # Create list of spoken forms of the words in command
        #
        words = []
        for a_word in cmd:
            a_word, dummy = sr_interface.spoken_written_form(a_word)
            words = words + [a_word]

        #
        # Starting with the whole command and dropping words from the end,
        # check if that corresponds to the spoken form of a known CSC.
        #
        upto = len(words)
        while upto:
            a_spoken_form = string.join(words[:upto], ' ')
#            print '-- CmdInterp.chop_CSC: upto=%s, a_spoken_form=%s' % (upto, a_spoken_form)

            if self.cmd_index.has_key(a_spoken_form):
                #
                # This corresponds to the spoken form of a CSC and it's the
                # longest one we'll ever find.
                #
#                print '-- CmdInterp.chop_CSC: matches known CSC \'%s\'' % a_spoken_form
                chopped_CSC = a_spoken_form
                rest = cmd[upto:]
                consumed = upto
                break
            upto = upto - 1
        return chopped_CSC, consumed, rest
예제 #5
0
    def load_language_specific_aliases(self):
        
        """Loads words specific to the language of the current buffer,
        if needed.

        Also, unloads words specific to previously loaded language if needed.
        
        **INPUTS**
        
        *none*

        **OUTPUTS**
        
        *none* -- 
        """

        language = self.on_app.active_language()
        last_language = self.last_loaded_language
#        print '-- CmdInterp.load_language_specific_aliases: called, language=%s, last_language=%s' % (language, last_language)
#        print '-- CmdInterp.load_language_specific_aliases: self.language_specific_aliases[%s]=%s' % (language, self.language_specific_aliases[language])
        if language != last_language:
            #
            # Remove words from previous language (unless it was None
            # which means LSAs not specific to a particular language)
            #
            if last_language != None and \
               self.language_specific_aliases.has_key(last_language):
                for a_word in self.language_specific_aliases[last_language]:
                    sr_interface.deleteWord(a_word)
                    spoken_as, written_as = sr_interface.spoken_written_form(a_word)
            
            #
            # Add words for new language (unless language == None, which
            # means LSAs that are not language specific)
            #
            if language != None and \
               self.language_specific_aliases.has_key(language):
                for a_word in self.language_specific_aliases[language]:
                    sr_interface.addWord(a_word)
                    spoken_as, written_as = sr_interface.spoken_written_form(a_word)

            self.last_loaded_language = language
예제 #6
0
    def chop_symbol(self, command):
        """Chops off the beginning of a command if it is a known symbol.
        
        **INPUTS**
        
        *[STR]* command -- The command. It is a list of written\spoken words.

        **OUTPUTS**

        Returns a tuple *(chopped_symbol, consumed, rest)* where:
        
        *STR* chopped_symbol -- The written form of the known symbol that
        was chopped off. If *None*, it means *command* did
        not start with a known symbol.

        *INT* consumed* -- Number of words consumed by the symbol from
         the command

        *[STR]* rest -- is what was left of *command* after the symbol
         was chopped off.        
        """

#        print '-- CmdInterp.chop_symbols: command=%s' % command

        chopped_symbol, consumed, rest = None, 0, command

        #
        # Create list of spoken forms of the words in command
        #
        words = []
        for a_word in command:
            a_word, dummy = sr_interface.spoken_written_form(a_word)
            words = words + [a_word]

        #
        # Starting with the whole command and dropping words from the end,
        # check if that corresponds to a known symbol.
        #
        upto = len(words)
        while upto:
            a_spoken_form = string.join(words[:upto], ' ')
#            print '-- CmdInterp.chop_symbols: upto=%s, a_spoken_form=%s' % (upto, a_spoken_form)
            if self.known_symbols.spoken_form_info.has_key(a_spoken_form):
                # This corresponds to the spoken form of a symbol
#                print '-- CmdInterp.chop_symbols: matches a known symbol'
                chopped_symbol = self.choose_best_symbol(a_spoken_form, self.known_symbols.spoken_form_info[a_spoken_form].symbols)
                rest = command[upto:]
                consumed = upto
                break
            upto = upto - 1
        return chopped_symbol, consumed, rest
예제 #7
0
    def say(self,
            utterance,
            user_input=None,
            never_bypass_sr_recog=0,
            echo_utterance=0,
            echo_cmd=0):
        """Simulate an utterance *STR utterance*

        *STR utterance* -- The utterance. This can be a string with the
         written form of what should be recognised by the SR system. If
         it's a list, it should be a list of words in their written\spoken
         form (or just written if it doesn't have a spoken form different
         from its written form).

        In general, it's better to specify *utterance* as a list of
        written\spoken words because it allows to simulate exactly what
        the SR does (e.g. what if the SR recognises an LSA as a sequence
        of words instead of its written\spoken form?)

        *STR user_input* -- A string that will be sent to the mediator console's
         standard input. Use in automated regression testing, if the *say*
         command requires user additional user input (e.g. confirmation of
         a symbol match).
        
        *BOOL echo_utterance=0* -- If true, echo the utterance on STDOUT.
        
        BOOL *never_bypass_sr_recog* -- If *TRUE*, the interpretation will always be done
        through NatLink's recognitionMimic function, even if the 'bypass' switch was on.



        Examples: say('x not equal to') -> 'x != '
                  say(['x', ' != \\not equal to'] -> 'x != '
        """

        global sleep_before_recognitionMimic

        if self.should_exit:
            trace('SimCmdsObj.say', 'cancelling testing')
            raise mediator_exceptions.CancelTesting()

        if echo_cmd:
            self.echo_command('say', utterance, user_input,
                              never_bypass_sr_recog, echo_utterance)

        #    print 'Saying: %s' % utterance
        sys.stdout.flush()
        if echo_utterance:
            print 'Saying: %s' % utterance

        if user_input:
            #
            # Create temporary user input file
            #
            old_stdin = sys.stdin
            temp_file = None
            if 0:
                temp_file_name = vc_globals.tmp + os.sep + 'user_input.dat'
                temp_file = open(temp_file_name, 'w')
                #        print 'temp file opened for writing'
                sys.stdout.flush()
                temp_file.write(user_input)
                temp_file.close()
                temp_file = open(temp_file_name, 'r')
                #        print 'temp file opened for reading'
                sys.stdin = temp_file
            else:
                sys.stdin = StringIO(user_input)
            sys.stdout.flush()

        try:
            if self.bypass_sr_recog and not never_bypass_sr_recog:
                trace('SimCmdsObj.say', 'bypassing NatSpeak')
                sys.stdout.flush()
                if util.islist(utterance) or util.istuple(utterance):
                    spoken = self.utterance_spoken_forms(utterance)
                else:
                    utterance = re.split('\s+', utterance)
                    spoken = utterance

                print "Heard %s" % string.join(spoken)
                dictation_allowed = self.app.recog_begin(None)
                self.app.synchronize_with_app()
                buff_name = self.app.curr_buffer_name()
                active_field = self.app.active_field()
                dictation_allowed = dictation_allowed and \
                    (active_field == None)
                if self.testing and not dictation_allowed:
                    trace('SimCmdsObj.say', 'cancelling testing')
                    raise mediator_exceptions.CancelTesting()

                self.interp.interpret_NL_cmd(utterance, self.app)
                self.app.recog_end()
                self.show_buff()
            else:
                trace('SimCmdsObj.say', 'NOT bypassing NatSpeak')
                if util.islist(utterance) or util.istuple(utterance):
                    words = []
                    #
                    # Clean up the written form in case user didn't type
                    # special characters in the form that the SR expects
                    # (e.g. '\n' instead of '{Enter}'
                    #
                    for a_word in utterance:
                        # Make sure word is in-vocabulary
                        make_sure_word_is_in_vocab(a_word)
                        # don't want to clean any more
                        spoken, written = sr_interface.spoken_written_form(
                            a_word, clean_written=0, clean_spoken=0)
                        if spoken != written:
                            # don't want to do this any more
                            #                        written = sr_interface.clean_written_form(written, clean_for='sr')
                            words = words + [
                                sr_interface.vocabulary_entry(spoken, written)
                            ]
                        else:
                            words = words + [written]
                else:
                    words = re.split('\s+', utterance)
                    for a_word in words:
                        make_sure_word_is_in_vocab(a_word)

                trace('SimCmdsObj.say', 'words=%s' % words)

                #            for word in words:
                #                print word, natlink.getWordInfo(word)
                #        print '-- mediator.say: words=%s' % words
                sys.stderr.flush()

                #
                # During interactive sessions, may need to pause a few seconds before
                # doing *recognitionMimic*, to give user time to switch to the editor
                # window.
                #
                if sleep_before_recognitionMimic:
                    print '\n\n********************\nPlease click on the editor window before I "say" your utterance.\nYou have %s seconds to do so.\n********************' % sleep_before_recognitionMimic
                    time.sleep(sleep_before_recognitionMimic)

                sys.stderr.flush()

                natlink.recognitionMimic(words)
                sys.stderr.flush()
                if not self.app.alive:
                    trace('SimCmdsObj.say', 'about to raise socket error')
                    sys.stderr.flush()
                    raise \
                        SocketError("socket connection broken during callbacks")
                if self.should_exit:
                    trace('SimCmdsObj.say', 'cancelling testing')
                    sys.stderr.flush()
                    raise mediator_exceptions.CancelTesting()

        finally:
            sys.stderr.flush()
            #
            # Redirect stdin back to what it was
            #
            if user_input:
                sys.stdin = old_stdin
                if not (temp_file is None):
                    temp_file.close()