AS3: Large text files with indexOf() on an array

[亡魂溺海] 提交于 2019-12-25 02:21:46

问题


I am embedding the sowpods dictionary into an array in AS3 then submit searches using indexOf() to verify existence of the word.

When I load a smaller text file it seems to work but not the larger. Since the file is embedded during compile, there shouldn't be an event for loading to listen to right?

Code:

package {
    import flash.display.MovieClip;

    public class DictionaryCheck extends MovieClip {

        [Embed(source="test.txt",mimeType="application/octet-stream")] // Works fine 10 rows.
                //[Embed(source="sowpods.txt",mimeType="application/octet-stream")] //Won't work too large.
        private static const DictionaryFile:Class;

        private static var words:Array = new DictionaryFile().toString().split("\n");

        public function DictionaryCheck() {
            containsWord("AARDVARKS");
        }

        public static function containsWord(word:String):* {
            trace(words[10]); //Traces "AARDVARKS" in both versions of file
            trace((words[10]) == word); // Traces true in shorter text file false in longer
            trace("Returning: " + (words.indexOf(word))); // traces Returning: 10 in smaller file
            if((words.indexOf(word)) > -1){
               trace("Yes!"); // traces "Yes" in shorter file not in longer
            }
        }
    }
}

回答1:


In my experience (I have no direct documentation to back me up), Flash cannot open very large text files. I've had the same problem as you while importing a dictionary before.

What I ended up doing, was to transform the dictionary into an ActionScript class, that way I didn't have to load a file and parse it into a dictionary for better search, the dictionary had already been parsed and stored in an Array. Since the array members were already sorted, I used a simple half-interval search function (http://en.wikipedia.org/wiki/Binary_search_algorithm) to determine if the dictionary contained the word or not.

Basically, your dictionary would look like this:

public class DictSOWPODS {
    protected var parsedDictionary : Array = ["firstword", "secondword", ..., "lastword"]; // yes, this will be the hugest array initialization you've ever seen, just make sure it's sorted so you can search it fast

    public function containsWord(word : String) : Boolean {
        var result : Boolean = false;
        // perform the actual half-interval search here (please do not keep it this way)
        var indexFound : int = parsedDictionary.indexOf(word);
        result = (indexFound >= 0)
        // end of perform the actual half-interval search (please do not keep it this way)
        return result;
    }
}

The only thing you lose by using a AS Class instead of a text file is that you cannot change it in runtime (unless you use a swc to hold the class), but since you were already embedding the text file in the .swf, this should be by far the best solution (no need to load and parse the file). It's also important to note that if your dictionary is really really big, the flash compiler will eventually explode in shame.

EDIT:

I've transformed the SOWPODS I found in here http://www.isc.ro/en/commands/lists.html into a working class, get it here: http://www.4shared.com/file/yQl659Bq/DictSOWPODS.html?



来源:https://stackoverflow.com/questions/8099543/as3-large-text-files-with-indexof-on-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!