#NEWLINE# def recognize_sphinx( self , audio_data , language = 'en-US' , show_all = False ) : #NEWLINE# #INDENT# '\n        Performs speech recognition on ``audio_data`` (an ``AudioData`` instance), using CMU Sphinx.\n\n        The recognition language is determined by ``language``, an RFC5646 language tag like ``"en-US"`` or ``"en-GB"``, defaulting to US English. Out of the box, only ``en-US`` is supported. See `Notes on using `PocketSphinx <https://github.com/Uberi/speech_recognition/blob/master/reference/pocketsphinx.rst>`__ for information about installing other languages. This document is also included under ``reference/pocketsphinx.rst``.\n\n        Returns the most likely transcription if ``show_all`` is false (the default). Otherwise, returns the Sphinx ``pocketsphinx.pocketsphinx.Decoder`` object resulting from the recognition.\n\n        Raises a ``speech_recognition.UnknownValueError`` exception if the speech is unintelligible. Raises a ``speech_recognition.RequestError`` exception if there are any issues with the Sphinx installation.\n        ' #NEWLINE# assert isinstance ( audio_data , AudioData ) , '`audio_data` must be audio data' #NEWLINE# assert isinstance ( language , str ) , '`language` must be a string' #NEWLINE# try : #NEWLINE# #INDENT# from pocketsphinx import pocketsphinx #NEWLINE# from sphinxbase import sphinxbase #NEWLINE# #UNINDENT# except ImportError : #NEWLINE# #INDENT# raise RequestError ( 'missing PocketSphinx module: ensure that PocketSphinx is set up correctly.' ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# raise RequestError ( 'bad PocketSphinx installation detected; make sure you have PocketSphinx version 0.0.9 or better.' ) #NEWLINE# #UNINDENT# language_directory = os . path . join ( os . path . dirname ( os . path . realpath ( __file__ ) ) , 'pocketsphinx-data' , language ) #NEWLINE# if ( not os . path . isdir ( language_directory ) ) : #NEWLINE# #INDENT# raise RequestError ( 'missing PocketSphinx language data directory: "{0}"' . format ( language_directory ) ) #NEWLINE# #UNINDENT# acoustic_parameters_directory = os . path . join ( self , 'acoustic-model' ) #NEWLINE# if ( not os . path . isdir ( acoustic_parameters_directory ) ) : #NEWLINE# #INDENT# raise RequestError ( 'missing PocketSphinx language model parameters directory: "{0}"' . format ( acoustic_parameters_directory ) ) #NEWLINE# #UNINDENT# language_model_file = os . path . join ( language_directory , 'language-model.lm.bin' ) #NEWLINE# if ( not os . path . isfile ( language_model_file ) ) : #NEWLINE# #INDENT# raise RequestError ( 'missing PocketSphinx language model file: "{0}"' . format ( language_model_file ) ) #NEWLINE# #UNINDENT# phoneme_dictionary_file = os . path . join ( language_directory , 'pronounciation-dictionary.dict' ) #NEWLINE# if ( not os . path . isfile ( phoneme_dictionary_file ) ) : #NEWLINE# #INDENT# raise RequestError ( 'missing PocketSphinx phoneme dictionary file: "{0}"' . format ( phoneme_dictionary_file ) ) #NEWLINE# #UNINDENT# config = pocketsphinx . Decoder . default_config ( ) #NEWLINE# config . set_string ( '-hmm' , acoustic_parameters_directory ) #NEWLINE# config . set_string ( '-lm' , language_model_file ) #NEWLINE# config . set_string ( '-dict' , phoneme_dictionary_file ) #NEWLINE# config . set_string ( '-logfn' , os . devnull ) #NEWLINE# decoder = pocketsphinx . Decoder ( config ) #NEWLINE# raw_data = audio_data . get_raw_data ( convert_rate = 16000 , convert_width = 2 ) #NEWLINE# decoder . start_utt ( ) #NEWLINE# decoder . process_raw ( raw_data , False , True ) #NEWLINE# decoder . end_utt ( ) #NEWLINE# if show_all : #NEWLINE# #INDENT# return decoder #NEWLINE# #UNINDENT# hypothesis = decoder . hyp ( ) #NEWLINE# if ( hypothesis is not None ) : #NEWLINE# #INDENT# return hypothesis . hypstr #NEWLINE# #UNINDENT# raise UnknownValueError ( )