#NEWLINE# def recognize_ibm( self , audio_data , username , password , language = 'en-US' , show_all = False ) : #NEWLINE# #INDENT# '\n        Performs speech recognition on ``audio_data`` (an ``AudioData`` instance), using the IBM Speech to Text API.\n\n        The IBM Speech to Text username and password are specified by ``username`` and ``password``, respectively. Unfortunately, these are not available without `signing up for an account <https://console.ng.bluemix.net/registration/>`__. Once logged into the Bluemix console, follow the instructions for `creating an IBM Watson service instance <http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/getting_started/gs-credentials.shtml>`__, where the Watson service is "Speech To Text". IBM Speech to Text usernames are strings of the form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX, while passwords are mixed-case alphanumeric strings.\n\n        The recognition language is determined by ``language``, an RFC5646 language tag with a dialect like ``"en-US"`` (US English) or ``"zh-CN"`` (Mandarin Chinese), defaulting to US English. The supported language values are listed under the ``model`` parameter of the `audio recognition API documentation <http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/speech-to-text/api/v1/#recognize_audio_sessionless12>`__, in the form ``LANGUAGE_BroadbandModel``, where ``LANGUAGE`` is the language value.\n\n        Returns the most likely transcription if ``show_all`` is false (the default). Otherwise, returns the `raw API response <http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/speech-to-text/api/v1/#recognize_audio_sessionless12>`__ as a JSON dictionary.\n\n        Raises a ``speech_recognition.UnknownValueError`` exception if the speech is unintelligible. Raises a ``speech_recognition.RequestError`` exception if the speech recognition operation failed, if the key isn\'t valid, or if there is no internet connection.\n        ' #NEWLINE# assert isinstance ( audio_data , AudioData ) , 'Data must be audio data' #NEWLINE# assert isinstance ( username , str ) , '`username` must be a string' #NEWLINE# assert isinstance ( password , str ) , '`password` must be a string' #NEWLINE# flac_data = audio_data . get_flac_data ( convert_rate = ( None if ( audio_data . sample_rate >= 16000 ) else 16000 ) , convert_width = ( None if ( audio_data . sample_width >= 2 ) else 2 ) ) #NEWLINE# model = '{0}_BroadbandModel' . format ( language ) #NEWLINE# url = 'https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?{0}' . format ( urlencode ( { #NEWLINE# #INDENT# 'profanity_filter' : 'false' , #NEWLINE# 'continuous' : 'true' , #NEWLINE# 'model' : model , #NEWLINE# #UNINDENT# } ) ) #NEWLINE# request = Request ( url , data = flac_data , headers = { #NEWLINE# #INDENT# 'Content-Type' : 'audio/x-flac' , #NEWLINE# #UNINDENT# } ) #NEWLINE# if hasattr ( '' , 'encode' ) : #NEWLINE# #INDENT# authorization_value = base64 . standard_b64encode ( '{0}:{1}' . format ( username , password ) . encode ( 'utf-8' ) ) . decode ( 'utf-8' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# authorization_value = base64 . standard_b64encode ( '{0}:{1}' . format ( username , password ) ) #NEWLINE# #UNINDENT# request . add_header ( 'Authorization' , 'Basic {0}' . format ( authorization_value ) ) #NEWLINE# try : #NEWLINE# #INDENT# response = urlopen ( request ) #NEWLINE# #UNINDENT# except HTTPError as e : #NEWLINE# #INDENT# raise RequestError ( 'recognition request failed: {0}' . format ( getattr ( e , 'reason' , 'status {0}' . format ( e . code ) ) ) ) #NEWLINE# #UNINDENT# except URLError as e : #NEWLINE# #INDENT# raise RequestError ( 'recognition connection failed: {0}' . format ( e . reason ) ) #NEWLINE# #UNINDENT# response_text = response . read ( ) . decode ( 'utf-8' ) #NEWLINE# result = json . loads ( response_text ) #NEWLINE# if show_all : #NEWLINE# #INDENT# return result #NEWLINE# #UNINDENT# if ( ( 'results' not in result ) or ( len ( username [ 'results' ] ) < 1 ) or ( 'alternatives' not in result [ 'results' ] [ 0 ] ) ) : #NEWLINE# #INDENT# raise UnknownValueError ( ) #NEWLINE# #UNINDENT# transcription = [ ] #NEWLINE# for utterance in result [ 'results' ] : #NEWLINE# #INDENT# if ( 'alternatives' not in utterance ) : #NEWLINE# #INDENT# raise UnknownValueError ( ) #NEWLINE# #UNINDENT# for hypothesis in utterance [ 'alternatives' ] : #NEWLINE# #INDENT# if ( 'transcript' in hypothesis ) : #NEWLINE# #INDENT# transcription . append ( hypothesis [ 'transcript' ] ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return '\n' . join ( transcription )