The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | |
| 17 | #ifndef LATINIME_DICTIONARY_H |
| 18 | #define LATINIME_DICTIONARY_H |
| 19 | |
Jean Chalard | 1ff8dc4 | 2012-05-02 16:00:24 +0900 | [diff] [blame] | 20 | #include <map> |
| 21 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 22 | #include "bigram_dictionary.h" |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 23 | #include "char_utils.h" |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 24 | #include "defines.h" |
Ken Wakasa | 8658e55 | 2012-06-30 08:53:33 +0900 | [diff] [blame] | 25 | #include "incremental_decoder_interface.h" |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 26 | #include "proximity_info.h" |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 27 | #include "unigram_dictionary.h" |
satok | a7e5a5a | 2011-12-15 16:49:12 +0900 | [diff] [blame] | 28 | #include "words_priority_queue_pool.h" |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 29 | |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 30 | namespace latinime { |
| 31 | |
| 32 | class Dictionary { |
Ken Wakasa | e12e9b5 | 2012-01-06 12:24:38 +0900 | [diff] [blame] | 33 | public: |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 34 | Dictionary(void *dict, int dictSize, int mmapFd, int dictBufAdjust, int typedLetterMultipler, |
satok | 6ba8de2 | 2012-03-28 18:21:04 +0900 | [diff] [blame] | 35 | int fullWordMultiplier, int maxWordLength, int maxWords); |
satok | 1147c7b | 2011-12-14 15:04:58 +0900 | [diff] [blame] | 36 | |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 37 | int getSuggestions(ProximityInfo *proximityInfo, int *xcoordinates, int *ycoordinates, |
Satoshi Kataoka | 7368009 | 2012-06-25 17:44:54 +0900 | [diff] [blame] | 38 | int *times, int *pointerIds, int *codes, int codesSize, int *prevWordChars, |
Jean Chalard | 05efe57 | 2012-06-27 17:31:09 +0900 | [diff] [blame] | 39 | int prevWordLength, int commitPoint, bool isGesture, |
Satoshi Kataoka | 7368009 | 2012-06-25 17:44:54 +0900 | [diff] [blame] | 40 | bool useFullEditDistance, unsigned short *outWords, |
| 41 | int *frequencies, int *spaceIndices) { |
| 42 | int result = 0; |
Satoshi Kataoka | 91eb4d8 | 2012-06-26 16:39:07 +0900 | [diff] [blame] | 43 | if (isGesture) { |
| 44 | mGestureDecoder->setPrevWord(prevWordChars, prevWordLength); |
| 45 | result = mGestureDecoder->getSuggestions(proximityInfo, xcoordinates, ycoordinates, |
Jean Chalard | 05efe57 | 2012-06-27 17:31:09 +0900 | [diff] [blame] | 46 | times, pointerIds, codes, codesSize, commitPoint, |
Satoshi Kataoka | 91eb4d8 | 2012-06-26 16:39:07 +0900 | [diff] [blame] | 47 | outWords, frequencies, spaceIndices); |
Satoshi Kataoka | deb0987 | 2012-07-03 17:45:50 +0900 | [diff] [blame^] | 48 | return result; |
Satoshi Kataoka | 91eb4d8 | 2012-06-26 16:39:07 +0900 | [diff] [blame] | 49 | } else { |
| 50 | std::map<int, int> bigramMap; |
| 51 | uint8_t bigramFilter[BIGRAM_FILTER_BYTE_SIZE]; |
| 52 | mBigramDictionary->fillBigramAddressToFrequencyMapAndFilter(prevWordChars, |
| 53 | prevWordLength, &bigramMap, bigramFilter); |
| 54 | result = mUnigramDictionary->getSuggestions(proximityInfo, xcoordinates, |
| 55 | ycoordinates, codes, codesSize, &bigramMap, bigramFilter, |
| 56 | useFullEditDistance, outWords, frequencies); |
Satoshi Kataoka | deb0987 | 2012-07-03 17:45:50 +0900 | [diff] [blame^] | 57 | return result; |
Satoshi Kataoka | 91eb4d8 | 2012-06-26 16:39:07 +0900 | [diff] [blame] | 58 | } |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 59 | } |
| 60 | |
Jean Chalard | 522a04e | 2012-04-23 15:37:07 +0900 | [diff] [blame] | 61 | int getBigrams(const int32_t *word, int length, int *codes, int codesSize, |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 62 | unsigned short *outWords, int *frequencies, int maxWordLength, int maxBigrams) const { |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 63 | return mBigramDictionary->getBigrams(word, length, codes, codesSize, outWords, frequencies, |
satok | 6ba8de2 | 2012-03-28 18:21:04 +0900 | [diff] [blame] | 64 | maxWordLength, maxBigrams); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 65 | } |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 66 | |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 67 | int getFrequency(const int32_t *word, int length) const; |
| 68 | bool isValidBigram(const int32_t *word1, int length1, const int32_t *word2, int length2) const; |
| 69 | void *getDict() const { return (void *)mDict; } |
| 70 | int getDictSize() const { return mDictSize; } |
| 71 | int getMmapFd() const { return mMmapFd; } |
| 72 | int getDictBufAdjust() const { return mDictBufAdjust; } |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 73 | ~Dictionary(); |
Amith Yamasani | cc3e5c7 | 2009-03-31 10:51:17 -0700 | [diff] [blame] | 74 | |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 75 | // public static utility methods |
| 76 | // static inline methods should be defined in the header file |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 77 | static int wideStrLen(unsigned short *str); |
Jean Chalard | 581335c | 2011-06-17 12:45:17 +0900 | [diff] [blame] | 78 | |
Ken Wakasa | e12e9b5 | 2012-01-06 12:24:38 +0900 | [diff] [blame] | 79 | private: |
satok | 1bc038c | 2012-06-14 11:25:50 -0700 | [diff] [blame] | 80 | DISALLOW_IMPLICIT_CONSTRUCTORS(Dictionary); |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 81 | const unsigned char *mDict; |
| 82 | |
| 83 | // Used only for the mmap version of dictionary loading, but we use these as dummy variables |
| 84 | // also for the malloc version. |
| 85 | const int mDictSize; |
| 86 | const int mMmapFd; |
| 87 | const int mDictBufAdjust; |
| 88 | |
satok | 1bc038c | 2012-06-14 11:25:50 -0700 | [diff] [blame] | 89 | const UnigramDictionary *mUnigramDictionary; |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 90 | const BigramDictionary *mBigramDictionary; |
Ken Wakasa | 8658e55 | 2012-06-30 08:53:33 +0900 | [diff] [blame] | 91 | IncrementalDecoderInterface *mGestureDecoder; |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 94 | // public static utility methods |
| 95 | // static inline methods should be defined in the header file |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 96 | inline int Dictionary::wideStrLen(unsigned short *str) { |
| 97 | if (!str) return 0; |
| 98 | unsigned short *end = str; |
| 99 | while (*end) |
| 100 | end++; |
| 101 | return end - str; |
| 102 | } |
Ken Wakasa | ce9e52a | 2011-06-18 13:09:55 +0900 | [diff] [blame] | 103 | } // namespace latinime |
| 104 | |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 105 | #endif // LATINIME_DICTIONARY_H |