o
    dZŽhµ  ã                   @  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	 |¡s„td |¡ƒ‚tj	 |¡s‘td |¡ƒ‚| ¡ }| d|
¡ | d|¡ | d|¡ | dtj¡ | |¡}|jddd}|durítdƒ"}| dd„ |D ƒ¡ | ¡  | d|j¡ | d¡ W d  ƒ n1 sçw   Y  nc|durPtj	 |¡sÿt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ÚkeywordZsensitivity© r   úZ/var/www/auris/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€>  é   )Zconvert_rateZ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#   ÚisfileZConfigZ
set_stringÚdevnullZDecoderZget_raw_datar   Ú
writelinesÚflushZadd_kwsÚnameZactivate_searchÚexistsÚ
ValueErrorÚabspathÚsplitextÚbasenameZget_ruleZ	build_fsgZget_logmathZ	writefileZset_fsgZ
set_searchZ	start_uttZprocess_rawZend_uttZhypZhypstrr   )Z
recognizerr	   r
   r   r   r   r   r   r   Zlanguage_directoryZacoustic_parameters_directoryZlanguage_model_fileZphoneme_dictionary_fileÚconfigÚdecoderÚraw_dataÚfZgrammar_pathZgrammar_nameZfsg_pathÚjsgfÚruleZfsgZ
hypothesisr   r   r   Ú	recognize   sp   "ÿ
(


ù€


r?   )r   NNF)
r	   r   r
   r   r   r   r   r   r   r   )Ú
__future__r   r(   Úcollections.abcr   Zspeech_recognitionr   Zspeech_recognition.audior   Zspeech_recognition.exceptionsr   r   r'   ZAcousticParametersDirectoryPathZLanguageModelFilePathZPhonemeDictionaryFilePathÚtupleZSphinxDataFilePathsÚKeywordÚfloatZSensitivityZKeywordEntryr?   r   r   r   r   Ú<module>   s$    ú