blob: 6c722117d460103b43c0bc49acb38767378647c0 [file] [log] [blame]
The Android Open Source Project923bf412009-03-13 15:11:42 -07001/*
Ken Wakasa0bbb9172012-07-25 17:51:43 +09002 * 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 Project923bf412009-03-13 15:11:42 -070016
satoke808e432010-12-02 14:53:24 +090017#define LOG_TAG "LatinIME: dictionary.cpp"
18
Ken Wakasa77e8e812012-08-02 19:48:08 +090019#include <stdint.h>
20
21#include "bigram_dictionary.h"
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"
Satoshi Kataokadeb09872012-07-03 17:45:50 +090025#include "gesture_decoder_wrapper.h"
Ken Wakasa77e8e812012-08-02 19:48:08 +090026#include "unigram_dictionary.h"
satokd4952c82010-12-01 19:09:29 +090027
The Android Open Source Project923bf412009-03-13 15:11:42 -070028namespace latinime {
29
satok8fbd5522011-02-22 17:28:55 +090030// TODO: Change the type of all keyCodes to uint32_t
Ken Wakasae90b3332011-01-07 15:01:51 +090031Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust,
32 int typedLetterMultiplier, int fullWordMultiplier,
Jean Chalardb7d7c5a2012-07-11 11:31:48 +090033 int maxWordLength, int maxWords, int maxPredictions)
Ken Wakasae90b3332011-01-07 15:01:51 +090034 : mDict((unsigned char*) dict), mDictSize(dictSize),
Jean Chalard5b0761e2012-04-06 17:52:18 +090035 mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust) {
satok662fe692010-12-08 17:05:39 +090036 if (DEBUG_DICT) {
37 if (MAX_WORD_LENGTH_INTERNAL < maxWordLength) {
satok9fb6f472012-01-13 18:01:22 +090038 AKLOGI("Max word length (%d) is greater than %d",
satok662fe692010-12-08 17:05:39 +090039 maxWordLength, MAX_WORD_LENGTH_INTERNAL);
satok9fb6f472012-01-13 18:01:22 +090040 AKLOGI("IN NATIVE SUGGEST Version: %d", (mDict[0] & 0xFF));
satok662fe692010-12-08 17:05:39 +090041 }
satok715514d2010-12-02 20:19:59 +090042 }
Jean Chalard46a1eec2012-02-27 19:48:47 +090043 const unsigned int headerSize = BinaryFormat::getHeaderSize(mDict);
Jean Chalardcd274b12012-04-06 18:26:00 +090044 const unsigned int options = BinaryFormat::getFlags(mDict);
Jean Chalard46a1eec2012-02-27 19:48:47 +090045 mUnigramDictionary = new UnigramDictionary(mDict + headerSize, typedLetterMultiplier,
Jean Chalardcd274b12012-04-06 18:26:00 +090046 fullWordMultiplier, maxWordLength, maxWords, options);
Jean Chalardb7d7c5a2012-07-11 11:31:48 +090047 mBigramDictionary = new BigramDictionary(mDict + headerSize, maxWordLength, maxPredictions);
Satoshi Kataokadeb09872012-07-03 17:45:50 +090048 mGestureDecoder = new GestureDecoderWrapper(maxWordLength, maxWords);
Satoshi Kataokaefb63242012-06-27 14:52:40 +090049 mGestureDecoder->setDict(mUnigramDictionary, mBigramDictionary,
50 mDict + headerSize /* dict root */, 0 /* root pos */);
The Android Open Source Project923bf412009-03-13 15:11:42 -070051}
52
satok662fe692010-12-08 17:05:39 +090053Dictionary::~Dictionary() {
satok30088252010-12-01 21:22:15 +090054 delete mUnigramDictionary;
55 delete mBigramDictionary;
Ken Wakasa8658e552012-06-30 08:53:33 +090056 delete mGestureDecoder;
The Android Open Source Project923bf412009-03-13 15:11:42 -070057}
satoke808e432010-12-02 14:53:24 +090058
Ken Wakasa77e8e812012-08-02 19:48:08 +090059int Dictionary::getSuggestions(ProximityInfo *proximityInfo, int *xcoordinates, int *ycoordinates,
60 int *times, int *pointerIds, int *codes, int codesSize, int *prevWordChars,
61 int prevWordLength, int commitPoint, bool isGesture,
62 bool useFullEditDistance, unsigned short *outWords,
63 int *frequencies, int *spaceIndices, int *outputTypes) {
64 int result = 0;
65 if (isGesture) {
66 mGestureDecoder->setPrevWord(prevWordChars, prevWordLength);
67 result = mGestureDecoder->getSuggestions(proximityInfo, xcoordinates, ycoordinates,
68 times, pointerIds, codes, codesSize, commitPoint,
69 outWords, frequencies, spaceIndices, outputTypes);
70 return result;
71 } else {
72 std::map<int, int> bigramMap;
73 uint8_t bigramFilter[BIGRAM_FILTER_BYTE_SIZE];
74 mBigramDictionary->fillBigramAddressToFrequencyMapAndFilter(prevWordChars,
75 prevWordLength, &bigramMap, bigramFilter);
76 result = mUnigramDictionary->getSuggestions(proximityInfo, xcoordinates,
77 ycoordinates, codes, codesSize, &bigramMap, bigramFilter,
78 useFullEditDistance, outWords, frequencies, outputTypes);
79 return result;
80 }
81}
82
83int Dictionary::getBigrams(const int32_t *word, int length, int *codes, int codesSize,
84 unsigned short *outWords, int *frequencies, int *outputTypes) const {
85 if (length <= 0) return 0;
86 return mBigramDictionary->getBigrams(word, length, codes, codesSize, outWords, frequencies,
87 outputTypes);
88}
89
satokb1ed1d42012-06-14 16:35:23 -070090int Dictionary::getFrequency(const int32_t *word, int length) const {
Satoshi Kataoka2f854e12012-05-29 15:58:13 +090091 return mUnigramDictionary->getFrequency(word, length);
satoke808e432010-12-02 14:53:24 +090092}
93
Tom Ouyang4d289d32012-04-26 23:50:21 -070094bool Dictionary::isValidBigram(const int32_t *word1, int length1, const int32_t *word2,
satokb1ed1d42012-06-14 16:35:23 -070095 int length2) const {
Tom Ouyang4d289d32012-04-26 23:50:21 -070096 return mBigramDictionary->isValidBigram(word1, length1, word2, length2);
97}
The Android Open Source Project923bf412009-03-13 15:11:42 -070098} // namespace latinime