blob: 9e32ee80f39242c568ad5bb7f5528f2f0e75e5d4 [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
The Android Open Source Project923bf412009-03-13 15:11:42 -070022#include "dictionary.h"
satokd4952c82010-12-01 19:09:29 +090023
The Android Open Source Project923bf412009-03-13 15:11:42 -070024namespace latinime {
25
satok8fbd5522011-02-22 17:28:55 +090026// TODO: Change the type of all keyCodes to uint32_t
Ken Wakasae90b3332011-01-07 15:01:51 +090027Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust,
28 int typedLetterMultiplier, int fullWordMultiplier,
satok30088252010-12-01 21:22:15 +090029 int maxWordLength, int maxWords, int maxAlternatives)
Ken Wakasae90b3332011-01-07 15:01:51 +090030 : mDict((unsigned char*) dict), mDictSize(dictSize),
31 mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust),
satoke808e432010-12-02 14:53:24 +090032 // Checks whether it has the latest dictionary or the old dictionary
satok662fe692010-12-08 17:05:39 +090033 IS_LATEST_DICT_VERSION((((unsigned char*) dict)[0] & 0xFF) >= DICTIONARY_VERSION_MIN) {
34 if (DEBUG_DICT) {
35 if (MAX_WORD_LENGTH_INTERNAL < maxWordLength) {
36 LOGI("Max word length (%d) is greater than %d",
37 maxWordLength, MAX_WORD_LENGTH_INTERNAL);
Ken Wakasae90b3332011-01-07 15:01:51 +090038 LOGI("IN NATIVE SUGGEST Version: %d", (mDict[0] & 0xFF));
satok662fe692010-12-08 17:05:39 +090039 }
satok715514d2010-12-02 20:19:59 +090040 }
Ken Wakasae90b3332011-01-07 15:01:51 +090041 mUnigramDictionary = new UnigramDictionary(mDict, typedLetterMultiplier, fullWordMultiplier,
satok18c28f42010-12-02 18:11:54 +090042 maxWordLength, maxWords, maxAlternatives, IS_LATEST_DICT_VERSION);
Ken Wakasae90b3332011-01-07 15:01:51 +090043 mBigramDictionary = new BigramDictionary(mDict, maxWordLength, maxAlternatives,
satok18c28f42010-12-02 18:11:54 +090044 IS_LATEST_DICT_VERSION, hasBigram(), this);
The Android Open Source Project923bf412009-03-13 15:11:42 -070045}
46
satok662fe692010-12-08 17:05:39 +090047Dictionary::~Dictionary() {
satok30088252010-12-01 21:22:15 +090048 delete mUnigramDictionary;
49 delete mBigramDictionary;
The Android Open Source Project923bf412009-03-13 15:11:42 -070050}
satoke808e432010-12-02 14:53:24 +090051
52bool Dictionary::hasBigram() {
Ken Wakasae90b3332011-01-07 15:01:51 +090053 return ((mDict[1] & 0xFF) == 1);
satoke808e432010-12-02 14:53:24 +090054}
55
satok8fbd5522011-02-22 17:28:55 +090056bool Dictionary::isValidWord(unsigned short *word, int length) {
Jean Chalard8124e642011-06-16 22:33:41 +090057 return mUnigramDictionary->isValidWord(word, length);
satoke808e432010-12-02 14:53:24 +090058}
59
Jean Chalard581335c2011-06-17 12:45:17 +090060int Dictionary::getBigramPosition(unsigned short *word, int length) {
61 if (IS_LATEST_DICT_VERSION) {
62 return mUnigramDictionary->getBigramPosition(DICTIONARY_HEADER_SIZE, word, 0, length);
63 } else {
64 return mUnigramDictionary->getBigramPosition(0, word, 0, length);
65 }
66}
67
The Android Open Source Project923bf412009-03-13 15:11:42 -070068} // namespace latinime