Script Away your Annoyances: Creating Word Groupings from Anki’s Database

Why manually dig through the Anki browser, looking for words, when I could have the computer tell me automatically?Part of my efforts to learn French have been in producing little stories on lang-8. These stories are corrected by native speakers; I then take those corrections and feed them back into my Anki deck.

Production is still quite difficult for me, and I realize that what I truly need is more practice. Thus, after reviewing my daily vocabulary in Anki, I’ve started creating lots of little sentences using the words I reviewed that day. A typical lang-8 post like this will have about a half a dozen to ten sentences.

But creating these groups of words in the Anki browser is a manual task that’s annoying. Wait. An annoying manual task? I’ve written about this in the past:

Automation is good. Performing tasks manually is bad. Performing tasks manually is especially bad when the tasks are annoying. Let’s use a Ruby script to alleviate the pain of an annoying task.

Why manually dig through the Anki browser, looking for words, when I could have the computer tell me automatically?

The Script

To begin, all of the scripts are hosted within my GitHub repository (for the record, this is the repository I wrote about in my previous post, but I’ve updated the name).

Check out the WordsReviewedToday#run method. At a high level, it describes the steps this script must step through:

  1. Figure out a time to represent the beginning of today. At least one of the Anki database fields stores time, and we need today’s start time in order to make comparisons.
  2. Find the cards that were reviewed today.
  3. Filter those cards down to the vocabulaire cards that were reviewed today.
  4. Look up the word that is being tested by the vocabulaire cards.
  5. Remove words that are images. (This is to work around an old way I had of representing notes.)
  6. Print out groups of randomly selected words.

Thoughts on Abstraction

I’ll defer to the source for the details of how all of these steps are implemented, but there is one thing I’d like to discuss: abstraction.

In my previous post I described the count_known_words script. While creating the group_words_reviewed_today script, I took a lazy approach to abstraction: whenever I needed to share a little piece of code, I made a helper function, but not until I was certain I needed it in more than one place.

Premature abstraction can be more confusing than helpful.
Premature abstraction can be more confusing than helpful. Image credit Daniel Hoelbling-Inzko

I think it would be tempting and easy to create some abstractions for the Anki database — go look up this field, split this other thing, join these tables, etc. But in my experience, it’s difficult to accurately predict how effective those abstractions will be until I have at least three different components using them. That is why I’ve created little helper functions for the things the two scripts specifically need to share, but no more.

In my opinion, a good time to look for higher-level, broader abstractions is once I have three different Anki scripts. Then it will be a lot easier to come up with sensible abstractions that are broadly useful. And those abstractions, in turn, will make it easier to create scripts in the future.

One Fewer Annoyance

My new script is quite a helpful little tool. It could still use some improvements — for example, an astute reader may notice the groups in the above screenshot are fairly noun heavy. Building out groups with more verbs, adjectives, and adverbs will make the script even more useful.

Regardless, I’ve now got the group_words_reviewed_today script to automate an annoying task, and I’ve no excuse not to create lots of posts on lang-8. I’m looking forward to getting more practice producing sentences and, in turn, improving my French skills.