Word Association Game - Behind the Scenes

 


This is a simple word association game. In this game there are 5 cards. Each card has four words, and two options. The task is simple: pick the word that is the most similar! One of the words is an antonym of the other, so once you know the meanings the difference should be stark. Pick the option by pressing one of the green buttons. A correct option increments the score, and an incorrect option decrements the score.  Note that you can pick both options for one card (so even if you get it wrong, just press the other one to return your lost score).

The frontend is based on this video, but I have modified it to use AJAX calls to the API for random questions. The API is relatively simple: it picks randomly 5 questions from a dataset of thousands of questions, and then returns the chosen ones to the caller of the API. It is written in Django and Python.

To compute the dataset, and making the API lighter and faster later on rather than computing it on-the-fly – which I’ve tried and is woefully slow – I used a random word API to get the base word (which is the correct answer) and a Python module named PyMultiDictionary to find the antonyms (for the incorrect option) and synonyms of the word (for the question). Then, all of this is assembled into one complete question and put into a JSON file. One JSON file either contains nothing or a question file – I’ll explain why just now.

The PyMultiDictionary module is quite reliable, but sometimes it cannot generate synonyms or antonyms of several words. In this case, the computation will fail and raise an exception. The solution is simple: if the operation fails, leave the file empty (using a context manager to do this) and then catch the exception with a try block.

This operation is very slow, so I used the multiprocessing library built into Python to do the operation, making things faster by processing multiple words concurrently. I downloaded the dataset of the random word API (it’s free, so do check it out if you need those kinds of data), and then wrote a simple script to delete a chunk of word from the file and then process it. I set up a cron task to repeat this task every minute on the hosting service I used to deploy the app. It ran well, and it produced thousands of entries. After that I wrote another Python script to concatenate the data and clean the dataset from empty or corrupt entries. The dataset is done!

Check it out at https://wag.ackhava.com/!

Popular posts from this blog

Flood Warning System using Arduino

Remotion: Video with React!