Last active
August 29, 2015 13:56
-
-
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.
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
// 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