#NEWLINE# def recognize_bing( self , audio_data , key , language = 'en-US' , show_all = False ) : #NEWLINE# #INDENT# '\n        Performs speech recognition on ``audio_data`` (an ``AudioData`` instance), using the Microsoft Bing Voice Recognition API.\n\n        The Microsoft Bing Voice Recognition API key is specified by ``key``. Unfortunately, these are not available without `signing up for an account <https://www.microsoft.com/cognitive-services/en-us/speech-api>`__ with Microsoft Cognitive Services.\n\n        To get the API key, go to the `Microsoft Cognitive Services subscriptions overview <https://www.microsoft.com/cognitive-services/en-us/subscriptions>`__, go to the entry titled "Speech", and look for the key under the "Keys" column. Microsoft Bing Voice Recognition API keys are 32-character lowercase hexadecimal strings.\n\n        The recognition language is determined by ``language``, an RFC5646 language tag like ``"en-US"`` (US English) or ``"fr-FR"`` (International French), defaulting to US English. A list of supported language values can be found in the `API documentation <https://www.microsoft.com/cognitive-services/en-us/speech-api/documentation/api-reference-rest/BingVoiceRecognition#user-content-4-supported-locales>`__.\n\n        Returns the most likely transcription if ``show_all`` is false (the default). Otherwise, returns the `raw API response <https://www.microsoft.com/cognitive-services/en-us/speech-api/documentation/api-reference-rest/BingVoiceRecognition#user-content-3-voice-recognition-responses>`__ 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 ( key , str ) , '`key` must be a string' #NEWLINE# assert isinstance ( language , str ) , '`language` must be a string' #NEWLINE# ( access_token , expire_time ) = ( getattr ( self , 'bing_cached_access_token' , None ) , getattr ( self , 'bing_cached_access_token_expiry' , None ) ) #NEWLINE# allow_caching = True #NEWLINE# try : #NEWLINE# #INDENT# from time import monotonic #NEWLINE# #UNINDENT# except ImportError : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# from monotonic import monotonic #NEWLINE# #UNINDENT# except ( ImportError , RuntimeError ) : #NEWLINE# #INDENT# expire_time = None #NEWLINE# allow_caching = False #NEWLINE# #UNINDENT# #UNINDENT# if ( ( expire_time is None ) or ( monotonic ( ) > expire_time ) ) : #NEWLINE# #INDENT# credential_url = 'https://oxford-speech.cloudapp.net/token/issueToken' #NEWLINE# credential_request = Request ( credential_url , data = urlencode ( { #NEWLINE# #INDENT# 'grant_type' : 'client_credentials' , #NEWLINE# 'client_id' : 'python' , #NEWLINE# 'client_secret' : key , #NEWLINE# 'scope' : 'https://speech.platform.bing.com' , #NEWLINE# #UNINDENT# } ) . encode ( 'utf-8' ) ) #NEWLINE# if allow_caching : #NEWLINE# #INDENT# start_time = monotonic ( ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# credential_response = urlopen ( credential_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# credential_text = credential_response . read ( ) . decode ( 'utf-8' ) #NEWLINE# credentials = json . loads ( credential_text ) #NEWLINE# ( access_token , expiry_seconds ) = ( credentials [ 'access_token' ] , float ( credentials [ 'expires_in' ] ) ) #NEWLINE# if allow_caching : #NEWLINE# #INDENT# self . bing_cached_access_token = access_token #NEWLINE# self . bing_cached_access_token_expiry = ( start_time + expiry_seconds ) #NEWLINE# #UNINDENT# #UNINDENT# wav_data = audio_data . get_wav_data ( convert_rate = 16000 , convert_width = 2 ) #NEWLINE# url = 'https://speech.platform.bing.com/recognize/query?{0}' . format ( urlencode ( { #NEWLINE# #INDENT# 'version' : '3.0' , #NEWLINE# 'requestid' : uuid . uuid4 ( ) , #NEWLINE# 'appID' : 'D4D52672-91D7-4C74-8AD8-42B1D98141A5' , #NEWLINE# 'format' : 'json' , #NEWLINE# 'locale' : language , #NEWLINE# 'device.os' : 'wp7' , #NEWLINE# 'scenarios' : 'ulm' , #NEWLINE# 'instanceid' : uuid . uuid4 ( ) , #NEWLINE# 'result.profanitymarkup' : '0' , #NEWLINE# #UNINDENT# } ) ) #NEWLINE# request = Request ( url , data = wav_data , headers = { #NEWLINE# #INDENT# 'Authorization' : 'Bearer {0}' . format ( access_token ) , #NEWLINE# 'Content-Type' : 'audio/wav; samplerate=16000; sourcerate={0}; trustsourcerate=true' . format ( audio_data . sample_rate ) , #NEWLINE# #UNINDENT# } ) #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 ( ( 'header' not in result ) or ( 'lexical' not in result [ 'header' ] ) ) : #NEWLINE# #INDENT# raise UnknownValueError ( ) #NEWLINE# #UNINDENT# return result [ 'header' ] [ 'lexical' ]