~eda-qa/dhlib/main

« back to all changes in this revision

Viewing changes to app-words/words/Encoder_EN.hx

  • Committer: edA-qa mort-ora-y
  • Date: 2010-02-16 05:36:32 UTC
  • Revision ID: eda-qa@disemia.com-20100216053632-60lt7fndfi3fgblw
first

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* <license>
 
2
 * This file is part of the dis-Emi-A HaXe Library. Copyright © edA-qa mort-ora-y
 
3
 * For full copyright and license information please refer to doc/license.txt.
 
4
 * </license> 
 
5
 */
 
6
package words;
 
7
 
 
8
/**
 
9
 * An English locale encoder
 
10
 */
 
11
class Encoder_EN implements Encoder
 
12
{
 
13
        inline static var SKIPCHAR : Int = -1;
 
14
        inline static var BADCHAR : Int = -1;
 
15
        
 
16
        inline static var charsetSize : Int = 27;
 
17
        
 
18
        public function new() 
 
19
        {
 
20
        }
 
21
        
 
22
        inline function encodeChar( char : String ) : Int
 
23
        {
 
24
                var ret;        //do this way to allow inlining
 
25
                
 
26
                var raw = char.toLowerCase().charCodeAt( 0 );
 
27
                raw -= 'a'.charCodeAt( 0 );
 
28
                if( raw < 0 )
 
29
                        ret = BADCHAR;
 
30
                else 
 
31
                {
 
32
                        raw += 1;
 
33
                        if( raw >= charsetSize )
 
34
                                ret = BADCHAR;
 
35
                        else
 
36
                                ret = raw;
 
37
                }
 
38
                        
 
39
                return ret;
 
40
        }
 
41
        
 
42
        public function encode( str : String ) : Array<Int>
 
43
        {
 
44
                var word = new Array<Int>();
 
45
                
 
46
                for( i in 0...str.length )
 
47
                {
 
48
                        var c = encodeChar( str.charAt( i ) );
 
49
                        if( c == SKIPCHAR )
 
50
                                continue;
 
51
                        if( c == BADCHAR )
 
52
                                throw new EncoderException( "Cannot encode character: " + str.charAt(i) );
 
53
                                
 
54
                        word.push( c );
 
55
                }
 
56
                
 
57
                if( word.length == 0 )
 
58
                        throw new EncoderException( "Resulting word is 0 length: " + str );
 
59
                        
 
60
                return word;
 
61
        }
 
62
        
 
63
        inline function decodeChar( val : Int ) : String
 
64
        {
 
65
                return String.fromCharCode( val + 'a'.charCodeAt(0) - 1 );
 
66
        }
 
67
        
 
68
        public function decode( str : Array<Int> ) : String
 
69
        {
 
70
                var ret = "";
 
71
                for( q in str )
 
72
                        ret += decodeChar( q );
 
73
                        
 
74
                return ret;
 
75
        }
 
76
        
 
77
        public function getSeqDistrib() : Array<Array<Dynamic>>
 
78
        {
 
79
                return [
 
80
                                ["e",58434],
 
81
                                ["s",45630],
 
82
                                ["i",43152],
 
83
                                ["a",36442],
 
84
                                ["r",36112],
 
85
                                ["n",35722],
 
86
                                ["t",35038],
 
87
                                ["o",29558],
 
88
                                ["l",25473],
 
89
                                ["c",20440],
 
90
                                ["d",20230],
 
91
                                ["u",16972],
 
92
                                ["g",16065],
 
93
                                ["p",14787],
 
94
                                ["m",13172],
 
95
                                ["h",11218],
 
96
                                ["b",9639],
 
97
                                ["y",7550],
 
98
                                ["f",7282],
 
99
                                ["v",5064],
 
100
                                ["k",4779],
 
101
                                ["w",4707],
 
102
                                ["x",1388],
 
103
                                ["qu",951],
 
104
                                ["j",908],
 
105
                                ["z",896]
 
106
                        ];
 
107
        }
 
108
}