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.
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?
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. p>
#! / bin / bash word = "$ 1" export word python /home/vishaag/hacks/bash_scripts/terminal_dictionary.py "word"
To make the bash file globally available, you need to:
“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