|
| 1 | +#include "aarraylex.h" |
| 2 | +#include "acandidatewindows.h" |
| 3 | +#include <Windows.h> |
| 4 | +#include <sstream> |
| 5 | + |
| 6 | +AArrayLex::AArrayLex( wstring primary, wstring secondary, wstring simple, bool useSimpleCandidate, int nkey ) |
| 7 | + :state( 0 ), root( 0 ), backable( false ), parent( 0 ), simpleCodeReady( false ), nKey( 31 ) |
| 8 | +{ |
| 9 | + AArrayLex::useSimpleCandidate = useSimpleCandidate; |
| 10 | + |
| 11 | + nKey = nkey; |
| 12 | + |
| 13 | + // get module path |
| 14 | + |
| 15 | + wchar_t path[ 256 ]; |
| 16 | + int szPath; |
| 17 | + szPath = GetModuleFileNameW( ACandidateWindow::hInstance, path, 255 ); |
| 18 | + |
| 19 | + int l( 0 ); |
| 20 | + for( int i = 0; i < szPath; ++i ) |
| 21 | + if( path[i] == L'\\' ) |
| 22 | + l = i; |
| 23 | + |
| 24 | + path[ l + 1 ] = 0; |
| 25 | + |
| 26 | + mPath = path; |
| 27 | + |
| 28 | + // init root |
| 29 | + root = new struct Node; |
| 30 | + state = root; |
| 31 | + root->desc.resize( nKey ); |
| 32 | + //root->desc = new struct Node * [ nKey ]; |
| 33 | + |
| 34 | + for( int i = 0; i < nkey; ++i ) |
| 35 | + root->desc[ i ] = 0; |
| 36 | + |
| 37 | + if( primary != L"" ) tinit( mPath + primary, false ); |
| 38 | + if( secondary != L"" ) tinit( mPath + secondary, true ); |
| 39 | + if( simple != L"" ) |
| 40 | + { |
| 41 | + sinit( mPath + simple ); |
| 42 | + simpleCodeReady = true; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +AArrayLex::~AArrayLex() |
| 47 | +{ |
| 48 | + if( root ) |
| 49 | + tdelete( root ); |
| 50 | +} |
| 51 | + |
| 52 | +int AArrayLex::put( int asciikey ) |
| 53 | +{ |
| 54 | + // reset |
| 55 | + if( asciikey == 0 ) |
| 56 | + { |
| 57 | + state = root; |
| 58 | + parent = 0; |
| 59 | + backable = false; |
| 60 | + return ST_RESET; |
| 61 | + } |
| 62 | + |
| 63 | + struct Node * sdesc = state->desc[ getStrokeNumFromChar( asciikey ) ]; |
| 64 | + if( sdesc ) |
| 65 | + { |
| 66 | + parent = state; |
| 67 | + backable = true; |
| 68 | + state = sdesc; |
| 69 | + return ST_VALID; |
| 70 | + } |
| 71 | + else |
| 72 | + { |
| 73 | + //state = root; |
| 74 | + return ST_RESET; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +void AArrayLex::back() |
| 79 | +{ |
| 80 | + if( backable ) |
| 81 | + { |
| 82 | + state = parent; |
| 83 | + backable = false; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +vector<wchar_t> AArrayLex::getCandidates( wchar_t ascii ) |
| 88 | +{ |
| 89 | + vector<wchar_t> candidates = state->characters; |
| 90 | + |
| 91 | + if( simpleCodeReady && parent == root && ascii != 0 && simpleCode.find( ascii ) != simpleCode.end() ) |
| 92 | + for( int i = state->characters.size(); i < simpleCode[ ascii ].size(); ++i ) |
| 93 | + candidates.push_back( simpleCode[ ascii ].at( i ) ); |
| 94 | + |
| 95 | + else if( useSimpleCandidate ) |
| 96 | + for( int i = state->characters.size(); i < 10; ++i ) |
| 97 | + candidates.push_back( getOneChild( state, i + 1 ) ); |
| 98 | + |
| 99 | + return candidates; |
| 100 | +} |
| 101 | + |
| 102 | +wstring AArrayLex::getCandidateString( wchar_t ascii ) |
| 103 | +{ |
| 104 | + wstring cstring; |
| 105 | + |
| 106 | + vector<wchar_t> & canvec = getCandidates( ascii ); |
| 107 | + for( int i = 1; i <= canvec.size(); ++i ) |
| 108 | + { |
| 109 | + std::wstringstream ss; |
| 110 | + |
| 111 | + if( i == 10 ) |
| 112 | + ss << L"0" ; |
| 113 | + else |
| 114 | + ss << i; |
| 115 | + |
| 116 | + if( canvec.at( i - 1 ) == 0 ) |
| 117 | + cstring += ss.str() + L"□ "; |
| 118 | + else |
| 119 | + cstring += ss.str() + canvec.at( i - 1 ) + L" "; |
| 120 | + } |
| 121 | + |
| 122 | + return cstring; |
| 123 | +} |
| 124 | + |
| 125 | +wchar_t AArrayLex::getOneChild( struct Node * node, int n ) |
| 126 | +{ |
| 127 | + vector<wchar_t> c; |
| 128 | + if( n == 1 ) |
| 129 | + { |
| 130 | + c.push_back( 'Q' ); c.push_back( 'A' ); c.push_back( 'Z' ); |
| 131 | + } |
| 132 | + else if ( n == 2 ) |
| 133 | + { |
| 134 | + c.push_back( 'W' ); c.push_back( 'S' ); c.push_back( 'X' ); |
| 135 | + } |
| 136 | + else if ( n == 3 ) |
| 137 | + { |
| 138 | + c.push_back( 'E' ); c.push_back( 'D' ); c.push_back( 'C' ); |
| 139 | + } |
| 140 | + else if ( n == 4 ) |
| 141 | + { |
| 142 | + c.push_back( 'R' ); c.push_back( 'F' ); c.push_back( 'V' ); |
| 143 | + } |
| 144 | + else if ( n == 5 ) |
| 145 | + { |
| 146 | + c.push_back( 'T' ); c.push_back( 'G' ); c.push_back( 'B' ); |
| 147 | + } |
| 148 | + else if ( n == 6 ) |
| 149 | + { |
| 150 | + c.push_back( 'Y' ); c.push_back( 'H' ); c.push_back( 'N' ); |
| 151 | + } |
| 152 | + else if ( n == 7 ) |
| 153 | + { |
| 154 | + c.push_back( 'U' ); c.push_back( 'J' ); c.push_back( 'M' ); |
| 155 | + } |
| 156 | + else if ( n == 8 ) |
| 157 | + { |
| 158 | + c.push_back( 'I' ); c.push_back( 'K' ); c.push_back( ',' ); |
| 159 | + } |
| 160 | + else if ( n == 9 ) |
| 161 | + { |
| 162 | + c.push_back( 'O' ); c.push_back( 'L' ); c.push_back( '.' ); |
| 163 | + } |
| 164 | + else if ( n == 0 ) |
| 165 | + { |
| 166 | + c.push_back( 'P' ); c.push_back( ';' ); c.push_back( '/' ); |
| 167 | + } |
| 168 | + |
| 169 | + |
| 170 | + for( vector<wchar_t>::iterator i = c.begin(); i != c.end(); ++i ) |
| 171 | + { |
| 172 | + struct Node * sdesc = node->desc[ getStrokeNumFromChar( * i ) ]; |
| 173 | + if( sdesc && sdesc->characters.size() != 0 ) |
| 174 | + return sdesc->characters[0]; |
| 175 | + } |
| 176 | + return 0; |
| 177 | +} |
| 178 | + |
| 179 | +void AArrayLex::tinit( wstring & fi, bool ext ) |
| 180 | +{ |
| 181 | + FILE * fs; |
| 182 | + _wfopen_s( & fs, fi.c_str(), L"r" ); |
| 183 | + ifstream f( fs ); |
| 184 | + if( ! f ) |
| 185 | + { |
| 186 | + } |
| 187 | + |
| 188 | + put( 0 ); |
| 189 | + |
| 190 | + char s[256]; |
| 191 | + wchar_t ws[256]; |
| 192 | + int szws; |
| 193 | + while( f.getline( s, 255 ) ) |
| 194 | + { |
| 195 | + // szws include null terminator |
| 196 | + szws = MultiByteToWideChar( CP_UTF8, 0, s, -1, ws, 255 ); |
| 197 | + |
| 198 | + wstringstream ss; |
| 199 | + ss << ws; |
| 200 | + wstring stroke; wchar_t character; |
| 201 | + ss >> character >> stroke; |
| 202 | + |
| 203 | + if( ext ) |
| 204 | + stroke += L"I"; |
| 205 | + |
| 206 | + tmake( root, 0, stroke, character ); |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +void AArrayLex::sinit( wstring & fi ) |
| 211 | +{ |
| 212 | + FILE * fs; |
| 213 | + _wfopen_s( & fs, fi.c_str(), L"r" ); |
| 214 | + ifstream f( fs ); |
| 215 | + if( ! f ) |
| 216 | + { |
| 217 | + } |
| 218 | + |
| 219 | + char s[256]; |
| 220 | + wchar_t ws[256]; |
| 221 | + int szws; |
| 222 | + while( f.getline( s, 255 ) ) |
| 223 | + { |
| 224 | + // szws include null terminator |
| 225 | + szws = MultiByteToWideChar( CP_UTF8, 0, s, -1, ws, 255 ); |
| 226 | + |
| 227 | + wstringstream ss; |
| 228 | + ss << ws; |
| 229 | + wstring codestr; wchar_t code; |
| 230 | + ss >> code >> codestr; |
| 231 | + |
| 232 | + simpleCode.insert( make_pair( code, codestr ) ); |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +void AArrayLex::tmake( struct Node * node, int strokeIndex, const wstring & stroke, const wchar_t & ch ) |
| 237 | +{ |
| 238 | + if( strokeIndex == stroke.size() ) |
| 239 | + { |
| 240 | + node->characters.push_back( ch ); |
| 241 | + return; |
| 242 | + } |
| 243 | + |
| 244 | + int strokeNum = getStrokeNumFromChar( stroke.at( strokeIndex ) ); |
| 245 | + |
| 246 | + if( node->desc[ strokeNum ] == 0 ) |
| 247 | + { |
| 248 | + struct Node * child = new struct Node; |
| 249 | + //child->desc = new struct Node * [ nKey ]; |
| 250 | + child->desc.resize( nKey ); |
| 251 | + for( int i = 0; i < nKey; ++i ) |
| 252 | + child->desc[ i ] = 0; |
| 253 | + |
| 254 | + node->desc[ strokeNum ] = child; |
| 255 | + } |
| 256 | + |
| 257 | + tmake( node->desc[ strokeNum ], strokeIndex + 1, stroke, ch ); |
| 258 | +} |
| 259 | + |
| 260 | +void AArrayLex::tdelete( struct Node * node ) |
| 261 | +{ |
| 262 | + if( node == 0 ) |
| 263 | + node = root; |
| 264 | + |
| 265 | + for( int i = 0; i < nKey; ++i ) |
| 266 | + if( node->desc[ i ] != 0 ) |
| 267 | + { |
| 268 | + tdelete( node->desc[ i ] ); |
| 269 | + delete node->desc[ i ]; |
| 270 | + } |
| 271 | + |
| 272 | + //delete node->desc; |
| 273 | +} |
| 274 | + |
| 275 | +int AArrayLex::getStrokeNumFromChar( wchar_t c ) |
| 276 | +{ |
| 277 | + if( nKey == 10 ) |
| 278 | + { |
| 279 | + return c - 48; |
| 280 | + } |
| 281 | + |
| 282 | + else |
| 283 | + { |
| 284 | + if( c == 44 ) // ` |
| 285 | + return 0; |
| 286 | + else if( c == 46 ) // . |
| 287 | + return 1; |
| 288 | + else if( c == 47 ) // / |
| 289 | + return 2; |
| 290 | + else if( c == 59 ) // ; |
| 291 | + return 3; |
| 292 | + else if( c >= 65 && c <= 90 ) // A-Z |
| 293 | + return c - 61; |
| 294 | + else if( c == 96 ) // ` |
| 295 | + return 30; |
| 296 | + else if( c >= 48 && c <= 57 ) // 0-9 |
| 297 | + return c - 17; |
| 298 | + else |
| 299 | + return -1; |
| 300 | + } |
| 301 | +} |
| 302 | + |
| 303 | +wchar_t AArrayLex::getStrokeCharFromNum( int n ) |
| 304 | +{ |
| 305 | + if( nKey == 10 ) |
| 306 | + { |
| 307 | + return n - 48; |
| 308 | + } |
| 309 | + |
| 310 | + else |
| 311 | + { |
| 312 | + if( n == 0 ) |
| 313 | + return 44; |
| 314 | + else if( n == 1 ) |
| 315 | + return 46; |
| 316 | + else if( n == 2 ) |
| 317 | + return 47; |
| 318 | + else if( n == 3 ) |
| 319 | + return 59; |
| 320 | + else if( n > 3 && n < 30 ) // A-Z |
| 321 | + return n + 61; |
| 322 | + else if( n == 40 ) // ` |
| 323 | + return 96; |
| 324 | + else if( n >= 31 && n <= 40 ) // 0-9 |
| 325 | + return n + 17; |
| 326 | + else |
| 327 | + return -1; |
| 328 | + } |
| 329 | +} |
0 commit comments