By Dipak Ghosh
Personal Assistant using Python Speech Recognition and Voice Input from user with various features like opening several websites and applications etc.
import speech_recognition as sr from win32com.client import constants, Dispatch import datetime, wikipedia, webbrowser
Modules used: speech_recognition, datetime, wikipedia, webbrowser, win32com
Know more about these and their installation here.
def asst_speaks(audio) def greet() def user_speaks() def reg_browser() def main()
Functions used: asst_speaks(audio), greet(), user_speaks(), reg_browser(), main()
Let us see about these functions in detail.
asst_speaks(audio) - This function is used whenever the assistant has to speak. The 'audio' parameter is the text that it will speak which are based on evaluation of user input passed.
def asst_speaks(audio):
print(name+": "+audio.upper())
speaker = Dispatch("SAPI.SpVoice")
speaker.speak(audio)
del speaker
greet() - This function is used to generate the greeting message by detecting the hour when asked to greet the user like "Good Morning!", "Good Afternoon", "Good Evening" etc. By default it greets on loading the system.
def greet():
h = int(datetime.datetime.now().hour)
if h>=0 and h<12:
asst_speaks("Good morning!")
elif h==12:
asst_speaks("Good noon!")
elif h>12 and h<18:
asst_speaks("good afternoon!")
else:
asst_speaks("good evening")
asst_speaks("I am "+name+", how can I help you?")
def user_speaks():
r=sr.Recognizer()
with sr.Microphone(device_index=1) as source:
print("Listening...")
audio = r.listen(source, phrase_time_limit=5)
try:
print("Recognising...")
com=r.recognize_google(audio,language="en-in")
print("You:"+com.upper())
except : #if poor audio quality or excessive background noise
asst_speaks("I didn't get you. Try saying it again!")
return com
reg_browser() - This function is used to register the browser (Google Chrome) in this case with the program. It is executed at the beginning of the execution of the program for hassle free execution og thr program.
def reg_browser():
chrome_path="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
webbrowser.register('google-chrome', None,webbrowser.BackgroundBrowser(chrome_path))
main() - This function is the driver code for the entire program. Here is where all the functions are brought to operation. Firstly, the greet() and reg_browser() functions are executed. Then user is asked for input of command and as per the command the task is executed and this process continues...
def main(): #driver code
reg_browser()
greet()
while(True): #infinite loop for seamless interaction
com=user_speaks().lower() #taking user command
if "wikipedia" in com: #if users says to wiki something
asst_speaks("Searching wikipedia")
com=com.replace("wikipedia","")
res=wikipedia.summary(com,sentences=2) #extracting summary of the wikipedia search
asst_speaks("According to wikipedia:"+res) #audio output of the summary
asst_speaks("Want to know more! Shall I open wikipedia?")
dec=user_speaks().lower()
if "yes" or "yeah" in dec:
webbrowser.get("google-chrome").open("en.wikipedia.com") #opens wikipedia home page
else:
asst_speaks("As you wish sir!")
elif "open youtube" in com: #if users says to open youtube
webbrowser.get("google-chrome").open("youtube.com")
elif "open google" in com: #if user says to open google
webbrowser.get("google-chrome").open("google.com")
elif "open stackoverflow" in com: #if user says to open stack overflow
webbrowser.get("google-chrome").open("stackoverflow.com")
elif "open geeks for geeks" in com: #if user says to open Geeeks for Geeks
webbrowser.get("google-chrome").open("geeksforgeeks.com")
elif "open codechef" in com: #if user says to open Codechef
webbrowser.get("google-chrome").open("codechef.com")
elif "open github" in com: #if user says to open Github
webbrowser.get("google-chrome").open("github.com")
elif "play music" in com or "open spotify" in com: #if user says to open Spotify
webbrowser.get("google-chrome").open("spotify.com")
elif "open hotstar" in com: #if user says to open Hotstar
webbrowser.get("google-chrome").open("hotstar.com")
elif "time" in com: #if user asks the time
timestr=datetime.datetime.now().strftime("%H:%M")
asst_speaks("Sir the time is "+timestr)
elif "your name" in com: #if user asks the assistant his name
asst_speaks("Hi, my name is"+name)
elif "about creator" in com: #if user wants to know about the creator
asst_speaks("I am one of the many creations of sir Dipak Ghosh. Want to know more?")
dec=user_speaks().lower()
if "yes" or "yeah" in dec:
webbrowser.get("google-chrome").open("https://dgreat49251.github.io/dipak.ghosh/") #more about the creator
else:
asst_speaks("As you wish sir!")
elif "exit" or "shutdown" in com: #if user says to exit
asst_speaks("Are you sure you want to exit?")
dec=user_speaks().lower()
if "yes" or "yeah" in dec: #confirmation
break
else:
continue
else:
continue
Features of the Voice Assistant:-
1. Tell about itself
2. Tell about its creator
3. Wikipedia something.
4. Open Websites like Google, GeeksforGeeks, StackOverflow, Spotify, Hotstar etc.
5. Tell date and time.
and much more...
Submitted by Dipak Ghosh (DGhosh49251)
Download packets of source code on Coders Packet
Comments