The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 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> |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 19 | |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 20 | #define LOG_TAG "LatinIME: dictionary.cpp" |
| 21 | |
Jean Chalard | 46a1eec | 2012-02-27 19:48:47 +0900 | [diff] [blame] | 22 | #include "binary_format.h" |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 23 | #include "dictionary.h" |
satok | d4952c8 | 2010-12-01 19:09:29 +0900 | [diff] [blame] | 24 | |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 25 | namespace latinime { |
| 26 | |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 27 | // TODO: Change the type of all keyCodes to uint32_t |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 28 | Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust, |
| 29 | int typedLetterMultiplier, int fullWordMultiplier, |
satok | 6ba8de2 | 2012-03-28 18:21:04 +0900 | [diff] [blame] | 30 | int maxWordLength, int maxWords) |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 31 | : mDict((unsigned char*) dict), mDictSize(dictSize), |
Jean Chalard | 5b0761e | 2012-04-06 17:52:18 +0900 | [diff] [blame] | 32 | mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust) { |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 33 | if (DEBUG_DICT) { |
| 34 | if (MAX_WORD_LENGTH_INTERNAL < maxWordLength) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 35 | AKLOGI("Max word length (%d) is greater than %d", |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 36 | maxWordLength, MAX_WORD_LENGTH_INTERNAL); |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 37 | AKLOGI("IN NATIVE SUGGEST Version: %d", (mDict[0] & 0xFF)); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 38 | } |
satok | 715514d | 2010-12-02 20:19:59 +0900 | [diff] [blame] | 39 | } |
satok | 1147c7b | 2011-12-14 15:04:58 +0900 | [diff] [blame] | 40 | mCorrection = new Correction(typedLetterMultiplier, fullWordMultiplier); |
satok | a7e5a5a | 2011-12-15 16:49:12 +0900 | [diff] [blame] | 41 | mWordsPriorityQueuePool = new WordsPriorityQueuePool( |
| 42 | maxWords, SUB_QUEUE_MAX_WORDS, maxWordLength); |
Jean Chalard | 46a1eec | 2012-02-27 19:48:47 +0900 | [diff] [blame] | 43 | const unsigned int headerSize = BinaryFormat::getHeaderSize(mDict); |
Jean Chalard | cd274b1 | 2012-04-06 18:26:00 +0900 | [diff] [blame] | 44 | const unsigned int options = BinaryFormat::getFlags(mDict); |
Jean Chalard | 46a1eec | 2012-02-27 19:48:47 +0900 | [diff] [blame] | 45 | mUnigramDictionary = new UnigramDictionary(mDict + headerSize, typedLetterMultiplier, |
Jean Chalard | cd274b1 | 2012-04-06 18:26:00 +0900 | [diff] [blame] | 46 | fullWordMultiplier, maxWordLength, maxWords, options); |
Jean Chalard | 5b0761e | 2012-04-06 17:52:18 +0900 | [diff] [blame] | 47 | mBigramDictionary = new BigramDictionary(mDict + headerSize, maxWordLength, this); |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 48 | } |
| 49 | |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 50 | Dictionary::~Dictionary() { |
satok | 1147c7b | 2011-12-14 15:04:58 +0900 | [diff] [blame] | 51 | delete mCorrection; |
satok | a7e5a5a | 2011-12-15 16:49:12 +0900 | [diff] [blame] | 52 | delete mWordsPriorityQueuePool; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 53 | delete mUnigramDictionary; |
| 54 | delete mBigramDictionary; |
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 | |
Jean Chalard | 522a04e | 2012-04-23 15:37:07 +0900 | [diff] [blame] | 57 | bool Dictionary::isValidWord(const int32_t *word, int length) { |
Jean Chalard | 8124e64 | 2011-06-16 22:33:41 +0900 | [diff] [blame] | 58 | return mUnigramDictionary->isValidWord(word, length); |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 59 | } |
| 60 | |
Tom Ouyang | 4d289d3 | 2012-04-26 23:50:21 -0700 | [diff] [blame^] | 61 | bool Dictionary::isValidBigram(const int32_t *word1, int length1, const int32_t *word2, |
| 62 | int length2) { |
| 63 | return mBigramDictionary->isValidBigram(word1, length1, word2, length2); |
| 64 | } |
| 65 | |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 66 | } // namespace latinime |