blob: 8e252f73058b3f930d324704cddfd01b88ceef2c [file] [log] [blame]
The Android Open Source Project923bf412009-03-13 15:11:42 -07001/*
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 Project923bf412009-03-13 15:11:42 -070019
satoke808e432010-12-02 14:53:24 +090020#define LOG_TAG "LatinIME: dictionary.cpp"
21
Jean Chalard46a1eec2012-02-27 19:48:47 +090022#include "binary_format.h"
The Android Open Source Project923bf412009-03-13 15:11:42 -070023#include "dictionary.h"
satokd4952c82010-12-01 19:09:29 +090024
The Android Open Source Project923bf412009-03-13 15:11:42 -070025namespace latinime {
26
satok8fbd5522011-02-22 17:28:55 +090027// TODO: Change the type of all keyCodes to uint32_t
Ken Wakasae90b3332011-01-07 15:01:51 +090028Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust,
29 int typedLetterMultiplier, int fullWordMultiplier,
satok30088252010-12-01 21:22:15 +090030 int maxWordLength, int maxWords, int maxAlternatives)
Ken Wakasae90b3332011-01-07 15:01:51 +090031 : mDict((unsigned char*) dict), mDictSize(dictSize),
32 mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust),
satoke808e432010-12-02 14:53:24 +090033 // Checks whether it has the latest dictionary or the old dictionary
satok662fe692010-12-08 17:05:39 +090034 IS_LATEST_DICT_VERSION((((unsigned char*) dict)[0] & 0xFF) >= DICTIONARY_VERSION_MIN) {
35 if (DEBUG_DICT) {
36 if (MAX_WORD_LENGTH_INTERNAL < maxWordLength) {
satok9fb6f472012-01-13 18:01:22 +090037 AKLOGI("Max word length (%d) is greater than %d",
satok662fe692010-12-08 17:05:39 +090038 maxWordLength, MAX_WORD_LENGTH_INTERNAL);
satok9fb6f472012-01-13 18:01:22 +090039 AKLOGI("IN NATIVE SUGGEST Version: %d", (mDict[0] & 0xFF));
satok662fe692010-12-08 17:05:39 +090040 }
satok715514d2010-12-02 20:19:59 +090041 }
satok1147c7b2011-12-14 15:04:58 +090042 mCorrection = new Correction(typedLetterMultiplier, fullWordMultiplier);
satoka7e5a5a2011-12-15 16:49:12 +090043 mWordsPriorityQueuePool = new WordsPriorityQueuePool(
44 maxWords, SUB_QUEUE_MAX_WORDS, maxWordLength);
Jean Chalard46a1eec2012-02-27 19:48:47 +090045 const unsigned int headerSize = BinaryFormat::getHeaderSize(mDict);
46 mUnigramDictionary = new UnigramDictionary(mDict + headerSize, typedLetterMultiplier,
47 fullWordMultiplier, maxWordLength, maxWords, maxAlternatives, IS_LATEST_DICT_VERSION);
48 mBigramDictionary = new BigramDictionary(mDict + headerSize, maxWordLength, maxAlternatives,
49 IS_LATEST_DICT_VERSION, true /* hasBigram */, this);
The Android Open Source Project923bf412009-03-13 15:11:42 -070050}
51
satok662fe692010-12-08 17:05:39 +090052Dictionary::~Dictionary() {
satok1147c7b2011-12-14 15:04:58 +090053 delete mCorrection;
satoka7e5a5a2011-12-15 16:49:12 +090054 delete mWordsPriorityQueuePool;
satok30088252010-12-01 21:22:15 +090055 delete mUnigramDictionary;
56 delete mBigramDictionary;
The Android Open Source Project923bf412009-03-13 15:11:42 -070057}
satoke808e432010-12-02 14:53:24 +090058
satok8fbd5522011-02-22 17:28:55 +090059bool Dictionary::isValidWord(unsigned short *word, int length) {
Jean Chalard8124e642011-06-16 22:33:41 +090060 return mUnigramDictionary->isValidWord(word, length);
satoke808e432010-12-02 14:53:24 +090061}
62
The Android Open Source Project923bf412009-03-13 15:11:42 -070063} // namespace latinime