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#define LOG_TAG "LatinIME: dictionary.cpp" 18 19#include <stdint.h> 20 21#include "bigram_dictionary.h" 22#include "binary_format.h" 23#include "defines.h" 24#include "dictionary.h" 25#include "dic_traverse_wrapper.h" 26#include "gesture_decoder_wrapper.h" 27#include "unigram_dictionary.h" 28 29namespace latinime { 30 31// TODO: Change the type of all keyCodes to uint32_t 32Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust, 33 int typedLetterMultiplier, int fullWordMultiplier, int maxWordLength, int maxWords, 34 int maxPredictions) 35 : mDict(static_cast<unsigned char *>(dict)), 36 mOffsetDict((static_cast<unsigned char *>(dict)) + BinaryFormat::getHeaderSize(mDict)), 37 mDictSize(dictSize), mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust), 38 mUnigramDictionary(new UnigramDictionary(mOffsetDict, typedLetterMultiplier, 39 fullWordMultiplier, maxWordLength, maxWords, BinaryFormat::getFlags(mDict))), 40 mBigramDictionary(new BigramDictionary(mOffsetDict, maxWordLength, maxPredictions)), 41 mGestureDecoder(new GestureDecoderWrapper(maxWordLength, maxWords)) { 42 if (DEBUG_DICT) { 43 if (MAX_WORD_LENGTH_INTERNAL < maxWordLength) { 44 AKLOGI("Max word length (%d) is greater than %d", 45 maxWordLength, MAX_WORD_LENGTH_INTERNAL); 46 AKLOGI("IN NATIVE SUGGEST Version: %d", (mDict[0] & 0xFF)); 47 } 48 } 49} 50 51Dictionary::~Dictionary() { 52 delete mUnigramDictionary; 53 delete mBigramDictionary; 54 delete mGestureDecoder; 55} 56 57int Dictionary::getSuggestions(ProximityInfo *proximityInfo, void *traverseSession, 58 int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, 59 int *codes, int codesSize, int *prevWordChars, 60 int prevWordLength, int commitPoint, bool isGesture, 61 bool useFullEditDistance, unsigned short *outWords, 62 int *frequencies, int *spaceIndices, int *outputTypes) const { 63 int result = 0; 64 if (isGesture) { 65 DicTraverseWrapper::initDicTraverseSession( 66 traverseSession, this, prevWordChars, prevWordLength); 67 result = mGestureDecoder->getSuggestions(proximityInfo, traverseSession, 68 xcoordinates, ycoordinates, times, pointerIds, codes, codesSize, commitPoint, 69 outWords, frequencies, spaceIndices, outputTypes); 70 if (DEBUG_DICT) { 71 DUMP_RESULT(outWords, frequencies, 18 /* MAX_WORDS */, MAX_WORD_LENGTH_INTERNAL); 72 } 73 return result; 74 } else { 75 std::map<int, int> bigramMap; 76 uint8_t bigramFilter[BIGRAM_FILTER_BYTE_SIZE]; 77 mBigramDictionary->fillBigramAddressToFrequencyMapAndFilter(prevWordChars, 78 prevWordLength, &bigramMap, bigramFilter); 79 result = mUnigramDictionary->getSuggestions(proximityInfo, xcoordinates, 80 ycoordinates, codes, codesSize, &bigramMap, bigramFilter, 81 useFullEditDistance, outWords, frequencies, outputTypes); 82 return result; 83 } 84} 85 86int Dictionary::getBigrams(const int32_t *word, int length, int *codes, int codesSize, 87 unsigned short *outWords, int *frequencies, int *outputTypes) const { 88 if (length <= 0) return 0; 89 return mBigramDictionary->getBigrams(word, length, codes, codesSize, outWords, frequencies, 90 outputTypes); 91} 92 93int Dictionary::getFrequency(const int32_t *word, int length) const { 94 return mUnigramDictionary->getFrequency(word, length); 95} 96 97bool Dictionary::isValidBigram(const int32_t *word1, int length1, const int32_t *word2, 98 int length2) const { 99 return mBigramDictionary->isValidBigram(word1, length1, word2, length2); 100} 101} // namespace latinime 102