Created
January 8, 2014 05:11
-
-
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
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 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