blob: 10ea9fe06dc12bb41ed247cedf05cd00ad34cd42 [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"
Ken Wakasa3b088a22012-05-16 23:05:32 +090023#include "defines.h"
The Android Open Source Project923bf412009-03-13 15:11:42 -070024#include "dictionary.h"
satokd4952c82010-12-01 19:09:29 +090025
The Android Open Source Project923bf412009-03-13 15:11:42 -070026namespace latinime {
27
satok8fbd5522011-02-22 17:28:55 +090028// TODO: Change the type of all keyCodes to uint32_t
Ken Wakasae90b3332011-01-07 15:01:51 +090029Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust,
30 int typedLetterMultiplier, int fullWordMultiplier,
satok6ba8de22012-03-28 18:21:04 +090031 int maxWordLength, int maxWords)
Ken Wakasae90b3332011-01-07 15:01:51 +090032 : mDict((unsigned char*) dict), mDictSize(dictSize),
Jean Chalard5b0761e2012-04-06 17:52:18 +090033 mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust) {
satok662fe692010-12-08 17:05:39 +090034 if (DEBUG_DICT) {
35 if (MAX_WORD_LENGTH_INTERNAL < maxWordLength) {
satok9fb6f472012-01-13 18:01:22 +090036 AKLOGI("Max word length (%d) is greater than %d",
satok662fe692010-12-08 17:05:39 +090037 maxWordLength, MAX_WORD_LENGTH_INTERNAL);
satok9fb6f472012-01-13 18:01:22 +090038 AKLOGI("IN NATIVE SUGGEST Version: %d", (mDict[0] & 0xFF));
satok662fe692010-12-08 17:05:39 +090039 }
satok715514d2010-12-02 20:19:59 +090040 }
Jean Chalard46a1eec2012-02-27 19:48:47 +090041 const unsigned int headerSize = BinaryFormat::getHeaderSize(mDict);
Jean Chalardcd274b12012-04-06 18:26:00 +090042 const unsigned int options = BinaryFormat::getFlags(mDict);
Jean Chalard46a1eec2012-02-27 19:48:47 +090043 mUnigramDictionary = new UnigramDictionary(mDict + headerSize, typedLetterMultiplier,
Jean Chalardcd274b12012-04-06 18:26:00 +090044 fullWordMultiplier, maxWordLength, maxWords, options);
satokb1ed1d42012-06-14 16:35:23 -070045 mBigramDictionary = new BigramDictionary(mDict + headerSize, maxWordLength);
Satoshi Kataoka91eb4d82012-06-26 16:39:07 +090046 mGestureDecoder = new GestureDecoder(maxWordLength, maxWords);
Satoshi Kataokaefb63242012-06-27 14:52:40 +090047 mGestureDecoder->setDict(mUnigramDictionary, mBigramDictionary,
48 mDict + headerSize /* dict root */, 0 /* root pos */);
The Android Open Source Project923bf412009-03-13 15:11:42 -070049}
50
satok662fe692010-12-08 17:05:39 +090051Dictionary::~Dictionary() {
satok30088252010-12-01 21:22:15 +090052 delete mUnigramDictionary;
53 delete mBigramDictionary;
The Android Open Source Project923bf412009-03-13 15:11:42 -070054}
satoke808e432010-12-02 14:53:24 +090055
satokb1ed1d42012-06-14 16:35:23 -070056int Dictionary::getFrequency(const int32_t *word, int length) const {
Satoshi Kataoka2f854e12012-05-29 15:58:13 +090057 return mUnigramDictionary->getFrequency(word, length);
satoke808e432010-12-02 14:53:24 +090058}
59
Tom Ouyang4d289d32012-04-26 23:50:21 -070060bool Dictionary::isValidBigram(const int32_t *word1, int length1, const int32_t *word2,
satokb1ed1d42012-06-14 16:35:23 -070061 int length2) const {
Tom Ouyang4d289d32012-04-26 23:50:21 -070062 return mBigramDictionary->isValidBigram(word1, length1, word2, length2);
63}
64
The Android Open Source Project923bf412009-03-13 15:11:42 -070065} // namespace latinime