예제 #1
0
class State:
    keywords = {}
    def __init__(self):
        logging.info("Instantiating State class: %s" % self.__class__.__name__)
        
        # Add keywords from superclasses
        self.keywords = State.fold_keywords( self.__class__, self.keywords)

        # If the State doesn't have a LanguageModel set, then
        # Automatically create LanguageModel specific to the keywords of this State
        if not hasattr(self,'lm'):
            logging.info("We need to create a LanguageModel for this State")
            commands_array = self.keywords.keys()
            self.lm = LanguageModel(self.__class__.__name__,commands_array)
            self.lm.update_all()
            logging.info("LanguageModel created")
    
    @staticmethod
    def fold_keywords(clazz, keywords):
        for base in clazz.__bases__:
            keywords.update( State.fold_keywords(base, base.keywords))
        return keywords

    def process(self, text):
        state_change = []
        if text in self.keywords:
            state_change = self.keywords[text]
            if type(state_change) not in [list,tuple]:
                state_change = [ state_change ]
        logging.info('Processed text = %s with result = %s' % (text, state_change))
        return state_change
예제 #2
0
    }
class PlayingMedia(Base):
    keywords = {
        'STOP': (lambda: rc.stop_playing(), 'Idle'),
        'PAUSE': lambda: rc.pause()
    }
    context = {
        'menu': 'Idle'
    }

# Create a LanguageModel that supports all the keywords defined in all the States
keywords = []
for state in [Base,Idle,SelectMedia,PlayingMedia]:
    keywords += state.keywords.keys()
all_state_lm = LanguageModel('all_state_lm', keywords)
all_state_lm.update_all()
Base.lm = all_state_lm
   
#########################################
# Old states - OBSOLETE
#########################################
    
#class InitialState(State):
#    lm = ManualLanguageModel('initial')   # Overrides automatic creation of language model
#    keywords = {
#        'MARY': 'Listening'
#    }
#
#class Listening(State):
#    keywords = {
#        'CANCEL': 'InitialState',
예제 #3
0
 def test_update_all(self):
     lm = LanguageModel('playing')
     lm.update_all( True)
     lm.reset_files()