blob: 5fbe0461b4bfa2843bb88d4392fc29fe3cec9c19 [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 Kataokae9f3e182012-08-09 23:23:08 +090025#include "dic_traverse_wrapper.h"
Satoshi Kataokadeb09872012-07-03 17:45:50 +090026#include "gesture_decoder_wrapper.h"
Ken Wakasa77e8e812012-08-02 19:48:08 +090027#include "unigram_dictionary.h"
satokd4952c82010-12-01 19:09:29 +090028
The Android Open Source Project923bf412009-03-13 15:11:42 -070029namespace latinime {
30
satok8fbd5522011-02-22 17:28:55 +090031// TODO: Change the type of all keyCodes to uint32_t
Ken Wakasae90b3332011-01-07 15:01:51 +090032Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust,
Ken Wakasab02ee3d2012-10-08 11:46:14 +090033 int fullWordMultiplier, int maxWordLength, int maxWords, int maxPredictions)
Ken Wakasa162c2112012-08-24 14:51:15 +090034 : mDict(static_cast<unsigned char *>(dict)),
35 mOffsetDict((static_cast<unsigned char *>(dict)) + BinaryFormat::getHeaderSize(mDict)),
36 mDictSize(dictSize), mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust),
Ken Wakasab02ee3d2012-10-08 11:46:14 +090037 mUnigramDictionary(new UnigramDictionary(mOffsetDict, fullWordMultiplier, maxWordLength,
38 maxWords, BinaryFormat::getFlags(mDict))),
Ken Wakasa162c2112012-08-24 14:51:15 +090039 mBigramDictionary(new BigramDictionary(mOffsetDict, maxWordLength, maxPredictions)),
40 mGestureDecoder(new GestureDecoderWrapper(maxWordLength, maxWords)) {
satok662fe692010-12-08 17:05:39 +090041 if (DEBUG_DICT) {
42 if (MAX_WORD_LENGTH_INTERNAL < maxWordLength) {
satok9fb6f472012-01-13 18:01:22 +090043 AKLOGI("Max word length (%d) is greater than %d",
satok662fe692010-12-08 17:05:39 +090044 maxWordLength, MAX_WORD_LENGTH_INTERNAL);
satok9fb6f472012-01-13 18:01:22 +090045 AKLOGI("IN NATIVE SUGGEST Version: %d", (mDict[0] & 0xFF));
satok662fe692010-12-08 17:05:39 +090046 }
satok715514d2010-12-02 20:19:59 +090047 }
The Android Open Source Project923bf412009-03-13 15:11:42 -070048}
49
satok662fe692010-12-08 17:05:39 +090050Dictionary::~Dictionary() {
satok30088252010-12-01 21:22:15 +090051 delete mUnigramDictionary;
52 delete mBigramDictionary;
Ken Wakasa8658e552012-06-30 08:53:33 +090053 delete mGestureDecoder;
The Android Open Source Project923bf412009-03-13 15:11:42 -070054}
satoke808e432010-12-02 14:53:24 +090055
Satoshi Kataoka91278112012-08-08 21:23:25 +090056int Dictionary::getSuggestions(ProximityInfo *proximityInfo, void *traverseSession,
Ken Wakasa1e614932012-10-29 18:06:22 +090057 int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int *codes,
58 int codesSize, int *prevWordChars, int prevWordLength, int commitPoint, bool isGesture,
59 bool useFullEditDistance, int *outWords, int *frequencies, int *spaceIndices,
60 int *outputTypes) const {
Ken Wakasa77e8e812012-08-02 19:48:08 +090061 int result = 0;
62 if (isGesture) {
Satoshi Kataokae9f3e182012-08-09 23:23:08 +090063 DicTraverseWrapper::initDicTraverseSession(
64 traverseSession, this, prevWordChars, prevWordLength);
Satoshi Kataoka91278112012-08-08 21:23:25 +090065 result = mGestureDecoder->getSuggestions(proximityInfo, traverseSession,
66 xcoordinates, ycoordinates, times, pointerIds, codes, codesSize, commitPoint,
Ken Wakasa77e8e812012-08-02 19:48:08 +090067 outWords, frequencies, spaceIndices, outputTypes);
Satoshi Kataoka586b0ca2012-08-06 11:20:54 +090068 if (DEBUG_DICT) {
69 DUMP_RESULT(outWords, frequencies, 18 /* MAX_WORDS */, MAX_WORD_LENGTH_INTERNAL);
70 }
Ken Wakasa77e8e812012-08-02 19:48:08 +090071 return result;
72 } else {
73 std::map<int, int> bigramMap;
74 uint8_t bigramFilter[BIGRAM_FILTER_BYTE_SIZE];
75 mBigramDictionary->fillBigramAddressToFrequencyMapAndFilter(prevWordChars,
76 prevWordLength, &bigramMap, bigramFilter);
77 result = mUnigramDictionary->getSuggestions(proximityInfo, xcoordinates,
78 ycoordinates, codes, codesSize, &bigramMap, bigramFilter,
79 useFullEditDistance, outWords, frequencies, outputTypes);
80 return result;
81 }
82}
83
84int Dictionary::getBigrams(const int32_t *word, int length, int *codes, int codesSize,
Ken Wakasa1e614932012-10-29 18:06:22 +090085 int *outWords, int *frequencies, int *outputTypes) const {
Ken Wakasa77e8e812012-08-02 19:48:08 +090086 if (length <= 0) return 0;
87 return mBigramDictionary->getBigrams(word, length, codes, codesSize, outWords, frequencies,
88 outputTypes);
89}
90
satokb1ed1d42012-06-14 16:35:23 -070091int Dictionary::getFrequency(const int32_t *word, int length) const {
Satoshi Kataoka2f854e12012-05-29 15:58:13 +090092 return mUnigramDictionary->getFrequency(word, length);
satoke808e432010-12-02 14:53:24 +090093}
94
Tom Ouyang4d289d32012-04-26 23:50:21 -070095bool Dictionary::isValidBigram(const int32_t *word1, int length1, const int32_t *word2,
satokb1ed1d42012-06-14 16:35:23 -070096 int length2) const {
Tom Ouyang4d289d32012-04-26 23:50:21 -070097 return mBigramDictionary->isValidBigram(word1, length1, word2, length2);
98}
The Android Open Source Project923bf412009-03-13 15:11:42 -070099} // namespace latinime