Created
November 23, 2015 22:09
-
-
Save lovasoa/dac8b2d1d796ae35fdb9 to your computer and use it in GitHub Desktop.
Create a word trie from a dictionnary file
This file contains hidden or 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 fs = require("fs"); | |
var dic = fs.readFileSync("/dev/stdin", "utf8").split("\n"); | |
var trie = {}; | |
for(var i=0; i<dic.length; i++) { | |
var w = dic[i]; | |
var t = trie; | |
for(var j=0; j<w.length; j++) { | |
var l = w[j]; | |
if(!t[l]) t[l] = {}; | |
t = t[l]; | |
} | |
} | |
console.log(JSON.stringify(trie)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment