Created
April 8, 2015 17:58
-
-
Save syzer/502b6e7a0038561015e8 to your computer and use it in GitHub Desktop.
bigram text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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