1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef LATINIME_DICTIONARY_H 18 #define LATINIME_DICTIONARY_H 19 20 namespace latinime { 21 22 // 22-bit address = ~4MB dictionary size limit, which on average would be about 200k-300k words 23 #define ADDRESS_MASK 0x3FFFFF 24 25 // The bit that decides if an address follows in the next 22 bits 26 #define FLAG_ADDRESS_MASK 0x40 27 // The bit that decides if this is a terminal node for a word. The node could still have children, 28 // if the word has other endings. 29 #define FLAG_TERMINAL_MASK 0x80 30 31 class Dictionary { 32 public: 33 Dictionary(void *dict, int typedLetterMultipler, int fullWordMultiplier); 34 int getSuggestions(int *codes, int codesSize, unsigned short *outWords, int *frequencies, 35 int maxWordLength, int maxWords, int maxAlternatives, int skipPos, 36 int *nextLetters, int nextLettersSize); 37 bool isValidWord(unsigned short *word, int length); 38 void setAsset(void *asset) { mAsset = asset; } 39 void *getAsset() { return mAsset; } 40 ~Dictionary(); 41 42 private: 43 44 int getAddress(int *pos); 45 bool getTerminal(int *pos) { return (mDict[*pos] & FLAG_TERMINAL_MASK) > 0; } 46 int getFreq(int *pos) { return mDict[(*pos)++] & 0xFF; } 47 int getCount(int *pos) { return mDict[(*pos)++] & 0xFF; } 48 unsigned short getChar(int *pos); 49 int wideStrLen(unsigned short *str); 50 51 bool sameAsTyped(unsigned short *word, int length); 52 bool addWord(unsigned short *word, int length, int frequency); 53 unsigned short toLowerCase(unsigned short c); 54 void getWordsRec(int pos, int depth, int maxDepth, bool completion, int frequency, 55 int inputIndex, int diffs); 56 bool isValidWordRec(int pos, unsigned short *word, int offset, int length); 57 void registerNextLetter(unsigned short c); 58 59 unsigned char *mDict; 60 void *mAsset; 61 62 int *mFrequencies; 63 int mMaxWords; 64 int mMaxWordLength; 65 unsigned short *mOutputChars; 66 int *mInputCodes; 67 int mInputLength; 68 int mMaxAlternatives; 69 unsigned short mWord[128]; 70 int mSkipPos; 71 int mMaxEditDistance; 72 73 int mFullWordMultiplier; 74 int mTypedLetterMultiplier; 75 int *mNextLettersFrequencies; 76 int mNextLettersSize; 77 }; 78 79 // ---------------------------------------------------------------------------- 80 81 }; // namespace latinime 82 83 #endif // LATINIME_DICTIONARY_H 84