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" |
Ken Wakasa | ffd08e3 | 2012-12-20 18:32:44 +0900 | [diff] [blame] | 26 | #include "gesture_suggest.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 | |
Ken Wakasa | 5db594a | 2013-01-12 01:18:00 +0900 | [diff] [blame] | 31 | Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust) |
Ken Wakasa | 162c211 | 2012-08-24 14:51:15 +0900 | [diff] [blame] | 32 | : mDict(static_cast<unsigned char *>(dict)), |
| 33 | mOffsetDict((static_cast<unsigned char *>(dict)) + BinaryFormat::getHeaderSize(mDict)), |
| 34 | mDictSize(dictSize), mMmapFd(mmapFd), mDictBufAdjust(dictBufAdjust), |
Ken Wakasa | 5db594a | 2013-01-12 01:18:00 +0900 | [diff] [blame] | 35 | mUnigramDictionary(new UnigramDictionary(mOffsetDict, BinaryFormat::getFlags(mDict))), |
| 36 | mBigramDictionary(new BigramDictionary(mOffsetDict)), |
| 37 | mGestureSuggest(new GestureSuggest()) { |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 38 | } |
| 39 | |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 40 | Dictionary::~Dictionary() { |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 41 | delete mUnigramDictionary; |
| 42 | delete mBigramDictionary; |
Ken Wakasa | ffd08e3 | 2012-12-20 18:32:44 +0900 | [diff] [blame] | 43 | delete mGestureSuggest; |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 44 | } |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 45 | |
Satoshi Kataoka | 9127811 | 2012-08-08 21:23:25 +0900 | [diff] [blame] | 46 | int Dictionary::getSuggestions(ProximityInfo *proximityInfo, void *traverseSession, |
Ken Wakasa | 5db594a | 2013-01-12 01:18:00 +0900 | [diff] [blame] | 47 | int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int *inputCodePoints, |
| 48 | int inputSize, int *prevWordCodePoints, int prevWordLength, int commitPoint, bool isGesture, |
Ken Wakasa | 1e61493 | 2012-10-29 18:06:22 +0900 | [diff] [blame] | 49 | bool useFullEditDistance, int *outWords, int *frequencies, int *spaceIndices, |
| 50 | int *outputTypes) const { |
Ken Wakasa | 77e8e81 | 2012-08-02 19:48:08 +0900 | [diff] [blame] | 51 | int result = 0; |
| 52 | if (isGesture) { |
Satoshi Kataoka | e9f3e18 | 2012-08-09 23:23:08 +0900 | [diff] [blame] | 53 | DicTraverseWrapper::initDicTraverseSession( |
Ken Wakasa | 5db594a | 2013-01-12 01:18:00 +0900 | [diff] [blame] | 54 | traverseSession, this, prevWordCodePoints, prevWordLength); |
| 55 | result = mGestureSuggest->getSuggestions(proximityInfo, traverseSession, xcoordinates, |
| 56 | ycoordinates, times, pointerIds, inputCodePoints, inputSize, commitPoint, outWords, |
| 57 | frequencies, spaceIndices, outputTypes); |
Satoshi Kataoka | 586b0ca | 2012-08-06 11:20:54 +0900 | [diff] [blame] | 58 | if (DEBUG_DICT) { |
Ken Wakasa | 5db594a | 2013-01-12 01:18:00 +0900 | [diff] [blame] | 59 | DUMP_RESULT(outWords, frequencies); |
Satoshi Kataoka | 586b0ca | 2012-08-06 11:20:54 +0900 | [diff] [blame] | 60 | } |
Ken Wakasa | 77e8e81 | 2012-08-02 19:48:08 +0900 | [diff] [blame] | 61 | return result; |
| 62 | } else { |
| 63 | std::map<int, int> bigramMap; |
| 64 | uint8_t bigramFilter[BIGRAM_FILTER_BYTE_SIZE]; |
Ken Wakasa | 5db594a | 2013-01-12 01:18:00 +0900 | [diff] [blame] | 65 | mBigramDictionary->fillBigramAddressToFrequencyMapAndFilter(prevWordCodePoints, |
Ken Wakasa | 77e8e81 | 2012-08-02 19:48:08 +0900 | [diff] [blame] | 66 | prevWordLength, &bigramMap, bigramFilter); |
Ken Wakasa | 5db594a | 2013-01-12 01:18:00 +0900 | [diff] [blame] | 67 | result = mUnigramDictionary->getSuggestions(proximityInfo, xcoordinates, ycoordinates, |
| 68 | inputCodePoints, inputSize, &bigramMap, bigramFilter, useFullEditDistance, outWords, |
| 69 | frequencies, outputTypes); |
Ken Wakasa | 77e8e81 | 2012-08-02 19:48:08 +0900 | [diff] [blame] | 70 | return result; |
| 71 | } |
| 72 | } |
| 73 | |
Ken Wakasa | 5db594a | 2013-01-12 01:18:00 +0900 | [diff] [blame] | 74 | int Dictionary::getBigrams(const int *word, int length, int *inputCodePoints, int inputSize, |
Ken Wakasa | 1e61493 | 2012-10-29 18:06:22 +0900 | [diff] [blame] | 75 | int *outWords, int *frequencies, int *outputTypes) const { |
Ken Wakasa | 77e8e81 | 2012-08-02 19:48:08 +0900 | [diff] [blame] | 76 | if (length <= 0) return 0; |
Ken Wakasa | 5db594a | 2013-01-12 01:18:00 +0900 | [diff] [blame] | 77 | return mBigramDictionary->getBigrams(word, length, inputCodePoints, inputSize, outWords, |
| 78 | frequencies, outputTypes); |
Ken Wakasa | 77e8e81 | 2012-08-02 19:48:08 +0900 | [diff] [blame] | 79 | } |
| 80 | |
Ken Wakasa | aa5a3e8 | 2012-12-03 19:54:30 +0900 | [diff] [blame] | 81 | int Dictionary::getFrequency(const int *word, int length) const { |
Satoshi Kataoka | 2f854e1 | 2012-05-29 15:58:13 +0900 | [diff] [blame] | 82 | return mUnigramDictionary->getFrequency(word, length); |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 83 | } |
| 84 | |
Ken Wakasa | aa5a3e8 | 2012-12-03 19:54:30 +0900 | [diff] [blame] | 85 | bool Dictionary::isValidBigram(const int *word1, int length1, const int *word2, int length2) const { |
Tom Ouyang | 4d289d3 | 2012-04-26 23:50:21 -0700 | [diff] [blame] | 86 | return mBigramDictionary->isValidBigram(word1, length1, word2, length2); |
| 87 | } |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 88 | } // namespace latinime |