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 | |
Jean Chalard | 46a1eec | 2012-02-27 19:48:47 +0900 | [diff] [blame] | 19 | #include "binary_format.h" |
Ken Wakasa | 3b088a2 | 2012-05-16 23:05:32 +0900 | [diff] [blame] | 20 | #include "defines.h" |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 21 | #include "dictionary.h" |
Satoshi Kataoka | deb0987 | 2012-07-03 17:45:50 +0900 | [diff] [blame] | 22 | #include "gesture_decoder_wrapper.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 | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 26 | // TODO: Change the type of all keyCodes to uint32_t |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 27 | Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust, |
| 28 | int typedLetterMultiplier, int fullWordMultiplier, |
Jean Chalard | b7d7c5a | 2012-07-11 11:31:48 +0900 | [diff] [blame] | 29 | int maxWordLength, int maxWords, int maxPredictions) |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 30 | : mDict((unsigned char*) dict), mDictSize(dictSize), |
Jean Chalard | 5b0761e | 2012-04-06 17:52:18 +0900 | [diff] [blame] | 31 | mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust) { |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 32 | if (DEBUG_DICT) { |
| 33 | if (MAX_WORD_LENGTH_INTERNAL < maxWordLength) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 34 | AKLOGI("Max word length (%d) is greater than %d", |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 35 | maxWordLength, MAX_WORD_LENGTH_INTERNAL); |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 36 | AKLOGI("IN NATIVE SUGGEST Version: %d", (mDict[0] & 0xFF)); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 37 | } |
satok | 715514d | 2010-12-02 20:19:59 +0900 | [diff] [blame] | 38 | } |
Jean Chalard | 46a1eec | 2012-02-27 19:48:47 +0900 | [diff] [blame] | 39 | const unsigned int headerSize = BinaryFormat::getHeaderSize(mDict); |
Jean Chalard | cd274b1 | 2012-04-06 18:26:00 +0900 | [diff] [blame] | 40 | const unsigned int options = BinaryFormat::getFlags(mDict); |
Jean Chalard | 46a1eec | 2012-02-27 19:48:47 +0900 | [diff] [blame] | 41 | mUnigramDictionary = new UnigramDictionary(mDict + headerSize, typedLetterMultiplier, |
Jean Chalard | cd274b1 | 2012-04-06 18:26:00 +0900 | [diff] [blame] | 42 | fullWordMultiplier, maxWordLength, maxWords, options); |
Jean Chalard | b7d7c5a | 2012-07-11 11:31:48 +0900 | [diff] [blame] | 43 | mBigramDictionary = new BigramDictionary(mDict + headerSize, maxWordLength, maxPredictions); |
Satoshi Kataoka | deb0987 | 2012-07-03 17:45:50 +0900 | [diff] [blame] | 44 | mGestureDecoder = new GestureDecoderWrapper(maxWordLength, maxWords); |
Satoshi Kataoka | efb6324 | 2012-06-27 14:52:40 +0900 | [diff] [blame] | 45 | mGestureDecoder->setDict(mUnigramDictionary, mBigramDictionary, |
| 46 | mDict + headerSize /* dict root */, 0 /* root pos */); |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 47 | } |
| 48 | |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 49 | Dictionary::~Dictionary() { |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 50 | delete mUnigramDictionary; |
| 51 | delete mBigramDictionary; |
Ken Wakasa | 8658e55 | 2012-06-30 08:53:33 +0900 | [diff] [blame] | 52 | delete mGestureDecoder; |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 53 | } |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 54 | |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 55 | int Dictionary::getFrequency(const int32_t *word, int length) const { |
Satoshi Kataoka | 2f854e1 | 2012-05-29 15:58:13 +0900 | [diff] [blame] | 56 | return mUnigramDictionary->getFrequency(word, length); |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 57 | } |
| 58 | |
Tom Ouyang | 4d289d3 | 2012-04-26 23:50:21 -0700 | [diff] [blame] | 59 | bool Dictionary::isValidBigram(const int32_t *word1, int length1, const int32_t *word2, |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 60 | int length2) const { |
Tom Ouyang | 4d289d3 | 2012-04-26 23:50:21 -0700 | [diff] [blame] | 61 | return mBigramDictionary->isValidBigram(word1, length1, word2, length2); |
| 62 | } |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 63 | } // namespace latinime |