我从互联网上的一篇文章中获得了这段代码,并且已经完成了故障排除,现在它用退出代码0运行。它不会做任何事情,比如问候或要求输入或什么也不做。它只会运行并给出退出代码。
我在安装ecapture时遇到了问题,似乎很多使用Python3.9的人都有这个问题,所以我已经把它注释掉了。
这是我得到代码的来源。
https://towardsdatascience.com/how-to-build-your-own-ai-personal-assistant-using-python-f57247b4494b
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print_hi('PyCharm')
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
import speech_recognition as sr
import pyttsx3
import datetime
import wikipedia
import webbrowser
import os
import time
import subprocess
"""from ecapture import ecapture as ec"""
import wolframalpha
import json
import requests
engine=pyttsx3.init('sapi5')
voice=engine.getProperty('voices')
engine.setProperty('voice', 'voices[0].id')
def speak(text):
engine.say(text)
engine.runAndWait()
def wishMe():
hour=datetime.datetime.now().hour
if hour>=0 and hour<12:
speak("Hello,Good Morning")
print("Hello,Good Morning")
elif hour >= 12 and hour <18:
speak("Hello, Good Afternoon")
print("Hello, Good Afternoon")
else:
speak ("Hello, Good Evening")
print ("Hello, Good Evening")
def takeCommand():
r=sr.Recognizer()
with sr.Microphone() as source:
print ("Listening...")
audio=r.listen(source)
try:
statement=r.recognize_google(audio,language='en-uk')
print(f"user said:{statement}\n")
except Exception as e:
speak ("Run that by me again")
return "None"
return statement
print ("Loading your AI personal assistant G-One")
speak ("Loading your AI personal assistant G-One")
wishMe()
if __name__ == '__main__':
while True:
speak("How may I be of assistance?")
statement = takeCommand().lower()
if statement==0:
continue
if "good bye" in statement or "ok bye" in statement or "that'll be all" in statement:
speak ('your personal assistant G-One is shutting down, Good bye')
print ('your personal assistant G-One is shutting down, Good bye')
if '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 now open")
time.sleep(5)
elif 'open google' in statement:
webbrowser.open_new_tab("https://www.google.com")
speak ("Google chrome is now open")
time.sleep(5)
elif 'open gmail' in statement:
webbrowser.open_new_tab("gmail.com")
speak ("Gmail is now open")
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://news.google.com/topics/CAAqJggKIiBDQkFTRWdvSUwyMHZNRFZxYUdjU0FtVnVHZ0pKVGlnQVAB?hl=en-IN&gl=IN&ceid=IN%3Aen")
speak('Here are some headlines from Google news')
time.sleep(6)
elif 'search' in statement:
statement = statement.replace("search","")
webbrowser.open_new_tab(statement)
time.sleep(5)
elif 'ask' in statement:
speak('I can answer to computational and geographical questions, which of these would you like to ask now')
question=takeCommand()
app_id="LURX84-VPJ6LEHWXV" """Wolfram App ID"""
client = wolframalpha.Client('R2K75H-7ELALHR35X')
res = client.query(question)
answer = next(res.results).text
speak (answer)
print (answer)
elif 'who are you' in statement or 'what are your capabilities' in statement:
speak ('I am Arthur, and am still in my infancy. I am capable of accomplishing minor tasks at this point in time such as'
'opening google products such as youtube, gmail, and chrome.'
'I have other functions such as relating the time, taking pictures,searching wikipedia, relating the weather'
'as well as reading headlines from google news')
elif 'who made you' in statement or 'who built you' in statement or 'who discovered you' in statement:
speak("I was built by Ahmed Hamadto")
print ("I was built by Ahmed Hamadto")
"""include the weather part of the AI here"""
if 'log off' in statement or 'lights out' in statement:
speak ("You've got 10 seconds to clear things up")
subprocess.call(["shutdown","/1"])
time.sleep(3)
""" elif 'camera'in statement or 'take a photo' in statement or 'snap this' in statement:
ec.capture(0,"robo camera","img.jpg")"""发布于 2021-03-02 06:44:40
你的压痕是错的。您已经将if __name__ == "__main__"放入wishMe函数中。Put在外面,应该可以正常工作。
https://stackoverflow.com/questions/66434490
复制相似问题