<s> import rospy <EOL> import sys <EOL> import subprocess <EOL> import time <EOL> from greek_support import * <EOL> from english_support import * <EOL> from sphinx4_wrapper import * <EOL> from rapp_tools import * <EOL> from sphinx4_configuration_params import * <EOL> from global_parameters import GlobalParams <EOL> from rapp_platform_ros_communications . srv import ( <EOL> SpeechRecognitionSphinx4Srv , <EOL> SpeechRecognitionSphinx4SrvResponse , <EOL> SpeechRecognitionSphinx4SrvRequest , <EOL> SpeechRecognitionSphinx4ConfigureSrv , <EOL> SpeechRecognitionSphinx4ConfigureSrvResponse , <EOL> SpeechRecognitionSphinx4ConfigureSrvRequest , <EOL> SpeechRecognitionSphinx4TotalSrv , <EOL> SpeechRecognitionSphinx4TotalSrvResponse <EOL> ) <EOL> from rapp_platform_ros_communications . srv import ( <EOL> fetchDataSrv , <EOL> fetchDataSrvRequest <EOL> ) <EOL> from rapp_platform_ros_communications . msg import ( <EOL> StringArrayMsg <EOL> ) <EOL> from std_msgs . msg import ( <EOL> String <EOL> ) <EOL> class SpeechRecognitionSphinx4 ( GlobalParams ) : <EOL> def __init__ ( self ) : <EOL> GlobalParams . __init__ ( self ) <EOL> self . _sphinx4 = Sphinx4Wrapper ( ) <EOL> self . _greek_support = GreekSupport ( ) <EOL> self . _english_support = EnglishSupport ( ) <EOL> self . _configuration_params = SphinxConfigurationParams ( ) <EOL> self . _word_mapping = { } <EOL> self . _use_db_authentication = rospy . get_param ( "<STR_LIT>" ) <EOL> if self . _use_db_authentication == True : <EOL> serv_db_topic = rospy . get_param ( "<STR_LIT>" ) <EOL> self . _authentication_service = rospy . ServiceProxy ( serv_db_topic , fetchDataSrv ) <EOL> if ( not self . _use_db_authentication ) : <EOL> rospy . logerror ( "<STR_LIT>" ) <EOL> total_path = "<STR_LIT>" + self . _sphinx_jar_files_url + "<STR_LIT:/>" + self . _sphinx_jar_file + "<STR_LIT::>" + self . _sphinx_package_url + "<STR_LIT>" <EOL> sphinx_configuration = { '<STR_LIT>' : total_path , '<STR_LIT>' : self . _language_models_url + "<STR_LIT>" , '<STR_LIT>' : self . _acoustic_models_url , '<STR_LIT>' : '<STR_LIT:hello>' , '<STR_LIT>' : self . _language_models_url + "<STR_LIT>" , '<STR_LIT>' : self . _language_models_url + "<STR_LIT>" , '<STR_LIT>' : self . _language_models_url + "<STR_LIT>" , '<STR_LIT>' : True <EOL> } <EOL> self . _sphinx4 . initializeSphinx ( sphinx_configuration ) <EOL> def speechRecognitionBatch ( self , req ) : <EOL> total_res = SpeechRecognitionSphinx4TotalSrvResponse ( ) <EOL> if self . _use_db_authentication == True : <EOL> req_db = fetchDataSrv ( ) <EOL> req_db . req_cols = [ "<STR_LIT:username>" ] <EOL> entry1 = [ "<STR_LIT:username>" , req . user ] <EOL> req_db . where_data = [ StringArrayMsg ( s = entry1 ) ] <EOL> resp = self . _authentication_service ( req_db . req_cols , req_db . where_data ) <EOL> if resp . success . data != True or len ( resp . res_data ) == <NUM_LIT:0> : <EOL> total_res . error = "<STR_LIT>" <EOL> return total_res <EOL> conf_req = SpeechRecognitionSphinx4ConfigureSrvRequest ( ) <EOL> spee_req = SpeechRecognitionSphinx4SrvRequest ( ) <EOL> conf_req . language = req . language <EOL> conf_req . words = req . words <EOL> conf_req . grammar = req . grammar <EOL> conf_req . sentences = req . sentences <EOL> conf_res = SpeechRecognitionSphinx4ConfigureSrvResponse ( ) <EOL> conf_res = self . _configureSpeechRecognition ( conf_req ) <EOL> total_res . error = conf_res . error <EOL> if conf_res . error != '<STR_LIT>' : <EOL> total_res . error = total_res . error + '<STR_LIT:\n>' + conf_res . error <EOL> return total_res <EOL> spee_req . path = req . path <EOL> spee_req . audio_source = req . audio_source <EOL> spee_req . user = req . user <EOL> spee_res = self . _speechRecognition ( spee_req ) <EOL> total_res . words = spee_res . words <EOL> total_res . error = spee_res . error <EOL> return total_res <EOL> def _speechRecognition ( self , req ) : <EOL> res = SpeechRecognitionSphinx4SrvResponse ( ) <EOL> words = self . _sphinx4 . performSpeechRecognition ( req . path , req . audio_source , req . user ) <EOL> rapp_print ( words ) <EOL> if len ( words ) == <NUM_LIT:1> and "<STR_LIT>" in words [ <NUM_LIT:0> ] : <EOL> res . error = words [ <NUM_LIT:0> ] <EOL> res . words = [ ] <EOL> return res <EOL> for word in words : <EOL> if self . _configuration_params . _language != "<STR_LIT>" : <EOL> rapp_print ( "<STR_LIT>" + word + "<STR_LIT:#>" ) <EOL> if word == "<STR_LIT>" or word == '<STR_LIT>' : <EOL> continue <EOL> res . words . append ( self . _word_mapping [ word ] ) <EOL> else : <EOL> res . words . append ( word . replace ( "<STR_LIT:'>" , "<STR_LIT:U+0020>" ) ) <EOL> return res ; <EOL> def _configureSpeechRecognition ( self , req ) : <EOL> res = SpeechRecognitionSphinx4ConfigureSrvResponse ( ) <EOL> res . error = '<STR_LIT>' <EOL> conf = { } <EOL> reconfigure = False <EOL> if self . _configuration_params . _language != req . language : <EOL> reconfigure = True <EOL> elif self . _configuration_params . _words != req . words : <EOL> reconfigure = True <EOL> elif self . _configuration_params . _grammar != req . grammar : <EOL> reconfigure = True <EOL> elif self . _configuration_params . _sentences != req . sentences : <EOL> reconfigure = True <EOL> self . _configuration_params . _language = req . language <EOL> self . _configuration_params . _words = req . words <EOL> self . _configuration_params . _grammar = req . grammar <EOL> self . _configuration_params . _sentences = req . sentences <EOL> if reconfigure == False : <EOL> return res <EOL> if self . _configuration_params . _language == '<STR_LIT>' : <EOL> rapp_print ( "<STR_LIT>" ) <EOL> if len ( self . _configuration_params . _words ) == <NUM_LIT:0> : <EOL> rapp_print ( "<STR_LIT>" ) <EOL> try : <EOL> conf = self . _english_support . getGenericConfiguration ( ) <EOL> except RappError as e : <EOL> res . error = e . value <EOL> return res <EOL> else : <EOL> rapp_print ( "<STR_LIT>" ) <EOL> try : <EOL> conf = self . _english_support . getLimitedVocebularyConfiguration ( self . _configuration_params . _words , self . _configuration_params . _grammar , self . _configuration_params . _sentences ) <EOL> except RappError as e : <EOL> res . error = e . value <EOL> return res <EOL> elif self . _configuration_params . _language == "<STR_LIT>" : <EOL> rapp_print ( "<STR_LIT>" ) <EOL> if len ( self . _configuration_params . _words ) == <NUM_LIT:0> : <EOL> rapp_print ( "<STR_LIT>" ) <EOL> else : <EOL> rapp_print ( "<STR_LIT>" + str ( len ( self . _configuration_params . _words ) ) + "<STR_LIT>" ) <EOL> try : <EOL> [ conf , eng_w ] = self . _greek_support . getLimitedVocebularyConfiguration ( self . _configuration_params . _words , self . _configuration_params . _grammar , self . _configuration_params . _sentences ) <EOL> except RappError as e : <EOL> res . error = e . value <EOL> return res <EOL> self . _word_mapping = { } <EOL> for ew in eng_w : <EOL> self . _word_mapping [ ew ] = eng_w [ ew ] <EOL> rapp_print ( self . _word_mapping ) <EOL> else : <EOL> res . error = "<STR_LIT>" <EOL> return res <EOL> rapp_print ( "<STR_LIT>" ) <EOL> rapp_print ( conf ) <EOL> self . _sphinx4 . configureSphinx ( conf ) <EOL> return res <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> rospy . init_node ( '<STR_LIT>' ) <EOL> SpeechRecognitionSphinx4Node = SpeechRecognitionSphinx4 ( ) <EOL> rospy . spin ( ) </s>
