Skip to content

Instantly share code, notes, and snippets.

@syzer
Created April 8, 2015 17:58
Show Gist options
  • Save syzer/502b6e7a0038561015e8 to your computer and use it in GitHub Desktop.
Save syzer/502b6e7a0038561015e8 to your computer and use it in GitHub Desktop.
bigram text
function bigramText(arr) {
return arr.reduce(bigramArray);
}
function bigramArray(acc, word, i, arr) {
if (1 === i) {
acc = {last: acc, out: {}};
}
var out = acc.out;
var last = acc.last;
out[last] = out[last] || {};
out[last][word] = out[last][word] + 1 || 1;
acc.last = word;
acc.out = out;
if (i === arr.length - 1) {
return acc.out;
}
return acc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment