Created and recorded by Yiming Cai in December 2021.
Music: "Call of the Void" by Justin Miles, https://lmms.io/lsp/?action=show&file... License: Creative Commons (by)
Script:
Last video, we talked about how to make a program that could listen to audio commands, and convert audio to text. Today, I will talk about how to write simple script to let program understand certain commands, and convert text response to audio
Let's talk about examples of how to process voice commands.
The first example is printing out the date. To find out if the user wants to know the date, we can give a specific command like “what is the date today” or choose to search if there is both “what” and “date” in the transcript.
Code
After setting up the if statements, we need to teach our program how to respond. It's easy to get the date from the system. After we get the string of the date, just print it out.
The second example is to search certain keywords. Let's set our commands to “google something”
Then, the next step is to give an if statement so the program will enter the process. I will set the if statement as:
if “google” in transcript:
The next thing we want is to get the keyword from the input command. Based on the command format, we can get the string after the word “google” from the input command.
Now the keyword is ready, next step will be to open google and search
Code
With the window open, we can add the response message:
“Here is what I found.”
Now we know how to process the input command. Then, we can start to respond with audio.
The library I will use here is gtts. The first step here is still installation.
pip3 install gTTS
This library can convert text to mp3 files, or mp3 file objects which can be played directly without saving files. Note that this also requires internet connection.
What we gonna do to will be put our response message into this library, and then, play the mp3 object as voice output in each process.
In this video, we talked about how to extract keywords from the input commands and respond. Also, how to convert text response to audio. Hope this helps.