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
- Run in a command line window;
- When the program runs, it asks you to type in a sentence of English, which you then have to read into a microphone.
- 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 Speechrecognize_google()
: Google Web Speech APIrecognize_google_cloud()
: Google Cloud Speech- Installation requiredgoogle-cloud-speech
The modulerecognize_houndify()
: Houndify by SoundHoundrecognize_ibm()
: IBM Speech to Textrecognize_sphinx()
: CMU Sphinx- Installation requiredPocketSphinx
The modulerecognize_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
- use
pip install requirements.txt
Installation module:pyaudio
andSpeechRecognition
- run
python 4.py
Copy the code