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