Skip to content

Instantly share code, notes, and snippets.

@kkoch986
Created January 8, 2014 05:11
Show Gist options
  • Save kkoch986/8312121 to your computer and use it in GitHub Desktop.
Save kkoch986/8312121 to your computer and use it in GitHub Desktop.
Use of a Trie to compute all possible word combinations found in a single word
var Trie = require("./index").Trie;
var t = new Trie();
// ... add your word dictionary ....
t.addStrings(["experts", "exchange", "pen", "island", "choose", "spain", "kids", "express", "childrens", "wear", "dickson", "web"]);
var testDomains = ["expertsexchange", "penisland", "choosespain", "kidsexpress", "childrenswear", "dicksonweb"];
function decompose(search) {
// find all of the words which start from the first letter
var firstWords = t.findMatchesOnPath(search);
// recurse on the second half of the string
var results = [];
for(var w in firstWords) {
var word = firstWords[w];
var searchResults = decompose(search.substring(word.length, search.length));
if(searchResults.length === 0) {
results.push([ word ]);
continue ;
}
for(var r in searchResults) {
results.push([word].concat(searchResults[r]));
}
}
return results;
}
for(var x in testDomains) {
var testDomain = testDomains[x];
console.log(testDomain + ": ", decompose(testDomain));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment