The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 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 | |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame^] | 20 | #include "basechars.h" |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 21 | #include "bigram_dictionary.h" |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame^] | 22 | #include "char_utils.h" |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 23 | #include "defines.h" |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 24 | #include "proximity_info.h" |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 25 | #include "unigram_dictionary.h" |
| 26 | |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 27 | namespace latinime { |
| 28 | |
| 29 | class Dictionary { |
| 30 | public: |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 31 | Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust, int typedLetterMultipler, |
| 32 | int fullWordMultiplier, int maxWordLength, int maxWords, int maxAlternatives); |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 33 | int getSuggestions(ProximityInfo *proximityInfo, int *xcoordinates, int *ycoordinates, |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 34 | int *codes, int codesSize, int flags, unsigned short *outWords, int *frequencies) { |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 35 | return mUnigramDictionary->getSuggestions(proximityInfo, xcoordinates, ycoordinates, codes, |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 36 | codesSize, flags, outWords, frequencies); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // TODO: Call mBigramDictionary instead of mUnigramDictionary |
Jae Yong Sung | 80aa14f | 2010-07-26 11:43:29 -0700 | [diff] [blame] | 40 | int getBigrams(unsigned short *word, int length, int *codes, int codesSize, |
| 41 | unsigned short *outWords, int *frequencies, int maxWordLength, int maxBigrams, |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 42 | int maxAlternatives) { |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 43 | return mBigramDictionary->getBigrams(word, length, codes, codesSize, outWords, frequencies, |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 44 | maxWordLength, maxBigrams, maxAlternatives); |
| 45 | } |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 46 | |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 47 | bool isValidWord(unsigned short *word, int length); |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 48 | void *getDict() { return (void *)mDict; } |
| 49 | int getDictSize() { return mDictSize; } |
| 50 | int getMmapFd() { return mMmapFd; } |
| 51 | int getDictBufAdjust() { return mDictBufAdjust; } |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 52 | ~Dictionary(); |
Amith Yamasani | cc3e5c7 | 2009-03-31 10:51:17 -0700 | [diff] [blame] | 53 | |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 54 | // public static utility methods |
| 55 | // static inline methods should be defined in the header file |
| 56 | static unsigned short getChar(const unsigned char *dict, int *pos); |
| 57 | static int getCount(const unsigned char *dict, int *pos); |
| 58 | static bool getTerminal(const unsigned char *dict, int *pos); |
| 59 | static int getAddress(const unsigned char *dict, int *pos); |
| 60 | static int getFreq(const unsigned char *dict, const bool isLatestDictVersion, int *pos); |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 61 | static int wideStrLen(unsigned short *str); |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 62 | // returns next sibling's position |
| 63 | static int setDictionaryValues(const unsigned char *dict, const bool isLatestDictVersion, |
| 64 | const int pos, unsigned short *c, int *childrenPosition, |
| 65 | bool *terminal, int *freq); |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame^] | 66 | static inline unsigned short toBaseLowerCase(unsigned short c); |
Jean Chalard | 581335c | 2011-06-17 12:45:17 +0900 | [diff] [blame] | 67 | // TODO: delete this |
| 68 | int getBigramPosition(unsigned short *word, int length); |
| 69 | |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 70 | private: |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 71 | bool hasBigram(); |
| 72 | |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 73 | const unsigned char *mDict; |
| 74 | |
| 75 | // Used only for the mmap version of dictionary loading, but we use these as dummy variables |
| 76 | // also for the malloc version. |
| 77 | const int mDictSize; |
| 78 | const int mMmapFd; |
| 79 | const int mDictBufAdjust; |
| 80 | |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 81 | const bool IS_LATEST_DICT_VERSION; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 82 | UnigramDictionary *mUnigramDictionary; |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 83 | BigramDictionary *mBigramDictionary; |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 86 | // public static utility methods |
| 87 | // static inline methods should be defined in the header file |
| 88 | inline unsigned short Dictionary::getChar(const unsigned char *dict, int *pos) { |
| 89 | unsigned short ch = (unsigned short) (dict[(*pos)++] & 0xFF); |
| 90 | // If the code is 255, then actual 16 bit code follows (in big endian) |
| 91 | if (ch == 0xFF) { |
| 92 | ch = ((dict[*pos] & 0xFF) << 8) | (dict[*pos + 1] & 0xFF); |
| 93 | (*pos) += 2; |
| 94 | } |
| 95 | return ch; |
| 96 | } |
| 97 | |
| 98 | inline int Dictionary::getCount(const unsigned char *dict, int *pos) { |
| 99 | return dict[(*pos)++] & 0xFF; |
| 100 | } |
| 101 | |
| 102 | inline bool Dictionary::getTerminal(const unsigned char *dict, int *pos) { |
| 103 | return (dict[*pos] & FLAG_TERMINAL_MASK) > 0; |
| 104 | } |
| 105 | |
| 106 | inline int Dictionary::getAddress(const unsigned char *dict, int *pos) { |
| 107 | int address = 0; |
| 108 | if ((dict[*pos] & FLAG_ADDRESS_MASK) == 0) { |
| 109 | *pos += 1; |
| 110 | } else { |
| 111 | address += (dict[*pos] & (ADDRESS_MASK >> 16)) << 16; |
| 112 | address += (dict[*pos + 1] & 0xFF) << 8; |
| 113 | address += (dict[*pos + 2] & 0xFF); |
| 114 | *pos += 3; |
| 115 | } |
| 116 | return address; |
| 117 | } |
| 118 | |
| 119 | inline int Dictionary::getFreq(const unsigned char *dict, |
| 120 | const bool isLatestDictVersion, int *pos) { |
| 121 | int freq = dict[(*pos)++] & 0xFF; |
| 122 | if (isLatestDictVersion) { |
| 123 | // skipping bigram |
| 124 | int bigramExist = (dict[*pos] & FLAG_BIGRAM_READ); |
| 125 | if (bigramExist > 0) { |
| 126 | int nextBigramExist = 1; |
| 127 | while (nextBigramExist > 0) { |
| 128 | (*pos) += 3; |
| 129 | nextBigramExist = (dict[(*pos)++] & FLAG_BIGRAM_CONTINUED); |
| 130 | } |
| 131 | } else { |
| 132 | (*pos)++; |
| 133 | } |
| 134 | } |
| 135 | return freq; |
| 136 | } |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 137 | |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 138 | inline int Dictionary::wideStrLen(unsigned short *str) { |
| 139 | if (!str) return 0; |
| 140 | unsigned short *end = str; |
| 141 | while (*end) |
| 142 | end++; |
| 143 | return end - str; |
| 144 | } |
| 145 | |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 146 | inline int Dictionary::setDictionaryValues(const unsigned char *dict, |
| 147 | const bool isLatestDictVersion, const int pos, unsigned short *c,int *childrenPosition, |
| 148 | bool *terminal, int *freq) { |
| 149 | int position = pos; |
| 150 | // -- at char |
| 151 | *c = Dictionary::getChar(dict, &position); |
| 152 | // -- at flag/add |
| 153 | *terminal = Dictionary::getTerminal(dict, &position); |
| 154 | *childrenPosition = Dictionary::getAddress(dict, &position); |
| 155 | // -- after address or flag |
| 156 | *freq = (*terminal) ? Dictionary::getFreq(dict, isLatestDictVersion, &position) : 1; |
| 157 | // returns next sibling's position |
| 158 | return position; |
| 159 | } |
| 160 | |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame^] | 161 | |
| 162 | inline unsigned short Dictionary::toBaseLowerCase(unsigned short c) { |
| 163 | if (c < sizeof(BASE_CHARS) / sizeof(BASE_CHARS[0])) { |
| 164 | c = BASE_CHARS[c]; |
| 165 | } |
| 166 | if (c >='A' && c <= 'Z') { |
| 167 | c |= 32; |
| 168 | } else if (c > 127) { |
| 169 | c = latin_tolower(c); |
| 170 | } |
| 171 | return c; |
| 172 | } |
| 173 | |
Ken Wakasa | ce9e52a | 2011-06-18 13:09:55 +0900 | [diff] [blame] | 174 | } // namespace latinime |
| 175 | |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 176 | #endif // LATINIME_DICTIONARY_H |