o
    dZh'                     @  s  d dl mZ d dlZd dlmZmZmZ d dlmZm	Z	 d dl
mZ d dlmZmZ d dlmZ d dlmZ d d	lmZmZ G d
d deZG dd deZG dd deZed Zeeef ZdZG dd dZddd dd2ddZG d d! d!Zd3d&d'Z 			 	(	(d4ed)d5d0d1Z!dS )6    )annotationsN)DictLiteral	TypedDict)	HTTPErrorURLError)	urlencode)Requesturlopen)NotRequired)	AudioData)RequestErrorUnknownValueErrorc                   @     e Zd ZU ded< ded< dS )Alternativestr
transcriptfloat
confidenceN__name__
__module____qualname____annotations__ r   r   T/var/www/auris/lib/python3.10/site-packages/speech_recognition/recognizers/google.pyr         
 r   c                   @  r   )Resultlist[Alternative]alternativeboolfinalNr   r   r   r   r   r      r   r   c                   @  r   )GoogleResponsezlist[Result]resultzNotRequired[int]Zresult_indexNr   r   r   r   r   r"      r   r"   )r      z-http://www.google.com/speech-api/v2/recognizec                   @  sL   e Zd Zdd	d
ZdddZdddZd ddZd!ddZed"ddZ	dS )#RequestBuilderendpointr   keylanguagefilter_levelProfanityFilterLevelreturnNonec                C  s   || _ || _|| _|| _d S Nr&   r'   r(   r)   )selfr&   r'   r(   r)   r   r   r   __init__%   s   
zRequestBuilder.__init__
audio_datar   r	   c                 C  s@   t |ts	td|  }| |}| |}t|||d}|S )Nz!``audio_data`` must be audio data)dataheaders)
isinstancer   
ValueError	build_urlbuild_headers
build_datar	   )r/   r1   urlr3   	flac_datarequestr   r   r   build2   s   


zRequestBuilder.buildc                 C  s(   t d| j| j| jd}| j d| S )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'
        Zchromium)clientlangr'   ZpFilter?)r   r(   r'   r)   r&   )r/   paramsr   r   r   r6   <   s   zRequestBuilder.build_urlRequestHeadersc                 C  s   |j }dd| i}|S )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)r/   r1   Zrater3   r   r   r   r7   L   s   zRequestBuilder.build_headersbytesc                 C  s   |j | |jdd}|S )N   )Zconvert_rateZconvert_width)Zget_flac_datato_convert_raterC   )r/   r1   r:   r   r   r   r8   W   s
   
zRequestBuilder.build_datarC   intc                 C  s   | dkrdS d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   rB   r   r   r   rF   ^   s   	zRequestBuilder.to_convert_rateN)
r&   r   r'   r   r(   r   r)   r*   r+   r,   )r1   r   r+   r	   )r+   r   )r1   r   r+   rA   )r1   r   r+   rD   )rC   rG   r+   rG   )
r   r   r   r0   r<   r6   r7   r8   staticmethodrF   r   r   r   r   r%   $   s    





r%   en-US)r'   r(   r)   r&   r   r'   
str | Noner(   r)   r*   r+   c                 C  sH   t |ts	td|d urt |tstd|d u rd}t| |||dS )Nz``language`` must be a stringz$``key`` must be ``None`` or a stringz'AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgwr.   )r4   r   r5   r%   r.   r   r   r   create_request_builderj   s   
rK   c                   @  s<   e Zd ZdddZdd
dZedddZedddZdS )OutputParsershow_allr    with_confidencer+   r,   c                C  s   || _ || _d S r-   rM   rN   )r/   rM   rN   r   r   r   r0      s   
zOutputParser.__init__response_textr   c                 C  sH   |  |}| jr
|S | |d }|dd}| jr |d |fS |d S )Nr   r   g      ?r   )convert_to_resultrM   find_best_hypothesisgetrN   )r/   rP   Zactual_resultbest_hypothesisr   r   r   r   parse   s   
zOutputParser.parser   c                 C  s`   |  dD ]'}|s
qt|d }t|dkr,t|d dg dkr&t |d   S qt )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lenrS   r   )rP   liner#   r   r   r   rQ      s   zOutputParser.convert_to_resultalternativesr   r   c                 C  s4   d| v rt | dd d}n| d }d|vrt |S )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                 S  s   | d S )Nr   r   )r   r   r   r   <lambda>   s    z3OutputParser.find_best_hypothesis.<locals>.<lambda>)r'   r   r   )maxr   )r\   rT   r   r   r   rR      s   z!OutputParser.find_best_hypothesisN)rM   r    rN   r    r+   r,   )rP   r   )rP   r   r+   r   )r\   r   r+   r   )r   r   r   r0   rU   rH   rQ   rR   r   r   r   r   rL      s    

$rL   r;   r	   timeoutrG   c              
   C  sn   zt | |d}W n' ty } ztd|jd }~w ty/ } ztd|jd }~ww | dS )Nr_   zrecognition request failed: {}z!recognition connection failed: {}zutf-8)r
   r   r   formatreasonr   readdecode)r;   r_   responseer   r   r   obtain_transcription   s   
rg   F)r&   r1   r   pfilterrM   r    rN   c                C  s>   t ||||d}||}	t|	| jd}
t||d}||
S )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.
    r.   r`   rO   )rK   r<   rg   Zoperation_timeoutrL   rU   )Z
recognizerr1   r'   r(   rh   rM   rN   r&   Zrequest_builderr;   rP   Zoutput_parserr   r   r   recognize_legacy   s   

ri   )
r&   r   r'   rJ   r(   r   r)   r*   r+   r%   )r;   r	   r_   rG   r+   r   )NrI   r   FF)r1   r   r'   rJ   r(   r   rh   r*   rM   r    rN   r    r&   r   )"
__future__r   rX   typingr   r   r   urllib.errorr   r   urllib.parser   urllib.requestr	   r
   Ztyping_extensionsr   Zspeech_recognition.audior   Zspeech_recognition.exceptionsr   r   r   r   r"   r*   r   rA   ZENDPOINTr%   rK   rL   rg   ri   r   r   r   r   <module>   s<    I
U	