📜 ⬆️ ⬇️

Algorithm for memorizing foreign words

At the moment, created many applications to memorize words. From those that I remember, I can highlight such Android applications as Lingualeo , English words , Word Uch .

The main drawback of these applications for me was a paid account for adding my own database of words. Therefore, the question arose about writing your application to memorize words. The main idea was to connect an external dictionary and translator API to translate words into their native language. Yandex API ( Translator API and Dictionary API ) was chosen as such API.

The first step was to get the developer keys. For the translator and for the dictionary .

JavaScript and the jQuery library were chosen as the language and development platform.
')
To get the translation of the word into the desired language, I used the following code:

var oneWord = function() { $.post("https://dictionary.yandex.net/api/v1/dicservice.json/lookup", { key: apiKey, lang: lang, text: words[index].text }, function(data) { words[index].tr = ""; words[index].ts = ""; for (var j = 0; j < data.def.length; j++) { var def = data.def[j]; for (var k = 0; k < def.tr.length; k++) { var tr = def.tr[k]; words[index].tr += tr.text + "; "; } if (def.ts) words[index].ts = def.ts; } if (words[index].tr == "") { translateWords(); tsWords(); return; } else { var str = words[index].tr; words[index].tr = str.substring(0, str.length - 2); } complete(); }, "json"); }; var tsWords = function() { var text = words[index].text; var tsText = ""; var tsWords = text.match(/\S+/gi); var tsIndex = 0; var tsPost = function() { $.post("https://dictionary.yandex.net/api/v1/dicservice.json/lookup", { key: apiKey, lang: lang, text: tsWords[tsIndex] }, function(data) { var ts = ""; for (var j = 0; j < data.def.length; j++) { var def = data.def[j]; if (def.ts) ts = def.ts; } tsText += ts + " "; if ((tsIndex < (tsWords.length - 1)) && (tsIndex < 5)) { tsIndex++; tsPost(); } else { words[index].ts = tsText.trim(); complete(false, true); } }, "json"); }; tsPost(); }; var translateWords = function() { $.post("https://translate.yandex.net/api/v1.5/tr.json/translate", { key: apiKeyTranslate, lang: slang, text: words[index].text }, function(data) { words[index].tr = ""; for (var j = 0; j < data.text.length; j++) { var text = data.text[j]; words[index].tr += text + "; "; } var str = words[index].tr; words[index].tr = str.substring(0, str.length - 2); complete(true, false); }, "json"); }; var qu = function() { if (!words[index].tr) { oneWord(); } else { complete(); } }; qu(); 

Here, the oneWord function translates one word, tsWords finds transcriptions of the first five words in the expression (if not a word, but a sentence), translateWords translates the sentence.

The resulting complete function is called to fill in the form of a word with transcription and translation:

  var complete = function(tr, ts) { if (ts == undefined) ts = true; if (tr == undefined) tr = true; var word = words[index]; if (tr) $("#text").html(word.text); if (ts) $("#ts").html("[" + word.ts + "]"); $("#tr").hide(); $("#attempt").hide(); $("#show").show(); $("#tr").html(word.tr); $("#tts").show(); }; 

In the array of words, words index reflects the current words to be memorized. The following word is selected by the following algorithm:

  var words = [], patternCount = 5, indexMemory = {}, indexMemoryCount = 0, patternIndex = [], lastIndex = -1, lastIndexs = [], lastIndexsCount = 2, wasAttempt = false, wasMemory = false, deep = 0, deepMax = 100; var index = nextIndex(); var nextIndex = function() { deep++; if (lastIndexsCount - words.length >= 0) { lastIndexsCount = 0; } if ((patternIndex.length < patternCount) && (indexMemoryCount < words.length)) { if (deep > deepMax) { var index = maxAttemptsIndex(true); return index; } var index = Math.floor(Math.random() * words.length); if (indexMemory[index]) { return nextIndex(); } indexMemory[index] = "do"; indexMemoryCount++; patternIndex.push(index); lastIndex = index; pushIndex(lastIndex); return index; } else { var index = Math.floor(Math.random() * (patternIndex.length + 1)); if (index == patternIndex.length || (patternIndex.length == 0)) { wasMemory = true; var ind = maxAttemptsIndex(); if (inArray(lastIndexs, ind)) { if (deep > deepMax) { ind = Math.floor(Math.random() * words.length); lastIndex = ind; pushIndex(lastIndex); return ind; } return nextIndex(); } lastIndex = ind; pushIndex(lastIndex); return ind; } if (inArray(lastIndexs, patternIndex[index])) return nextIndex(); lastIndex = patternIndex[index]; pushIndex(lastIndex); return patternIndex[index]; } }; var maxAttemptsIndex = function(notAttempts) { var arr = sortMemoryIndexes(indexMemory); var index = getRandomFishIndex(arr, notAttempts); return index; }; var pushIndex = function(index) { if (lastIndexsCount == 0) return; if (lastIndexs.length < lastIndexsCount) { lastIndexs.push(index); } else { lastIndexs[0] = lastIndexs[1]; lastIndexs[1] = index; } }; var inArray = function(arr, elem) { for (var i = 0; i < arr.length; i++) { if (arr[i] == elem) return true; } return false; }; function getRandomFishIndex(arr, notAttempts) { var fishForLevel = arr; var fishTotalWeight = 0, fishCumWeight = 0, i; // sum up the weights for (i = 0; i < fishForLevel.length; i++) { fishTotalWeight += fishForLevel[i].attempts; } if (notAttempts) { fishTotalWeight = 0; } var random = Math.floor(Math.random() * fishTotalWeight); // now find which bucket out random value is in if (fishTotalWeight == 0) return fishForLevel[Math.floor(Math.random() * fishForLevel.length)].index; for (i = 0; i < fishForLevel.length; i++) { fishCumWeight += fishForLevel[i].attempts; if (random <= fishCumWeight) { return fishForLevel[i].index; } } } function sortMemoryIndexes(indexMemory) { var arr = []; for (var key in indexMemory) { if (indexMemory[key] == "do") { var word = jQuery.extend(true, {}, words[key]); word.index = key; arr.push(word); } } var sAttempt = function(first, second) { if (first.attempts < second.attempts) return 1; else if (first.attempts > second.attempts) return -1; else return 0; }; return arr.sort(sAttempt); } 

The bottom line is that you want to select the next word from a set of previously unexplored, as well as previous studied words. The probability of showing the latter should be greater if the word is poorly remembered.

It is the “Wrong” button that implements the permutation of the word display probabilities.

  $("#attempt").click(function() { words[index].attempts++; wasAttempt = true; $("#attempt").hide(); }); 

This method of memorizing words seemed to me the most effective. The rest of the application code implements the events and actions of the interface elements. HTML and accompanying JavaScript code was wrapped in Cordova for the Android platform.

The EnglishWords application allows you to learn English words and the words of many other languages. The program has a basic set of words to learn. The main feature of the program is the ability to create your own sets of words for study . SCREEN INFORMATION * Percent. Means the percentage of words learned in the dictionary. HOW IT WORKS. The process of learning words begins with the program recruiting 5 random words from the selected dictionaries and starts to show them in a random order. After the words are learned from the dictionary, the next 5 random words are extracted. If you answer the word incorrectly, the word will appear more often. When all words are learned, only those words for which most often were given are not the correct answer. DICTIONARY The basic set of words contains about 1000 of the most commonly used English words.

The application uses yandex and google api to get translation, transcription and sound playback. Internet access is required for the application to work.

I will give screenshots of the application:

Source: https://habr.com/ru/post/335212/


All Articles