a woman reading in bed

Product of a RESTful night

reading in bed

For this week’s homework in Understanding Networks, our assignment was as follows:

REST assignment. Communication in not-so-realtime. Deliver information to the user through the browser from somewhere else. Update the information they’re seeing while they interact, preferably based on input from multiple sources.

As I mentioned last week, I wanted to work with the newly released Wordnik API. I thought it would be fun to have a Web page that would let you RESTfully look up a word, for which it would return usage examples from Wordnik. And each word in the examples would be linked to the example set for itself, so you could click through endlessly.

And then I thought, given my habit of biting off more than I can chew, homeworkwise, that it would be a better plan to just do the minimum amount of programming required for the assignment, and then, if I had time left over after debugging, I could fancy it up. After I had lost (a) an hour’s worth of work to having the folly to trust BBEdit’s clipboard history; (b) at least two more hours to trying to route around what I thought was an authentication issue but that was, in fact, just me-not-understanding-the-example-code issue; and (c) maybe another hour trying to fix a genuine permissions problem, which I gave up on but will have to solve later, finishing even the most basic version of this toy seemed unlikely. I like PHP, though, and I find the documentation reasonably accessible, so I don’t get as discouraged as easily in it as I do with Java. So after a million or so edit-save-refresh cycles, I finally came up with this silly thing: Wordhopper.

Wordhopper - ITPindia
http://itp.nyu.edu/~ia303/networks/wordhopper/

So, if you go to the base URL, you’ll see the current word of the day from Wordnik, followed by a definition and a set of usage examples.

If you tack a word onto the end of that URL, you get the word and just its usage examples.

Wordhopper - ITPindia
http://itp.nyu.edu/~ia303/networks/wordhopper/gastropod

And, finally, if you add a /d to the end of the URL, after your personally chosen word, you get all the available definitions for that word.

Wordhopper - ITPindia
http://itp.nyu.edu/~ia303/networks/wordhopper/monster/d

Ta-da.

The PHP looks like this:

[sourcecode language=”PHP”]




Wordhopper – ITPindia

Wordhopper

4) {
$word = $tokens[4];
if ($word != “”) {
echo “

Your Word

“;
echo “

” . $word . “

“;
}
}

// If no word was inserted, look up the word of the day.
if ($word == “” ) {
$wotd_request_url = “http://api.wordnik.com/api/wordoftheday.xml?api_key=” . $api_key;
$wotdfile = file_get_contents($wotd_request_url);
$wotdxmldata = simplexml_load_string($wotdfile);
$word = $wotdxmldata->word;
$wotddef = $wotdxmldata->definition->text;
echo “

Wordnik.com Word of the Day

“;
echo “

” . $word . “

“;
echo ‘

Definition

‘;
echo ‘

‘ . $wotddef . ‘

‘;
}

// if you got five tokens, you’ve received some other command.
if (count($tokens) > 5) {
$details = $tokens[5];

// Does token 5 contain the letter d?
$pos = strpos($details, “d”);
if ($pos === false) {
// It wasn’t a d, so we’ll ignore it for now. Later we may add some other options.
} else {
// d stands for definition, so let’s look that up.
$def_request_url = ‘http://api.wordnik.com/api/word.xml/’ . $word . ‘/definitions?api_key=’ . $api_key;
$deffile = file_get_contents($def_request_url);
$defxmldata = simplexml_load_string($deffile);
echo ‘

Definitions

‘;

// Display definitions for the word.
foreach ($defxmldata->definition as $definition) {
$headword = $definition->headword;
$partOfSpeech = $definition->partOfSpeech;
$defTxtSummary = $definition->defTxtSummary;
$defTxtExtended = $definition->defTxtExtended;
if ($headword != $word) {
echo ‘

‘ . $headword . ‘

‘;
}
echo ‘

‘ . $partOfSpeech . ‘ ‘ . $defTxtSummary . ‘
‘ . $defTxtExtended . ‘

‘;
}
}
}

// Build a request to look up a word’s examples.
$request_url = $baseurl . $word . “/examples?api_key=” . $api_key;

$file = file_get_contents($request_url);
$xmldata = simplexml_load_string($file);

echo ‘

Usage Examples

‘;

// Display usage examples for the word.
foreach ($xmldata->example as $example) {
$quote = $example->display;
$source = $example->title;
$url = $example->url;
$year = $example->year;
echo ‘

‘ . $quote . ‘

‘;
echo ‘

‘ . $source . ‘, ‘ . $year . ‘

‘;
}

?>



[/sourcecode]

So, I learned a lot from doing this, which was the goal, and I feel more optimistic about finding some way to tie Wordnik data into the Bookalator, as the semester rolls on.

Photo: 61/365: I try to read before sleeping every night. by Betsssssy / Betsy Fletcher; some rights reserved.

Leave a Reply

Your email address will not be published. Required fields are marked *