The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 1 | /* |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 2 | * 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 Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 16 | |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 17 | #define LOG_TAG "LatinIME: dictionary.cpp" |
| 18 | |
Ken Wakasa | 77e8e81 | 2012-08-02 19:48:08 +0900 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | |
| 21 | #include "bigram_dictionary.h" |
Jean Chalard | 46a1eec | 2012-02-27 19:48:47 +0900 | [diff] [blame] | 22 | #include "binary_format.h" |
Ken Wakasa | 3b088a2 | 2012-05-16 23:05:32 +0900 | [diff] [blame] | 23 | #include "defines.h" |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 24 | #include "dictionary.h" |
Satoshi Kataoka | e9f3e18 | 2012-08-09 23:23:08 +0900 | [diff] [blame] | 25 | #include "dic_traverse_wrapper.h" |
Satoshi Kataoka | deb0987 | 2012-07-03 17:45:50 +0900 | [diff] [blame] | 26 | #include "gesture_decoder_wrapper.h" |
Ken Wakasa | 77e8e81 | 2012-08-02 19:48:08 +0900 | [diff] [blame] | 27 | #include "unigram_dictionary.h" |
satok | d4952c8 | 2010-12-01 19:09:29 +0900 | [diff] [blame] | 28 | |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 29 | namespace latinime { |
| 30 | |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 31 | // TODO: Change the type of all keyCodes to uint32_t |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 32 | Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust, |
Ken Wakasa | b02ee3d | 2012-10-08 11:46:14 +0900 | [diff] [blame] | 33 | int fullWordMultiplier, int maxWordLength, int maxWords, int maxPredictions) |
Ken Wakasa | 162c211 | 2012-08-24 14:51:15 +0900 | [diff] [blame] | 34 | : mDict(static_cast<unsigned char *>(dict)), |
| 35 | mOffsetDict((static_cast<unsigned char *>(dict)) + BinaryFormat::getHeaderSize(mDict)), |
| 36 | mDictSize(dictSize), mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust), |
Ken Wakasa | b02ee3d | 2012-10-08 11:46:14 +0900 | [diff] [blame] | 37 | mUnigramDictionary(new UnigramDictionary(mOffsetDict, fullWordMultiplier, maxWordLength, |
| 38 | maxWords, BinaryFormat::getFlags(mDict))), |
Ken Wakasa | 162c211 | 2012-08-24 14:51:15 +0900 | [diff] [blame] | 39 | mBigramDictionary(new BigramDictionary(mOffsetDict, maxWordLength, maxPredictions)), |
| 40 | mGestureDecoder(new GestureDecoderWrapper(maxWordLength, maxWords)) { |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 41 | if (DEBUG_DICT) { |
| 42 | if (MAX_WORD_LENGTH_INTERNAL < maxWordLength) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 43 | AKLOGI("Max word length (%d) is greater than %d", |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 44 | maxWordLength, MAX_WORD_LENGTH_INTERNAL); |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 45 | AKLOGI("IN NATIVE SUGGEST Version: %d", (mDict[0] & 0xFF)); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 46 | } |
satok | 715514d | 2010-12-02 20:19:59 +0900 | [diff] [blame] | 47 | } |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 48 | } |
| 49 | |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 50 | Dictionary::~Dictionary() { |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 51 | delete mUnigramDictionary; |
| 52 | delete mBigramDictionary; |
Ken Wakasa | 8658e55 | 2012-06-30 08:53:33 +0900 | [diff] [blame] | 53 | delete mGestureDecoder; |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 54 | } |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 55 | |
Satoshi Kataoka | 9127811 | 2012-08-08 21:23:25 +0900 | [diff] [blame] | 56 | int Dictionary::getSuggestions(ProximityInfo *proximityInfo, void *traverseSession, |
Ken Wakasa | 1e61493 | 2012-10-29 18:06:22 +0900 | [diff] [blame^] | 57 | 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 Wakasa | 77e8e81 | 2012-08-02 19:48:08 +0900 | [diff] [blame] | 61 | int result = 0; |
| 62 | if (isGesture) { |
Satoshi Kataoka | e9f3e18 | 2012-08-09 23:23:08 +0900 | [diff] [blame] | 63 | DicTraverseWrapper::initDicTraverseSession( |
| 64 | traverseSession, this, prevWordChars, prevWordLength); |
Satoshi Kataoka | 9127811 | 2012-08-08 21:23:25 +0900 | [diff] [blame] | 65 | result = mGestureDecoder->getSuggestions(proximityInfo, traverseSession, |
| 66 | xcoordinates, ycoordinates, times, pointerIds, codes, codesSize, commitPoint, |
Ken Wakasa | 77e8e81 | 2012-08-02 19:48:08 +0900 | [diff] [blame] | 67 | outWords, frequencies, spaceIndices, outputTypes); |
Satoshi Kataoka | 586b0ca | 2012-08-06 11:20:54 +0900 | [diff] [blame] | 68 | if (DEBUG_DICT) { |
| 69 | DUMP_RESULT(outWords, frequencies, 18 /* MAX_WORDS */, MAX_WORD_LENGTH_INTERNAL); |
| 70 | } |
Ken Wakasa | 77e8e81 | 2012-08-02 19:48:08 +0900 | [diff] [blame] | 71 | 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 | |
| 84 | int Dictionary::getBigrams(const int32_t *word, int length, int *codes, int codesSize, |
Ken Wakasa | 1e61493 | 2012-10-29 18:06:22 +0900 | [diff] [blame^] | 85 | int *outWords, int *frequencies, int *outputTypes) const { |
Ken Wakasa | 77e8e81 | 2012-08-02 19:48:08 +0900 | [diff] [blame] | 86 | if (length <= 0) return 0; |
| 87 | return mBigramDictionary->getBigrams(word, length, codes, codesSize, outWords, frequencies, |
| 88 | outputTypes); |
| 89 | } |
| 90 | |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 91 | int Dictionary::getFrequency(const int32_t *word, int length) const { |
Satoshi Kataoka | 2f854e1 | 2012-05-29 15:58:13 +0900 | [diff] [blame] | 92 | return mUnigramDictionary->getFrequency(word, length); |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 93 | } |
| 94 | |
Tom Ouyang | 4d289d3 | 2012-04-26 23:50:21 -0700 | [diff] [blame] | 95 | bool Dictionary::isValidBigram(const int32_t *word1, int length1, const int32_t *word2, |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 96 | int length2) const { |
Tom Ouyang | 4d289d3 | 2012-04-26 23:50:21 -0700 | [diff] [blame] | 97 | return mBigramDictionary->isValidBigram(word1, length1, word2, length2); |
| 98 | } |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 99 | } // namespace latinime |