Python Project – Speak the Meaning of the Word

Python Speak the Meaning of the Word is a project that uses the user’s input to determine the meaning of a word. The output is then spoken out loud as the meaning of the input is displayed on the screen.

About Python Speak the Meaning of the Word

This project will be implemented in Python using the modules Tkinter, pyttsx3, and requests. Tkinter is used to create the GUI that accepts input and displays output. Other modules we use include pyttsx3, which is a Python text-to-speech library, and requests to fetch the meaning of words from the Dictionary API.

Prerequisites for Speak the Meaning of the Word Using Python

  • Basic knowledge of the Python programming language and how functions are defined in it
  • How to make a request to an API
  • How to work with the pyttsx3 library

Download Python Speak the Meaning of the Word Project

Please download the source code of Python Speak the Meaning of the Word Project from the following link: Python Speak the Meaning of the Word Project Code

Steps to Create Speak the Meaning of the Word using Python

Following are the steps for developing the Speak the Meaning of the Word:

Step 1: Importing the necessary modules

Step 2: Making a window for our project

Step 3: Functions

Step 4: Making Labels and Mapping the Buttons to Their Functionalities

Step 1: Importing the necessary modules

To use Tkinter, we need to import the Tkinter module. We are also going to import the pyttsx3 module to speak the meaning of the input word and the requests module to send a get request to the Dictionary API.

Code

# Importing modules
from tkinter import *
import pyttsx3
import requests

Step 2: Making a window for our project

This code sets the title of the window as ‘TechVidvan’, and sets the dimensions ‘width x length’.

Code

dicti = Tk()
dicti.title("TechVidvan Dictionary")
dicti.geometry('550x500')

Step 3: Functions

These functions give the meaning of the word as an output and speak it out loud.

Code

def speaking(audio):
   # initial constructor of pyttsx3 and having the sapi5 in it as a parameter
   engine = pyttsx3.init('sapi5')
   # Making it speak
   engine.say(audio)
   engine.runAndWait()




def word_meaning():
   input_word = e1.get()
   req = requests.get(f"https://api.dictionaryapi.dev/api/v2/entries/en/{input_word}")
   # If the response was successful, get the meaning of the word
   if req.ok:
       # Convert the response to JSON
       data = req.json()
       # Get the first definition of the word
       definition = data[0]["meanings"][0]["definitions"][0]["definition"]
       spokenText.set(definition)
       speaking("the meaning  is" + str(definition))
   # Otherwise, display an error message
   else:
       spokenText.set("Word not found")

Explanation:

We use engine.say() to get the audio output and requests.get send a get request to the Dictionary API to get the meaning of a word.

Step 4: Making Labels and Mapping the Buttons to Their Functionalities

Now we add the required widgets for our GUI. We make a label to give the heading “ Speak the meaning”, and a label to display meaning as an output from the user by creating an entry widget to get input words, and after that, we create a button widget and assign the word_meaning() function to it.

Code

spokenText = StringVar(dicti)
Label(dicti, text='Speak the Meaning', font="45", fg='blue').pack(pady=10)
# Getting the input of word from the user
Label(dicti, text='Enter the word:', font="15").pack(pady=10)


e1 = Entry(dicti, width=25)
e1.pack(pady=7)
# Label to show the correct word
Label(dicti, text="Meaning:", font="15").pack(pady=10)
Label(dicti, textvariable=spokenText, width=80, height=15, font=('Arial 10')).pack(pady=10)
spokenText.set("Type the word for which you want to find the meaning")
speaking("Type the word for which you want to find the meaning")


Button(dicti, text="Speak Meaning", font="bold", command=word_meaning).pack(pady=10)
# to keep running the window
dicti.mainloop()

Full Code

# Importing modules
from tkinter import *
import pyttsx3
import requests


dicti = Tk()
dicti.title("TechVidvan Dictionary")
dicti.geometry('550x500')




def speaking(audio):
   # initial constructor of pyttsx3 and having the sapi5 in it as a parameter
   engine = pyttsx3.init('sapi5')
   # Making it speak
   engine.say(audio)
   engine.runAndWait()




def word_meaning():
   input_word = e1.get()
   req = requests.get(f"https://api.dictionaryapi.dev/api/v2/entries/en/{input_word}")
   # If the response was successful, get the meaning of the word
   if req.ok:
       # Convert the response to JSON
       data = req.json()
       # Get the first definition of the word
       definition = data[0]["meanings"][0]["definitions"][0]["definition"]
       spokenText.set(definition)
       speaking("the meaning  is" + str(definition))
   # Otherwise, display an error message
   else:
       spokenText.set("Word not found")




spokenText = StringVar(dicti)
Label(dicti, text='Speak the Meaning', font="45", fg='blue').pack(pady=10)
# Getting the input of word from the user
Label(dicti, text='Enter the word:', font="15").pack(pady=10)


e1 = Entry(dicti, width=25)
e1.pack(pady=7)
# Label to show the correct word
Label(dicti, text="Meaning:", font="15").pack(pady=10)
Label(dicti, textvariable=spokenText, width=80, height=15, font=('Arial 10')).pack(pady=10)
spokenText.set("Type the word for which you want to find the meaning")
speaking("Type the word for which you want to find the meaning")


Button(dicti, text="Speak Meaning", font="bold", command=word_meaning).pack(pady=10)
# to keep running the window
dicti.mainloop()

 

Speak the Meaning of the Word Output

python speak the meaning of the word output

speak the meaning of the word output

Summary

Using the Tkinter and pyttsx3 Python libraries, we have completed the project that gives the meaning of the input word and provides its meaning as an audio output. You can also try to modify it to listen to your audio sources. We hope you enjoyed developing with us! Have fun coding!