The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 1 | /* |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 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 | */ |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 16 | |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 17 | #define LOG_TAG "LatinIME: dictionary.cpp" |
| 18 | |
Ken Wakasa | 77e8e81 | 2012-08-02 19:48:08 +0900 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | |
| 21 | #include "bigram_dictionary.h" |
Jean Chalard | 46a1eec | 2012-02-27 19:48:47 +0900 | [diff] [blame] | 22 | #include "binary_format.h" |
Ken Wakasa | 3b088a2 | 2012-05-16 23:05:32 +0900 | [diff] [blame] | 23 | #include "defines.h" |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 24 | #include "dictionary.h" |
Satoshi Kataoka | e9f3e18 | 2012-08-09 23:23:08 +0900 | [diff] [blame] | 25 | #include "dic_traverse_wrapper.h" |
Satoshi Kataoka | deb0987 | 2012-07-03 17:45:50 +0900 | [diff] [blame] | 26 | #include "gesture_decoder_wrapper.h" |
Ken Wakasa | 77e8e81 | 2012-08-02 19:48:08 +0900 | [diff] [blame] | 27 | #include "unigram_dictionary.h" |
satok | d4952c8 | 2010-12-01 19:09:29 +0900 | [diff] [blame] | 28 | |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 29 | namespace latinime { |
| 30 | |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 31 | // TODO: Change the type of all keyCodes to uint32_t |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 32 | Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust, |
Ken Wakasa | 162c211 | 2012-08-24 14:51:15 +0900 | [diff] [blame] | 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)) { |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 42 | if (DEBUG_DICT) { |
| 43 | if (MAX_WORD_LENGTH_INTERNAL < maxWordLength) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 44 | AKLOGI("Max word length (%d) is greater than %d", |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 45 | maxWordLength, MAX_WORD_LENGTH_INTERNAL); |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 46 | AKLOGI("IN NATIVE SUGGEST Version: %d", (mDict[0] & 0xFF)); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 47 | } |
satok | 715514d | 2010-12-02 20:19:59 +0900 | [diff] [blame] | 48 | } |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 49 | } |
| 50 | |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 51 | Dictionary::~Dictionary() { |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 52 | delete mUnigramDictionary; |
| 53 | delete mBigramDictionary; |
Ken Wakasa | 8658e55 | 2012-06-30 08:53:33 +0900 | [diff] [blame] | 54 | delete mGestureDecoder; |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 55 | } |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 56 | |
Satoshi Kataoka | 9127811 | 2012-08-08 21:23:25 +0900 | [diff] [blame] | 57 | int Dictionary::getSuggestions(ProximityInfo *proximityInfo, void *traverseSession, |
| 58 | int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, |
| 59 | int *codes, int codesSize, int *prevWordChars, |
Ken Wakasa | 77e8e81 | 2012-08-02 19:48:08 +0900 | [diff] [blame] | 60 | int prevWordLength, int commitPoint, bool isGesture, |
| 61 | bool useFullEditDistance, unsigned short *outWords, |
Satoshi Kataoka | f6be15c | 2012-08-15 16:57:32 +0900 | [diff] [blame] | 62 | int *frequencies, int *spaceIndices, int *outputTypes) const { |
Ken Wakasa | 77e8e81 | 2012-08-02 19:48:08 +0900 | [diff] [blame] | 63 | int result = 0; |
| 64 | if (isGesture) { |
Satoshi Kataoka | e9f3e18 | 2012-08-09 23:23:08 +0900 | [diff] [blame] | 65 | DicTraverseWrapper::initDicTraverseSession( |
| 66 | traverseSession, this, prevWordChars, prevWordLength); |
Satoshi Kataoka | 9127811 | 2012-08-08 21:23:25 +0900 | [diff] [blame] | 67 | result = mGestureDecoder->getSuggestions(proximityInfo, traverseSession, |
| 68 | xcoordinates, ycoordinates, times, pointerIds, codes, codesSize, commitPoint, |
Ken Wakasa | 77e8e81 | 2012-08-02 19:48:08 +0900 | [diff] [blame] | 69 | outWords, frequencies, spaceIndices, outputTypes); |
Satoshi Kataoka | 586b0ca | 2012-08-06 11:20:54 +0900 | [diff] [blame] | 70 | if (DEBUG_DICT) { |
| 71 | DUMP_RESULT(outWords, frequencies, 18 /* MAX_WORDS */, MAX_WORD_LENGTH_INTERNAL); |
| 72 | } |
Ken Wakasa | 77e8e81 | 2012-08-02 19:48:08 +0900 | [diff] [blame] | 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 | |
| 86 | int 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 | |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 93 | int Dictionary::getFrequency(const int32_t *word, int length) const { |
Satoshi Kataoka | 2f854e1 | 2012-05-29 15:58:13 +0900 | [diff] [blame] | 94 | return mUnigramDictionary->getFrequency(word, length); |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 95 | } |
| 96 | |
Tom Ouyang | 4d289d3 | 2012-04-26 23:50:21 -0700 | [diff] [blame] | 97 | bool Dictionary::isValidBigram(const int32_t *word1, int length1, const int32_t *word2, |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 98 | int length2) const { |
Tom Ouyang | 4d289d3 | 2012-04-26 23:50:21 -0700 | [diff] [blame] | 99 | return mBigramDictionary->isValidBigram(word1, length1, word2, length2); |
| 100 | } |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 101 | } // namespace latinime |