Today I started on the XML dictionary hoping it would be the easiest part. It wasn't.
The problem is that, when you convert a dictionary consisting of 120,000 words (give or take) into XML, it takes up 2.81 MB of space. That, friends, is a BIG file, and it turns out Flash won't load it without some serious hemming and hawing. Unfortunately, Flash has a built-in bailout feature where if a function takes longer than 15 seconds to execute, it will automatically terminate it. How long does it take to load a 2.81 MB file into Flash? I was never able to figure it out exactly, but it's longer than 15 seconds.
Fortunately, there was another way I hadn't thought of. In addition to XML, I also have this dictionary in a simple .txt format. That file takes up about 1.24 MB, which, while still huge for a plain text file, is less than half the size of the XML file. More importantly, in this file, each word is on a new line. I will explain why this makes things easier for me.
The non-programming types among you may not know this, but every time you press the Enter key on your keyboard, your operating system inserts what's called a "newline" character. You don't see this character, but it has to be there or the computer doesn't know where to put a new line. This character is different on different computers, but on mine it's "\r." Hence, if I have this file:
a
an
the
it's really more like this:
a\ran\rthe
When the file is loaded, the "\r"s are replaced with new lines, so we as end users never actually see these characters. But Flash knows they're there, and it also comes with a handy "split" function that can be used on chunks of text (called "strings" in Flash, incidentally). The split function, as the name suggests, splits a string apart wherever a specified character occurs and saves the resulting collection of strings as an array, which is Flash's way of keeping a group of things together. Therefore, if you tell the split function to split up the string "a,an,the" at the commas, you get an array containing the strings "a", ",an", and ",the." Do you see where I'm going with this?
YES! If I use the split function to break up the text file at the newline characters, then trim off the first character from each resulting string, I get an array containing all the words in my dictionary. Nifty, eh?
And the rest, as they say, is history. To load the text file into Flash so I could modify it, I used something called a URLLoader, which is how Flash loads data from external sources. This gives me the dictionary as one long string that I can break up into an array of smaller strings. XML isn't necessary, and best of all, this way actually works, a cornerstone of any good Flash application. Sweet.
Tomorrow I'll start working on adding all the words into a trie, which should make searching the dictionary for a specific word considerably faster. Until then, folks, until then.
No comments:
Post a Comment