Archive for August 2018

Creating an Anki flashcard deck from Twitter feed to learn sign language

TLDR: Twitter feed sign language signs in Anki:

Anki

The problem

Right, so I’m trying to learn some sign language. For anyone who’s not read the blog before: my hearing is really bad. So now I’m trying to get ahead of it getting worse, just in case. I’m going to do a course at the local uni next year but before that I figured it’d be nice to memorise some vocab. The great thing about being able to program is that you can improve your life in lots of little ways with some imagination and some help from Google.

Memorisation (Anki)

I’ve mentioned before that a really useful skill for devs is the ability to memorise. There are loads of techniques but something like a new language (spoken or sign) is a perfect use case for spaced repetition. I use Anki for my spaced repetition needs so I need to get some signs in there in a new deck so that I can start learning.

If you’re not familiar with spaced repetition, it’s a technique based on research that shows the best time to be retain something you’re trying to learn is to recall it just before you forget it. Then, each time you try to recall from memory you’ll be able to go a lot longer before needing to be reminded again.

Anki flashcards are similar to physical flashcards in that there is a front and a back. On one side you’ll put a prompt, e.g. a word to learn, and on the other you’ll put the “answer”, e.g. the corresponding sign for that word.

The solution

So the steps to get what I need are fairly simple:

  1. Get British Sign Language signs from somewhere
  2. Iterate over the signs
  3. Get the word and store it
  4. Get the sign and store it with the word
  5. The storage format should be importable by Anki

A quick bit of research and we’ve got a wealth of signs from a great Twitter feed by British Sign! I also know from reading somewhere before that Anki can import CSVs and you can even ref images if you add them to your Anki media folder so that’s the unknowns from steps 1 and 5 sorted. Like any programming task, once the steps are clear it’s a fairly trivial task to implement.

Code

Python (3) is the logical choice here. It’s quick and easy and I don’t need anything sexy. Just need it to do its job. So here’s the code with a few comments:


import twitter
import re
import csv
import urllib.request

# The Twitter API is awesome. First you have to get your 
# API keys (removed my actual ones!) and create an instance of 
# the client
api = twitter.Api(consumer_key='MY_CONSUMER_KEY',
  consumer_secret='MY_CONSUMER_SECRET',
  access_token_key='MY_ACCESS_TOKEN_KEY',
  access_token_secret='MY_ACCESS_TOKEN_SECRET')

# Searching the last 1k tweets will be plenty 
t = api.GetUserTimeline(screen_name="BritishSignBSL", count=1000)
tweets = [i.AsDict() for i in t]
with open('bsl_signs.csv', 'w', newline='') as csvfile:
    signwriter = csv.writer(csvfile, delimiter=',')
    for t in tweets:
        tweet_text = t['text']

# The sign of the day tweets always have the same format so Regex 
# is handy for getting the word
        m = re.search('sign is: (.+?) - http', tweet_text)
        if m:
            english_word = m.group(1)
            if 'media' in t:
                media = t['media']
                image_url = media[0]['media_url']
                filename = 'bsl-' + 
                english_word.replace(' ', '-').lower() + '.jpg'

# Downloading the image. Once copied to Anki's collection.media 
# folder you just need to ref them in the CSV with img src
                urllib.request.urlretrieve(image_url, filename)

# The important line. Write a row to the CSV, first column is 
# the word pulled from the tweet and second is the ref to the image
                signwriter.writerow([english_word, 
                f'<img src="{filename}" />'])

Copy the downloaded images to the collection.media folder for Anki and import the CSV and you’re done. I can run the script again in a few weeks/months to add new signs that get released but plenty to learn in the meantime!

Thanks to British Sign (www.british-sign.co.uk) for kindly agreeing to me using their content.