'Virtual Assistant - Python
So I just started programming on python and I'm making my own virtual assistant, I have a problem where I want "Eve" to say a phrase before I talk, but I does not work. I know is something stupid but I just started recently so I'm stuck with this problem, hope someone help me with this.
import speech_recognition as sr
import pyttsx3
import pywhatkit
import datetime
import wikipedia
listener = sr.Recognizer()
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
def greeting():
engine.say("Hello I'm Eve. How can help you?")
def talk(text):
engine.say(text)
engine.runAndWait()
def take_command():
try:
with sr.Microphone() as source:
print('listening...')
voice = listener.listen(source)
command = listener.recognize_google(voice)
command = command.lower()
if 'Eve' in command:
command = command.replace('Eve', '')
print(command)
except:
pass
return command
def run_eve():
command = take_command()
print(command)
if 'play' in command:
song = command.replace('play', '')
talk('playing ' + song)
pywhatkit.playonyt(song)
elif 'time' in command:
time = datetime.datetime.now().strftime('%I:%M %p')
talk('Current time is ' + time)
elif 'who is this' in command:
person = command.replace('who is this', '')
info = wikipedia.summary(person, 1)
print(info)
talk(info)
else:
talk('Please say the command again.')
while True:
run_eve()
Solution 1:[1]
It's not running because you hav'nt called the function greeting. So just add greeting() in run_eve function.
If you want to run greeting only once throught out the program, there are 2 ways:
- Changing the function:
def run_eve():
Greeting()
while True:
command = take_command()
print(command)
if 'play' in command:
song = command.replace('play', '')
talk('playing ' + song)
pywhatkit.playonyt(song)
elif 'time' in command:
time =
datetime.datetime.now().strftime('%I:%M %p')
talk('Current time is ' + time)
elif 'who is this' in command:
person = command.replace('who is this', '')
info = wikipedia.summary(person, 1)
print(info)
talk(info)
else:
talk('Please say the command again.')
Just all the code you want to repeat in a loop, and the code you don't want to repeat: keep it out side the loop.
- Changing while loop
Index=0
while True:
If index==0:
Greeting()
Run_eve()
Index+=1
Else:
Run_eve()
Just use a variable to check how many time loop is running, if it's 0 then run the greeting otherwise just run the Main function.
Solution 2:[2]
You don't call greeting(). Maybe:
def take_command():
try:
with sr.Microphone() as source:
print('listening...')
voice = listener.listen(source)
command = listener.recognize_google(voice)
command = command.lower()
if 'Eve' in command:
command = command.replace('Eve', '')
print(command)
except:
command = False # Change this line
return command
def run_eve():
command = take_command()
if not command: # add this and two line below
greeting()
return
print(command)
if 'play' in command:
song = command.replace('play', '')
talk('playing ' + song)
pywhatkit.playonyt(song)
elif 'time' in command:
time = datetime.datetime.now().strftime('%I:%M %p')
talk('Current time is ' + time)
elif 'who is this' in command:
person = command.replace('who is this', '')
info = wikipedia.summary(person, 1)
print(info)
talk(info)
else:
talk('Please say the command again.')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Faraaz Kurawle |
| Solution 2 | Radek Rojík |
