Building a terminal based online dictionary with Python and bash
Last Updated :
22 May, 2024

I was watching a movie yesterday and I didn’t understand some of the words used. So what I did was, each time I didn’t understand any word, I’d fire up my browser and type in ‘define [word]’ and google would turn me up with the meaning of the word. But it’s annoying to open the browser every time (Blame me for that ;P).
What do we geeks users love to operate on our linux systems? Yes. The Terminal!
Since opening a terminal is as easy as Ctrl+Alt+T, I thought it would help if there was an application to use a dictionary in there. Hence, the motivation to build this!
So what we need now is, the database of words for the dictionary (duh!).There is a free and an easily usable online dictionary called Glosbe. It offered a pretty neat API, though in beta stage. It gives the output in a format called JSON, which is pretty standard for all API's.
In order to use this API, it is required to give a query (HTTP GET request) containing the word to Glosbe and it would return the meaning (+other items like phrases, etc.) of the word.
For example, to search for the word ‘hello’, we would need to use:
http://glosbe.com/gapi/translate?from=eng&dest=eng&format=json&phrase=hello&pretty=true
It’s pretty obvious to see that the word is ‘hello’ [phrase=hello]. Glosbe can offer to translate from one language to other as well. But right now, we need only an English dictionary. So set the from-dest both to English. And of course, the output format here is JSON (can be changed to XML as well). Visit the above link to see the output format in JSON.
Now, the rest of the task is to write a script which would replace the word ‘hello’ (here) with the word the user will want to enter, receive the output, parse it for the meaning alone and display.
Python enters! We need to write python scripts to parse the returned JSON object.
Python
import urllib #library for fetching internet resources
import json #library for json operations
#import os
#title=os.environ["word"]
title = input("Enter word to search: ") #Input word to search dictionary
print ("Word: ",title )
#stores the json formatted output to a variable
url = 'http://glosbe.com/gapi/translate?from=eng&dest=eng&\
format=json&phrase='+title+'&pretty=true'
#json representation of url is stored in variable result
result = json.load(urllib.urlopen(url))
#get the first text in "meaning" in "tuc" from result
print ("Meaning: ", result["tuc"][0]["meanings"][0]["text"])
Input
Geek
Output:
Enter word to search: Word: geek
Meaning: expert in a technical field, particularly to do with computers
Surprised? Just a mere 9 lines of code will accomplish this task! Run this and see if you have a python compiler installed.
How it works?
- Here, the variable ‘url’ stores the JSON formatted output from Glosbe.
- load will take a python object and dump it to a string [stored in the variable ‘result]’ which is a JSON representation of that object.
- Finally the JSON is parsed for the meaning alone using ‘result[“tuc”][0][“meanings”][0][“text”]’ and printed.
So now it works when this script is executed. In order to do this, I have to navigate to the directory where it’s stored and then run it. Again, we can simplify this by writing a shell script which will invoke the python script. The point is, it will be accessible from anywhere irrespective of the directory in which the terminal is.
So a bash script is written in order to call the python code.
#!/bin/bash
word="$1"
export word
python /home/vishaag/hacks/bash_scripts/terminal_dictionary.py "word"
To make a bash file globally accessible, it is required to,
- Add the directory you wish for Linux to search, which is also where your script is located.
- Add the directory in the file .bashrc (which is located in the home folder and hidden; press Ctrl+H to see hidden files). For example, I had to add the directory/home/vishaag/hacks/bash_scripts at the top of the .bashrc file (using a text editor like gedit/kate etc. ).
- After this, linux can access your bash script from this folder.
The ‘word=”$1? and export word’ in the script is to take the arguments from the bash to the python script.
$1 denotes the first argument ($2 the second and so on. Write $@ for n number of arguments). For example, when you write,
$dict hello
(dict is the name of the bash script) hello is stored in $1 (and copied to ‘word‘ here)
Then this is exported to the python script.
Note: Remove both the comments on the above python code and remove/comment ‘title = raw_input(“Enter word to search: “)’ in order to use the arguments from bash.
import os
title=os.environ["word"]
#title = input("Enter word to search: ")
And its done!!
Now all you have to do is save the shell script and run it like you run any application on your terminal.
Here’s another sample output

Cheers! References:
Similar Reads
Check If Dictionary Value Contains Certain String with Python
We need to check if the value associated with a key in a dictionary contains a specific substring. For example, if we have a dictionary of user profiles and we want to check if any userâs description contains a particular word, we can do this easily using various methods. Letâs look at a few ways to
4 min read
Dictionary with Tuple as Key in Python
Dictionaries allow a wide range of key types, including tuples. Tuples, being immutable, are suitable for use as dictionary keys when storing compound data. For example, we may want to map coordinates (x, y) to a specific value or track unique combinations of values. Let's explores multiple ways to
4 min read
Write a dictionary to a file in Python
A dictionary store data using key-value pairs. Our task is that we need to save a dictionary to a file so that we can use it later, even after the program is closed. However, a dictionary cannot be directly written to a file. It must first be changed into a format that a file can store and read late
3 min read
Python - Keys associated with value list in dictionary
Sometimes, while working with Python dictionaries, we can have a problem finding the key of a particular value in the value list. This problem is quite common and can have applications in many domains. Let us discuss certain ways in which we can Get Keys associated with Values in the Dictionary in P
4 min read
Python | Pretty Print a dictionary with dictionary value
This article provides a quick way to pretty How to Print Dictionary in Python that has a dictionary as values. This is required many times nowadays with the advent of NoSQL databases. Let's code a way to perform this particular task in Python. Example Input:{'gfg': {'remark': 'good', 'rate': 5}, 'c
7 min read
Print anagrams together in Python using List and Dictionary
An anagram is a word or phrase formed by rearranging the letters of another word or phrase, using all the original letters exactly once. The task of grouping anagrams together in Python can be efficiently solved using lists and dictionaries. The key idea is to process each word by sorting its charac
2 min read
Building Powerful Telegram Bots with Telethon in Python
Creating a Telegram bot can significantly enhance how you interact with users, automate tasks, and provide information quickly. Here, we'll use the Telethon library in Python to build a powerful Telegram bot. Creating Telegram Bot in PythonPrerequisites Telegram Account: We need a Telegram account t
4 min read
Map function and Dictionary in Python to sum ASCII values
We are given a sentence in the English language(which can also contain digits), and we need to compute and print the sum of ASCII values of the characters of each word in that sentence. Examples: Input : GeeksforGeeks, a computer science portal for geeksOutput : Sentence representation as sum of ASC
2 min read
How to use a List as a key of a Dictionary in Python 3?
In Python, we use dictionaries to check if an item is present or not . Dictionaries use key:value pair to search if a key is present or not and if the key is present what is its value . We can use integer, string, tuples as dictionary keys but cannot use list as a key of it . The reason is explained
3 min read
Building A Weather CLI Using Python
Trying to know the weather is something that we all do, every day. But have you ever dreamed of making a tool on your own that can do the same? If yes, then this article is for you. In this article, we will look at a step-by-step guide on how to build a Weather CLI using OpenWeather API. What is Ope
4 min read