def __init__(self, settings_): self.settings = settings_ self.input_engine = STTEngine( pause_threshold=self.settings.SPEECH_RECOGNITION.get( 'pause_threshold'), energy_theshold=self.settings.SPEECH_RECOGNITION.get( 'energy_threshold'), ambient_duration=self.settings.SPEECH_RECOGNITION.get( 'ambient_duration'), dynamic_energy_threshold=self.settings.SPEECH_RECOGNITION.get( 'dynamic_energy_threshold'), sr=sr) if self.settings.GENERAL_SETTINGS.get( 'input_mode') == InputMode.VOICE.value else TTTEngine() self.console_manager = ConsoleManager( log_settings=self.settings.ROOT_LOG_CONF, ) self.output_engine = TTSEngine( console_manager=self.console_manager, speech_response_enabled=self.settings.GENERAL_SETTINGS.get( 'response_in_speech')) self.response_creator = ResponseCreator() self.skill_analyzer = SkillAnalyzer( weight_measure=TfidfVectorizer, similarity_measure=cosine_similarity, args=self.settings.SKILL_ANALYZER.get('args'), skills_=SKILLS, sensitivity=self.settings.SKILL_ANALYZER.get('sensitivity'))
def setUp(self): all_skills = { MongoCollections.CONTROL_SKILLS.value: CONTROL_SKILLS, MongoCollections.ENABLED_BASIC_SKILLS.value: ENABLED_BASIC_SKILLS, } for collection, documents in all_skills.items(): db.update_collection(collection, documents) default_assistant_name = settings.DEFAULT_GENERAL_SETTINGS[ 'assistant_name'] default_input_mode = settings.DEFAULT_GENERAL_SETTINGS['input_mode'] default_response_in_speech = settings.DEFAULT_GENERAL_SETTINGS[ 'response_in_speech'] default_settings = { 'assistant_name': default_assistant_name, 'input_mode': default_input_mode, 'response_in_speech': default_response_in_speech, } db.update_collection( collection=MongoCollections.GENERAL_SETTINGS.value, documents=[default_settings]) self.skill_analyzer = SkillAnalyzer( weight_measure=TfidfVectorizer, similarity_measure=cosine_similarity, args=settings.SKILL_ANALYZER.get('args'), sensitivity=settings.SKILL_ANALYZER.get('sensitivity'), )
def setUp(self): start_mongoDB_server() db = MongoDB() self.skill_analyzer = SkillAnalyzer( weight_measure=TfidfVectorizer, similarity_measure=cosine_similarity, args=SKILL_ANALYZER.get('args'), sensitivity=SKILL_ANALYZER.get('sensitivity'), db=db)
def __init__(self, console_manager, settings_): self.console_manager = console_manager self.settings = settings_ self.response_creator = ResponseCreator() self.skill_analyzer = SkillAnalyzer( weight_measure=TfidfVectorizer, similarity_measure=cosine_similarity, args=self.settings.SKILL_ANALYZER.get('args'), sensitivity=self.settings.SKILL_ANALYZER.get('sensitivity'), )
def __init__(self): self.input_engine = STTEngine( pause_threshold=SPEECH_RECOGNITION['pause_threshold'], energy_theshold=SPEECH_RECOGNITION['energy_threshold'], ambient_duration=SPEECH_RECOGNITION['ambient_duration'], dynamic_energy_threshold=SPEECH_RECOGNITION[ 'dynamic_energy_threshold'], sr=sr) if GENERAL_SETTINGS['user_voice_input'] else TTTEngine() self.console_manager = ConsoleManager(log_settings=ROOT_LOG_CONF, ) self.output_engine = TTSEngine( console_manager=self.console_manager, speech_response_enabled=GENERAL_SETTINGS['response_in_speech']) self.response_creator = ResponseCreator() self.skill_analyzer = SkillAnalyzer( weight_measure=TfidfVectorizer, similarity_measure=cosine_similarity, args=ANALYZER['args'], skills_=SKILLS, sensitivity=ANALYZER['sensitivity']) self.skill_controller = SkillController( settings_=GENERAL_SETTINGS, input_engine=self.input_engine, analyzer=self.skill_analyzer, control_skills=CONTROL_SKILLS, )
class SkillTests(unittest.TestCase): def setUp(self): start_mongoDB_server() db = MongoDB() self.skill_analyzer = SkillAnalyzer( weight_measure=TfidfVectorizer, similarity_measure=cosine_similarity, args=SKILL_ANALYZER.get('args'), sensitivity=SKILL_ANALYZER.get('sensitivity'), db=db) def tearDown(self): stop_mongoDB_server() def test_extract(self): """ Test few extraction cases on basic skills """ # Internet skill extraction tell_date_func_name = InternetSkills.internet_availability.__name__ internet_conenction_tag = 'internet conection' extracted_internet_conenction_func_name = self.skill_analyzer.extract( internet_conenction_tag).get('func') self.assertEqual(tell_date_func_name, extracted_internet_conenction_func_name) # Time skill extraction tell_time_func_name = DatetimeSkills.tell_the_time.__name__ time_tag = 'time' extracted_time_func_name = self.skill_analyzer.extract(time_tag).get( 'func') self.assertEqual(tell_time_func_name, extracted_time_func_name) # Date skill extraction tell_date_func_name = DatetimeSkills.tell_the_date.__name__ date_tag = 'date' extracted_date_func_name = self.skill_analyzer.extract(date_tag).get( 'func') self.assertEqual(tell_date_func_name, extracted_date_func_name)
def __init__(self, settings_, db): self.settings = settings_ self.db = db self.input_engine = engines.STTEngine( pause_threshold=self.settings.SPEECH_RECOGNITION.get('pause_threshold'), energy_theshold=self.settings.SPEECH_RECOGNITION.get('energy_threshold'), ambient_duration=self.settings.SPEECH_RECOGNITION.get('ambient_duration'), dynamic_energy_threshold=self.settings.SPEECH_RECOGNITION.get( 'dynamic_energy_threshold'), sr=sr ) if db.get_documents(collection='general_settings')[0]['input_mode'] == InputMode.VOICE.value \ else engines.TTTEngine() self.output_engine = engines.TTSEngine() if db.get_documents(collection='general_settings')[0]['response_in_speech'] \ else engines.TTTEngine() self.response_creator = ResponseCreator() self.skill_analyzer = SkillAnalyzer( weight_measure=TfidfVectorizer, similarity_measure=cosine_similarity, args=self.settings.SKILL_ANALYZER.get('args'), sensitivity=self.settings.SKILL_ANALYZER.get('sensitivity'), db=self.db)
def __init__(self): self.input_engine = SPEECH_ENGINES[SPEECH_RECOGNITION['recognizer']]() self.console_manager = ConsoleManager(log_settings=ROOT_LOG_CONF, ) self.output_engine = TTSEngine( console_manager=self.console_manager, speech_response_enabled=GENERAL_SETTINGS['response_in_speech']) self.response_creator = ResponseCreator() self.skill_analyzer = SkillAnalyzer( weight_measure=TfidfVectorizer, similarity_measure=cosine_similarity, args=ANALYZER['args'], skills_=SKILLS, sensitivity=ANALYZER['sensitivity']) self.skill_controller = SkillController( settings_=GENERAL_SETTINGS, input_engine=self.input_engine, analyzer=self.skill_analyzer, control_skills=CONTROL_SKILLS, )
class Processor: def __init__(self, console_manager, settings_): self.console_manager = console_manager self.settings = settings_ self.response_creator = ResponseCreator() self.skill_analyzer = SkillAnalyzer( weight_measure=TfidfVectorizer, similarity_measure=cosine_similarity, args=self.settings.SKILL_ANALYZER.get('args'), sensitivity=self.settings.SKILL_ANALYZER.get('sensitivity'), ) def run(self): """ Assistant starting point. - STEP 1: Get user input based on the input mode (voice or text) - STEP 2: Matches the input with a skill - STEP 3: Create a response - STEP 4: Execute matched skill - STEP 5: Insert user transcript and response in history collection (in MongoDB) """ # STEP 1 transcript = jarvis.input_engine.recognize_input() # STEP 2 skill_to_execute = self._extract_skill(transcript) # STEP 3 response = self.response_creator.create_positive_response(transcript) if skill_to_execute \ else self.response_creator.create_negative_response(transcript) jarvis.output_engine.assistant_response(response) # STEP 4 self._execute_skill(skill_to_execute) # STEP 5 record = { 'user_transcript': transcript, 'response': response if response else '--', 'executed_skill': skill_to_execute if skill_to_execute else '--' } db.insert_many_documents('history', [record]) def _extract_skill(self, transcript): skill = self.skill_analyzer.extract(transcript) if skill: return {'voice_transcript': transcript, 'skill': skill} @staticmethod def _execute_skill(skill): if skill: ActivationSkills.enable_assistant() try: logging.debug('Executing skill {0}'.format( skill.get('skill').get('name'))) skill_func_name = skill.get('skill').get('func') skill_func = skill_objects[skill_func_name] skill_func(**skill) except Exception as e: logging.debug( "Error with the execution of skill with message {0}". format(e))
class Processor: def __init__(self, settings_): self.settings = settings_ self.input_engine = STTEngine( pause_threshold=self.settings.SPEECH_RECOGNITION.get( 'pause_threshold'), energy_theshold=self.settings.SPEECH_RECOGNITION.get( 'energy_threshold'), ambient_duration=self.settings.SPEECH_RECOGNITION.get( 'ambient_duration'), dynamic_energy_threshold=self.settings.SPEECH_RECOGNITION.get( 'dynamic_energy_threshold'), sr=sr) if self.settings.GENERAL_SETTINGS.get( 'input_mode') == InputMode.VOICE.value else TTTEngine() self.console_manager = ConsoleManager( log_settings=self.settings.ROOT_LOG_CONF, ) self.output_engine = TTSEngine( console_manager=self.console_manager, speech_response_enabled=self.settings.GENERAL_SETTINGS.get( 'response_in_speech')) self.response_creator = ResponseCreator() self.skill_analyzer = SkillAnalyzer( weight_measure=TfidfVectorizer, similarity_measure=cosine_similarity, args=self.settings.SKILL_ANALYZER.get('args'), skills_=SKILLS, sensitivity=self.settings.SKILL_ANALYZER.get('sensitivity')) def run(self): self._traped_until_assistant_is_enabled() transcript = self.input_engine.recognize_input() skill_to_execute = self._extract_skill(transcript) response = self.response_creator.create_positive_response(transcript) if skill_to_execute \ else self.response_creator.create_negative_response(transcript) self.output_engine.assistant_response(response) self._execute_skill(skill_to_execute) def _execute_skill(self, skill): if skill: try: skill_method = skill.get('skill').get('skill') logging.debug('Executing skill {0}'.format(skill)) skill_method(**skill) except Exception as e: logging.debug( "Error with the execution of skill with message {0}". format(e)) def _traped_until_assistant_is_enabled(self): if self.settings.GENERAL_SETTINGS.get( 'input_mode') == InputMode.VOICE.value: while not ExecutionState.is_ready_to_execute(): voice_transcript = self.input_engine.recognize_input() transcript_words = voice_transcript.split() enable_tag = set(transcript_words).intersection( CONTROL_SKILLS.get('enable_assistant').get('tags')) if bool(enable_tag): CONTROL_SKILLS.get('enable_assistant').get('skill')() ExecutionState.update() def _extract_skill(self, transcript): skill = self.skill_analyzer.extract(transcript) if skill: return {'voice_transcript': transcript, 'skill': skill}
class TestSkillMatching(unittest.TestCase): def setUp(self): all_skills = { MongoCollections.CONTROL_SKILLS.value: CONTROL_SKILLS, MongoCollections.ENABLED_BASIC_SKILLS.value: ENABLED_BASIC_SKILLS, } for collection, documents in all_skills.items(): db.update_collection(collection, documents) default_assistant_name = settings.DEFAULT_GENERAL_SETTINGS[ 'assistant_name'] default_input_mode = settings.DEFAULT_GENERAL_SETTINGS['input_mode'] default_response_in_speech = settings.DEFAULT_GENERAL_SETTINGS[ 'response_in_speech'] default_settings = { 'assistant_name': default_assistant_name, 'input_mode': default_input_mode, 'response_in_speech': default_response_in_speech, } db.update_collection( collection=MongoCollections.GENERAL_SETTINGS.value, documents=[default_settings]) self.skill_analyzer = SkillAnalyzer( weight_measure=TfidfVectorizer, similarity_measure=cosine_similarity, args=settings.SKILL_ANALYZER.get('args'), sensitivity=settings.SKILL_ANALYZER.get('sensitivity'), ) def test_all_skill_matches(self): """ In this test we examine the matches or ALL skill tags with the functions If all skills matched right then the test passes otherwise not. At the end we print a report with all the not matched cases. """ verifications_errors = [] for basic_skill in BASIC_SKILLS: print( '--------------------------------------------------------------------------------------' ) print('Examine skill: {0}'.format(basic_skill.get('name'))) for tag in basic_skill.get('tags', ).split(','): # If the skill has matching tags if tag: expected_skill = basic_skill.get('name') actual_skill = self.skill_analyzer.extract(tag).get('name') try: self.assertEqual(expected_skill, actual_skill) except AssertionError as e: verifications_errors.append({'tag': tag, 'error': e}) print( '-------------------------------------- SKILLS MATCHING REPORT --------------------------------------' ) if verifications_errors: for increment, e in enumerate(verifications_errors): print('{0})'.format(increment)) print('Not correct match with tag: {0}'.format(e.get('tag'))) print('Assertion values (expected != actual): {0}'.format( e.get('error'))) raise AssertionError else: print('All skills matched correctly!')
class Processor: def __init__(self, settings_, db): self.settings = settings_ self.db = db self.input_engine = engines.STTEngine( pause_threshold=self.settings.SPEECH_RECOGNITION.get('pause_threshold'), energy_theshold=self.settings.SPEECH_RECOGNITION.get('energy_threshold'), ambient_duration=self.settings.SPEECH_RECOGNITION.get('ambient_duration'), dynamic_energy_threshold=self.settings.SPEECH_RECOGNITION.get( 'dynamic_energy_threshold'), sr=sr ) if db.get_documents(collection='general_settings')[0]['input_mode'] == InputMode.VOICE.value \ else engines.TTTEngine() self.output_engine = engines.TTSEngine() if db.get_documents(collection='general_settings')[0]['response_in_speech'] \ else engines.TTTEngine() self.response_creator = ResponseCreator() self.skill_analyzer = SkillAnalyzer( weight_measure=TfidfVectorizer, similarity_measure=cosine_similarity, args=self.settings.SKILL_ANALYZER.get('args'), sensitivity=self.settings.SKILL_ANALYZER.get('sensitivity'), db=self.db) def run(self): """ This method is the assistant starting point. - STEP 1: Waiting for enable keyword (ONLY in 'voice' mode) - STEP 2: Retrieve input (voice or text) - STEP 3: Matches the input with a skill - STEP 4: Create a response - STEP 5: Execute matched skill - STEP 6: Insert user transcript and response in history collection (in MongoDB) """ # STEP 1 self._trapped_until_assistant_is_enabled() # STEP 2 transcript = self.input_engine.recognize_input() # STEP 3 skill_to_execute = self._extract_skill(transcript) # STEP 4 response = self.response_creator.create_positive_response(transcript) if skill_to_execute \ else self.response_creator.create_negative_response(transcript) self.output_engine.assistant_response(response) # STEP 5 self._execute_skill(skill_to_execute) # STEP 6 record = { 'user_transcript': transcript, 'response': response if response else '--', 'executed_skill': skill_to_execute if skill_to_execute else '--' } self.db.insert_many_documents('history', [record]) def _trapped_until_assistant_is_enabled(self): """ In voice mode assistant waiting to hear an enable keyword to start, until then is trapped in a loop. """ if self.db.get_documents(collection='general_settings' )[0]['input_mode'] == InputMode.VOICE.value: while not ExecutionState.is_ready_to_execute(): voice_transcript = self.input_engine.recognize_input() transcript_words = voice_transcript.split() enable_skills = self.db.get_documents( 'control_skills', {'name': 'enable_assistant'}) enable_tags = [skill.get('tags') for skill in enable_skills] enable_tag = set(transcript_words).intersection(enable_tags) if bool(enable_tag): skill_name = self.db.get_documents( 'control_skills', { 'name': 'enable_assistant' }).get('skills') skill_object = skill_objects[skill_name] skill_object() ExecutionState.update() def _extract_skill(self, transcript): skill = self.skill_analyzer.extract(transcript) if skill: return {'voice_transcript': transcript, 'skill': skill} @staticmethod def _execute_skill(skill): if skill: try: logging.debug('Executing skill {0}'.format( skill.get('skill').get('name'))) skill_func_name = skill.get('skill').get('func') skill_func = skill_objects[skill_func_name] skill_func(**skill) except Exception as e: logging.debug( "Error with the execution of skill with message {0}". format(e))