def _createSupportConfiguration(self, support, res):
        # Whole dictionary utilization
        if len(self._configuration_params._words) == 0:
            RappUtilities.rapp_print("Generic model used")
            # success is either True (bool) or error (string)
            try:
                conf = support.getGenericConfiguration()
            except RappError as e:
                raise RappError(e.value)
        # Limited dictionary utilization
        else:
            RappUtilities.rapp_print("Limited model used")
            # success is either True (bool) or error (string)
            try:
                [conf, mapping] = support.getLimitedVocebularyConfiguration(\
                  self._configuration_params._words, \
                  self._configuration_params._grammar, \
                  self._configuration_params._sentences)
            except RappError as e:
                raise RappError(e.value)

            self._word_mapping = {}
            for ew in mapping:
                self._word_mapping[ew] = mapping[ew]
            RappUtilities.rapp_print(self._word_mapping)

        return conf
    def perform_request(self,
                        url_add=None,
                        params_dict=None,
                        headers_dict=None):

        #  Handle arguments
        url = self._url
        if url_add is not None:
            url = url_add
        param = self._params.copy()
        if params_dict is not None:
            param.update(params_dict)
        head = self._headers.copy()
        if headers_dict is not None:
            head.update(headers_dict)

        try:
            response = requests.get(url, params=param,
                                    headers=head, timeout=self._server_timeout)
        except requests.RequestException as err:
            RappUtilities.rapp_print(err, 'ERROR')
            raise RappError(err)
        RappUtilities.rapp_print('URL: ' + response.url, 'DEBUG')

        if (isinstance(self._accepted_status, int) and
                response.status_code == self._accepted_status) or \
            (isinstance(self._accepted_status, list) and
                response.status_code in self._accepted_status):
            return self._modify_response(response)

        error = 'Request error. Status code: ' + str(response.status_code)
        RappUtilities.rapp_print(error, 'ERROR')
        raise RappError(error)
Example #3
0
    def get_user_password(self, username):
        req = getUserPasswordSrvRequest()
        req.username = username

        response = self._get_user_passwd_proxy(req)
        if response.success:
            return response.password
        else:
            raise RappError('Wrong credentials')
 def _selectLanguageSupport(self, req):
     if self._configuration_params._language == 'en':
         RappUtilities.rapp_print("Language set to English")
         return self._english_support
     elif self._configuration_params._language == 'el':
         RappUtilities.rapp_print("Language set to Greek")
         return self._greek_support
     else:
         raise RappError("Wrong Language")
 def _modify_response(self, response, form=None):
     if form is None and self._response_format == 'json' or form == 'json':
         try:
             return response.json()
         except ValueError as err:
             RappUtilities.rapp_print(err, 'ERROR')
             raise RappError(err)
     else:
         return response.text()
Example #6
0
    def select_geolocator(self, geolocator=''):

        # Set google as a default geolocator
        if geolocator == '':
            geolocator = 'ip-api'

        if geolocator == 'ip-api':
            RappUtilities.rapp_print('Creating ip-api locator', 'DEBUG')
            return IpAPILocator()
        else:
            RappUtilities.rapp_print('Wrong ip locator provided', 'ERROR')
            raise RappError('Wrong ip locator provided')
Example #7
0
    def validate_user_role(self, username):
        req = validateUserRoleSrvRequest()
        req.username = username
        res = self._validate_user_role_proxy(req)

        if res.error == '':
            RappUtilities.rapp_print('Proper user role', 'DEBUG')
        else:
            #  RappUtilities.rapp_print(res.trace, 'ERROR')
            msg = 'Invalid user role'
            RappUtilities.rapp_print(msg, 'ERROR')
            raise RappError(msg)
Example #8
0
    def add_store_token_to_device(self, store_token):
        req = addStoreTokenToDeviceSrvRequest()
        req.store_token = store_token
        res = self._add_store_token_to_device_proxy(req)

        if res.error == '':
            RappUtilities.rapp_print('Succesfully wrote new token', 'DEBUG')
        else:
            RappUtilities.rapp_print(res.error, 'ERROR')
            msg = 'Could not write store token to the database'
            RappUtilities.rapp_print(msg, 'ERROR')
            raise RappError(msg)
  def getLimitedVocebularyConfiguration(self, words, grammar, sentences):
    rapp_print(words)

    enhanced_words = self.getWordPhonemes( words )

    try:
        self.limited_sphinx_configuration= \
            self.vocabulary.createConfigurationFiles(enhanced_words, grammar, sentences)
    except RappError as e:
        raise RappError(e.value)

    return self.limited_sphinx_configuration
 def _verify_new_username_uniqueness(self, username):
     if self._db_handler.username_exists(username):
         counter = 0
         while True and counter <= 10:
             counter += 1
             suggestion = '_' + \
                 ''.join(random.SystemRandom().choice(string.digits)
                         for _ in range(5))
             if not self._db_handler.username_exists(username + suggestion):
                 return username + suggestion
         raise RappError('Could specify a username suggestion')
     else:
         return ''
Example #11
0
    def write_new_application_token(self, username, store_token, appl_token):
        req = createNewApplicationTokenSrvRequest()
        req.username = username
        req.store_token = store_token
        req.application_token = appl_token

        response = self._create_new_app_token_proxy(req)
        if response.success:
            RappUtilities.rapp_print('Succesfully wrote new token', 'DEBUG')
        else:
            msg = 'Could not write new application token to the database'
            RappUtilities.rapp_print(msg, 'ERROR')
            raise RappError(msg)
    def getWordPhonemes(self, words):
        enhanced_words = {}
        for word in words:
            inner_words = []
            inner_phonemes = []
            if "-" not in word:  # Check for conjoined english words
                index = self._english_dict_mapping.find("\n" + word + " ")
                if index == -1:
                    raise RappError("ERROR: Word " + word +\
                             " does not exist in the English Dictionary")
                else:
                    self._english_dict_file.seek(index + 1)
                    line = self._english_dict_file.readline()
                    line = line[:-1]
                    split_line = line.split(" ")
                    inner_phonemes = split_line[1:]

            else:
                inner_words = word.split("-")
                for in_w in inner_words:
                    index = self._english_dict_mapping.find("\n" + in_w + " ")
                    if index == -1:
                        raise RappError("ERROR: Word " + in_w +\
                                " does not exist in the English Dictionary")
                    else:
                        self._english_dict_file.seek(
                            index + 1)  # +1 because of the extra \n
                        line = self._english_dict_file.readline()
                        line = line[:-1]  # to erase the \n
                        split_line = line.split(" ")

                        #enhanced_words[split_line[0]] = []
                        for i in range(1, len(split_line)):
                            inner_phonemes.append(split_line[i])
            enhanced_words[word] = inner_phonemes

        return enhanced_words
Example #13
0
    def add_new_user(self, username, password, creator, language):
        req = createNewPlatformUserSrvRequest()
        req.username = username
        req.password = password
        req.creator_username = creator
        req.language = language

        response = self._add_new_user_proxy(req)
        if response.success:
            RappUtilities.rapp_print('Succesfully wrote new user', 'DEBUG')
        else:
            RappUtilities.rapp_print(response.error, 'ERROR')
            msg = 'Could not write new user to the database'
            RappUtilities.rapp_print(msg, 'ERROR')
            raise RappError(msg)
    def select_weather_reporter(self, weather_reporter=''):

        # Set yweather(Yahoo) as a default weather reporter
        if weather_reporter == '':
            weather_reporter = 'forecastio'

        if weather_reporter == 'yweather':
            RappUtilities.rapp_print('Creating yweather weather reporter', 'DEBUG')
            return YWeatherReporter()
        elif weather_reporter == 'forecastio':
            RappUtilities.rapp_print('Creating forecast.io weather reporter', 'DEBUG')
            return ForecastIOReporter()
        else:
            RappUtilities.rapp_print('Wrong weather reporter provided', 'ERROR')
            raise RappError('Wrong weather reporter provided')
Example #15
0
    def select_news_engine(self, engine):

        # Set google as a default news engine
        if engine == '':
            RappUtilities.rapp_print('No search engine provided. Falling ' +
                                     'back to default (Google News)',
                                     'DEBUG')
            engine = 'event_registry'

        if engine == 'google':
            RappUtilities.rapp_print('Creating Google News engine', 'DEBUG')
            return GoogleNewsEngine()
        elif engine == 'event_registry':
            RappUtilities.rapp_print('Creating Envents Registry engine',
                                     'DEBUG')
            return EventRegistryEngine()
        else:
            RappUtilities.rapp_print('Wrong news engine provided', 'ERROR')
            raise RappError('Wrong news engine provided')
Example #16
0
  def getLimitedVocebularyConfiguration(self, words, grammar, sentences):

    # Separate English from Greek words
    [ english_words, english_grammar, english_sentences, \
      greek_words, greek_grammar, greek_sentences ] = \
      self._separateEngGrWords( words, grammar, sentences )

    # Get phonemes for Greek words and dictionary for Englified->Greek mapping
    [englified_phonems_dict, englified_to_greek_dict] = \
        self._transformWords( greek_words )

    # Append english words to Englified->Greek mapping dictionary
    for word in english_words:
      englified_to_greek_dict.update( {word: word} )

    # Get phonemes for English words
    english_phonem_dict = self._english_support.getWordPhonemes( english_words )

    # Englify Greek grammar and sentences
    englified_grammar = self._englify_words(greek_grammar)
    englified_sentences = self._englify_words(greek_sentences)


    # Join English and Greek processed files
    final_phoneme_dict = english_phonem_dict
    final_phoneme_dict.update(englified_phonems_dict)
    final_sentences = englified_sentences + english_sentences
    final_grammar = english_grammar + englified_grammar

    try:
        limited_sphinx_configuration = \
            self._vocabulary.createConfigurationFiles( \
              final_phoneme_dict, final_grammar, final_sentences
            )
    except RappError as e:
        raise RappError(e.value)

    return [limited_sphinx_configuration, englified_to_greek_dict]
    def createConfigurationFiles(self, words, grammar, sentences):
        tmp_configuration = self._sphinx_configuration

        RappUtilities.rapp_print(
            "Creating configuration files with parameters:")
        RappUtilities.rapp_print("Words: " + str(words))
        RappUtilities.rapp_print("Sentences: " + str(sentences))
        RappUtilities.rapp_print("Grammar: " + str(grammar))
        # Create custom dictionary file
        tmp_configuration['dictionary'] = os.path.join( self._languages_package, \
            'custom.dict' )
        custom_dict = open(tmp_configuration['dictionary'], 'w')
        for word in words:
            tmp_line = word
            for phoneme in words[word]:
                tmp_line += " " + phoneme.replace('-', '').replace('_', '')
            custom_dict.write(tmp_line + '\n')
        custom_dict.close()

        # Check grammar
        if len(grammar) == 0:
            tmp_configuration['grammar_disabled'] = True
        else:
            tmp_configuration['grammar_disabled'] = False
        tmp_configuration['grammar_name'] = 'custom'
        tmp_configuration['grammar_folder'] = self._languages_package
        custom_grammar = open(os.path.join( tmp_configuration['grammar_folder'], \
            tmp_configuration['grammar_name']) + '.gram', 'w')
        custom_grammar.write('#JSGF V1.0;\n')
        custom_grammar.write("grammar " + tmp_configuration['grammar_name'] +
                             ';\n')
        counter = 1
        #custom_grammar.write("public <cmd1>=(")
        for i in range(0, len(grammar)):
            gram = grammar[i]
            # check is all words in grammar exist in words
            gram_words = gram.split(" ")
            for gw in gram_words:
                if gw not in words and gram not in words:
                    raise RappError('Word ' + gw +
                                    ' is not in words but exists in grammar')

            custom_grammar.write("public <cmd" + str(counter) + ">=" + "\"" +
                                 gram + "\";\n")
            #custom_grammar.write("\"" + gram + "\"")
            #if i == len(grammar) - 1:
            #custom_grammar.write(");")
            #else:
            #custom_grammar.write(" | ")
            counter += 1
        custom_grammar.close()

        # Fix sentences / language model
        # Check sentences: All words must exist in sentences
        # Continue with the sentences setup
        tmp_configuration['language_model'] = os.path.join( self._languages_package, \
          "sentences.lm.bin" )
        custom_sentences = open(self._languages_package + '/sentences.txt',
                                'w')
        if len(sentences) != 0:
            for sent in sentences:
                # Split sentence
                sent_words = sent.split(" ")
                for sw in sent_words:
                    if sw not in words and sent not in words:
                        raise RappError(
                            'Word ' + sw +
                            ' is not in words but exists in a sentence')

                custom_sentences.write("<s> " + sent + " </s>\n")
        else:
            for word in words:
                custom_sentences.write("<s> " + word + " </s>\n")
        custom_sentences.close()

        # Run script to fix the language model
        RappUtilities.rapp_print("Sphinx: Creating language model files\n")
        if self._globalParams._allow_sphinx_output == True:
            bash_file = self._globalParams._language_models_url + "/greekPack/run.sh"
            bash_command = "cp " + bash_file + " " + self._languages_package + \
                " && cd " + self._languages_package + " && bash run.sh"
        else:
            bash_file = self._globalParams._language_models_url + \
                "/greekPack/run_silent.sh"
            bash_command = "cp " + bash_file + " " + self._languages_package + \
                " && cd " + self._languages_package + " && bash run_silent.sh"

        os.system(bash_command)

        return tmp_configuration
 def _validate_username_format(self, username):
     if not re.match("^[\w\d_-]*$", username) or len(username) < 5 or \
             len(username) > 15:
         raise RappError('Invalid username characters')
 def _verify_user_credentials(self, username, password):
     passwd = self._db_handler.get_user_password(username)
     if bcrypt.hashpw(password, passwd) != passwd:
         raise RappError("Wrong Credentials")
  def speech_to_text(self, file_path, user, audio_file_type, language):

    # Check the user
    serv_db_topic = rospy.get_param("rapp_mysql_wrapper_user_fetch_data_topic")
    authentication_service = rospy.ServiceProxy(serv_db_topic, fetchDataSrv)
    req_db = fetchDataSrv()
    req_db.req_cols=["username"]
    entry1=["username", user]
    req_db.where_data=[StringArrayMsg(s=entry1)]

    resp = authentication_service(req_db.req_cols, req_db.where_data)
    if resp.success.data != True or len(resp.res_data) == 0:
      raise RappError("Non authenticated user")

    # Check if file exists
    if not os.path.isfile(file_path):
        raise RappError("Error: file " + file_path + ' not found')

    # Check if file is flac. If not convert it
    new_audio = file_path

    audio_trans_topic = rospy.get_param("rapp_audio_processing_transform_audio_topic")
    audio_transform_srv = rospy.ServiceProxy( audio_trans_topic, AudioProcessingTransformAudioSrv )

    cleanup = []

    transform_req = AudioProcessingTransformAudioSrvRequest()
    transform_req.source_type = audio_file_type
    transform_req.source_name = new_audio
    transform_req.target_type = 'wav'
    new_audio += '.wav'
    transform_req.target_name = new_audio
    transform_req.target_channels = 1
    transform_req.target_rate = 16000

    trans_response = audio_transform_srv( transform_req )

    if trans_response.error != 'success':
        raise RappError( trans_response.error )
    cleanup.append(new_audio)

    # Denoise if necessary
    prev_audio_file = new_audio
    next_audio_file = prev_audio_file
    if audio_file_type in ['nao_ogg', 'nao_wav_1_ch', 'nao_wav_4_ch']:
        denoise_topic = rospy.get_param("rapp_audio_processing_denoise_topic")
        energy_denoise_topic = \
            rospy.get_param("rapp_audio_processing_energy_denoise_topic")
        denoise_service = rospy.ServiceProxy(\
            denoise_topic, AudioProcessingDenoiseSrv)
        energy_denoise_service = rospy.ServiceProxy(\
            energy_denoise_topic, AudioProcessingDenoiseSrv)

        manipulation = {}
        manipulation['sox_transform'] = False
        manipulation['sox_denoising'] = False
        manipulation['sox_channels_and_rate'] = False
        if audio_file_type == "headset":
            pass
        elif audio_file_type == "nao_ogg":
            manipulation['sox_transform'] = True
            manipulation['sox_denoising'] = True
            manipulation['sox_denoising_scale'] = 0.15
        elif audio_file_type == "nao_wav_4_ch":
            manipulation['sox_channels_and_rate'] = True
            manipulation['sox_denoising'] = True
            manipulation['sox_denoising_scale'] = 0.15
        elif audio_file_type == "nao_wav_1_ch":
            manipulation['sox_denoising'] = True
            manipulation['sox_denoising_scale'] = 0.15
            manipulation['detect_silence'] = True
            manipulation['detect_silence_threshold'] = 0.25

        # Check if sox_transform is needed
        if manipulation['sox_transform'] == True:
            next_audio_file += "_transformed.wav"
            command = "sox " + prev_audio_file + " " + next_audio_file
            com_res = os.system(command)
            if com_res != 0:
                raise RappError("Error: sox malfunctioned")
            cleanup.append(next_audio_file)
            prev_audio_file = next_audio_file
        if manipulation['sox_channels_and_rate'] == True:
            next_audio_file += "_mono16k.wav"
            command = "sox " + prev_audio_file + " -r 16000 -c 1 " + next_audio_file
            com_res = os.system(command)
            if com_res != 0:
                raise RappError("Error: sox malfunctioned")
            cleanup.append(next_audio_file)
            prev_audio_file = next_audio_file
        if manipulation['sox_denoising'] == True:
            next_audio_file = prev_audio_file + "_denoised.wav"
            den_request = AudioProcessingDenoiseSrvRequest()
            den_request.audio_file = prev_audio_file
            den_request.denoised_audio_file = next_audio_file
            den_request.audio_type = audio_file_type
            den_request.user = user
            den_request.scale = manipulation['sox_denoising_scale']
            den_response = denoise_service(den_request)
            if den_response.success != "true":
                raise RappError("Error:" + den_response.success)
            cleanup.append(next_audio_file)
            prev_audio_file = next_audio_file

            # must implement a fallback function to clear redundant files

    # Transform to flac
    transform_req = AudioProcessingTransformAudioSrvRequest()
    transform_req.source_type = 'headset'
    transform_req.source_name = new_audio
    transform_req.target_type = 'flac'
    newer_audio = new_audio + '.flac'
    transform_req.target_name = newer_audio
    transform_req.target_channels = 1
    transform_req.target_rate = 16000

    trans_response = audio_transform_srv( transform_req )
    cleanup.append(newer_audio)

    if trans_response.error != 'success':
        raise RappError( trans_response.error )


    # Open the file
    with open(newer_audio, "r") as f:
      speech = f.read()
    url = "www.google.com"

    # Fix language
    if language == 'en':
        language = "en-US"
    elif language == 'gr':
        language = 'el'

    #NOTE - Thats a general usage key. They may disable it in the future.
    key = "AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw"
    path = "/speech-api/v2/recognize?lang=" + language + "&key=" + key
    headers = { "Content-type": "audio/x-flac; rate=22050" };
    params = {"xjerr": "1", "client": "chromium"}
    conn = httplib.HTTPSConnection(url)
    conn.request("POST", path, speech, headers)
    response = conn.getresponse()
    data = response.read()
    initial_data = data
    # Google returns one empty result for some reason here. Removing it..
    index = data.find("}")
    data = data[index + 1:]
    if data == '\n':
        # Returned nothing.. something went wrong
        data = initial_data
    jsdata = json.loads(data)

    # Remove the flac if needed
    for f in cleanup:
        command = 'rm -f ' + f
        if os.system(command):
            raise RappError("Error: Removal of temporary file malfunctioned")
    return jsdata