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 | 1147c7b | 2011-12-14 15:04:58 +0900 | [diff] [blame] | 24 | #include "correction.h" |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 25 | #include "defines.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, |
Jean Chalard | 351864b | 2012-04-24 18:06:51 +0900 | [diff] [blame] | 38 | int *codes, int codesSize, const int32_t* prevWordChars, const int prevWordLength, |
| 39 | bool useFullEditDistance, unsigned short *outWords, int *frequencies) { |
Jean Chalard | 1ff8dc4 | 2012-05-02 16:00:24 +0900 | [diff] [blame] | 40 | std::map<int, int> bigramMap; |
Jean Chalard | f1634c8 | 2012-05-02 19:05:27 +0900 | [diff] [blame] | 41 | uint8_t bigramFilter[BIGRAM_FILTER_BYTE_SIZE]; |
| 42 | mBigramDictionary->fillBigramAddressToFrequencyMapAndFilter(prevWordChars, |
| 43 | prevWordLength, &bigramMap, bigramFilter); |
satok | a7e5a5a | 2011-12-15 16:49:12 +0900 | [diff] [blame] | 44 | return mUnigramDictionary->getSuggestions(proximityInfo, mWordsPriorityQueuePool, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame] | 45 | mCorrection, xcoordinates, ycoordinates, codes, codesSize, &bigramMap, |
| 46 | bigramFilter, useFullEditDistance, outWords, frequencies); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 47 | } |
| 48 | |
Jean Chalard | 522a04e | 2012-04-23 15:37:07 +0900 | [diff] [blame] | 49 | int getBigrams(const int32_t *word, int length, int *codes, int codesSize, |
satok | 6ba8de2 | 2012-03-28 18:21:04 +0900 | [diff] [blame] | 50 | unsigned short *outWords, int *frequencies, int maxWordLength, int maxBigrams) { |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 51 | return mBigramDictionary->getBigrams(word, length, codes, codesSize, outWords, frequencies, |
satok | 6ba8de2 | 2012-03-28 18:21:04 +0900 | [diff] [blame] | 52 | maxWordLength, maxBigrams); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 53 | } |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 54 | |
Satoshi Kataoka | 2f854e1 | 2012-05-29 15:58:13 +0900 | [diff] [blame] | 55 | int getFrequency(const int32_t *word, int length); |
Tom Ouyang | 4d289d3 | 2012-04-26 23:50:21 -0700 | [diff] [blame] | 56 | bool isValidBigram(const int32_t *word1, int length1, const int32_t *word2, int length2); |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 57 | void *getDict() { return (void *)mDict; } |
| 58 | int getDictSize() { return mDictSize; } |
| 59 | int getMmapFd() { return mMmapFd; } |
| 60 | int getDictBufAdjust() { return mDictBufAdjust; } |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 61 | ~Dictionary(); |
Amith Yamasani | cc3e5c7 | 2009-03-31 10:51:17 -0700 | [diff] [blame] | 62 | |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 63 | // public static utility methods |
| 64 | // static inline methods should be defined in the header file |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 65 | static int wideStrLen(unsigned short *str); |
Jean Chalard | 581335c | 2011-06-17 12:45:17 +0900 | [diff] [blame] | 66 | |
Ken Wakasa | e12e9b5 | 2012-01-06 12:24:38 +0900 | [diff] [blame] | 67 | private: |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 68 | const unsigned char *mDict; |
| 69 | |
| 70 | // Used only for the mmap version of dictionary loading, but we use these as dummy variables |
| 71 | // also for the malloc version. |
| 72 | const int mDictSize; |
| 73 | const int mMmapFd; |
| 74 | const int mDictBufAdjust; |
| 75 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 76 | UnigramDictionary *mUnigramDictionary; |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 77 | BigramDictionary *mBigramDictionary; |
satok | a7e5a5a | 2011-12-15 16:49:12 +0900 | [diff] [blame] | 78 | WordsPriorityQueuePool *mWordsPriorityQueuePool; |
satok | 1147c7b | 2011-12-14 15:04:58 +0900 | [diff] [blame] | 79 | Correction *mCorrection; |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 82 | // public static utility methods |
| 83 | // static inline methods should be defined in the header file |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 84 | inline int Dictionary::wideStrLen(unsigned short *str) { |
| 85 | if (!str) return 0; |
| 86 | unsigned short *end = str; |
| 87 | while (*end) |
| 88 | end++; |
| 89 | return end - str; |
| 90 | } |
Ken Wakasa | ce9e52a | 2011-06-18 13:09:55 +0900 | [diff] [blame] | 91 | } // namespace latinime |
| 92 | |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 93 | #endif // LATINIME_DICTIONARY_H |