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> 19 20 #define LOG_TAG "LatinIME: dictionary.cpp" 21 22 #include "binary_format.h" 23 #include "defines.h" 24 #include "dictionary.h" 25 26 namespace latinime { 27 28 // TODO: Change the type of all keyCodes to uint32_t 29 Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust, 30 int typedLetterMultiplier, int fullWordMultiplier, 31 int maxWordLength, int maxWords) 32 : mDict((unsigned char*) dict), mDictSize(dictSize), 33 mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust) { 34 if (DEBUG_DICT) { 35 if (MAX_WORD_LENGTH_INTERNAL < maxWordLength) { 36 AKLOGI("Max word length (%d) is greater than %d", 37 maxWordLength, MAX_WORD_LENGTH_INTERNAL); 38 AKLOGI("IN NATIVE SUGGEST Version: %d", (mDict[0] & 0xFF)); 39 } 40 } 41 mCorrection = new Correction(typedLetterMultiplier, fullWordMultiplier); 42 mWordsPriorityQueuePool = new WordsPriorityQueuePool( 43 maxWords, SUB_QUEUE_MAX_WORDS, maxWordLength); 44 const unsigned int headerSize = BinaryFormat::getHeaderSize(mDict); 45 const unsigned int options = BinaryFormat::getFlags(mDict); 46 mUnigramDictionary = new UnigramDictionary(mDict + headerSize, typedLetterMultiplier, 47 fullWordMultiplier, maxWordLength, maxWords, options); 48 mBigramDictionary = new BigramDictionary(mDict + headerSize, maxWordLength, this); 49 } 50 51 Dictionary::~Dictionary() { 52 delete mCorrection; 53 delete mWordsPriorityQueuePool; 54 delete mUnigramDictionary; 55 delete mBigramDictionary; 56 } 57 58 int Dictionary::getFrequency(const int32_t *word, int length) { 59 return mUnigramDictionary->getFrequency(word, length); 60 } 61 62 bool Dictionary::isValidBigram(const int32_t *word1, int length1, const int32_t *word2, 63 int length2) { 64 return mBigramDictionary->isValidBigram(word1, length1, word2, length2); 65 } 66 67 } // namespace latinime 68