Created
March 19, 2012 18:57
-
-
Save beezee/2123957 to your computer and use it in GitHub Desktop.
caclulate every possible combination of n words up to x from a string
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
var nGramr = {} | |
nGramr.words = prompt('Whats the phrase?').split(' '); | |
nGramr.wordlen = nGramr.words.length; | |
nGramr.depth = prompt('Whats the n?'); | |
nGramr.results = 0; | |
nGramr.grams = function(aWords) { | |
var words = aWords; | |
var depth = words.length; | |
console.log(depth); | |
_.each(nGramr.words, function(word) { | |
if (words.indexOf(word) == -1) { | |
console.log(words.join(' '), word); | |
nGramr.results ++; | |
var nextCall = words.slice(0); | |
nextCall.push(word); | |
if (depth < nGramr.depth - 1) nGramr.grams(nextCall); | |
} | |
}); | |
}; | |
_.each(nGramr.words, function(word) { | |
nGramr.grams([word]); | |
}); | |
console.log(nGramr.results + ' results'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
because it took me 4 hours out of my weekend to realize i had to properly clone the words array before recursing... so i will never forget this lesson