Build your own Terminal Dictionary tool just using Python(Beginners)

Rahul Negi
4 min readMay 19, 2021

Introduction

Ever felt annoyed and lazy to google up a word meaning while watching a movie or doing some task especially not on browser. Yeah i feel you but no worries I am here to show you how you can build your own terminal based Google Dictionary just using python. Just hang in there for a bit.

And believe me life has never been this easier before tdict (name of our tool).

Prerequisites

  • Basic knowledge of Python i.e. lists, functions, exceptions etc.

Yes, this is enough.

How it will work

In order to build something we must know how it gonna work. We need an API to fetch data from google. Don’t worry we are not going to make an API now, someone has already done that here thanks to him.

Let me breakdown it in this way

  • Our Python program will be called using a bash(will define this later how) terminal.
  • We will pass the word to our terminal using tdict <word>
  • Then the API we are using in our python program will fetch the data.
  • And our python program will print the data in readable form.

Example :

Linux terminal based google dictionary
Terminal Dictionary

Let’s Code

I am dividing the code in 3 parts for easy readability.

  1. Importing Libraries and initiating our call to API.

Explanation :

  • Imported necessary libraries like os, sys, urllib and json.
  • Providing styling outputs with print statements (i think so)
  • Storing our word in the title variable using os.environ[“word”] in spite of taking normal input something like title = input()(will explain this later)
  • Initiated a variable flag=0 (it will be used later)
  • Using exception HTTPError in case if system is not connected to internet.

Exceptions are used to prevent our program from failing miserably.

2. Making Functions to get the desired response

a. Function to get meaning

get_meaning(title)

Explanation :

In our get_meaning function we are going to extract only the necessary information here(only meaning). The API response will be in JSON format so we have to use list indices to extract data .

For example the JSON response for word hello will be like this :

[
{
"word": "hello",
"phonetics": [
{
"text": "/həˈloʊ/",
"audio": "https://lex-audio.useremarkable.com/mp3/hello_us_1_rr.mp3"
},
{
"text": "/hɛˈloʊ/",
"audio": "https://lex-audio.useremarkable.com/mp3/hello_us_2_rr.mp3"
}
],
"meanings": [
{
"partOfSpeech": "exclamation",
"definitions": [
{
"definition": "Used as a greeting or to begin a phone conversation.",
"example": "hello there, Katie!"
}
]
},
{
"partOfSpeech": "noun",
"definitions": [
{
"definition": "An utterance of “hello”; a greeting.",
"example": "she was getting polite nods and hellos from people",
"synonyms": [
"greeting",
"welcome",
"salutation",
"saluting",
"hailing",
"address",
"hello",
"hallo"
]
}
]
},
{
"partOfSpeech": "intransitive verb",
"definitions": [
{
"definition": "Say or shout “hello”; greet someone.",
"example": "I pressed the phone button and helloed"
}
]
}
]
}
]

Line 16 in get_meaning will do that for us i.e.

print(result[0]["meanings"][0]["definitions"][0]["definition"])

b. Function to get synonyms

Explanation :

Nothing new here we are just using loop here to iterate over 3 synonyms. You can use less but more would be troublesome as some words have not many synonyms it may cause to raise exceptions, so i will suggest you to go for 3 or less.

print(result[0]["meanings"][0]["definitions"][0]["synonyms"][x])

This image is example of the get_example function which i have used in my code, i will suggest you to do this yourself.

Terminal Based Dictionary Google Dictionary
Examples of the word in terminal dictionary

Similarly you can make other functions to get different responses from our JSON response, you can use that audio file too to make you program play the pronunciation it would require some extra libraries and some more line of code .

In case you need reference to build other functions you can refer to my GitHub repository here.

3. Presenting the data

Explanation :

  • We are calling get_meaning function to return the meaning of the word in line 1.
  • now the flag=0 we declared above in code will be used here to iterate over the options until user chose to exit the program.

That’s it! we have build out terminal based Google Dictionary tool!

But in order to call it from any location in spite of going to the location of our python file every time we need a shell script(for linux) to call it from anywhere using terminal. This is the link to script or you can find it my git repository.

At line 2 word=”$1" will look for the first argument we will pass in our terminal after the word tdict <word> that’s the reason we used os.environ[“word”] above to take input directly from terminal.

You need to provide location of your python program at line 4.

Conclusion

This is a small but very helpful tool, i use this all the time and i bet if you keen to learn more words you will use it regularly too!

If you want to add some feature or do find an issue feel free to contribute. Comment your doubts if any.

Github repository : here

Connect with me on LinkedIn : here

Python is a great tool and easy to learn. Follow for more such blogs i will keep posting about tools and techs to make life easier and more.

--

--

Rahul Negi

Passionate Learner | Python | Django | Selenium | ML