Skip to content

Instantly share code, notes, and snippets.

@kkoch986
Last active August 29, 2015 13:56
Show Gist options
  • Save kkoch986/8852473 to your computer and use it in GitHub Desktop.
Save kkoch986/8852473 to your computer and use it in GitHub Desktop.
Load an english language dictionary into a Trie and report the number of nodes required.
// Requires Node.js and [Natural](https://github.com/NaturalNode/natural)
var natural = require("natural");
var fs = require("fs");
// NOTE: This file should exist on most UNIX varieties
var dictionary = "/usr/share/dict/words";
var trie = new natural.Trie(false);
console.time("Build Trie");
fs.readFile(dictionary, {"encoding":"ascii"}, function (err, data) {
if (err) throw err;
var words = data.split("\n");
var size = data.replace("\n", "").trim().length;
console.log(words.length + " Words ("+size+" characters) Added.");
trie.addStrings(words);
console.timeEnd("Build Trie");
console.log(trie.getSize());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment