赞
踩
import speech_recognition as sr
import pyttsx3
import datetime
import wikipedia
import webbrowser
import os
import smtplib
import pyowm
# 初始化语音引擎
engine = pyttsx3.init()
# 初始化语音识别器
recognizer = sr.Recognizer()
# 设置邮件发送相关信息
email_address = "your_email@gmail.com"
email_password = "your_password"
smtp_server = "smtp.gmail.com"
smtp_port = 587
# 初始化天气API
owm_api_key = "your_owm_api_key"
owm = pyowm.OWM(owm_api_key)
def speak(text):
engine.say(text)
engine.runAndWait()
def listen():
with sr.Microphone() as source:
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
try:
user_input = recognizer.recognize_google(audio)
return user_input.lower()
except sr.UnknownValueError:
return "Could not understand audio"
except sr.RequestError as e:
return "Could not request results; {0}".format(e)
def wish_me():
hour = datetime.datetime.now().hour
if 0 <= hour < 12:
speak("Good morning!")
elif 12 <= hour < 18:
speak("Good afternoon!")
else:
speak("Good evening!")
speak("How can I assist you today?")
def send_email(receiver_email, subject, body):
try:
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(email_address, email_password)
message = "Subject: {}\n\n{}".format(subject, body)
server.sendmail(email_address, receiver_email, message)
server.quit()
speak("Email sent successfully.")
except Exception as e:
speak("Sorry, I am unable to send the email at the moment.")
def get_weather_forecast(city):
try:
observation = owm.weather_at_place(city)
weather = observation.get_weather()
status = weather.get_status()
temperature = weather.get_temperature('celsius')['temp']
speak("The weather in {} is currently {}. The temperature is {} degrees Celsius.".format(city, status, temperature))
except Exception as e:
speak("Sorry, I couldn't retrieve the weather information for {}".format(city))
def main():
wish_me()
while True:
user_input = listen()
if "wikipedia" in user_input:
speak("Searching Wikipedia...")
user_input = user_input.replace("wikipedia", "")
result = wikipedia.summary(user_input, sentences=2)
speak("According to Wikipedia, ")
speak(result)
elif "open youtube" in user_input:
webbrowser.open("https://www.youtube.com")
elif "open google" in user_input:
webbrowser.open("https://www.google.com")
elif "play music" in user_input:
music_dir = "C:\\Users\\User\\Music"
songs = os.listdir(music_dir)
if songs:
os.startfile(os.path.join(music_dir, songs[0]))
elif "send email" in user_input:
speak("Who is the recipient?")
recipient = listen()
speak("What is the subject of the email?")
subject = listen()
speak("What should I say in the email?")
body = listen()
send_email(recipient, subject, body)
elif "weather forecast" in user_input:
speak("Which city's weather forecast would you like to know?")
city = listen()
get_weather_forecast(city)
elif "exit" in user_input:
speak("Goodbye!")
break
else:
speak("I'm sorry, I didn't understand that.")
if __name__ == "__main__":
main()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。