任务一:会说话的计算机
import pyttsx3
engine = pyttsx3.init()
engine.setProperty('rate',150)
engine.say("吃葡萄不吐葡萄皮,吐香蕉皮r")
engine.runAndWait()
任务二:英语单词学习助手
import pyttsx3
import time
engine = pyttsx3.init()
engine.setProperty('rate',150)
def say(t):
engine.say(t)
engine.runAndWait()
words = {
"apple": "苹果",
"banana": "香蕉",
"cat": "猫",
"dog": "狗",
"elephant": "大象"
}
print("欢迎使用英语单词学习助手!")
print("*" * 30)
# 听力训练
for k, v in words.items():
print(f"{k}: {v}")
say({k})
time.sleep(0.5)
say({k})
time.sleep(0.5)
print("*" * 30)
print("学习结束,做得很好!")
任务三:计算机能听懂我说话
import speech_recognition as sr
# 初始化识别器
r = sr.Recognizer()
# 语音识别 使用 Sphinx
with sr.Microphone() as source:
print("请说话...")
audio = r.listen(source)
try:
text = r.recognize_sphinx(audio, language='en-US')
print(f"你说的是: {text}")
except:
print(f"未能识别")
综合任务:英语听说训练助手
import pyttsx3
import speech_recognition as sr
import time
import random
engine = pyttsx3.init()
engine.setProperty('rate', 150)
recognizer = sr.Recognizer()
def say(text):
engine.say(text)
engine.runAndWait()
def listen():
with sr.Microphone() as source:
print("请说出这个单词...")
audio = recognizer.listen(source)
try:
return recognizer.recognize_sphinx(audio, language='en-US').lower()
except sr.UnknownValueError:
print("抱歉,我没听清。")
return None
except sr.RequestError:
print("无法连接到语音识别服务。")
return None
words = {
"apple": "苹果",
"banana": "香蕉",
"cat": "猫",
"dog": "狗",
"elephant": "大象"
}
# 主程序
if __name__ == "__main__":
print("欢迎使用英语单词听说训练助手!")
print("*" * 30)
print("* 1.听力训练")
print("* 2.阅读训练")
print("*" * 30)
s = input("请选择功能序号:")
if s=='1' or s=='':
print("让我们来进行听力训练。")
for k, v in words.items():
print(f"{k} : {v}")
say(k)
time.sleep(0.5)
say(k)
time.sleep(0.5)
else:
# 阅读训练
print("n现在,让我们来练习发音。")
word_list = list(words.keys())
random.shuffle(word_list)
score = 0
for word in word_list:
print(f"n请说出 '{words[word]}' 的英文:")
say(f"请说出 {words[word]} 的英文")
user_said = listen()
if user_said == word:
print("发音正确!真棒!")
say("发音正确!真棒!")
score += 1
else:
print(f"再试一次,正确发音是: {word}")
say(f"再试一次,正确发音是: {word}")
time.sleep(1)
print("n*" * 30)
print(f"训练结束!你的得分是: {score}/{len(word_list)}")
say(f"训练结束!你的得分是: {score}分,满分{len(word_list)}分")
print("做得很好!继续努力!")
参考资料