要怎么能力学会Python编程呢?我感觉最好的办法就是“做中学,玩中学”,只有亲自动手去做Python我的项目,能力学以致用,真正把握这门编程语言,为我所用。编程玩家俱乐部推出了挑战100+ Python我的项目,代码和文档开源在:https://github.com/zhiwehu/10...

来吧,让咱们动手做起来!

英语口语学习是很多人都感觉艰难的,明天咱们就来应用Python编程来进行英语口语练习!

我的项目需要

  1. 在命令行窗口运行;
  2. 程序运行时,会让你输出一句英语,而后你须要对着麦克风读出这句英语;
  3. 程序会判断你读的对不对,如果不对会让你重读,直到读对为止。

Python编程知识点

  • while循环
  • 用户输出字符串
  • 字符串小写
  • 条件判断
  • 自定义函数
  • 异样解决
  • SpeechRecognition 模块 (装置: 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 - 须要装置google-cloud-speech模块
    • recognize_houndify(): Houndify by SoundHound
    • recognize_ibm(): IBM Speech to Text
    • recognize_sphinx(): CMU Sphinx - 须要装置PocketSphinx模块
    • recognize_wit(): Wit.ai
  • pyaudio 模块 (装置: pip install pyaudio)

参考代码

import speech_recognition as srdef recognize_speech_from_mic(recognizer, microphone):    '''    麦克风录音并转文字 `microphone`.    :param recognizer: 语音识别器    :param microphone: 麦克风    :return: `None` 如果辨认失败返回None,否则返回语音文字    '''    print('开始朗诵')    # 录音并去除乐音    with microphone as source:        recognizer.adjust_for_ambient_noise(source)        audio = recognizer.listen(source)    # 调用语音辨认,亲测微软bing国内可用,国外倡议应用google    try:        text = recognizer.recognize_google(audio)    except Exception as e:        print(e)        text = None    return textif __name__ == '__main__':    # 输出    text = input('请输出一句英语: ').strip()    # 创立语音识别器和麦克风    recognizer = sr.Recognizer()    microphone = sr.Microphone()    # 录音并获取文字    speech_text = recognize_speech_from_mic(recognizer, microphone)    while speech_text != None and text.lower() != speech_text.lower():        print(speech_text)        speech_text = recognize_speech_from_mic(recognizer, microphone)    if speech_text:        print('{} {}'.format(speech_text, '✓'))    else:        print('语音辨认服务暂不可用,请稍后再试。')
留神:本代码应用的是google语音辨认,有些地区可能无奈失常应用,请注册并应用其余的如微软Bing等语音辨认服务等。

运行测试

  • 应用 pip install requirements.txt 装置模块: pyaudio and SpeechRecognition
  • 运行
python 4.py