aboutsummaryrefslogtreecommitdiffstats
path: root/release/build/js/Words.js
blob: cbd9cab45cee76528f4b91050dfb7a193dacfb24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var Words = Collection.extend({
	constructor: function(script) {
		this.base();
		forEach (script.match(WORDS), this.add, this);
		this.encode();
	},
	
	add: function(word) {
		if (!this.exists(word)) this.base(word);
		word = this.fetch(word);
		word.count++;
		return word;
	},
	
	encode: function() {
		// sort by frequency
		this.sort(function(word1, word2) {
			return word2.count - word1.count;
		});
		
		eval("var a=62,e=" + Packer.ENCODE62);
		var encode = e;		
		var encoded = new Collection; // a dictionary of base62 -> base10
		var count = this.count();
		for (var i = 0; i < count; i++) {
			encoded.store(encode(i), i);
		}
		
		var empty = function() {return ""};
		var index = 0;
		forEach (this, function(word) {
			if (encoded.exists(word)) {
				word.index = encoded.fetch(word);
				word.toString = empty;
			} else {
				while (this.exists(encode(index))) index++;
				word.index = index++;
			}
			word.encoded = encode(word.index);
		}, this);
		
		// sort by encoding
		this.sort(function(word1, word2) {
			return word1.index - word2.index;
		});
	},
	
	toString: function() {
		return this.values().join("|");
	}
}, {
	Item: {
		constructor: function(word) {
			this.toString = function() {return word};
		},
		
		count: 0,
		encoded: "",
		index: -1
	}
});