
    1h'                    r   S SK Jr  S SKrS SKJrJrJr  S SKJrJ	r	  S SK
Jr  S SKJrJr  S SKJr  S SKJr  S S	KJrJr   " S
 S\5      r " S S\5      r " S S\5      r\S   r\\\4   rSr " S S5      rSSS S.         SS jjr " S S5      rSS jr      S\S.             SS jjjr!g)     )annotationsN)DictLiteral	TypedDict)	HTTPErrorURLError)	urlencode)Requesturlopen)NotRequired)	AudioData)RequestErrorUnknownValueErrorc                  *    \ rS rSr% S\S'   S\S'   Srg)Alternative   str
transcriptfloat
confidence N__name__
__module____qualname____firstlineno____annotations____static_attributes__r       ]/var/www/auris/envauris/lib/python3.13/site-packages/speech_recognition/recognizers/google.pyr   r      s    Or   r   c                  *    \ rS rSr% S\S'   S\S'   Srg)Result   list[Alternative]alternativeboolfinalr   Nr   r   r   r    r"   r"      s    ""Kr   r"   c                  *    \ rS rSr% S\S'   S\S'   Srg)GoogleResponse   zlist[Result]resultzNotRequired[int]result_indexr   Nr   r   r   r    r)   r)      s    ""r   r)   )r      z-http://www.google.com/speech-api/v2/recognizec                  n    \ rS rSr          S
S jrSS jrSS jrSS jrSS jr\	SS j5       r
Srg	)RequestBuilder$   c               4    Xl         X l        X0l        X@l        g Nendpointkeylanguagefilter_level)selfr4   r5   r6   r7   s        r    __init__RequestBuilder.__init__%   s     ! (r   c                    [        U[        5      (       d  [        S5      eU R                  5       nU R	                  U5      nU R                  U5      n[        X$US9nU$ )Nz!``audio_data`` must be audio data)dataheaders)
isinstancer   
ValueError	build_urlbuild_headers
build_datar
   )r8   
audio_dataurlr=   	flac_datarequests         r    buildRequestBuilder.build2   sV    *i00@AAnn$$Z0OOJ/	#w?r   c                    [        SU R                  U R                  U R                  S.5      nU R                   SU 3$ )a
  
>>> builder = RequestBuilder(endpoint="http://www.google.com/speech-api/v2/recognize", key="awesome-key", language="en-US", filter_level=0)
>>> builder.build_url()
'http://www.google.com/speech-api/v2/recognize?client=chromium&lang=en-US&key=awesome-key&pFilter=0'
chromium)clientlangr5   pFilter?)r	   r6   r5   r7   r4   )r8   paramss     r    r@   RequestBuilder.build_url<   sD     $xx,,	
 --&**r   c                ,    UR                   nSSU 30nU$ )z
>>> builder = RequestBuilder(endpoint="", key="", language="", filter_level=1)
>>> audio_data = AudioData(b"", 16_000, 1)
>>> builder.build_headers(audio_data)
{'Content-Type': 'audio/x-flac; rate=16000'}
zContent-Typezaudio/x-flac; rate=sample_rate)r8   rC   rater=   s       r    rA   RequestBuilder.build_headersL   s&     %%!%8#?@r   c                X    UR                  U R                  UR                  5      SS9nU$ )N   )convert_rateconvert_width)get_flac_datato_convert_raterS   )r8   rC   rE   s      r    rB   RequestBuilder.build_dataW   s6    ,,--j.D.DE - 
	 r   c                    U S:  a  S$ S$ )zAudio samples must be at least 8 kHz

>>> RequestBuilder.to_convert_rate(16_000)
>>> RequestBuilder.to_convert_rate(8_000)
>>> RequestBuilder.to_convert_rate(7_999)
8000
i@  Nr   rR   s    r    r[   RequestBuilder.to_convert_rate^   s     #d*t44r   )r4   r7   r5   r6   N)
r4   r   r5   r   r6   r   r7   ProfanityFilterLevelreturnNone)rC   r   r`   r
   )r`   r   )rC   r   r`   RequestHeaders)rC   r   r`   bytes)rS   intr`   rd   )r   r   r   r   r9   rG   r@   rA   rB   staticmethodr[   r   r   r   r    r/   r/   $   sa    ) ) 	)
 ) +) 
)+ 	 5 5r   r/   en-US)r5   r6   r7   c                    [        U[        5      (       d  [        S5      eUb   [        U[        5      (       d  [        S5      eUc  Sn[        U UUUS9$ )Nz``language`` must be a stringz$``key`` must be ``None`` or a stringz'AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgwr3   )r>   r   r?   r/   r3   s       r    create_request_builderrh   j   s]     h$$899
z#s33?@@
{7!	 r   c                  P    \ rS rSrSS jrS	S jr\S
S j5       r\SS j5       rSr	g)OutputParser   c                   Xl         X l        g r2   show_allwith_confidence)r8   rn   ro   s      r    r9   OutputParser.__init__   s     .r   c                    U R                  U5      nU R                  (       a  U$ U R                  US   5      nUR                  SS5      nU R                  (       a  US   U4$ US   $ )Nr%   r   g      ?r   )convert_to_resultrn   find_best_hypothesisgetro   )r8   response_textactual_resultbest_hypothesisr   s        r    parseOutputParser.parse   so    ..}===  33-(

 %((s;
"<0*<<|,,r   c                   U R                  S5       Hg  nU(       d  M  [        R                  " U5      S   n[        U5      S:w  d  M6  [        US   R	                  S/ 5      5      S:X  a
  [        5       eUS   s  $    [        5       e)a  
>>> response_text = '''{"result":[]}
... {"result":[{"alternative":[{"transcript":"one two three","confidence":0.49585345},{"transcript":"1 2","confidence":0.42899391}],"final":true}],"result_index":0}
... '''
>>> OutputParser.convert_to_result(response_text)
{'alternative': [{'transcript': 'one two three', 'confidence': 0.49585345}, {'transcript': '1 2', 'confidence': 0.42899391}], 'final': True}

>>> OutputParser.convert_to_result("")
Traceback (most recent call last):
  ...
speech_recognition.exceptions.UnknownValueError
>>> OutputParser.convert_to_result('\n{"result":[]}')
Traceback (most recent call last):
  ...
speech_recognition.exceptions.UnknownValueError
>>> OutputParser.convert_to_result('{"result":[{"foo": "bar"}]}')
Traceback (most recent call last):
  ...
speech_recognition.exceptions.UnknownValueError
>>> OutputParser.convert_to_result('{"result":[{"alternative": []}]}')
Traceback (most recent call last):
  ...
speech_recognition.exceptions.UnknownValueError

r+   r   r%   )splitjsonloadslenrt   r   )ru   liner+   s      r    rr   OutputParser.convert_to_result   sx    6 "''-D#'::d#3H#=F6{avay}}]B78A=+--ay  .  !!r   c                T    SU ;   a  [        U S S9nOU S   nSU;  a
  [        5       eU$ )a  
>>> alternatives = [{"transcript": "one two three", "confidence": 0.42899391}, {"transcript": "1 2", "confidence": 0.49585345}]
>>> OutputParser.find_best_hypothesis(alternatives)
{'transcript': 'one two three', 'confidence': 0.42899391}

>>> alternatives = [{"confidence": 0.49585345}]
>>> OutputParser.find_best_hypothesis(alternatives)
Traceback (most recent call last):
  ...
speech_recognition.exceptions.UnknownValueError
r   c                    U S   $ )Nr   r   )r%   s    r    <lambda>3OutputParser.find_best_hypothesis.<locals>.<lambda>   s	    L(Ar   )r5   r   r   )maxr   )alternativesrw   s     r    rs   !OutputParser.find_best_hypothesis   s@     <' ,/A,O ,8?O.#%%r   rm   N)rn   r&   ro   r&   r`   ra   )ru   r   )ru   r   r`   r"   )r   r$   r`   r   )
r   r   r   r   r9   rx   re   rr   rs   r   r   r   r    rj   rj      s5    /- "" ""H  r   rj   c                "    [        XS9nUR                  5       R                  S5      $ ! [         a)  n[        SR                  UR                  5      5      eS nAf[
         a)  n[        SR                  UR                  5      5      eS nAff = f)Ntimeoutzrecognition request failed: {}z!recognition connection failed: {}zutf-8)r   r   r   formatreasonr   readdecode)rF   r   responsees       r    obtain_transcriptionr      s    
74 ==?!!'**  N;BB188LMM 
/66qxx@
 	

s    	* 
B$AB%$B		B)r4   c                   [        XrX4S9nUR                  U5      n	[        XR                  S9n
[	        XVS9nUR                  U
5      $ )a  Performs speech recognition on ``audio_data`` (an ``AudioData`` instance), using the Google Speech Recognition API.

The Google Speech Recognition API key is specified by ``key``. If not specified, it uses a generic key that works out of the box. This should generally be used for personal or testing purposes only, as it **may be revoked by Google at any time**.

To obtain your own API key, simply following the steps on the `API Keys <http://www.chromium.org/developers/how-tos/api-keys>`__ page at the Chromium Developers site. In the Google Developers Console, Google Speech Recognition is listed as "Speech API".

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 tags can be found in this `StackOverflow answer <http://stackoverflow.com/a/14302134>`__.

The profanity filter level can be adjusted with ``pfilter``: 0 - No filter, 1 - Only shows the first character and replaces the rest with asterisks. The default is level 0.

Returns the most likely transcription if ``show_all`` is false (the default). Otherwise, returns the raw API response as a JSON dictionary.

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.
r3   r   rm   )rh   rG   r   operation_timeoutrj   rx   )
recognizerrC   r5   r6   pfilterrn   ro   r4   request_builderrF   ru   output_parsers               r    recognize_legacyr      sZ    2 -XO ##J/G(55M !M }--r   )
r4   r   r5   
str | Noner6   r   r7   r_   r`   r/   )rF   r
   r   rd   r`   r   )Nrf   r   FF)rC   r   r5   r   r6   r   r   r_   rn   r&   ro   r&   r4   r   )"
__future__r   r}   typingr   r   r   urllib.errorr   r   urllib.parser	   urllib.requestr
   r   typing_extensionsr   speech_recognition.audior   speech_recognition.exceptionsr   r   r   r"   r)   r_   r   rb   ENDPOINTr/   rh   rj   r   r   r   r   r    <module>r      s(   "  + + , " + ) . I) 
Y 
#Y #
 t} c3h:C5 C5R )* 
 	
 ' ,R Rj	+ $%!%. %.%. 
%. 	%.
 "%. %. %. %.r   