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 "dictionary.h" 20 21 #include <map> // TODO: remove 22 #include <stdint.h> 23 24 #include "bigram_dictionary.h" 25 #include "binary_format.h" 26 #include "defines.h" 27 #include "dic_traverse_wrapper.h" 28 #include "suggest/core/suggest.h" 29 #include "suggest/policyimpl/gesture/gesture_suggest_policy_factory.h" 30 #include "suggest/policyimpl/typing/typing_suggest_policy_factory.h" 31 #include "unigram_dictionary.h" 32 33 namespace latinime { 34 35 Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust) 36 : mDict(static_cast<unsigned char *>(dict)), 37 mOffsetDict((static_cast<unsigned char *>(dict)) 38 + BinaryFormat::getHeaderSize(mDict, dictSize)), 39 mDictSize(dictSize), mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust), 40 mUnigramDictionary(new UnigramDictionary(mOffsetDict, 41 BinaryFormat::getFlags(mDict, dictSize))), 42 mBigramDictionary(new BigramDictionary(mOffsetDict)), 43 mGestureSuggest(new Suggest(GestureSuggestPolicyFactory::getGestureSuggestPolicy())), 44 mTypingSuggest(new Suggest(TypingSuggestPolicyFactory::getTypingSuggestPolicy())) { 45 } 46 47 Dictionary::~Dictionary() { 48 delete mUnigramDictionary; 49 delete mBigramDictionary; 50 delete mGestureSuggest; 51 delete mTypingSuggest; 52 } 53 54 int Dictionary::getSuggestions(ProximityInfo *proximityInfo, void *traverseSession, 55 int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int *inputCodePoints, 56 int inputSize, int *prevWordCodePoints, int prevWordLength, int commitPoint, bool isGesture, 57 bool useFullEditDistance, int *outWords, int *frequencies, int *spaceIndices, 58 int *outputTypes) const { 59 int result = 0; 60 if (isGesture) { 61 DicTraverseWrapper::initDicTraverseSession( 62 traverseSession, this, prevWordCodePoints, prevWordLength); 63 result = mGestureSuggest->getSuggestions(proximityInfo, traverseSession, xcoordinates, 64 ycoordinates, times, pointerIds, inputCodePoints, inputSize, commitPoint, outWords, 65 frequencies, spaceIndices, outputTypes); 66 if (DEBUG_DICT) { 67 DUMP_RESULT(outWords, frequencies); 68 } 69 return result; 70 } else { 71 if (USE_SUGGEST_INTERFACE_FOR_TYPING) { 72 DicTraverseWrapper::initDicTraverseSession( 73 traverseSession, this, prevWordCodePoints, prevWordLength); 74 result = mTypingSuggest->getSuggestions(proximityInfo, traverseSession, xcoordinates, 75 ycoordinates, times, pointerIds, inputCodePoints, inputSize, commitPoint, 76 outWords, frequencies, spaceIndices, outputTypes); 77 if (DEBUG_DICT) { 78 DUMP_RESULT(outWords, frequencies); 79 } 80 return result; 81 } else { 82 std::map<int, int> bigramMap; 83 uint8_t bigramFilter[BIGRAM_FILTER_BYTE_SIZE]; 84 mBigramDictionary->fillBigramAddressToProbabilityMapAndFilter(prevWordCodePoints, 85 prevWordLength, &bigramMap, bigramFilter); 86 result = mUnigramDictionary->getSuggestions(proximityInfo, xcoordinates, ycoordinates, 87 inputCodePoints, inputSize, &bigramMap, bigramFilter, useFullEditDistance, 88 outWords, frequencies, outputTypes); 89 return result; 90 } 91 } 92 } 93 94 int Dictionary::getBigrams(const int *word, int length, int *inputCodePoints, int inputSize, 95 int *outWords, int *frequencies, int *outputTypes) const { 96 if (length <= 0) return 0; 97 return mBigramDictionary->getBigrams(word, length, inputCodePoints, inputSize, outWords, 98 frequencies, outputTypes); 99 } 100 101 int Dictionary::getProbability(const int *word, int length) const { 102 return mUnigramDictionary->getProbability(word, length); 103 } 104 105 bool Dictionary::isValidBigram(const int *word1, int length1, const int *word2, int length2) const { 106 return mBigramDictionary->isValidBigram(word1, length1, word2, length2); 107 } 108 109 int Dictionary::getDictFlags() const { 110 return mUnigramDictionary->getDictFlags(); 111 } 112 113 } // namespace latinime 114