Skip to content

Instantly share code, notes, and snippets.

@lovasoa
Created November 23, 2015 22:09
Show Gist options
  • Save lovasoa/dac8b2d1d796ae35fdb9 to your computer and use it in GitHub Desktop.
Save lovasoa/dac8b2d1d796ae35fdb9 to your computer and use it in GitHub Desktop.
Create a word trie from a dictionnary file
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