Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This script helps you study by quizzing you some percentage of the times you enter a command in your terminal.
Check out this blog post for details: https://igliu.com/how-to-learn-a-natural-language/
# ...
# Before all your aliases
alias ls="/usr/local/bin/precmd.js; ls"
alias cd="/usr/local/bin/precmd.js; cd"
alias git="/usr/local/bin/precmd.js; git"
# repeat with other non-urgent commands that you use often
# other aliases
# ...
# ...
#! /usr/bin/node
// @author Anthony Liu <igliu@mit.edu>
// @date 2017-01-11
var fs = require('fs');
var readlineSync = require('readline-sync');
// config
var incidence = 0.2; // occurs with this probability
var questionsPerIncident = 5; // this many right answers per incident
var likelyRatio = 8; // easiest word is this much less likely
var dirName = '~~ LOCATION OF YOUR WORDS FILE ~~'; // directory that the data file is in
var fileName = 'words.txt'; //the name of the data file
// parses a file into a list of from->to pairs
function deserialize(fileLoc, callback) {
fs.readFile(fileLoc, 'utf8', function(err, contents) {
if (err) {
return callback(err, false);
}
try {
var data = [];
var lines = contents.split('\n');
lines.forEach(function(line) {
if (line.length > 0) {
var components = line.split(',');
var from = components[0];
var to = components[1];
var correct = components.length == 4 ? components[2] : 0;
var incorrect = components.length == 4 ? components[3] : 0;
correct = parseInt(correct);
incorrect = parseInt(incorrect);
data.push({
'from': from,
'to': to,
'correct': correct,
'incorrect': incorrect
});
}
});
return callback(false, data);
} catch (e) {
console.log(e.message);
return callback(true);
}
});
}
// converts a list of from->to pairs to a string
function serialize(data) {
var str = '';
for (var i = 0; i < data.length; i++) {
var word = data[i];
var line = word.from + ',';
line += word.to + ',';
line += word.correct + ',';
line += word.incorrect;
str += line;
if (i !== data.length - 1) str += '\n';
}
return str;
}
if (Math.random() < incidence) {
// parse the data file
deserialize(dirName + fileName, function(err, data) {
if (err) return console.log('Unknown error.', err);
var answeredCorrectly = 0;
var y0 = 2/(likelyRatio + 1);
var a = 2*y0 - 2;
var b = 2 - y0;
do {
// sort the data
data.sort(function(a, b) {
var aRatio = a.correct/(a.incorrect + 1)
var bRatio = b.correct/(b.incorrect + 1)
return aRatio - bRatio;
});
// select a word, weighted by correct/incorrect ratio
var r = (-b + Math.sqrt(b*b + 2*a*Math.random()))/a;
var idx = Math.floor(r * data.length);
var word = data[idx];
// quiz the user on that word
var pos = answeredCorrectly + 1;
var guess = readlineSync.question(
'(' + pos + '/' + questionsPerIncident + ') ' +
'What does "' + word.from + '" correspond to? '
);
if (guess === word.to) {
answeredCorrectly += 1;
data[idx].correct += 1;
} else {
data[idx].incorrect += 1;
console.log(data[idx]);
}
// save the new data file every round
var str = serialize(data);
fs.writeFileSync(dirName + fileName, str)
} while (answeredCorrectly < questionsPerIncident);
console.log('--- --- ---');
});
}
goodbye,tchau,0,0
yes,sim,0,0
your,seu,0,0
who,quem,0,0
good afternoon,boa tarde,1,0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.