a
    d¸Ñgµ  ã                   @  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dddœdd„ZdS )é    )ÚannotationsN)ÚSequence)ÚPortableNamedTemporaryFile)Ú	AudioData)ÚRequestErrorÚUnknownValueErrorúen-USFr   zstr | SphinxDataFilePathszSequence[KeywordEntry] | Nonez
str | NoneÚbool)Ú
audio_dataÚlanguageÚkeyword_entriesÚgrammarÚshow_allc              	   C  sø  |du s"t dd„ |D ƒƒs"J dƒ‚zddlm}m}m} W n tyT   tdƒ‚Y n0 t|tƒrÎtj	 
tj	 tj	 tj	 t¡¡¡d|¡}	tj	 |	¡s¢td	 |	¡ƒ‚tj	 
|	d
¡}
tj	 
|	d¡}tj	 
|	d¡}n
|\}
}}tj	 |
¡sòt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 ƒ¡ | ¡  | d|j¡ | d¡ W d  ƒ n1 sÚ0    Y  nÈ|dur®tj	 |¡std |¡ƒ‚tj	 tj	 |¡¡}tj	 tj	  |¡¡d }d ||¡}tj	 |¡sˆ||ƒ}| !d |¡¡}| "|| #¡ d¡}| $|¡ n||| #¡ dƒ}| %||¡ | &|¡ | '¡  | (|dd¡ | )¡  |rÖ|S | *¡ }|durî|j+S t,ƒ ‚dS ) 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  s@   | ]8\}}t |td ƒtd ƒfƒo6d|  ko2dkn  V  qdS )Ú r   é   N)Ú
isinstanceÚtype©Ú.0ÚkeywordZsensitivity© r   úi/var/www/html/texto_audio/venv/lib/python3.9/site-packages/speech_recognition/recognizers/pocketsphinx.pyÚ	<genexpr>*   ó    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   r   Ú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_pathZjsgfZruleZfsgZ
hypothesisr   r   r   Ú	recognize   sj    "
(



,


r;   )r   NNF)Ú
__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"       ú