Check if a String is in a Word List

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 :slight_smile:

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 :slight_smile:

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)

Yeah, it’s a lot simpler if you just use System.Convert.ToInt32.

–Eric

Hey thanks for that, much easier than searching my filesystem!

Bumpies, haven’t updated this or anything, but maybe someone else might want to use :slight_smile:

Johnny, I am interested in giving your code a try for a game I am working on but I keep getting this error.

IndexOutOfRangeException: Array index is out of range.
(wrapper stelemref) object:stelemref (object,intptr,object)
Dictionary.addWord (System.String word, .WordTrie child, Int32 childPos) (at Assets/scripts/Dictionary.js:55)
Dictionary.addWord (System.String word, .WordTrie child, Int32 childPos) (at Assets/scripts/Dictionary.js:57)
Dictionary.generateDictionary () (at Assets/scripts/Dictionary.js:38)
Dictionary…ctor () (at Assets/scripts/Dictionary.js:10)
TestDictionary.Start () (at Assets/scripts/TestDictionary.js:3)

Have you had this working in a scene and if so could you post a scene with all the scripts running?

-Tim

Certainly working in a scene, it’s in a title that’s almost ready for release. However the code may have had some changes along the way. I’m at teh day job at the moment, but I’ll try and post a working scene here tonight.

Thanks for your interest :slight_smile: