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 | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 22 | #include "unigram_dictionary.h" |
| 23 | |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 24 | namespace latinime { |
| 25 | |
| 26 | class Dictionary { |
| 27 | public: |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 28 | Dictionary(void *dict, int typedLetterMultipler, int fullWordMultiplier, int maxWordLength, |
| 29 | int maxWords, int maxAlternatives); |
Amith Yamasani | cc3e5c7 | 2009-03-31 10:51:17 -0700 | [diff] [blame] | 30 | int getSuggestions(int *codes, int codesSize, unsigned short *outWords, int *frequencies, |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 31 | int *nextLetters, int nextLettersSize) { |
| 32 | return mUnigramDictionary->getSuggestions(codes, codesSize, outWords, frequencies, |
| 33 | nextLetters, nextLettersSize); |
| 34 | } |
| 35 | |
| 36 | // TODO: Call mBigramDictionary instead of mUnigramDictionary |
Jae Yong Sung | 80aa14f | 2010-07-26 11:43:29 -0700 | [diff] [blame] | 37 | int getBigrams(unsigned short *word, int length, int *codes, int codesSize, |
| 38 | unsigned short *outWords, int *frequencies, int maxWordLength, int maxBigrams, |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 39 | int maxAlternatives) { |
| 40 | return mUnigramDictionary->getBigrams(word, length, codes, codesSize, outWords, frequencies, |
| 41 | maxWordLength, maxBigrams, maxAlternatives); |
| 42 | } |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame^] | 43 | bool isValidWord(unsigned short *word, int length); |
| 44 | int isValidWordRec(int pos, unsigned short *word, int offset, int length); |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 45 | void setAsset(void *asset) { mAsset = asset; } |
| 46 | void *getAsset() { return mAsset; } |
| 47 | ~Dictionary(); |
Amith Yamasani | cc3e5c7 | 2009-03-31 10:51:17 -0700 | [diff] [blame] | 48 | |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame^] | 49 | // public static utility methods |
| 50 | // static inline methods should be defined in the header file |
| 51 | static unsigned short getChar(const unsigned char *dict, int *pos); |
| 52 | static int getCount(const unsigned char *dict, int *pos); |
| 53 | static bool getTerminal(const unsigned char *dict, int *pos); |
| 54 | static int getAddress(const unsigned char *dict, int *pos); |
| 55 | static int getFreq(const unsigned char *dict, const bool isLatestDictVersion, int *pos); |
| 56 | |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 57 | private: |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame^] | 58 | bool hasBigram(); |
| 59 | |
| 60 | const unsigned char *DICT; |
| 61 | const bool IS_LATEST_DICT_VERSION; |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 62 | void *mAsset; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 63 | BigramDictionary *mBigramDictionary; |
| 64 | UnigramDictionary *mUnigramDictionary; |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | // ---------------------------------------------------------------------------- |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame^] | 68 | // public static utility methods |
| 69 | // static inline methods should be defined in the header file |
| 70 | inline unsigned short Dictionary::getChar(const unsigned char *dict, int *pos) { |
| 71 | unsigned short ch = (unsigned short) (dict[(*pos)++] & 0xFF); |
| 72 | // If the code is 255, then actual 16 bit code follows (in big endian) |
| 73 | if (ch == 0xFF) { |
| 74 | ch = ((dict[*pos] & 0xFF) << 8) | (dict[*pos + 1] & 0xFF); |
| 75 | (*pos) += 2; |
| 76 | } |
| 77 | return ch; |
| 78 | } |
| 79 | |
| 80 | inline int Dictionary::getCount(const unsigned char *dict, int *pos) { |
| 81 | return dict[(*pos)++] & 0xFF; |
| 82 | } |
| 83 | |
| 84 | inline bool Dictionary::getTerminal(const unsigned char *dict, int *pos) { |
| 85 | return (dict[*pos] & FLAG_TERMINAL_MASK) > 0; |
| 86 | } |
| 87 | |
| 88 | inline int Dictionary::getAddress(const unsigned char *dict, int *pos) { |
| 89 | int address = 0; |
| 90 | if ((dict[*pos] & FLAG_ADDRESS_MASK) == 0) { |
| 91 | *pos += 1; |
| 92 | } else { |
| 93 | address += (dict[*pos] & (ADDRESS_MASK >> 16)) << 16; |
| 94 | address += (dict[*pos + 1] & 0xFF) << 8; |
| 95 | address += (dict[*pos + 2] & 0xFF); |
| 96 | *pos += 3; |
| 97 | } |
| 98 | return address; |
| 99 | } |
| 100 | |
| 101 | inline int Dictionary::getFreq(const unsigned char *dict, |
| 102 | const bool isLatestDictVersion, int *pos) { |
| 103 | int freq = dict[(*pos)++] & 0xFF; |
| 104 | if (isLatestDictVersion) { |
| 105 | // skipping bigram |
| 106 | int bigramExist = (dict[*pos] & FLAG_BIGRAM_READ); |
| 107 | if (bigramExist > 0) { |
| 108 | int nextBigramExist = 1; |
| 109 | while (nextBigramExist > 0) { |
| 110 | (*pos) += 3; |
| 111 | nextBigramExist = (dict[(*pos)++] & FLAG_BIGRAM_CONTINUED); |
| 112 | } |
| 113 | } else { |
| 114 | (*pos)++; |
| 115 | } |
| 116 | } |
| 117 | return freq; |
| 118 | } |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 119 | |
| 120 | }; // namespace latinime |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 121 | #endif // LATINIME_DICTIONARY_H |