o
    i'                     @  s"  d dl mZ d dlZd dlmZ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Optional	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   X/home/kuhnn/.local/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]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
   )r1   r3   urlr5   	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'
        chromium)clientlangr)   pFilter?)r	   r*   r)   r+   r(   )r1   paramsr   r   r   r8   <   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)r1   r3   rater5   r   r   r   r9   L   s   zRequestBuilder.build_headersbytesc                 C  s   |j | |jdd}|S )N   )convert_rateconvert_width)get_flac_datato_convert_raterG   )r1   r3   r<   r   r   r   r:   W   s
   
zRequestBuilder.build_datarG   intOptional[int]c                 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   rF   r   r   r   rN   ^   s   	zRequestBuilder.to_convert_rateN)
r(   r   r)   r   r*   r   r+   r,   r-   r.   )r3   r   r-   r
   )r-   r   )r3   r   r-   rE   )r3   r   r-   rI   )rG   rO   r-   rP   )
r   r   r   r2   r>   r8   r9   r:   staticmethodrN   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-6x9WDnZIjIeyEU21OpBXqWBgwr0   )r6   r   r7   r'   r0   r   r   r   create_request_builderj   s   
rT   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/   rV   rW   )r1   rV   rW   r   r   r   r2      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_resultrV   find_best_hypothesisgetrW   )r1   rY   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lenr\   r   )rY   liner$   r   r   r   rZ      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   )rf   r^   r   r   r   r[      s   z!OutputParser.find_best_hypothesisN)rV   r!   rW   r!   r-   r.   )rY   r   )rY   r   r-   r   )rf   r   r-   r   )r   r   r   r2   r_   rQ   rZ   r[   r   r   r   r   rU      s    

$rU   r=   r
   timeoutrO   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ri   zrecognition request failed: {}z!recognition connection failed: {}zutf-8)r   r   r   formatreasonr   readdecode)r=   ri   responseer   r   r   obtain_transcription   s   
rq   F)r(   r3   r   pfilterrV   r!   rW   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.
    r0   rj   rX   )rT   r>   rq   operation_timeoutrU   r_   )
recognizerr3   r)   r*   rr   rV   rW   r(   request_builderr=   rY   output_parserr   r   r   recognize_legacy   s   

rw   )
r(   r   r)   rS   r*   r   r+   r,   r-   r'   )r=   r
   ri   rO   r-   r   )NrR   r   FF)r3   r   r)   rS   r*   r   rr   r,   rV   r!   rW   r!   r(   r   )#
__future__r   rb   typingr   r   r   r   urllib.errorr   r   urllib.parser	   urllib.requestr
   r   typing_extensionsr   speech_recognition.audior   speech_recognition.exceptionsr   r   r   r   r#   r,   r   rE   ENDPOINTr'   rT   rU   rq   rw   r   r   r   r   <module>   s<    I
V	