Improving your vocabulary using the Raspberry Pi

As an example of self-improvement using the Raspberry Pi, we will implement an example that retrieves the word of the day using the Wordnik API (https://www.wordnik.com/).

Prepare for lift off

We need to install the Wordnik Python client (distributed under the Apache License) available as a Python package:

sudo pip install wordnik

The next step is to obtain an API key to make use of the API. This can be obtained by registering for an account with Wordnik and requesting an API key (http://developer.wordnik.com/).

Once the installation is complete, it is time to review the example.

Note

The Wordnik API is merely being demonstrated as an example. Refer to the API terms of agreement to determine how the API can be used in a commercial application.

Engage thrusters

  1. Let's review an example that fetches the word of the day using the Wordnik API. The first step is to import the Wordnik Python client:
    from wordnik import *
  2. The next step is to create a client that initializes the API to access the word list:
    url='http://api.wordnik.com/v4' 
    key='API Key'
    client=swagger.ApiClient(key,url)
  3. The next step is to initialize the Wordnik API and retrieve the word of the day:
    words = WordsApi.WordsApi(client)
    example = words.getWordOfTheDay()
  4. The returned object is a WordOfTheDay object (https://github.com/wordnik/wordnik-python/blob/master/wordnik/models/WordOfTheDay.py). Let's print the word, definition, and the published date:
    string = 'The Word of the Day is ' + example.word +'.'
    print string
    string = 'Definition: '+example.definitions[0].text
    print string
    string = 'Date = '+example.publishDate.strftime("%D")
    print string
  5. The output of this Python script is something like the one shown here:
    The Word of the Day is asportation.
    Definition: The felonious removal of goods from the place where they were deposited.
    Date = 01/19/15
    
  6. Now, this word of the day can be displayed on a screen at some prominent location using a GUI tool such as Tkinter (http://zetcode.com/gui/tkinter/) and updated every 24 hours. Refer to this book's website for some ideas.

Objective complete – mini debriefing

In this task, we reviewed an example of how to use your Raspberry Pi for self-improvement; similarly, there are APIs from other sources that provide a quote of the day, joke of the day, and so on.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.181.87