Wrote this code a while back… it loads a list of words from a dictionary file on instantiation, then it can be called to see if a given string is in the word file. Basically it’s for word puzzle games or spell checkers where you need to be able to check that the start of a word is okay. See TestDictionary.js for usage.
Good Stuff
Most importantly it uses a Trie based structure which is pretty fast, and doesn’t use much memory; I use a slightly adapted version in an iOS project.
Also the recursive function to check if a word is in the the dictionary is only a few lines long which is pretty cool ![]()
You can use this in your own project for commercial or non-commercial use. If you want to include a credit to me or jnamobile.com thats great but not required. You can distribute this code as part of another project, all I ask is that you don’t sell it unmodified ![]()
Bad Stuff
I used a rather long winded static function charToAscii(c:char) this was slightly annoying compared to c#/java (not js) version where you just subtract a number. I know there is an alternative using a system class, I think I have a version round here using it somewhere, but I feel like posting this right now!
No Exception handling/error checking!
Finally, I did a couple of readability edits without Unity handy, this might be very slightly broken (be nice if someone can confirm).
446341–15522–$TestDictionary.js (343 Bytes)
446341–15523–$Dictionary.js (3.78 KB)
446341–15524–$WordTrie.js (355 Bytes)