o
    i                     @  s   d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
mZ eZeZeZeeeef ZeZeZeeef Z				ddddZdS )    )annotationsN)Sequence)PortableNamedTemporaryFile)	AudioData)RequestErrorUnknownValueErroren-USF
audio_datar   languagestr | SphinxDataFilePathskeyword_entriesSequence[KeywordEntry] | Nonegrammar
str | Noneshow_allboolc              	   C  s  |du st dd |D sJ dzddlm}m}m} W n ty(   tdw t|tretj	
tj	tj	tj	td|}	tj	|	sOtd	|	tj	
|	d
}
tj	
|	d}tj	
|	d}n|\}
}}tj	|
swtd|
tj	|std|tj	|std|| }|d|
 |d| |d| |dtj ||}|jddd}|durtd"}|dd |D  |  |d|j |d W d   n1 sw   Y  nc|durPtj	|std|tj	tj	|}tj	tj	 |d }d||}tj	|s=||}|!d|}|"||# d}|$| n|||# d}|%|| |&| |'  |(|dd |)  |rd|S |* }|durp|j+S t, ) aP  
    Performs speech recognition on ``audio_data`` (an ``AudioData`` instance), using CMU Sphinx.

    The recognition language is determined by ``language``, an RFC5646 language tag like ``"en-US"`` or ``"en-GB"``, defaulting to US English. Out of the box, only ``en-US`` is supported. See `Notes on using `PocketSphinx <https://github.com/Uberi/speech_recognition/blob/master/reference/pocketsphinx.rst>`__ for information about installing other languages. This document is also included under ``reference/pocketsphinx.rst``. The ``language`` parameter can also be a tuple of filesystem paths, of the form ``(acoustic_parameters_directory, language_model_file, phoneme_dictionary_file)`` - this allows you to load arbitrary Sphinx models.

    If specified, the keywords to search for are determined by ``keyword_entries``, an iterable of tuples of the form ``(keyword, sensitivity)``, where ``keyword`` is a phrase, and ``sensitivity`` is how sensitive to this phrase the recognizer should be, on a scale of 0 (very insensitive, more false negatives) to 1 (very sensitive, more false positives) inclusive. If not specified or ``None``, no keywords are used and Sphinx will simply transcribe whatever words it recognizes. Specifying ``keyword_entries`` is more accurate than just looking for those same keywords in non-keyword-based transcriptions, because Sphinx knows specifically what sounds to look for.

    Sphinx can also handle FSG or JSGF grammars. The parameter ``grammar`` expects a path to the grammar file. Note that if a JSGF grammar is passed, an FSG grammar will be created at the same location to speed up execution in the next run. If ``keyword_entries`` are passed, content of ``grammar`` will be ignored.

    Returns the most likely transcription if ``show_all`` is false (the default). Otherwise, returns the Sphinx ``pocketsphinx.pocketsphinx.Decoder`` object resulting from the recognition.

    Raises a ``speech_recognition.UnknownValueError`` exception if the speech is unintelligible. Raises a ``speech_recognition.RequestError`` exception if there are any issues with the Sphinx installation.
    Nc                 s  sB    | ]\}}t |td td fod|  kodkn  V  qdS ) r      N)
isinstancetype.0keywordsensitivity r   ^/home/kuhnn/.local/lib/python3.10/site-packages/speech_recognition/recognizers/pocketsphinx.py	<genexpr>*   s   @ zrecognize.<locals>.<genexpr>z^``keyword_entries`` must be ``None`` or a list of pairs of strings and numbers between 0 and 1r   )FsgModelJsgfpocketsphinxzJmissing PocketSphinx module: ensure that PocketSphinx is set up correctly.zpocketsphinx-dataz2missing PocketSphinx language data directory: "{}"zacoustic-modelzlanguage-model.lm.binzpronounciation-dictionary.dictz>missing PocketSphinx language model parameters directory: "{}"z.missing PocketSphinx language model file: "{}"z2missing PocketSphinx phoneme dictionary file: "{}"z-hmmz-lmz-dictz-logfni>     )convert_rateconvert_widthwc                 s  s(    | ]\}}d  |d| d V  qdS )z
{} /1e{}/
d   n   N)formatr   r   r   r   r   P   s   & keywordszGrammar '{0}' does not exist.z{0}/{1}.fsgz{0}.{0}g      @FT)-allr   r   r   ImportErrorr   r   strospathjoindirnamerealpath__file__isdirr&   isfileConfig
set_stringdevnullDecoderget_raw_datar   
writelinesflushadd_kwsnameactivate_searchexists
ValueErrorabspathsplitextbasenameget_rule	build_fsgget_logmath	writefileset_fsg
set_search	start_uttprocess_rawend_utthyphypstrr   )
recognizerr	   r
   r   r   r   r   r   r   language_directoryacoustic_parameters_directorylanguage_model_filephoneme_dictionary_fileconfigdecoderraw_datafgrammar_pathgrammar_namefsg_pathjsgfrulefsg
hypothesisr   r   r   	recognize   sp   "
(





r]   )r   NNF)
r	   r   r
   r   r   r   r   r   r   r   )
__future__r   r+   collections.abcr   speech_recognitionr   speech_recognition.audior   speech_recognition.exceptionsr   r   r*   AcousticParametersDirectoryPathLanguageModelFilePathPhonemeDictionaryFilePathtupleSphinxDataFilePathsKeywordfloatSensitivityKeywordEntryr]   r   r   r   r   <module>   s$    