~eda-qa/dhlib/main

« back to all changes in this revision

Viewing changes to app-words/words/Dictionary.mhx

  • 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
#if flash
 
9
import flash.utils.ByteArray;
 
10
#else
 
11
#end
 
12
 
 
13
class Dictionary
 
14
{
 
15
        public var encoder(default,null) : Encoder;
 
16
        
 
17
#if flash
 
18
        static public function withByteArray( data : ByteArray, encoder : Encoder ) : Dictionary
 
19
        {
 
20
                data.position =  0;
 
21
                
 
22
                //check magic cookie
 
23
                var magic = "";
 
24
                magic += String.fromCharCode( data.readByte() );
 
25
                magic += String.fromCharCode( data.readByte() );
 
26
                magic += String.fromCharCode( data.readByte() );
 
27
                magic += String.fromCharCode( data.readByte() );
 
28
                ASSERT( magic == "WRAW" );
 
29
                
 
30
                //get the length at beginning and end and compare
 
31
                var len = data.readInt();
 
32
                data.position += len;
 
33
                var elen = data.readInt();
 
34
                ASSERT( len == elen, "Data Sanity Check" );
 
35
                
 
36
                return new ByteArrayDictionary( data, 8, encoder );
 
37
        }
 
38
#end
 
39
 
 
40
        static public function withHash( data : Hash<Int>, encoder : Encoder ) : Dictionary
 
41
        {
 
42
                return new HashDictionary( data, encoder );
 
43
        }
 
44
        
 
45
        public function lookup( str : String ) : Int
 
46
        {
 
47
                Assert.pureVirtual();
 
48
                return 0;
 
49
        }
 
50
        
 
51
        public function getVocabLevels() : Int
 
52
        {
 
53
                return 4;       //TODO: base on actually loaded data with new dictionaries
 
54
        }
 
55
}
 
56
 
 
57
/**
 
58
 * This dictionary is not really efficient for word games and is used strictly
 
59
 * for testing.  Word lists should be compiled with "packwords" and the
 
60
 * ByteArray form used.
 
61
 */
 
62
private class HashDictionary extends Dictionary
 
63
{
 
64
        var data : Hash<Int>;
 
65
        
 
66
        public function new( data : Hash<Int>, encoder : Encoder ) 
 
67
        {
 
68
                this.data = data;
 
69
                this.encoder = encoder;
 
70
        }
 
71
        
 
72
        override public function lookup( str : String ) : Int
 
73
        {
 
74
                if( data.exists( str ) )
 
75
                        return data.get( str );
 
76
                        
 
77
                //otherwise we're force to iterate through and look for any prefix matches
 
78
                for( d in data.keys() )
 
79
                        if( StringTools.startsWith( d, str ) )
 
80
                                return -1;      //something on branch
 
81
                        
 
82
                return -2;      //branch empty
 
83
        }       
 
84
}
 
85
 
 
86
#if flash
 
87
private class ByteArrayDictionary extends Dictionary
 
88
{
 
89
        var data : ByteArray;
 
90
        var offset : Int;
 
91
        
 
92
        public function new( data : ByteArray, offset : Int, encoder : Encoder )
 
93
        {
 
94
                this.data = data;
 
95
                this.encoder = encoder;
 
96
                this.offset = offset;
 
97
        }
 
98
        
 
99
        
 
100
        /**
 
101
         * Lookup a word in the dictionary.
 
102
         *
 
103
         * @param str [in] item to look for, a full match
 
104
         * @return class [out] 
 
105
         *              -1 if not found but branch continues
 
106
         *              -2 if not found and branch terminates
 
107
         *              otherwise the dictionary set value of the word
 
108
         * @throws EncoderException if string cannot be encoded
 
109
         */
 
110
        override public function lookup( str : String ) : Int
 
111
        {
 
112
                var enc = encoder.encode( str );
 
113
                return findWord( enc, 0, offset );
 
114
        }
 
115
        
 
116
        function findWord( enc : Array<Int>, enc_at : Int, data_at : Int ) : Int
 
117
        {
 
118
                var header = data[data_at++];
 
119
                var terminal = header & 1 == 1;
 
120
                var len = data[data_at++];
 
121
                
 
122
                if( enc_at == enc.length )
 
123
                {
 
124
                        if( terminal )
 
125
                                return header >> 4;     //rank
 
126
                        return len==0 ? -2 : -1; //end, but no match
 
127
                }
 
128
                
 
129
                for( i in 0...len )
 
130
                {
 
131
                        var c = data[data_at++];
 
132
                        var off = data[data_at++];
 
133
                        off |= data[data_at++] << 8;
 
134
                        off |= data[data_at++] << 16;
 
135
                        
 
136
                        if( c != enc[enc_at] )
 
137
                                continue;
 
138
                        return findWord( enc, enc_at+1, off + offset ); 
 
139
                }
 
140
                
 
141
                //nothing  on this prefix found
 
142
                return -2;
 
143
        }
 
144
}
 
145
#end