It is possible to improve your vocabulary using Python! Imagine setting up a large display that is installed somewhere prominently and updated on a daily basis. We will be making use of the wordnik API (sign up for an API key at https://www.wordnik.com/signup):
- The first step is to install the wordnik API client for python3:
git clone https://github.com/wordnik/wordnik-python3.git
cd wordnik-python3/
sudo python3 setup.py install
There are restrictions on the wordnik API usage. Refer to the API documentation for more details.
- Let's review writing our first example using the wordnik Python client. In order to fetch the word of the day, we need to initialize the WordsApi class. According to the API documentation, this could be done as follows:
# sign up for an API key
API_KEY = 'API_KEY'
apiUrl = 'http://api.wordnik.com/v4'
client = swagger.ApiClient(API_KEY, apiUrl)
wordsApi = WordsApi.WordsApi(client)
- Now that the WordsApi class is initialized, let's go ahead and fetch the word of the day:
example = wordsApi.getWordOfTheDay()
- This returns a WordOfTheDay object. According to the wordnik Python client documentation, this object consists of different parameters including the word, its synonym, source, usage, and so on. The word of the day and its synonym could be printed as follows:
print("The word of the day is %s" % example.word)
print("The definition is %s" %example.definitions[0].text)
- Putting it all together, we have this:
#!/usr/bin/python3
from wordnik import *
# sign up for an API key
API_KEY = 'API_KEY'
apiUrl = 'http://api.wordnik.com/v4'
if __name__ == "__main__":
client = swagger.ApiClient(API_KEY, apiUrl)
wordsApi = WordsApi.WordsApi(client)
example = wordsApi.getWordOfTheDay()
print("The word of the day is %s" % example.word)
print("The definition is %s" %example.definitions[0].text)
The preceding code snippet is available for download along with this chapter as wordOfTheDay.py. Sign up for an API key, and you should be able to retrieve the word of the day:
The word of the day is transpare
The definition is To be, or cause to be, transparent; to appear,
or cause to appear, or be seen, through something.