Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- AS3: Large text files with indexOf() on an array
- 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
- }
- }
- }
- }
- private static var dicts:Object;
- public function createDicts() {
- var first_letter:String;
- for (i = 0; i < words.length; i++) {
- first_letter = words[i].charAt(0);
- if (dicts[first_letter] == undefined) {
- dicts[first_letter] = {}
- }
- dicts[first_letter][words[i]] = 'X';
- }
- }
- public static function containsWord(word:String):Boolean {
- first_letter = words[i].charAt(0);
- if (dicts[first_letter]) {
- return dicts[first_letter][word] != undefined;
- }
- return false;
- }
- 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;
- }
- }
Add Comment
Please, Sign In to add comment