Build Alexa using Python
Have you ever thought of buying an Alexa for some talky business with it? Well, instead of spending 100s of dollars on it, why not build Alexa using Python. Now, wait a second. Is python even capable of making such an assistant? The answer is yes.
With the help of the SpeechRecognition library and a few others (such as Pyttsx3, OS, PyJokes, Wikipedia, and a few more). Before we move on to the step-by-step version of this tutorial, watch the video on the topic (you can do anything you want. If you can’t understand the article, refer to the video and if you can’t understand the latter, refer to the former).
So without further ado, let us get started in making our own Alexa. Firstly, you need to import the following libraries into your project.
import webbrowser import speech_recognition as sr import pyjokes import pyttsx3 as p import datetime import wikipedia import os
Now that you have imported the required libraries into your project, it’s time we begin work on our project. We’ll use pyttsx3 and SpeechRecognition to set up our speech and voice recognition for the first few lines of code. But before anything, you’ll see that some of your imports are giving you an error and that is because we didn’t yet install those packages. To do so, you’ll need the following commands. (The main code follows this minor fix).
pip3 install pyjokes pip3 install wikipedia pip3 install SpeechRecognition pip3 install pyttsx3
engine = p.init() engine.setProperty("rate", 100) def text(text): engine.say(text) engine.runAndWait() r = sr.Recognizer() try: with sr.Microphone() as source: print("Say Something...") voice = r.listen(source) command = r.recognize_google(voice) print("done..") print(command)
So basically, in this chunk of code, we are setting up our speech recognizer. First, we ask the program to listen by typing r.listen and then send the recorded audio to google’s speech recognition to be processed and be stored in a variable, named command. Later we print our command variable to show what we have spoken. Pretty cool, isn’t it?
So now we’ll add some more functionality, like making our program utter some jokes, searching some stuff from Wikipedia, and asking our program the current time and other funny questions. For this you need the following chunk of code:
try: with sr.Microphone() as source: print("Say Something...") voice = r.listen(source) command = r.recognize_google(voice) print("done..") print(command) if command == "tell me a joke": engine.say(pyjokes.get_joke()) engine.runAndWait() if command == "what is your name": engine.say("Oh, did I forget to introduce myself. Well I am Siri.") engine.runAndWait() if command == "what can you do": engine.say("I can tell you a joke, open apps for you, tell you summaries about stuff, tell the time and search google for you.") engine.runAndWait() if "time" in command: time = datetime.datetime.now().strftime("%I:%M %p") engine.say("The time is " + time) engine.runAndWait() print(time) if "who is" in command: person = command.replace("who is", '') info = wikipedia.summary(person, 1) engine.say(info) print(info) engine.runAndWait() if "what was" in command: person = command.replace("what was", '') infor = wikipedia.summary(person, 1) engine.say(infor) print(infor) engine.runAndWait() if "open" in command: app = command.split(" ", 1)[-1] text("opening " + app) os.startfile(app) if "search Google" in command: search = text("What do you want to search") webbrowser.open("https://www.google.com/search?safe=strict&sxsrf=ALeKk00D9uvVFHYRFjpH3KOVhTmAljMZfA%3A1609064981017&ei=FWLoX-JM_4GFsg_SgLfIBQ&q=" + str(search)) except: print("Couldn't hear you...") pass
So there you go, a fully functional speech recognition app using Python. Thank You!
Leave a Reply