虚拟助手代码在python中环回

问题描述

我正在研究虚拟助手,并且几乎完成了。但是,当我尝试对其进行测试时,它要么不会停止监听,要么会循环回到“我能为您提供什么帮助?”

我似乎无法弄清楚问题出在哪里。我已经尝试过更改内容,但是它并没有任何帮助。我已经检查了麦克风,似乎工作正常。实际上,该助手正在接受正确的输入,但没有按预期显示输出

该项目的最新GitHub链接https://github.com/ShishirModi/YourEdu

请帮助我。谢谢!

import speech_recognition as sr
import pyttsx3
import datetime
import wikipedia
import webbrowser
import os
import time
import subprocess
import wolframalpha
import json
import requests
import sys

engine=pyttsx3.init()
voices=engine.getProperty('voices')
engine.setProperty('voice','voices[1].id')

def speak(text):
    engine.say(text)
    engine.runAndWait()


def wishMe():
    hour=datetime.datetime.Now().hour
    if hour>=0 and hour<12:
        speak("Good morning!")
        print("Good morning!")
    elif hour>=12 and hour<18:
        speak("Good afternoon!")
        print("Good afternoon!")
    else:
        speak("Good evening!")
        print("Good evening!")

def takeCommand():
    r=sr.Recognizer()
    with sr.Microphone() as source:
        print("What can I help you with?")
        speak("What can I help you with?")
        print("Listening...")
        r.pause_threshold = 1
        r.adjust_for_ambient_noise(source,duration=1)
        audio=r.listen(source)

        try:
            statement=r.recognize_google(audio,language='en')
            print(f"You said: {statement}\n")

        except sr.UnkNownValueError:
            speak("I'm sorry,I didn't catch that")
            takeCommand()
        return statement

print("Please wait while your assistant loads...")
speak("Please wait while your assistant loads")
wishMe()
if __name__=='__main__':


    while True:
        statement = takeCommand().lower()
        if statement==0:
            continue

if "bye" in statement or "stop" in statement:
  speak('Glad to help you! I will shut down Now')
  print('Glad to help you! I will shut down Now')
  time.sleep(2)
  sys.exit()

elif 'wikipedia' in statement:
  speak('Searching Wikipedia...')
  statement =statement.replace("wikipedia","")
  results = wikipedia.summary(statement,sentences=3)
  speak("According to Wikipedia")
  print(results)
  speak(results)

elif 'open youtube' in statement:
  webbrowser.open_new_tab("https://www.youtube.com")
  speak("Youtube is open Now")
  time.sleep(5)

elif 'open google' in statement:
  webbrowser.open_new_tab("https://www.google.com")
  speak("Google is open Now")
  time.sleep(5)

elif 'open gmail' in statement:
  webbrowser.open_new_tab("gmail.com")
  speak("Gmail is open Now")
  time.sleep(5)

elif 'time' in statement:
  strTime=datetime.datetime.Now().strftime("%H:%M:%s")
  speak(f"the time is {strTime}")

elif 'news' in statement:
  news = webbrowser.open_new_tab("https://bbc.com")
  speak('Here are some headlines from the BBC news,Happy reading!')
  time.sleep(6)

elif 'search'  in statement:
  statement = statement.replace("search","")
  webbrowser.open_new_tab(statement)
  time.sleep(5)

elif 'what is' in statement:
  speak('I can get you answers for computational and geographical problems. Please ask your question Now')
  question=takeCommand()
  client = wolframalpha.Client('6QKTYA-66TGTQE6TX')
  res = client.query(question)
  answer = next(res.results).text
  speak(answer)
  print(answer)

elif 'who are you' in statement or 'what can you do' in statement:
  speak('I am your personal productivity assistant YourEdu. I am programmed to minor tasks like'
  'opening youtube,google chrome,gmail and stackoverflow,predict time,search wikipedia' 
  'get top headline news from BBC and you can ask me computational or geographical questions too!')

elif "who made you" in statement or "who created you" in statement or "who discovered you" in statement:
  speak("I was built by Shishir Modi")
  print("I was built by Shishir Modi")

elif "log off" in statement or "sign out" in statement:
  speak("Ok,your pc will log off in 10 sec make sure you exit from all applications")
  time.sleep(10)
  subprocess.call(["shutdown","/l"])

time.sleep(3)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)