--- /dev/null
+\r
+var Words = Collection.extend({\r
+ constructor: function(script) {\r
+ this.base();\r
+ forEach (script.match(WORDS), this.add, this);\r
+ this.encode();\r
+ },\r
+ \r
+ add: function(word) {\r
+ if (!this.exists(word)) this.base(word);\r
+ word = this.fetch(word);\r
+ word.count++;\r
+ return word;\r
+ },\r
+ \r
+ encode: function() {\r
+ // sort by frequency\r
+ this.sort(function(word1, word2) {\r
+ return word2.count - word1.count;\r
+ });\r
+ \r
+ eval("var a=62,e=" + Packer.ENCODE62);\r
+ var encode = e; \r
+ var encoded = new Collection; // a dictionary of base62 -> base10\r
+ var count = this.count();\r
+ for (var i = 0; i < count; i++) {\r
+ encoded.store(encode(i), i);\r
+ }\r
+ \r
+ var empty = function() {return ""};\r
+ var index = 0;\r
+ forEach (this, function(word) {\r
+ if (encoded.exists(word)) {\r
+ word.index = encoded.fetch(word);\r
+ word.toString = empty;\r
+ } else {\r
+ while (this.exists(encode(index))) index++;\r
+ word.index = index++;\r
+ }\r
+ word.encoded = encode(word.index);\r
+ }, this);\r
+ \r
+ // sort by encoding\r
+ this.sort(function(word1, word2) {\r
+ return word1.index - word2.index;\r
+ });\r
+ },\r
+ \r
+ toString: function() {\r
+ return this.values().join("|");\r
+ }\r
+}, {\r
+ Item: {\r
+ constructor: function(word) {\r
+ this.toString = function() {return word};\r
+ },\r
+ \r
+ count: 0,\r
+ encoded: "",\r
+ index: -1\r
+ }\r
+});\r