Advertisement
Guest User

Untitled

a guest
Mar 1st, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. class Dictionary : ZilchComponent
  2. {
  3. // --- ! DICTIONARY STUFF ! --//
  4. //
  5. // FUNCTIONS:
  6. // IsWord(word: String) : Boolean
  7. // Checks if a string is a word. Ignores case on both input word and dictionary.
  8. //
  9. // IsCaseWord(word : String) : Boolean
  10. // Checks if a string is a word. Checks case on both input word and dictionary.
  11. //
  12. // AddWord(word : String)
  13. // Adds a word to the word list.
  14.  
  15.  
  16. [Property] var WordList : TextBlock = null; // The TextBlock to generate words
  17. [Property] var MaxLength : Integer = 13; // The maximum length of a given word
  18.  
  19. var Words : Array[Array[Array[String]]] = null; // All words sorted
  20. var Master : String = " "; // Master of all words
  21.  
  22.  
  23. function Initialize(init : CogInitializer) {
  24. // --- ! INITIALIZING VARIABLES ! --- //
  25. this.Words = Array[Array[Array[String]]]();
  26. for(var i = 0; i < 26; i += 1) { this.Words.Add(this.InitArray()); }
  27. this.Master = this.WordList.Text;
  28. this.PopulateWords();
  29. }
  30.  
  31. function IsCaseWord(word : String) : Boolean {
  32. var toCheck = this.Words.Get(this.ToIndex(word.Get(0))).Get(word.ByteCount);
  33. for(var i = 0; i < toCheck.Count; i += 1) {
  34. if(word == toCheck.Get(i)) {
  35. return true; } }
  36. return false;
  37. }
  38.  
  39. function IsWord(word : String) : Boolean {
  40. var toCheck = this.Words.Get(this.ToIndex(word.Get(0))).Get(word.ByteCount);
  41. for(var i = 0; i < toCheck.Count; i += 1) {
  42. if(this.StrComp(word, toCheck.Get(i))) {
  43. return true; } }
  44. return false;
  45. }
  46.  
  47. function PopulateWords() {
  48. var index = 0; // Current index
  49. while(index != -1) { // While not complete
  50. index = this.FindNextWord(index); } // Find the next word
  51. }
  52.  
  53. function FindNextWord(i : Integer) : Integer {
  54. var l = 0; // Length of the word
  55. timeout(100) { // Increase the timeout so the operation can finish
  56. for(l; this.Master.Get(i + l) != 13; l += 1) { // Until a newline is hit
  57. if(i + l + 1 > this.Master.ByteCount - 1) { // If the index is outside the master
  58. return -1; } } } // Return and end
  59. var w = this.Master.SubString(i + 1, l - 1); // The substring clipped to correct size
  60. if(w.ByteCount > this.MaxLength) { Console.Write(w); Console.WriteLine("WAS TOO LONG AND WAS NOT ADDED"); }
  61. else { this.Words.Get(this.ToIndex(w.Get(0))).Get(l - 1).Add(w); } // Add the substring to the correct location
  62. return i + l + 1; // Return the current index past the newline
  63. }
  64.  
  65. function StrComp(l : String, r : String) : Boolean {
  66. if(l.ByteCount != r.ByteCount) { return false; }
  67. for(var i = 0; i < l.ByteCount; i += 1) {
  68. if(this.ToUpper(l.Get(i)) != this.ToUpper(r.Get(i))) {
  69. return false; } }
  70. return true;
  71. }
  72.  
  73. function AddWord(s : String) {
  74. if(s.ByteCount > this.MaxLength) { Console.Write(s); Console.WriteLine(" WAS TOO LONG AND WAS NOT ADDED"); }
  75. this.Words.Get(this.ToIndex(s.Get(0))).Get(s.ByteCount).Add(s); // Add the substring to the correct location
  76. }
  77.  
  78. function ToIndex(i : Integer) : Integer { return this.ToUpper(i) - 65; }
  79. function ToUpper(i : Integer) : Integer { if(i > 90) { return i - 32; } return i; }
  80. function InitArray() : Array[Array[String]]
  81. { var a = Array[Array[String]](); for(var i = 0; i < this.MaxLength + 1; i += 1) { a.Add(Array[String]()); } return a; }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement