AI Quote Generator with Python
In this article, we will learn how to build an artificial intelligence program that shares specific quotes with us. The program has different layers: speech recognition, generating quote, and converting text to speech. In my previous articles, I’ve covered Speech Recognition and Text-to-Speech topics.
When I decided to write an article about quote generation, I said why not spice things up a little by adding artificial intelligence. And it ended up being a much interesting project. I have couple more ideas around this topic, hoping to write about them in a future time.
Without losing anytime, let’s get started.
Table of Contents
- Getting Started
- Step 1 — Libraries
- Step 2 — Recognize Speech
- Step 3 — Generate A Quote
- Final Step — AI Reading the Quote
- Video Demonstration
Let me start by listing the ingredients of this project. All we need is three python libraries, which are: quote, pyttsx3 and speech recognition.
Quote is a python wrapper for the Goodreads quote API.
Pyttsx3 is a text-to-speech python library. One great advantage of using this module is that it can work offline without the need of internet connection.
Speech Recognition is a speech-to-text python library. It supports multiple speech recognition engines such as;
- Microsoft Bing Voice Recognition API
- IBM Speech-to-Text API
- Google Cloud Speech API
- And more.
Now we have an idea of the packages that we need. To be able to use them, we have to install them using pip. PIP is a python library manager tool. Helps to organize libraries and packages. We will run the following line in our terminal window to get the installation started:
pip install quote pyttsx3 speechrecognition
Step 1 — Libraries
Now that we have the libraries installed. We can go ahead and import them into our program. Feel free to use any kind of text editor.
from quote import quote
import speech_recognition as sr
I’ve shared the documentations of each package in the Getting Started section. You can check them if you want to learn more about how they work.
Step 2 — Recognize Speech
In this step, we will learn how to recognize speech using a python. The recognition is happening directly through the microphone. It listens until you stop talking, then converts it to text. I picked Google’s recognizer engine, and it doesn’t need an API key for basic use. But if you want to go with a different recognizer engine, feel free to do that.
The dynamic_energy_threshold is where we define that the microphone should stop listening when it is silent. I chose to go with this setting because the user might go with a long speech, I didn’t want to limit by a set duration.
The speech will be something like this “Hey AI, tell me a quote about rainbow.” The keyword has to be at the end of the speech. What we will do is split the recognized words into a list and get the last item as our keyword.
Here is the code:
r = sr.Recognizer()
r.dynamic_energy_threshold = False
with sr.Microphone() as source:
print("Say a keyword: ")
audio_text = r.listen(source)
print("Time is over.")
s = r.recognize_google(audio_text)
words = s.split()
keyword = str(words[-1])
Step 3 — Generate A Quote
keyword = str(keyword)
result = quote(keyword, limit=3)
Nothing complicated going on here.
We are just converting the keyword into a string. And then passing it into the quote function, which is the package that we installed and imported earlier.
It has two parameters, first one is the keyword, and the other one is the quantity of the quotes that we want to get. Quote function will connect us to Goodreads’ quote API.
r = (random.randint(0, 2))
quote_ = result[r]['quote']
author_ = result[r]['author']
In the code above, we are assigning the values of the result into two different variables. The returned result is a dictionary, and it is storing 3 different quotes right now. I preferred to do more than one, this way when we use the same keyword, we can get a different quote instead of the same one.
After getting the quote and author values from it, we are going to put in a sentence. It has to be in a well written sentence, because in the next step, we are going to ask the machine to read it for us.
Here is the sentence format that I wrote:
text = ("Quote of the day is about " + keyword + " by " + author_ + ". \n\n " + quote_)
Here are some quote examples when we run the program:
Final Step — AI Reading the Quote
In this final step, we are going to define our text-to-speech engine. We are using pyttsx3 library. This library has many different voice engines available for different languages. If you want to play around with different voice engines, check the article I shared in the introduction.
Here is the code:
engine = pyttsx3.init()
en_voice_id = "com.apple.speech.synthesis.voice.Alex"
It’s time to see the code in action.
Save the text file as a python file. I named mine as: listen_to_quote.py
Now, let’s open up the terminal window that we used to install the libraries. Then we’ll go to the folder that we saved the python code. And then we will run it.
Congrats! In this hands-on tutorial, we learned how to build a program Congrats! In this hands-on tutorial, we learned how to build a program that reads us quotes related to a specific keyword. We also had some experience with speech to text and text to speech libraries. I enjoy creating and working on these projects because they are great applications of machine learning and artificial intelligence in our daily lives. Hoping that you enjoyed reading this article and learned something new today.
I am Behic Guven, and I love sharing stories on programming, education, and life. Subscribe to my content to stay inspired. Ty,
If you are wondering what kind of articles I write, here are some: