Code Like Zell

Broaden your vocabulary with Genius, Ruby, and Dictionary.com

Genius is a flash card program for Mac OS X that I came across on Lifehacker a few days ago. Using it is simple: you create a flash card set, Genius determines how often to display each card based on how well you have answered it in the past. The thing is, I don’t want to create the flashcard set, and you probably don’t either; that is the reason we never used flashcards in the first place. But I do want to expand my vocabulary, and Genius is certainly a tool that can help with that. So if you are also interested in a shiny new vocabulary, here’s what we can do: automate the flash card creation process.

The way we will do this is by creating a Ruby script that will parse Dictionary.com’s “Word of the Day” rss feed, then use AppleScript to add the word/definition pair to Genius. Finally, we will use crontab to automate the entire process. If all this sounds complicated, don’t worry, it is 90% copy-pasting.

Step 1: Create necessary directories
I am placing the Ruby and AppleScript files in ~/scripts, and the Genius flash card file in ~/flashcards, but feel free to use directories of your choice. From terminal:

mkdir ~/scripts
mkdir ~/flashcards

Step 2: Create empty .genius file
Open Genius and save a new (empty) file as ‘dictionary_wotd’ in the ~/flashcards directory. Close Genius.

Step3: Create a Ruby file to parse Dictionary.com’s word of the day rss feed
From terminal, create the file with:

touch ~/scripts/parse_wotd.rb; chmod 664 ~/scripts/parse_wotd.rb

Next, open ~/scripts/parse_wotd.rb in an editor and paste in this chunk of code:

require 'rubygems'
require 'hpricot'
require 'open-uri'

xml = Hpricot(open("http://dictionary.reference.com/wordoftheday/wotd.rss"))
desc = (xml/"item"/"description").inner_html

desc =~ /^([-\w]+):\s+(.+)/
word = $1
definition = $2.gsub(/^(\w)/) {|x| x.upcase}

system "osascript /Users/HOMEDIR/scripts/create_fcard.scpt " + word + " '" + definition + "'"

Be sure to replace HOMEDIR (line 12) with your own directory name (e.g. your username).
Save and quit. Also, make sure you have the Hpricot gem installed.

Step4: Create AppleScript file to add the word/definition pair to Genius

open Applications > AppleScript > Script Editor
then File > New

Paste in this chunk of code:

on run argv
tell application "Finder"
activate
open document file "dictionary_wotd.genius" of folder "flashcards" of folder "HOMEDIR" of folder "Users" of startup disk
end tell

tell application "Genius"
activate
end tell

tell application "System Events"
keystroke "N" using command down
keystroke item 1 of argv
keystroke "\t"
keystroke item 2 of argv
keystroke "s" using command down
delay .2
keystroke "w" using command down
end tell
end run

Again, be sure to replace HOMEDIR (line 4) with your own directory name. Save this file as ‘create_fcard’ in the ~/scripts directory. Close Script Editor.

We should test what we have so far, open terminal and:

cd ~/scripts
ruby parse_wotd.rb

Now open ~/flashcards/dictionary_wotd.genius and make sure the new card exists, if it does not, double check your paths in the .rb and .scpt files.

Finally, automate with crontab
From terminal:

crontab -e

This will open vi, add the following line to run the Ruby script every morning at 2:30 am:

30 2 * * * /PATH/TO/RUBY /Users/HOMEDIR/scripts/parse_wotd.rb

You can find your path to ruby by $which ruby. Also, I put a 30 second tutorial on vi at the end of this post for those that don’t know how to add the above line.

And that’s it! Every morning a new word will be added to your Dictionary.com flash card set — now it’s just a matter of actually studying them.

vi in 30 seconds:
to launch:
$vi FILENAME
x to delete
i to insert
esc to return to normal mode
:q! quits discarding changes
:wq quits saving changes

Thanks for reading, now find something to do at  GoLark.com


No Comments Yet


There are no comments yet. You could be the first!

Leave a Comment

Optimizing Ruby Time Creation with Time.local How to modify the GNU linker’s default search path