satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2010, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 18 | #include <string.h> |
| 19 | |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 20 | #define LOG_TAG "LatinIME: bigram_dictionary.cpp" |
| 21 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 22 | #include "bigram_dictionary.h" |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 23 | #include "binary_format.h" |
Jean Chalard | 49ba135 | 2012-05-07 20:14:00 +0900 | [diff] [blame] | 24 | #include "bloom_filter.h" |
Ken Wakasa | 3b088a2 | 2012-05-16 23:05:32 +0900 | [diff] [blame] | 25 | #include "defines.h" |
Jean Chalard | 49ba135 | 2012-05-07 20:14:00 +0900 | [diff] [blame] | 26 | #include "dictionary.h" |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 27 | |
| 28 | namespace latinime { |
| 29 | |
Jean Chalard | b7d7c5a | 2012-07-11 11:31:48 +0900 | [diff] [blame] | 30 | BigramDictionary::BigramDictionary(const unsigned char *dict, int maxWordLength, int maxPredictions) |
| 31 | : DICT(dict), MAX_WORD_LENGTH(maxWordLength), MAX_PREDICTIONS(maxPredictions) { |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 32 | if (DEBUG_DICT) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 33 | AKLOGI("BigramDictionary - constructor"); |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 34 | } |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 35 | } |
| 36 | |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 37 | BigramDictionary::~BigramDictionary() { |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 38 | } |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 39 | |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 40 | bool BigramDictionary::addWordBigram(unsigned short *word, int length, int frequency, |
Jean Chalard | 1a69ad5 | 2012-07-11 11:37:36 +0900 | [diff] [blame] | 41 | int *bigramFreq, unsigned short *bigramChars) const { |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 42 | word[length] = 0; |
| 43 | if (DEBUG_DICT) { |
Doug Kwan | ce9efbf | 2011-07-07 22:53:50 -0700 | [diff] [blame] | 44 | #ifdef FLAG_DBG |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 45 | char s[length + 1]; |
| 46 | for (int i = 0; i <= length; i++) s[i] = word[i]; |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 47 | AKLOGI("Bigram: Found word = %s, freq = %d :", s, frequency); |
satok | 787945b | 2011-07-14 08:32:57 +0900 | [diff] [blame] | 48 | #endif |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // Find the right insertion point |
| 52 | int insertAt = 0; |
Jean Chalard | 1a69ad5 | 2012-07-11 11:37:36 +0900 | [diff] [blame] | 53 | while (insertAt < MAX_PREDICTIONS) { |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 54 | if (frequency > bigramFreq[insertAt] || (bigramFreq[insertAt] == frequency |
| 55 | && length < Dictionary::wideStrLen(bigramChars + insertAt * MAX_WORD_LENGTH))) { |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 56 | break; |
| 57 | } |
| 58 | insertAt++; |
| 59 | } |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 60 | if (DEBUG_DICT) { |
Jean Chalard | 1a69ad5 | 2012-07-11 11:37:36 +0900 | [diff] [blame] | 61 | AKLOGI("Bigram: InsertAt -> %d MAX_PREDICTIONS: %d", insertAt, MAX_PREDICTIONS); |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 62 | } |
Jean Chalard | 1a69ad5 | 2012-07-11 11:37:36 +0900 | [diff] [blame] | 63 | if (insertAt < MAX_PREDICTIONS) { |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 64 | memmove((char*) bigramFreq + (insertAt + 1) * sizeof(bigramFreq[0]), |
| 65 | (char*) bigramFreq + insertAt * sizeof(bigramFreq[0]), |
Jean Chalard | 1a69ad5 | 2012-07-11 11:37:36 +0900 | [diff] [blame] | 66 | (MAX_PREDICTIONS - insertAt - 1) * sizeof(bigramFreq[0])); |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 67 | bigramFreq[insertAt] = frequency; |
| 68 | memmove((char*) bigramChars + (insertAt + 1) * MAX_WORD_LENGTH * sizeof(short), |
| 69 | (char*) bigramChars + (insertAt ) * MAX_WORD_LENGTH * sizeof(short), |
Jean Chalard | 1a69ad5 | 2012-07-11 11:37:36 +0900 | [diff] [blame] | 70 | (MAX_PREDICTIONS - insertAt - 1) * sizeof(short) * MAX_WORD_LENGTH); |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 71 | unsigned short *dest = bigramChars + (insertAt ) * MAX_WORD_LENGTH; |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 72 | while (length--) { |
| 73 | *dest++ = *word++; |
| 74 | } |
| 75 | *dest = 0; // NULL terminate |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 76 | if (DEBUG_DICT) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 77 | AKLOGI("Bigram: Added word at %d", insertAt); |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 78 | } |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 79 | return true; |
| 80 | } |
| 81 | return false; |
| 82 | } |
| 83 | |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 84 | /* Parameters : |
| 85 | * prevWord: the word before, the one for which we need to look up bigrams. |
| 86 | * prevWordLength: its length. |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 87 | * inputCodes: what user typed, in the same format as for UnigramDictionary::getSuggestions. |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 88 | * codesSize: the size of the codes array. |
| 89 | * bigramChars: an array for output, at the same format as outwords for getSuggestions. |
| 90 | * bigramFreq: an array to output frequencies. |
| 91 | * maxWordLength: the maximum size of a word. |
| 92 | * maxBigrams: the maximum number of bigrams fitting in the bigramChars array. |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 93 | * This method returns the number of bigrams this word has, for backward compatibility. |
| 94 | * Note: this is not the number of bigrams output in the array, which is the number of |
| 95 | * bigrams this word has WHOSE first letter also matches the letter the user typed. |
| 96 | * TODO: this may not be a sensible thing to do. It makes sense when the bigrams are |
| 97 | * used to match the first letter of the second word, but once the user has typed more |
| 98 | * and the bigrams are used to boost unigram result scores, it makes little sense to |
| 99 | * reduce their scope to the ones that match the first letter. |
| 100 | */ |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 101 | int BigramDictionary::getBigrams(const int32_t *prevWord, int prevWordLength, int *inputCodes, |
Jean Chalard | 6a5d17c | 2012-07-11 11:55:25 +0900 | [diff] [blame^] | 102 | int codesSize, unsigned short *bigramChars, int *bigramFreq) const { |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 103 | // TODO: remove unused arguments, and refrain from storing stuff in members of this class |
| 104 | // TODO: have "in" arguments before "out" ones, and make out args explicit in the name |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 105 | |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 106 | const uint8_t* const root = DICT; |
Jean Chalard | e9a86e2 | 2012-06-28 21:01:29 +0900 | [diff] [blame] | 107 | int pos = getBigramListPositionForWord(prevWord, prevWordLength, |
| 108 | false /* forceLowerCaseSearch */); |
Jean Chalard | 351864b | 2012-04-24 18:06:51 +0900 | [diff] [blame] | 109 | // getBigramListPositionForWord returns 0 if this word isn't in the dictionary or has no bigrams |
Jean Chalard | e9a86e2 | 2012-06-28 21:01:29 +0900 | [diff] [blame] | 110 | if (0 == pos) { |
| 111 | // If no bigrams for this exact word, search again in lower case. |
| 112 | pos = getBigramListPositionForWord(prevWord, prevWordLength, |
| 113 | true /* forceLowerCaseSearch */); |
| 114 | } |
| 115 | // If still no bigrams, we really don't have them! |
Jean Chalard | ee396df | 2012-04-17 16:02:35 +0900 | [diff] [blame] | 116 | if (0 == pos) return 0; |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 117 | int bigramFlags; |
| 118 | int bigramCount = 0; |
| 119 | do { |
| 120 | bigramFlags = BinaryFormat::getFlagsAndForwardPointer(root, &pos); |
| 121 | uint16_t bigramBuffer[MAX_WORD_LENGTH]; |
Jean Chalard | 62cd919 | 2012-05-30 14:46:43 +0900 | [diff] [blame] | 122 | int unigramFreq = 0; |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 123 | const int bigramPos = BinaryFormat::getAttributeAddressAndForwardPointer(root, bigramFlags, |
| 124 | &pos); |
| 125 | const int length = BinaryFormat::getWordAtAddress(root, bigramPos, MAX_WORD_LENGTH, |
Jean Chalard | e308459 | 2012-05-29 16:22:46 +0900 | [diff] [blame] | 126 | bigramBuffer, &unigramFreq); |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 127 | |
Jean Chalard | ad290d6 | 2012-02-15 19:22:26 -0800 | [diff] [blame] | 128 | // codesSize == 0 means we are trying to find bigram predictions. |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 129 | if (codesSize < 1 || checkFirstCharacter(bigramBuffer, inputCodes)) { |
| 130 | const int bigramFreqTemp = UnigramDictionary::MASK_ATTRIBUTE_FREQUENCY & bigramFlags; |
Jean Chalard | 46fe49f | 2012-05-29 16:50:25 +0900 | [diff] [blame] | 131 | // Due to space constraints, the frequency for bigrams is approximate - the lower the |
| 132 | // unigram frequency, the worse the precision. The theoritical maximum error in |
| 133 | // resulting frequency is 8 - although in the practice it's never bigger than 3 or 4 |
| 134 | // in very bad cases. This means that sometimes, we'll see some bigrams interverted |
| 135 | // here, but it can't get too bad. |
Jean Chalard | e308459 | 2012-05-29 16:22:46 +0900 | [diff] [blame] | 136 | const int frequency = |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 137 | BinaryFormat::computeFrequencyForBigram(unigramFreq, bigramFreqTemp); |
| 138 | if (addWordBigram( |
Jean Chalard | 1a69ad5 | 2012-07-11 11:37:36 +0900 | [diff] [blame] | 139 | bigramBuffer, length, frequency, bigramFreq, bigramChars)) { |
Jean Chalard | 9715cc4 | 2012-03-21 15:26:45 +0900 | [diff] [blame] | 140 | ++bigramCount; |
| 141 | } |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 142 | } |
Tom Ouyang | 4d289d3 | 2012-04-26 23:50:21 -0700 | [diff] [blame] | 143 | } while (UnigramDictionary::FLAG_ATTRIBUTE_HAS_NEXT & bigramFlags); |
Jean Chalard | 588e2f2 | 2011-07-25 14:03:19 +0900 | [diff] [blame] | 144 | return bigramCount; |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 145 | } |
| 146 | |
Jean Chalard | ee396df | 2012-04-17 16:02:35 +0900 | [diff] [blame] | 147 | // Returns a pointer to the start of the bigram list. |
| 148 | // If the word is not found or has no bigrams, this function returns 0. |
Jean Chalard | 351864b | 2012-04-24 18:06:51 +0900 | [diff] [blame] | 149 | int BigramDictionary::getBigramListPositionForWord(const int32_t *prevWord, |
Jean Chalard | e9a86e2 | 2012-06-28 21:01:29 +0900 | [diff] [blame] | 150 | const int prevWordLength, const bool forceLowerCaseSearch) const { |
Jean Chalard | 1ff8dc4 | 2012-05-02 16:00:24 +0900 | [diff] [blame] | 151 | if (0 >= prevWordLength) return 0; |
Jean Chalard | 351864b | 2012-04-24 18:06:51 +0900 | [diff] [blame] | 152 | const uint8_t* const root = DICT; |
Jean Chalard | e9a86e2 | 2012-06-28 21:01:29 +0900 | [diff] [blame] | 153 | int pos = BinaryFormat::getTerminalPosition(root, prevWord, prevWordLength, |
| 154 | forceLowerCaseSearch); |
Jean Chalard | 9c2a96a | 2012-04-17 11:38:44 +0900 | [diff] [blame] | 155 | |
| 156 | if (NOT_VALID_WORD == pos) return 0; |
| 157 | const int flags = BinaryFormat::getFlagsAndForwardPointer(root, &pos); |
| 158 | if (0 == (flags & UnigramDictionary::FLAG_HAS_BIGRAMS)) return 0; |
| 159 | if (0 == (flags & UnigramDictionary::FLAG_HAS_MULTIPLE_CHARS)) { |
| 160 | BinaryFormat::getCharCodeAndForwardPointer(root, &pos); |
| 161 | } else { |
| 162 | pos = BinaryFormat::skipOtherCharacters(root, pos); |
| 163 | } |
Jean Chalard | 9c2a96a | 2012-04-17 11:38:44 +0900 | [diff] [blame] | 164 | pos = BinaryFormat::skipFrequency(flags, pos); |
Jean Chalard | 402b057 | 2012-05-29 15:56:30 +0900 | [diff] [blame] | 165 | pos = BinaryFormat::skipChildrenPosition(flags, pos); |
Jean Chalard | 9c2a96a | 2012-04-17 11:38:44 +0900 | [diff] [blame] | 166 | pos = BinaryFormat::skipShortcuts(root, flags, pos); |
| 167 | return pos; |
| 168 | } |
| 169 | |
Jean Chalard | f1634c8 | 2012-05-02 19:05:27 +0900 | [diff] [blame] | 170 | void BigramDictionary::fillBigramAddressToFrequencyMapAndFilter(const int32_t *prevWord, |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 171 | const int prevWordLength, std::map<int, int> *map, uint8_t *filter) const { |
Jean Chalard | f1634c8 | 2012-05-02 19:05:27 +0900 | [diff] [blame] | 172 | memset(filter, 0, BIGRAM_FILTER_BYTE_SIZE); |
Jean Chalard | 1ff8dc4 | 2012-05-02 16:00:24 +0900 | [diff] [blame] | 173 | const uint8_t* const root = DICT; |
Jean Chalard | e9a86e2 | 2012-06-28 21:01:29 +0900 | [diff] [blame] | 174 | int pos = getBigramListPositionForWord(prevWord, prevWordLength, |
| 175 | false /* forceLowerCaseSearch */); |
| 176 | if (0 == pos) { |
| 177 | // If no bigrams for this exact string, search again in lower case. |
| 178 | pos = getBigramListPositionForWord(prevWord, prevWordLength, |
| 179 | true /* forceLowerCaseSearch */); |
| 180 | } |
Jean Chalard | 1ff8dc4 | 2012-05-02 16:00:24 +0900 | [diff] [blame] | 181 | if (0 == pos) return; |
| 182 | |
| 183 | int bigramFlags; |
| 184 | do { |
| 185 | bigramFlags = BinaryFormat::getFlagsAndForwardPointer(root, &pos); |
| 186 | const int frequency = UnigramDictionary::MASK_ATTRIBUTE_FREQUENCY & bigramFlags; |
| 187 | const int bigramPos = BinaryFormat::getAttributeAddressAndForwardPointer(root, bigramFlags, |
| 188 | &pos); |
| 189 | (*map)[bigramPos] = frequency; |
Jean Chalard | f1634c8 | 2012-05-02 19:05:27 +0900 | [diff] [blame] | 190 | setInFilter(filter, bigramPos); |
Jean Chalard | 1ff8dc4 | 2012-05-02 16:00:24 +0900 | [diff] [blame] | 191 | } while (0 != (UnigramDictionary::FLAG_ATTRIBUTE_HAS_NEXT & bigramFlags)); |
| 192 | } |
| 193 | |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 194 | bool BigramDictionary::checkFirstCharacter(unsigned short *word, int *inputCodes) const { |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 195 | // Checks whether this word starts with same character or neighboring characters of |
| 196 | // what user typed. |
| 197 | |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 198 | int maxAlt = MAX_ALTERNATIVES; |
Tom Ouyang | aeda8a7 | 2012-03-24 15:31:27 +0900 | [diff] [blame] | 199 | const unsigned short firstBaseChar = toBaseLowerCase(*word); |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 200 | while (maxAlt > 0) { |
Tom Ouyang | aeda8a7 | 2012-03-24 15:31:27 +0900 | [diff] [blame] | 201 | if (toBaseLowerCase(*inputCodes) == firstBaseChar) { |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 202 | return true; |
| 203 | } |
| 204 | inputCodes++; |
| 205 | maxAlt--; |
| 206 | } |
| 207 | return false; |
| 208 | } |
| 209 | |
Tom Ouyang | 4d289d3 | 2012-04-26 23:50:21 -0700 | [diff] [blame] | 210 | bool BigramDictionary::isValidBigram(const int32_t *word1, int length1, const int32_t *word2, |
satok | b1ed1d4 | 2012-06-14 16:35:23 -0700 | [diff] [blame] | 211 | int length2) const { |
Tom Ouyang | 4d289d3 | 2012-04-26 23:50:21 -0700 | [diff] [blame] | 212 | const uint8_t* const root = DICT; |
Jean Chalard | e9a86e2 | 2012-06-28 21:01:29 +0900 | [diff] [blame] | 213 | int pos = getBigramListPositionForWord(word1, length1, false /* forceLowerCaseSearch */); |
Tom Ouyang | 4d289d3 | 2012-04-26 23:50:21 -0700 | [diff] [blame] | 214 | // getBigramListPositionForWord returns 0 if this word isn't in the dictionary or has no bigrams |
| 215 | if (0 == pos) return false; |
Jean Chalard | e9a86e2 | 2012-06-28 21:01:29 +0900 | [diff] [blame] | 216 | int nextWordPos = BinaryFormat::getTerminalPosition(root, word2, length2, |
| 217 | false /* forceLowerCaseSearch */); |
Tom Ouyang | 4d289d3 | 2012-04-26 23:50:21 -0700 | [diff] [blame] | 218 | if (NOT_VALID_WORD == nextWordPos) return false; |
| 219 | int bigramFlags; |
| 220 | do { |
| 221 | bigramFlags = BinaryFormat::getFlagsAndForwardPointer(root, &pos); |
| 222 | const int bigramPos = BinaryFormat::getAttributeAddressAndForwardPointer(root, bigramFlags, |
| 223 | &pos); |
| 224 | if (bigramPos == nextWordPos) { |
| 225 | return true; |
| 226 | } |
| 227 | } while (UnigramDictionary::FLAG_ATTRIBUTE_HAS_NEXT & bigramFlags); |
| 228 | return false; |
| 229 | } |
| 230 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 231 | // TODO: Move functions related to bigram to here |
| 232 | } // namespace latinime |