当前位置: 首页 > 后端技术 > Python

挑战100+Python项目4:如何练习说英语

时间:2023-03-26 16:07:55 Python

来学习Python编程?我认为最好的方法是“边做边学,边玩边学”。只有自己动手做Python项目,才能学以致用,真正掌握这门编程语言,为我所用。编程高手俱乐部发起100+Python项目挑战,代码和文档开源:https://github.com/zhiwehu/10...加油,一起来吧!学习英语口语对很多人来说都是困难的。今天我们就用Python编程来练习口语吧!项目需要在命令行窗口运行;程序运行时,会要求你输入一句英文,然后你需要对着麦克风读出这句英文;程序会判断你的阅读是否正确,如果不正确,它会让你重新阅读,直到正确为止。Python编程知识点while循环用户输入字符串字符串小写条件判断自定义函数异常处理SpeechRecognition模块(安装:pipinstallSpeechRecognition)支持这些语音识别:recognize_bing():MicrosoftBingSpeechrecognize_google():GoogleWebSpeechAPIrecognize_google_cloud():GoogleCloudSpeech-需要安装google-cloud-speech模块recognize_houndify():HoundifybySoundHoundrecognize_ibm():IBMSpeechtoTextrecognize_sphinx():CMUSphinx-需要安装PocketSphinx模块installpyaudio)参考代码importspeech_recognitionassrdefrecognize_speech_from_mic(recognizer,microphone):'''Microphone记录并转换文本`microphone`。:paramrecognizer:speechrecognizer:parammicrophone:microphone:return:`None`如果识别失败,返回None,否则返回语音文本'''print('Startreadingaloud')#以麦克风为源录制并去除噪音:recognizer.adjust_for_ambient_noise(source)audio=recognizer.listen(source)#调用语音识别,亲测微软bing国内可用,国外推荐使用google试试:text=recognizer.recognize_google(audio)exceptExceptionase:print(e)text=Nonereturntextif__name__=='__main__':#Inputtext=input('PleaseenteranEnglishsentence:').strip()#创建语音识别器和麦克风识别器=sr.Recognizer()microphone=sr.Microphone()#录制并获取文本mice_recognizer,microphone)ifspeech_text:print('{}{}'.format(speech_text,'?'))else:print('语音识别服务暂时不可用,请稍后再试')注意:这段代码使用了它是谷歌语音识别,部分地区可能无法使用,请注册并使用微软Bing等其他语音识别服务运行测试使用pipinstallrequirements.txt安装模块:pyaudio和SpeechRecognition运行python4.py