How to hijack the Un*x utility fortune for Flash Cards on the Mac

The Un*x utility fortune is a great source of amusement, providing lots of aphorisms from a wide variety of topic domains. Here we hijack it to use as a system of Flash Cards for Chinese vocabulary.
Table of Contents

Get some tools:

From an administrative account:

  • Install Homebrew

http://brew.sh

Homebrew is a great way to get a variety of Un*x utilities not included with MacOSX

  • once homebrew in installed
    • in Terminal:

brew install fortune

to install the fortune-cookie utility which is the basis for the flash card implementation.

  • Install TextWrangler

http://www.barebones.com/products/textwrangler/download.html

TextWrangler is a very powerful text editor, used here because it has a great search and replace capability which optionally can use regular expressions.

build the input file for fortune to use:

  • I used google translate, one english word per line, translated into Chinese(Simplified) to get a list of words and their Chinese representations
  • copied the list of English words and pasted them into a column in a spreadsheet
  • copied the chinese translations into an adjacent column of the spreadsheet
  • copied the two columns block into TextWrangler for further processing into an input file for fortune
    • did grep search/replace
      search for ^
      replace with %\r
      which delimits each entry with a % sign on a line by itself, then saved the result as chinese.txt
  • in Terminal, cd to the directory containing chinese.txt and use the utility strfile to create the index file fortune needs:

strfile chinese.txt

which will produce the file chinese.txt.dat which fortune uses to snag a random entry from the file chinese.txt and out put it.

Check it out

  • first, cd to the directory containing the files

fortune chinese.txt

Enjoy!

Variations

use pipes to format differently:

e.g.: use tr to translate the tab characters into an otherwise unused character for replacement using a generic sed implementation: fortune chinese.txt | tr '\t' '_' | sed -e 's/_/________/' | tr '_' '\n'

format the input file differently

Since strfile uses % on a line by itself to delimit entries, you can build more interesting flash card formatting into the input file itself (blank lines around the chinese and english representations, etc.). Consider using pipes to explore options for such formatting, then implement in the source file when you find one you like.

-- DickFurnas - 2016-05-18

Topic revision: r1 - 2016-05-18 - 20:23:56 - DickFurnas
 
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.