Change language

Building a terminal online dictionary with Python and bash

| |

Yesterday I watched a movie and I didn’t understand some of the words used. So every time I didn’t understand a single word, I would launch my browser and type in the word "define [word]" and Google inferred me from the meaning of the word. But it’s annoying to open the browser every time (blame me for that; P).

What do we love users love about our linux systems? Yes. Terminal!

Since opening a terminal is as easy as Ctrl + Alt + T, I thought it would be helpful if there was an app to use a dictionary. Hence the motivation to build this!

So what we need now is a word base for the dictionary (yes!). There is a free and easily used online dictionary called Glosbe . It was suggested quite neatly   API , albeit at the beta stage. This produces output in a format called JSON, which is pretty standard across all APIs.

To use this API, you must pass a request (HTTP GET request) containing the word, in Glosbe and it will return the value (+ other elements like phrases, etc.) Words.

For example, to find the word "hello", we you need to use:

Obviously, the word "hello" [phrase = hello]. Glosbe can offer translation from one language to another. But now we only need an English dictionary. So set from-dest to English. And, of course, the output format here — JSON (can also be changed to XML). Follow the link above to see the output format in JSON format.

Now the rest of the task is to write a script that replaces the word "hello" (here) word the user wants to enter, get the output, parse it for value only, and display it.

Python enters! We need to write Python scripts to parse the returned JSON object.

import urllib  # library for downloading Internet resources

import json  # library for json operations

# import os
# Title = os.environ [& quot; word & quot;]

title = raw_input ( "Enter word to search:" ) # Enter a word in the search dictionary

print " Word: " , title

# saves json output to a variable

url = ’http: / / / gapi / translate? from = eng & amp; dest = eng & amp; 

format = json & amp; phrase = ’+ title +’ & amp; pretty = true’

# json url representation is stored in result variable

result = json.load (urllib.urlopen (url)) 

# get the first text in "value" in "tuc" from the result

print "Meaning:" , result [ "tuc" ] [ 0 ] [ "meanings" ] [ 0 ] [ "text" ]



Exit :

 Enter a search word: Word: geek Meaning: An expert in the technical field, especially with regard to computers 

Surprised? Just 9 lines of code will do the job! Run this and see if you have the Python compiler installed.

How does it work?

  • Here variable & # 39; url & # 39; stores JSON output from Glosbe.
  • load will take a python object and load it into a string [stored in the variable & # 39; result] & # 39; which is the JSON representation of this object.
  • Finally, JSON is parsed for value only using & # 39; result [tuc] [0] [values] [0] [text] and printed.

So now this works when this script is executed. To do this, I need to go to the directory where it is stored and then start it. Again, we can simplify this by writing a shell script that will invoke the python script. The point is that it will be accessible from anywhere, regardless of the directory the terminal is in.

Thus, the bash script is written to invoke Python code.

 #! / bin / bash word = "$ 1" export word python /home/vishaag/hacks/bash_scripts/ "word" 

To make the bash file globally available, you need to:

  • Add the directory you want to find for Linux where your script is also located.
  • Add a directory to your .bashrc file (which is in your home folder and hidden; press Ctrl + H to see hidden files). For example, I had to add the / home / vishaag / hacks / bash_scripts directory at the top of the .bashrc file (using a text editor like gedit / kate, etc.)
  • Then linux can access your bash script from that folder.

"Word =" $ 1? and export the word & # 39; in the script — this is to take arguments from bash into a python script.

$ 1 stands for the first argument ($ 2 — second, etc. Write $ @ for n numbers arguments). For example, when you write

  $ dict hello  

(d5bd50d — this is the name of the bash script) hello is stored in $ 1 (and copied to " word " here)

This is then exported to a Python script.

Note. Remove both comments in the above Python code and remove / comment & # 39; title = raw_input ("Enter search word:") "to use arguments from bash.

 import os title = os.environ ["word"] #title = raw_input ("Enter word to search:") 

And it’s done !!

Now all you have to do is save the shell script and run it as if you were running any application on your terminal.

Here’s another example output




This article courtesy of Vishaag Suriya Narayanan . If you like Python.Engineering and would like to contribute, you can also write an article and mail it to [email protected] See your article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or would like to share more information on the topic under discussion


Learn programming in R: courses


Best Python online courses for 2022


Best laptop for Fortnite


Best laptop for Excel


Best laptop for Solidworks


Best laptop for Roblox


Best computer for crypto mining


Best laptop for Sims 4


Latest questions


Common xlabel/ylabel for matplotlib subplots

12 answers


How to specify multiple return types using type-hints

12 answers


Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers


Flake8: Ignore specific warning for entire file

12 answers


glob exclude pattern

12 answers


How to avoid HTTP error 429 (Too Many Requests) python

12 answers


Python CSV error: line contains NULL byte

12 answers


csv.Error: iterator should return strings, not bytes

12 answers



Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python


How to specify multiple return types using type-hints


Printing words vertically in Python


Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries


Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically