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.
9
import flash.utils.ByteArray;
15
public var encoder(default,null) : Encoder;
18
static public function withByteArray( data : ByteArray, encoder : Encoder ) : Dictionary
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" );
30
//get the length at beginning and end and compare
31
var len = data.readInt();
33
var elen = data.readInt();
34
ASSERT( len == elen, "Data Sanity Check" );
36
return new ByteArrayDictionary( data, 8, encoder );
40
static public function withHash( data : Hash<Int>, encoder : Encoder ) : Dictionary
42
return new HashDictionary( data, encoder );
45
public function lookup( str : String ) : Int
51
public function getVocabLevels() : Int
53
return 4; //TODO: base on actually loaded data with new dictionaries
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.
62
private class HashDictionary extends Dictionary
66
public function new( data : Hash<Int>, encoder : Encoder )
69
this.encoder = encoder;
72
override public function lookup( str : String ) : Int
74
if( data.exists( str ) )
75
return data.get( str );
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
82
return -2; //branch empty
87
private class ByteArrayDictionary extends Dictionary
92
public function new( data : ByteArray, offset : Int, encoder : Encoder )
95
this.encoder = encoder;
101
* Lookup a word in the dictionary.
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
110
override public function lookup( str : String ) : Int
112
var enc = encoder.encode( str );
113
return findWord( enc, 0, offset );
116
function findWord( enc : Array<Int>, enc_at : Int, data_at : Int ) : Int
118
var header = data[data_at++];
119
var terminal = header & 1 == 1;
120
var len = data[data_at++];
122
if( enc_at == enc.length )
125
return header >> 4; //rank
126
return len==0 ? -2 : -1; //end, but no match
131
var c = data[data_at++];
132
var off = data[data_at++];
133
off |= data[data_at++] << 8;
134
off |= data[data_at++] << 16;
136
if( c != enc[enc_at] )
138
return findWord( enc, enc_at+1, off + offset );
141
//nothing on this prefix found