Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Dictionary : ZilchComponent
- {
- // --- ! DICTIONARY STUFF ! --//
- //
- // FUNCTIONS:
- // IsWord(word: String) : Boolean
- // Checks if a string is a word. Ignores case on both input word and dictionary.
- //
- // IsCaseWord(word : String) : Boolean
- // Checks if a string is a word. Checks case on both input word and dictionary.
- //
- // AddWord(word : String)
- // Adds a word to the word list.
- [Property] var WordList : TextBlock = null; // The TextBlock to generate words
- [Property] var MaxLength : Integer = 13; // The maximum length of a given word
- var Words : Array[Array[Array[String]]] = null; // All words sorted
- var Master : String = " "; // Master of all words
- function Initialize(init : CogInitializer) {
- // --- ! INITIALIZING VARIABLES ! --- //
- this.Words = Array[Array[Array[String]]]();
- for(var i = 0; i < 26; i += 1) { this.Words.Add(this.InitArray()); }
- this.Master = this.WordList.Text;
- this.PopulateWords();
- }
- function IsCaseWord(word : String) : Boolean {
- var toCheck = this.Words.Get(this.ToIndex(word.Get(0))).Get(word.ByteCount);
- for(var i = 0; i < toCheck.Count; i += 1) {
- if(word == toCheck.Get(i)) {
- return true; } }
- return false;
- }
- function IsWord(word : String) : Boolean {
- var toCheck = this.Words.Get(this.ToIndex(word.Get(0))).Get(word.ByteCount);
- for(var i = 0; i < toCheck.Count; i += 1) {
- if(this.StrComp(word, toCheck.Get(i))) {
- return true; } }
- return false;
- }
- function PopulateWords() {
- var index = 0; // Current index
- while(index != -1) { // While not complete
- index = this.FindNextWord(index); } // Find the next word
- }
- function FindNextWord(i : Integer) : Integer {
- var l = 0; // Length of the word
- timeout(100) { // Increase the timeout so the operation can finish
- for(l; this.Master.Get(i + l) != 13; l += 1) { // Until a newline is hit
- if(i + l + 1 > this.Master.ByteCount - 1) { // If the index is outside the master
- return -1; } } } // Return and end
- var w = this.Master.SubString(i + 1, l - 1); // The substring clipped to correct size
- if(w.ByteCount > this.MaxLength) { Console.Write(w); Console.WriteLine("WAS TOO LONG AND WAS NOT ADDED"); }
- else { this.Words.Get(this.ToIndex(w.Get(0))).Get(l - 1).Add(w); } // Add the substring to the correct location
- return i + l + 1; // Return the current index past the newline
- }
- function StrComp(l : String, r : String) : Boolean {
- if(l.ByteCount != r.ByteCount) { return false; }
- for(var i = 0; i < l.ByteCount; i += 1) {
- if(this.ToUpper(l.Get(i)) != this.ToUpper(r.Get(i))) {
- return false; } }
- return true;
- }
- function AddWord(s : String) {
- if(s.ByteCount > this.MaxLength) { Console.Write(s); Console.WriteLine(" WAS TOO LONG AND WAS NOT ADDED"); }
- this.Words.Get(this.ToIndex(s.Get(0))).Get(s.ByteCount).Add(s); // Add the substring to the correct location
- }
- function ToIndex(i : Integer) : Integer { return this.ToUpper(i) - 65; }
- function ToUpper(i : Integer) : Integer { if(i > 90) { return i - 32; } return i; }
- function InitArray() : Array[Array[String]]
- { var a = Array[Array[String]](); for(var i = 0; i < this.MaxLength + 1; i += 1) { a.Add(Array[String]()); } return a; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement