Accessing word definitions via a web server
Several dictionaries on the Web offer an API to interact with their website via scripts. This recipe demonstrates how to use a popular one.
Getting ready
We are going to use curl
, sed
, and grep
for this define utility. There are a lot of dictionary websites where you can register and use their APIs for personal use for free. In this example, we are using Merriam-Webster's dictionary API. Perform the following steps:
- Go to http://www.dictionaryapi.com/register/index.htm, and register an account for yourself. Select
Collegiate Dictionary
andLearner's Dictionary
: - Log in using the newly created account and go to
My Keys
to access the keys. Note the key for the learner's dictionary.
How to do it...
This script will display a word definition:
#!/bin/bash #Filename: define.sh #Desc: A script to fetch definitions from dictionaryapi.com key=YOUR_API_KEY_HERE if [ $# -ne 2 ]; then echo -e "Usage: $0 WORD NUMBER" exit -1; fi curl --silent \ http...