JavaScript in Anki cards

[N.B. 2016-03-26 Nathan Ifill pointed out that it is possible to use Anki’s built-in conditional replacement feature to do what I’m illustrating. I’ll have to work on another example!]

Anki is a widely-used flashcard application. If you’re learning a foreign language and you’re not using Anki, you should be.

If you are using Anki and are picky about the appearance of the cards, you should know that JavaScript can be used in the card template. This opens up a number of possibilities for dynamic cards. I’m just touching on the technique here.

In my Russian deck, I sometimes have example sentences in Russian with an English translation. The fields are sentence_ru and sentence_en. Here’s the catch, when a sentence is available, I want the card to display it. When there’s not a sentence, I don’t want any formatting artifacts on the card. Here’s an example of a card with no example sentence:

Example card

The hyphen separating what should be the Russian and English sentences still appears on the card. Let’s fix that. To do that will use JavaScript to detect if a sentence is present and modify the card’s HTML if it’s not. All of our work will be in the card template definition.

First, we’ll define some CSS styling for the sentence fields.

span.en {
	font-family: Courier;
	font-size: 16px;
	font-style: italic;
}

span.ru {
	font-family: CourierNew;
	font-size: 16px;
	font-weight: bold;
}

Next we’ll define the part of the card where the sentence (if available) should be displayed.

<span id="ru" class="ru">{{sentence_ru}} - </span>   <span id="en" class="en">{{sentence_en}}</span>
Now, if we have an example sentence pair, then we’ll see a Russian sentence and the English translation separated by a hyphen. To deal with the case where no example sentence is available, we finally get to use some JavaScript. At the bottom of the card side HTML, add a script:

var content = document.getElementById("ru").innerHTML;
if (content.length < 4) {
	document.getElementById("ru").innerHTML = "";
}

This works by looking at the content of the Russian sentence tag. If the length isn’t long enough, then we conclude that it’s just the spaces and hyphen. So in that case, we just remove all of the content. You’ll need to enclose the script in <script></script> tags, of course.

Here’s what the card without example sentences looks like with our script applied:

Fixed card

By adding a sentence pair, we can see that the styling is applied and that the script finds our content and ignores it:

Fixed card with sentences

This is a simple example of how JavaScript can be used in an Anki card template. It looks like it could be a very useful way of dynamically styling cards.

Organizing knowledge for memorization

Memorization has a bad reputation in education today, but it underpins the abilities of all sorts of high-performing people. I often refer to this article from 1999 about how to better organize information for memorization.

My favorite pieces of advice:

  • Do not learn (memorize) if you do not understand.
  • Stick to the minimum information principle.
  • Use imagery
  • Avoid sets and enumerations
  • Use mnemonic techniques.

Observation: Facebook groups don't work

Web
I’m reluctant about using Facebook. Recently I returned after a 5 year sabbatical. It seems about the same as it was when I left. But I had never really used Facebook groups before. So when a friend launched a group around a topic of interest to me, I joined enthusiastically. While watching the numbers grow quickly in the first few days, I realized what a difficult platform it is for having any kind of meaningful discussion.

Detecting Russian letters with regex

How to identify Russian letters in a string? The short answer is: [А-Яа-яЁё] but depending on your regex flavor, [\p{Cyrillic}] might work. What in the word does this regex mean? It’s just like [A-Za-z] with a twist. The Ёё at the end adds support for ё (“yo”) which is in the Latin group of characters.

See this question on Stack Overflow.

Scalia and the secret society

It was recently reported in the Washington Post that the late Supreme Court Associate Justice Antonin Scalia had ties to a secret society of hunters called the International Order of St. Hubertus and that several of the guests at the Texas ranch where Scalia died were members of this group.

Surely I’m not the only one that finds this a bit weird. A secret society of hunters? I don’t know the first thing about hunting but from I’ve seen, the only thing secret about hunting is sneaking up on an animal so you can kill it.

Mak-kimchi 막김치

Years ago, my wife and I stayed overnight in Seoul on the way home from New Zealand. An amazing array of types of kimchi accompanied breakfast the following morning; and from then on, I was hooked on this Korean staple. For the last few years, I’ve gradually honed my kimchi-making skills. For simplicity, I tend to make mak-kimchi which means “roughly made kimchi.” In traditional kimchi, who cabbages are fermented intact (though usually split in half to permit the salt and later, spices to enter between the leaves.) However, getting the cabbages properly salted it very tricky and often neglected. So instead of making the more traditional kimchi, I’ve specialized in mak-kimchi. The main difference is in the way that the cabbages are handled. In this case, the cabbages are slided into approximately bite-sized pieces then salted, wilted, rinsed, mixed with the spices and allowed to ferment.

Automate hexo blogging tasks with Grunt

In my never-ending journey to find the optimal blogging platform, I wandered into the hexo camp. Among its many attributes is speed. Compared to Octopress, site generation is very fast. However, deployment has been tricky. Since I host my blogs from an Amazon S3 bucket, I tried to use the aws deployer commonly used with hexo; but I could never get it to install properly on OS X 10.11. So I wrote my own deployer that essentially just runs an AppleScript that handles the synchronization task. It is very slow. So I’m always on the lookout for faster deployment schemes. It looks like a Grunt-based system is the ticket.

Kimchi and hypertension

Given my obsession with kimchi, I sometimes wonder whether the salt in kimchi promotes hypertension. The good news seems to be that it doesn’t.

In a retrospective recall study^[Consumption of kimchi, a salt fermented vegetable, is not associated with hypertension prevalence, Song, Hong Ji et al., Journal of Ethnic Foods , Volume 1 , Issue 1 , 8 - 12] of over 20,000 Korean adults, there was no association between kimchi consumption and the prevalence of hypertension.

Privacy vs. security: Just this once.

In the showdown between Apple and the FBI over an iPhone belonging to one of the San Bernardino attackers, some would argue that the company should acquiesce to the government’s request that it create a “backdoor” into the device allowing it to bypass the built-in strong encryption. Here’s what people who make this argument are missing: the law doesn’t work that way.

The government filed a motion in the U.S. District Court asking the court to direct assist law enforcement in bypassing the security features of the device. Magistrate Judge Sheri Pym approved the government’s request and ordered Apple to comply. The FBI and others argue that this is a one-off instance of bypassing security. But it is not.

On the lack of association between taxes, GDP and happiness

During a February 6, 2016 debate, U.S. presidential aspirant Donald Trump claimed that: “Right now we’re the highest taxed country in the world.”

Well, living in a country with much higher tax rates, I can tell you that his statement is patently false.^[Maybe he’s referring to the corporate tax rate which is quite high; but even that is irrelevant to the fact that all sorts of deductions and exclusions on the U.S. corporate tax return put the U.S. effective tax rate much lower than that of most counties.]