AS3:大文本文件的indexOf()的阵列上 [英] AS3: Large text files with indexOf() on an array

查看:175
本文介绍了AS3:大文本文件的indexOf()的阵列上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我嵌入sowpods字典转为在AS3数组,然后提交使用搜索的indexOf(),以验证是否存在这个词。

当我打开一个小的文本文件,它似乎工作,但不是大。由于该文件是在编译过程嵌入式,不应该有一个事件加载听吧?

code:

 包{
    进口的flash.display.MovieClip;

    公共类DictionaryCheck扩展影片剪辑{

        [嵌入(来源=的test.txt,MIMETYPE =应用程序/八位字节流)] //工作正常10行。
                //[Embed(source="sowpods.txt",mimeType="application/octet-stream)] //不会太大。
        私有静态常量DictionaryFile:类;

        私人静止无功的话:阵列=新DictionaryFile()的toString()分裂(\ N);

        公共职能DictionaryCheck(){
            containsWord(土豚);
        }

        公共静态函数containsWord(字:字符串):* {
            迹线(字[10]); //痕迹文件的两个版本的土豚
            迹((字[10])==字); //在更短的文本文件,虚假的再跟踪真
            跟踪(回归+(words.indexOf(字))); //痕迹返回:10在更小的文件
            如果((words.indexOf(字))-1)〜{
               跟踪(是的!); //痕迹是,在较短的文件不是在较长
            }
        }
    }
}
 

解决方案

在我的经验(我没有直接说明文件,以支持我),Flash无法打开非常大的文本文件。我已经为你而之前导入词典同样的问题。

我落得这样做,是为了改造字典为ActionScript类,这样,我没有加载一个文件,并解析为一个字典更好的搜索,字典已经被解析并存储在数组。由于数组成员已经排序,我用一个简单的半区间搜索功能( HTTP://en.wikipedia 。组织/维基/ Binary_search_algorithm 的),以确定该词典包含单词或不

基本上,你的意思是这样的:

 公共类DictSOWPODS {
    保护变种parsedDictionary:数组= [firstword,secondword,...,最后字]; //是的,这将是你见过的最庞大的数组初始化,只是确保它的排序,所以你可以搜索它快

    公共职能containsWord(字:字符串):布尔{
        VAR结果:布尔= FALSE;
        //执行实际的半区间检索这里(请不要保持下去)
        VAR indexFound:INT = parsedDictionary.indexOf(字);
        结果=(indexFound> = 0)
        //结束执行实际的半间隔搜索(请不要保持下去)
        返回结果;
    }
}
 

您失去使用文本文件的AS类,而不是唯一的事情是,你不能在运行时(除非您使用SWC持有类)改变它,但既然你已经嵌入在文本文件中。瑞士法郎,这应该是迄今为止最好的溶液(无需加载并解析该文件)。 同样重要的是要注意,如果你的字典是真的大,Flash编译器最终将发生爆炸的耻辱。

编辑:

我改变,我发现在这里为http SOWPODS://www.isc。 RO / EN /命令/ lists.html 成工人阶级,它来到这里: http://www.4shared.com/file/yQl659Bq/DictSOWPODS.html

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
            }
        }
    }
}

解决方案

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?

这篇关于AS3:大文本文件的indexOf()的阵列上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆