Skip to content

Instantly share code, notes, and snippets.

@borgar
Created October 22, 2017 21:46
Show Gist options
  • Save borgar/5b59dc2d70d1a93bdce5e4fb15ec7d71 to your computer and use it in GitHub Desktop.
Save borgar/5b59dc2d70d1a93bdce5e4fb15ec7d71 to your computer and use it in GitHub Desktop.
// Orders a list of candidates having reply strings relative to a given reply string
// https://github.com/kristjanmik/kjosturett-web#how-does-the-reply-string-work
const replies = require('./ruv.json');
const userReplies = '354421552151555533355555555151';
function decode(s) {
const val = { '1': -1, '2': -0.5, '3': 0, '4': 0.5, '5': 1, '6': null };
return s.split('').map(d => val[+d]);
}
function match(myReplies, candReplies) {
if (myReplies.length !== candReplies.length) {
throw new Error("Answer lists don't match");
}
let ranks = 0;
let distance = 0;
myReplies.forEach((my, i) => {
const them = candReplies[i];
if (my !== null && them !== null) {
distance += Math.abs(my - them);
ranks += 1;
}
});
return (2 - distance / ranks) / 2 * 100;
}
const matches = replies
.map(d => {
const r = match(decode(userReplies), decode(d.reply));
return { cand: d, rating: r };
})
.sort((a, b) => b.rating - a.rating);
console.log( 'Testing:', userReplies );
console.log( '' );
matches.slice(0, 5).forEach(match => {
const cand = match.cand;
console.log( cand.bokstafur, '-', cand.nafn );
console.log( ' ', match.rating + '%' );
console.log( ' ', cand.reply );
console.log( '' );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment