Guest User

Untitled

a guest
Aug 14th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. AS3: Large text files with indexOf() on an array
  2. package {
  3. import flash.display.MovieClip;
  4.  
  5. public class DictionaryCheck extends MovieClip {
  6.  
  7. [Embed(source="test.txt",mimeType="application/octet-stream")] // Works fine 10 rows.
  8. //[Embed(source="sowpods.txt",mimeType="application/octet-stream")] //Won't work too large.
  9. private static const DictionaryFile:Class;
  10.  
  11. private static var words:Array = new DictionaryFile().toString().split("n");
  12.  
  13. public function DictionaryCheck() {
  14. containsWord("AARDVARKS");
  15. }
  16.  
  17. public static function containsWord(word:String):* {
  18. trace(words[10]); //Traces "AARDVARKS" in both versions of file
  19. trace((words[10]) == word); // Traces true in shorter text file false in longer
  20. trace("Returning: " + (words.indexOf(word))); // traces Returning: 10 in smaller file
  21. if((words.indexOf(word)) > -1){
  22. trace("Yes!"); // traces "Yes" in shorter file not in longer
  23. }
  24. }
  25. }
  26. }
  27.  
  28. private static var dicts:Object;
  29.  
  30. public function createDicts() {
  31. var first_letter:String;
  32. for (i = 0; i < words.length; i++) {
  33. first_letter = words[i].charAt(0);
  34. if (dicts[first_letter] == undefined) {
  35. dicts[first_letter] = {}
  36. }
  37. dicts[first_letter][words[i]] = 'X';
  38. }
  39. }
  40.  
  41. public static function containsWord(word:String):Boolean {
  42. first_letter = words[i].charAt(0);
  43. if (dicts[first_letter]) {
  44. return dicts[first_letter][word] != undefined;
  45. }
  46. return false;
  47. }
  48.  
  49. public class DictSOWPODS {
  50. 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
  51.  
  52. public function containsWord(word : String) : Boolean {
  53. var result : Boolean = false;
  54. // perform the actual half-interval search here (please do not keep it this way)
  55. var indexFound : int = parsedDictionary.indexOf(word);
  56. result = (indexFound >= 0)
  57. // end of perform the actual half-interval search (please do not keep it this way)
  58. return result;
  59. }
  60. }
Add Comment
Please, Sign In to add comment