<jupyter_start><jupyter_text>Speech RecognitionIn this part we'll cover different solutions to translate wo words what's been recorded by a microphone in an audio file or by mic streaming.<jupyter_code>import speech_recognition as sr<jupyter_output><empty_output><jupyter_text>SpeechRecognition[SpeechRecognition](https://github.com/Uberi/speech_recognition) is a speech recognition library for Python. It handles various language processors, online and offline. Let's dive into its capabilities. First of all, let's create *Recognizer()* instance.<jupyter_code>r = sr.Recognizer()<jupyter_output><empty_output><jupyter_text>Next, let's load an audio file. For it, select the location of the file and use the *record* method*<jupyter_code>harvard = sr.AudioFile('audio_data/harvard.wav')
with harvard as source:
    audio = r.record(source)<jupyter_output><empty_output><jupyter_text>There're several options to pass to the *record* method, such as:>**offset**: Seconds that shall pass before starting to perform the recognition  >**duration**: Duration in seconds of the clip of interest Finally, it's necessary to load any of the services that will deliver the actual text. These are:* CMU Sphinx (works offline)* Google Speech Recognition* Google Cloud Speech API* Wit.ai* Microsoft Bing Voice Recognition* Houndify API* IBM Speech to Text* Snowboy Hotword Detection (works offline)Once selected one of those, let's feed the data into it.<jupyter_code>r.recognize_google(audio)<jupyter_output><empty_output><jupyter_text>In case there is some noise into the audio, there's a useful method to try to deal with it:<jupyter_code>jackhammer = sr.AudioFile('audio_data/jackhammer.wav')

with jackhammer as source:
    r.adjust_for_ambient_noise(source, duration=0.5)
    audio = r.record(source)

r.recognize_google(audio, show_all=True)<jupyter_output><empty_output><jupyter_text>The duration parameter tries to take information of the audio's noise in the first given seconds and apply a sort of filter to the rest of the clip. Depending on the application, this or other techniques should be taken into consideration to achieve the best possible results.<jupyter_code>harvard = sr.AudioFile('audio_data/harvard.wav')

with harvard as source:
    audio = r.record(source)

r.recognize_sphinx(audio)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07989690721649484, 'fraction_numerical': 0.000859106529209622, 'mean_word_length': 6.512903225806451, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '207736', 'license': 'MIT', 'n_tokens_mistral': 710, 'n_tokens_neox': 665, 'n_words': 248, 'path': 'speech_recognition_.ipynb', 'repo_name': 'xavialex/nlp_poc', 'size': '2328'}