Bash scripts for vocabulary building (with images and pictures)

855 views
Skip to first unread message

Gwern Branwen

unread,
Feb 6, 2011, 11:59:32 PM2/6/11
to Mnemosyne mailing list
Thought people here might find this interesting; recently, I've been
revising hundreds/thousands of my vocabulary flashcards to include
audio pronunciation and also images (because I've found they help an
awful lot with remembering the card and also, obviously, being able to
pronounce words correctly). I started off doing it by hand but
realized that that was unsustainable in the long-term and I needed to
script various things.

Here's the first function, 'vocab'. You call it like '$ vocab
discursion' to use Wiktionary's current word. It opens up the
definition/pronunciation (if it exists) on Wikitionary, AWAD,
Merriam-Webster, and Forvo. You write the card with definitions,
examples, and so on. Then you use the 'mig' and 'mog' scripts to
handle images and sound files (described later). Vocab's source:

function vocab {
if (( $# == 2 )) # TODO: figure out how to do this in general someday
then
local -r FIRST="$(perl -MURI::Escape -e 'print
uri_escape($ARGV[0]);' $1)"
local -r SECOND="$(perl -MURI::Escape -e 'print
uri_escape($ARGV[0]);' $2)"
local -r ESCAPED=`echo $FIRST%20$SECOND`
else
local -r ESCAPED="$(perl -MURI::Escape -e 'print
uri_escape($ARGV[0]);' "$@")"
fi
firefox "http://en.wiktionary.org/wiki/"$ESCAPED
firefox "http://www.merriam-webster.com/dictionary/"$ESCAPED
firefox "http://dictionary.reference.com/browse/"$ESCAPED
firefox "http://wordsmith.org/awad/search.html?cx=partner-pub-0068747404870456%3A118gxl-7atb&cof=FORID%3A11&q="$ESCAPED"+-anagrams&sa=Search&siteurl=wordsmith.org%2Fawad%2Fsearch.html#1453"
firefox "http://www.forvo.com/search/"$ESCAPED"/"; }

Now you have the card for a word, but no images. Well, you punch the
search term into Google Images, and right-click on 4 of them, say, and
using C-v, download them into ~/. Once they've downloaded, you open a
terminal, and punch in... say, '$ mig discursion', to continue the
example. It spits out '<img src="images/discursion_1.jpg"><img
src="images/discursion_2.jpg"><img src="images/discursion_3.jpg"><img
src="images/discursion_4.jpg">', and you paste that into the card. The
images have already been converted into JPG, shrunk down to 200px, and
moved into ~/.mnemosyne/images/.

Mig's source code:

function mig {
function migConvert {
local -r NEW=$BASE"_"$2
convert $1 -scale 200 ~/.mnemosyne/images/$NEW.jpg && rm $1 &&
echo -n '<img src="images/'$NEW.jpg'"/>'; }

if (( $# == 0 ));
then return 1;
else local -r BASE=$1
shift
local ARGS=$@
if (( $# == 0 )); # if just the basename was supplied, default
to all in ./
then fmt
ARGS=`ls *.jpg *.gif *.png *.bmp 2> /dev/null`
fi
local -i COUNTER=1
for FILE in $ARGS;
do migConvert $FILE $COUNTER
COUNTER=$(($COUNTER+1))
done; echo
fi; }

Mog is pretty much the same thing. (The names are short for 'mnemosyne
ogg' or 'mnemosyne image'. It made sense to me at the time.) You run
something like '$ mog discursion' and with no other arguments, it
defaults to moving all the WAVs and OGGs and MP3s in the working
directory into OGG files inside ~/.mnemosyne/english/ and printing out
the necessary <sound> tags for you to copy-paste into Mnemosyne. The
source is basically that of mig:

function mog {
function mogConvert {
local -r NEW=$BASE"_"$2
case $1 in
*.mp3) mpg123 --quiet $1 -w $NEW && oggenc --quiet $NEW -o
~/.mnemosyne/english/$NEW.ogg && rm $1 $NEW;;
*.ogg) mv $1 ~/.mnemosyne/english/$NEW.ogg;;
*) (oggenc --quiet $1 -o ~/.mnemosyne/english/$NEW.ogg
&>/dev/null) && rm $1;;
esac
echo -n '<sound src="english/'$NEW.ogg'"/>'; }

if (( $# == 0 ));
then return 1;
else local -r BASE=$1
shift
local -i COUNTER=1
local ARGS=$@
if (( $# == 0 )); # if just the basename was supplied, default
to all in ./
then fmt
ARGS=`ls *.ogg *.mp3 2> /dev/null`
ARGS=$ARGS" "`file *|grep RIFF|cut -d ':' -f 1`
fi
echo -n " "
for FILE in $ARGS;
do mogConvert $FILE $COUNTER
COUNTER=$(($COUNTER+1))
done; echo
fi; }

Obviously, these scripts may not work for your exact needs, and
require a number of libraries/apps installed, like a Perl library in
'vocab' (or Firefox), or the ogg tools or ImageMagick. But they may be
useful as examples, and certainly have saved me an awful lot of time
already. I regret not writing them long ago.

--
gwern
http://www.gwern.net

Reply all
Reply to author
Forward
0 new messages