How do you learn to program In Python? I think the best way to do this is to “learn by doing, learn by playing”. Only by working on Python projects with my own hands can I really master the language and use it for my own purposes. The Programming Players Club has launched a challenge for 100+ Python projects, code and documentation open source at: github.com/zhiwehu/100…

Come on, let’s do it!

Learning spoken English is difficult for many people. Today we are going to use Python programming to practice spoken English!

The project requirements

  1. Run in a command line window;
  2. When the program runs, it asks you to type in a sentence of English, which you then have to read into a microphone.
  3. The program will judge if you read correctly, and if you don’t, it will ask you to reread it until you read correctly.

Python Programming Knowledge

  • The while loop
  • User input string
  • String lowercase
  • conditional
  • Custom function
  • Exception handling
  • SpeechRecognition module (Install:pip install SpeechRecognition) it support these speech recognitions:
    • recognize_bing(): Microsoft Bing Speech
    • recognize_google(): Google Web Speech API
    • recognize_google_cloud(): Google Cloud Speech- Installation requiredgoogle-cloud-speechThe module
    • recognize_houndify(): Houndify by SoundHound
    • recognize_ibm(): IBM Speech to Text
    • recognize_sphinx(): CMU Sphinx- Installation requiredPocketSphinxThe module
    • recognize_wit(): Wit.ai
  • Pyaudio module (Install:pip install pyaudio)

Reference code

import speech_recognition as sr


def recognize_speech_from_mic(recognizer, microphone) :
    Microphone recording and transfer text 'microphone'. : Param recognizer: param microphone: return: 'None' Returns None if recognition fails, otherwise returns voice text ''
    print('Start reading')
    # Record and remove noise
    with microphone as source:
        recognizer.adjust_for_ambient_noise(source)
        audio = recognizer.listen(source)

    # Call voice recognition, pro test Microsoft Bing available in China, foreign advice to use Google
    try:
        text = recognizer.recognize_google(audio)
    except Exception as e:
        print(e)
        text = None

    return text


if __name__ == '__main__':
    # enter
    text = input('Please enter an English sentence:').strip()

    # Create a speech recognizer and microphone
    recognizer = sr.Recognizer()
    microphone = sr.Microphone()

    # Record and get the text
    speech_text = recognize_speech_from_mic(recognizer, microphone)

    whilespeech_text ! =None andtext.lower() ! = speech_text.lower():print(speech_text)
        speech_text = recognize_speech_from_mic(recognizer, microphone)

    if speech_text:
        print('{} {}'.format(speech_text, '✓'))
    else:
        print('Voice recognition service is temporarily unavailable. Please try again later. ')

Copy the code

Run the test

  • usepip install requirements.txtInstallation module:pyaudio and SpeechRecognition
  • run
python 4.py
Copy the code