The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2009, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <stdio.h> |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 19 | |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame^] | 20 | #define LOG_TAG "LatinIME: dictionary.cpp" |
| 21 | |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 22 | #include "dictionary.h" |
satok | d4952c8 | 2010-12-01 19:09:29 +0900 | [diff] [blame] | 23 | |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 24 | namespace latinime { |
| 25 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 26 | Dictionary::Dictionary(void *dict, int typedLetterMultiplier, int fullWordMultiplier, |
| 27 | int maxWordLength, int maxWords, int maxAlternatives) |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame^] | 28 | : DICT((unsigned char*) dict), |
| 29 | // Checks whether it has the latest dictionary or the old dictionary |
| 30 | IS_LATEST_DICT_VERSION((((unsigned char*) dict)[0] & 0xFF) >= DICTIONARY_VERSION_MIN) |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 31 | { |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame^] | 32 | LOGI("IN NATIVE SUGGEST Version: %d \n", (DICT[0] & 0xFF)); |
| 33 | mUnigramDictionary = new UnigramDictionary(DICT, typedLetterMultiplier, fullWordMultiplier, |
| 34 | maxWordLength, maxWords, maxAlternatives, IS_LATEST_DICT_VERSION, |
| 35 | hasBigram(), this); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 36 | mBigramDictionary = new BigramDictionary(dict, typedLetterMultiplier, fullWordMultiplier, |
| 37 | maxWordLength, maxWords, maxAlternatives, this); |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | Dictionary::~Dictionary() |
| 41 | { |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 42 | delete mUnigramDictionary; |
| 43 | delete mBigramDictionary; |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 44 | } |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame^] | 45 | |
| 46 | bool Dictionary::hasBigram() { |
| 47 | return ((DICT[1] & 0xFF) == 1); |
| 48 | } |
| 49 | |
| 50 | // TODO: use uint16_t instead of unsigned short |
| 51 | bool Dictionary::isValidWord(unsigned short *word, int length) |
| 52 | { |
| 53 | if (IS_LATEST_DICT_VERSION) { |
| 54 | return (isValidWordRec(DICTIONARY_HEADER_SIZE, word, 0, length) != NOT_VALID_WORD); |
| 55 | } else { |
| 56 | return (isValidWordRec(0, word, 0, length) != NOT_VALID_WORD); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | int Dictionary::isValidWordRec(int pos, unsigned short *word, int offset, int length) { |
| 61 | // returns address of bigram data of that word |
| 62 | // return -99 if not found |
| 63 | |
| 64 | int count = Dictionary::getCount(DICT, &pos); |
| 65 | unsigned short currentChar = (unsigned short) word[offset]; |
| 66 | for (int j = 0; j < count; j++) { |
| 67 | unsigned short c = Dictionary::getChar(DICT, &pos); |
| 68 | int terminal = Dictionary::getTerminal(DICT, &pos); |
| 69 | int childPos = Dictionary::getAddress(DICT, &pos); |
| 70 | if (c == currentChar) { |
| 71 | if (offset == length - 1) { |
| 72 | if (terminal) { |
| 73 | return (pos+1); |
| 74 | } |
| 75 | } else { |
| 76 | if (childPos != 0) { |
| 77 | int t = isValidWordRec(childPos, word, offset + 1, length); |
| 78 | if (t > 0) { |
| 79 | return t; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | if (terminal) { |
| 85 | Dictionary::getFreq(DICT, IS_LATEST_DICT_VERSION, &pos); |
| 86 | } |
| 87 | // There could be two instances of each alphabet - upper and lower case. So continue |
| 88 | // looking ... |
| 89 | } |
| 90 | return NOT_VALID_WORD; |
| 91 | } |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 92 | } // namespace latinime |