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 | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 18 | #include <assert.h> |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 19 | #include <fcntl.h> |
satok | f5cded1 | 2010-12-06 21:28:24 +0900 | [diff] [blame] | 20 | #include <stdio.h> |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 21 | #include <string.h> |
| 22 | |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 23 | #define LOG_TAG "LatinIME: unigram_dictionary.cpp" |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 24 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 25 | #include "basechars.h" |
| 26 | #include "char_utils.h" |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 27 | #include "dictionary.h" |
| 28 | #include "unigram_dictionary.h" |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 29 | |
| 30 | namespace latinime { |
| 31 | |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame^] | 32 | const UnigramDictionary::digraph_t UnigramDictionary::GERMAN_UMLAUT_DIGRAPHS[] = |
| 33 | { { 'a', 'e' }, |
| 34 | { 'o', 'e' }, |
| 35 | { 'u', 'e' } }; |
| 36 | |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 37 | UnigramDictionary::UnigramDictionary(const unsigned char *dict, int typedLetterMultiplier, |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 38 | int fullWordMultiplier, int maxWordLength, int maxWords, int maxProximityChars, |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 39 | const bool isLatestDictVersion) |
Tadashi G. Takaoka | 887f11e | 2011-02-10 20:53:58 +0900 | [diff] [blame] | 40 | : DICT(dict), MAX_WORD_LENGTH(maxWordLength), MAX_WORDS(maxWords), |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 41 | MAX_PROXIMITY_CHARS(maxProximityChars), IS_LATEST_DICT_VERSION(isLatestDictVersion), |
| 42 | TYPED_LETTER_MULTIPLIER(typedLetterMultiplier), FULL_WORD_MULTIPLIER(fullWordMultiplier), |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame^] | 43 | ROOT_POS(isLatestDictVersion ? DICTIONARY_HEADER_SIZE : 0), |
| 44 | BYTES_IN_ONE_CHAR(MAX_PROXIMITY_CHARS * sizeof(*mInputCodes)) { |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 45 | if (DEBUG_DICT) LOGI("UnigramDictionary - constructor"); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 46 | } |
| 47 | |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 48 | UnigramDictionary::~UnigramDictionary() {} |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 49 | |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame^] | 50 | static inline unsigned int getCodesBufferSize(const int* codes, const int codesSize, |
| 51 | const int MAX_PROXIMITY_CHARS) { |
| 52 | return sizeof(*codes) * MAX_PROXIMITY_CHARS * codesSize; |
| 53 | } |
| 54 | |
| 55 | bool UnigramDictionary::isDigraph(const int* codes, const int i, const int codesSize) const { |
| 56 | |
| 57 | // There can't be a digraph if we don't have at least 2 characters to examine |
| 58 | if (i + 2 > codesSize) return false; |
| 59 | |
| 60 | // Search for the first char of some digraph |
| 61 | int lastDigraphIndex = -1; |
| 62 | const int thisChar = codes[i * MAX_PROXIMITY_CHARS]; |
| 63 | for (lastDigraphIndex = sizeof(GERMAN_UMLAUT_DIGRAPHS) / sizeof(GERMAN_UMLAUT_DIGRAPHS[0]) - 1; |
| 64 | lastDigraphIndex >= 0; --lastDigraphIndex) { |
| 65 | if (thisChar == GERMAN_UMLAUT_DIGRAPHS[lastDigraphIndex].first) break; |
| 66 | } |
| 67 | // No match: return early |
| 68 | if (lastDigraphIndex < 0) return false; |
| 69 | |
| 70 | // It's an interesting digraph if the second char matches too. |
| 71 | return GERMAN_UMLAUT_DIGRAPHS[lastDigraphIndex].second == codes[(i + 1) * MAX_PROXIMITY_CHARS]; |
| 72 | } |
| 73 | |
| 74 | // Mostly the same arguments as the non-recursive version, except: |
| 75 | // codes is the original value. It points to the start of the work buffer, and gets passed as is. |
| 76 | // codesSize is the size of the user input (thus, it is the size of codesSrc). |
| 77 | // codesDest is the current point in the work buffer. |
| 78 | // codesSrc is the current point in the user-input, original, content-unmodified buffer. |
| 79 | // codesRemain is the remaining size in codesSrc. |
| 80 | void UnigramDictionary::getWordWithDigraphSuggestionsRec(const ProximityInfo *proximityInfo, |
| 81 | const int *xcoordinates, const int* ycoordinates, const int *codesBuffer, |
| 82 | const int codesBufferSize, const int flags, const int* codesSrc, const int codesRemain, |
| 83 | int* codesDest, unsigned short* outWords, int* frequencies) { |
| 84 | |
| 85 | for (int i = 0; i < codesRemain; ++i) { |
| 86 | if (isDigraph(codesSrc, i, codesRemain)) { |
| 87 | // Found a digraph. We will try both spellings. eg. the word is "pruefen" |
| 88 | |
| 89 | // Copy the word up to the first char of the digraph, then continue processing |
| 90 | // on the remaining part of the word, skipping the second char of the digraph. |
| 91 | // In our example, copy "pru" and continue running on "fen" |
| 92 | memcpy(codesDest, codesSrc, i * BYTES_IN_ONE_CHAR); |
| 93 | getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates, codesBuffer, |
| 94 | codesBufferSize, flags, codesSrc + (i + 1) * MAX_PROXIMITY_CHARS, |
| 95 | codesRemain - i - 1, codesDest + i * MAX_PROXIMITY_CHARS, |
| 96 | outWords, frequencies); |
| 97 | |
| 98 | // Copy the second char of the digraph in place, then continue processing on |
| 99 | // the remaining part of the word. |
| 100 | // In our example, after "pru" in the buffer copy the "e", and continue running on "fen" |
| 101 | memcpy(codesDest + i * MAX_PROXIMITY_CHARS, codesSrc + i * MAX_PROXIMITY_CHARS, |
| 102 | BYTES_IN_ONE_CHAR); |
| 103 | getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates, codesBuffer, |
| 104 | codesBufferSize, flags, codesSrc + i * MAX_PROXIMITY_CHARS, codesRemain - i, |
| 105 | codesDest + i * MAX_PROXIMITY_CHARS, outWords, frequencies); |
| 106 | return; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // If we come here, we hit the end of the word: let's check it against the dictionary. |
| 111 | // In our example, we'll come here once for "prufen" and then once for "pruefen". |
| 112 | // If the word contains several digraphs, we'll come it for the product of them. |
| 113 | // eg. if the word is "ueberpruefen" we'll test, in order, against |
| 114 | // "uberprufen", "uberpruefen", "ueberprufen", "ueberpruefen". |
| 115 | const unsigned int remainingBytes = BYTES_IN_ONE_CHAR * codesRemain; |
| 116 | if (0 != remainingBytes) |
| 117 | memcpy(codesDest, codesSrc, remainingBytes); |
| 118 | |
| 119 | getWordSuggestions(proximityInfo, xcoordinates, ycoordinates, codesBuffer, |
| 120 | (codesDest - codesBuffer) / MAX_PROXIMITY_CHARS + codesRemain, outWords, frequencies); |
| 121 | } |
| 122 | |
| 123 | int UnigramDictionary::getSuggestions(const ProximityInfo *proximityInfo, const int *xcoordinates, |
| 124 | const int *ycoordinates, const int *codes, const int codesSize, const int flags, |
| 125 | unsigned short *outWords, int *frequencies) { |
| 126 | |
| 127 | if (REQUIRES_GERMAN_UMLAUT_PROCESSING & flags) |
| 128 | { // Incrementally tune the word and try all possibilities |
| 129 | int codesBuffer[getCodesBufferSize(codes, codesSize, MAX_PROXIMITY_CHARS)]; |
| 130 | getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates, codesBuffer, |
| 131 | codesSize, flags, codes, codesSize, codesBuffer, outWords, frequencies); |
| 132 | } else { // Normal processing |
| 133 | getWordSuggestions(proximityInfo, xcoordinates, ycoordinates, codes, codesSize, |
| 134 | outWords, frequencies); |
| 135 | } |
| 136 | |
| 137 | PROF_START(6); |
| 138 | // Get the word count |
| 139 | int suggestedWordsCount = 0; |
| 140 | while (suggestedWordsCount < MAX_WORDS && mFrequencies[suggestedWordsCount] > 0) { |
| 141 | suggestedWordsCount++; |
| 142 | } |
| 143 | |
| 144 | if (DEBUG_DICT) { |
| 145 | LOGI("Returning %d words", suggestedWordsCount); |
| 146 | LOGI("Next letters: "); |
| 147 | for (int k = 0; k < NEXT_LETTERS_SIZE; k++) { |
| 148 | if (mNextLettersFrequency[k] > 0) { |
| 149 | LOGI("%c = %d,", k, mNextLettersFrequency[k]); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | PROF_END(6); |
| 154 | PROF_CLOSE; |
| 155 | return suggestedWordsCount; |
| 156 | } |
| 157 | |
| 158 | void UnigramDictionary::getWordSuggestions(const ProximityInfo *proximityInfo, |
| 159 | const int *xcoordinates, const int *ycoordinates, const int *codes, const int codesSize, |
| 160 | unsigned short *outWords, int *frequencies) { |
| 161 | |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 162 | PROF_OPEN; |
| 163 | PROF_START(0); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 164 | initSuggestions(codes, codesSize, outWords, frequencies); |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 165 | if (DEBUG_DICT) assert(codesSize == mInputLength); |
| 166 | |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 167 | if (DEBUG_PROXIMITY_INFO) { |
| 168 | for (int i = 0; i < codesSize; ++i) { |
| 169 | LOGI("Input[%d] x = %d, y = %d", i, xcoordinates[i], ycoordinates[i]); |
| 170 | } |
| 171 | } |
| 172 | |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 173 | const int MAX_DEPTH = min(mInputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH); |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 174 | PROF_END(0); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 175 | |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 176 | PROF_START(1); |
Tadashi G. Takaoka | 887f11e | 2011-02-10 20:53:58 +0900 | [diff] [blame] | 177 | getSuggestionCandidates(-1, -1, -1, mNextLettersFrequency, NEXT_LETTERS_SIZE, MAX_DEPTH); |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 178 | PROF_END(1); |
| 179 | |
| 180 | PROF_START(2); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 181 | // Suggestion with missing character |
| 182 | if (SUGGEST_WORDS_WITH_MISSING_CHARACTER) { |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 183 | for (int i = 0; i < codesSize; ++i) { |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame] | 184 | if (DEBUG_DICT) LOGI("--- Suggest missing characters %d", i); |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 185 | getSuggestionCandidates(i, -1, -1, NULL, 0, MAX_DEPTH); |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame] | 186 | } |
| 187 | } |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 188 | PROF_END(2); |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame] | 189 | |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 190 | PROF_START(3); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 191 | // Suggestion with excessive character |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 192 | if (SUGGEST_WORDS_WITH_EXCESSIVE_CHARACTER |
| 193 | && mInputLength >= MIN_USER_TYPED_LENGTH_FOR_EXCESSIVE_CHARACTER_SUGGESTION) { |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame] | 194 | for (int i = 0; i < codesSize; ++i) { |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 195 | if (DEBUG_DICT) LOGI("--- Suggest excessive characters %d", i); |
| 196 | getSuggestionCandidates(-1, i, -1, NULL, 0, MAX_DEPTH); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 197 | } |
| 198 | } |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 199 | PROF_END(3); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 200 | |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 201 | PROF_START(4); |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 202 | // Suggestion with transposed characters |
| 203 | // Only suggest words that length is mInputLength |
| 204 | if (SUGGEST_WORDS_WITH_TRANSPOSED_CHARACTERS) { |
| 205 | for (int i = 0; i < codesSize; ++i) { |
| 206 | if (DEBUG_DICT) LOGI("--- Suggest transposed characters %d", i); |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 207 | getSuggestionCandidates(-1, -1, i, NULL, 0, mInputLength - 1); |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 208 | } |
| 209 | } |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 210 | PROF_END(4); |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 211 | |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 212 | PROF_START(5); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 213 | // Suggestions with missing space |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 214 | if (SUGGEST_WORDS_WITH_MISSING_SPACE_CHARACTER |
| 215 | && mInputLength >= MIN_USER_TYPED_LENGTH_FOR_MISSING_SPACE_SUGGESTION) { |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 216 | for (int i = 1; i < codesSize; ++i) { |
| 217 | if (DEBUG_DICT) LOGI("--- Suggest missing space characters %d", i); |
| 218 | getMissingSpaceWords(mInputLength, i); |
| 219 | } |
| 220 | } |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 221 | PROF_END(5); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 222 | } |
| 223 | |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame^] | 224 | void UnigramDictionary::initSuggestions(const int *codes, const int codesSize, |
| 225 | unsigned short *outWords, int *frequencies) { |
satok | f5cded1 | 2010-12-06 21:28:24 +0900 | [diff] [blame] | 226 | if (DEBUG_DICT) LOGI("initSuggest"); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 227 | mFrequencies = frequencies; |
| 228 | mOutputChars = outWords; |
| 229 | mInputCodes = codes; |
| 230 | mInputLength = codesSize; |
| 231 | mMaxEditDistance = mInputLength < 5 ? 2 : mInputLength / 2; |
| 232 | } |
| 233 | |
satok | 715514d | 2010-12-02 20:19:59 +0900 | [diff] [blame] | 234 | void UnigramDictionary::registerNextLetter( |
| 235 | unsigned short c, int *nextLetters, int nextLettersSize) { |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 236 | if (c < nextLettersSize) { |
| 237 | nextLetters[c]++; |
| 238 | } |
| 239 | } |
| 240 | |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 241 | // TODO: We need to optimize addWord by using STL or something |
satok | 28bd03b | 2010-12-03 16:39:16 +0900 | [diff] [blame] | 242 | bool UnigramDictionary::addWord(unsigned short *word, int length, int frequency) { |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 243 | word[length] = 0; |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 244 | if (DEBUG_DICT && DEBUG_SHOW_FOUND_WORD) { |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 245 | char s[length + 1]; |
| 246 | for (int i = 0; i <= length; i++) s[i] = word[i]; |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 247 | LOGI("Found word = %s, freq = %d", s, frequency); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 248 | } |
satok | f5cded1 | 2010-12-06 21:28:24 +0900 | [diff] [blame] | 249 | if (length > MAX_WORD_LENGTH) { |
| 250 | if (DEBUG_DICT) LOGI("Exceeded max word length."); |
| 251 | return false; |
| 252 | } |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 253 | |
| 254 | // Find the right insertion point |
| 255 | int insertAt = 0; |
| 256 | while (insertAt < MAX_WORDS) { |
satok | 715514d | 2010-12-02 20:19:59 +0900 | [diff] [blame] | 257 | if (frequency > mFrequencies[insertAt] || (mFrequencies[insertAt] == frequency |
| 258 | && length < Dictionary::wideStrLen(mOutputChars + insertAt * MAX_WORD_LENGTH))) { |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 259 | break; |
| 260 | } |
| 261 | insertAt++; |
| 262 | } |
| 263 | if (insertAt < MAX_WORDS) { |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame] | 264 | if (DEBUG_DICT) { |
| 265 | char s[length + 1]; |
| 266 | for (int i = 0; i <= length; i++) s[i] = word[i]; |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 267 | LOGI("Added word = %s, freq = %d", s, frequency); |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame] | 268 | } |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 269 | memmove((char*) mFrequencies + (insertAt + 1) * sizeof(mFrequencies[0]), |
| 270 | (char*) mFrequencies + insertAt * sizeof(mFrequencies[0]), |
| 271 | (MAX_WORDS - insertAt - 1) * sizeof(mFrequencies[0])); |
| 272 | mFrequencies[insertAt] = frequency; |
| 273 | memmove((char*) mOutputChars + (insertAt + 1) * MAX_WORD_LENGTH * sizeof(short), |
satok | 715514d | 2010-12-02 20:19:59 +0900 | [diff] [blame] | 274 | (char*) mOutputChars + insertAt * MAX_WORD_LENGTH * sizeof(short), |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 275 | (MAX_WORDS - insertAt - 1) * sizeof(short) * MAX_WORD_LENGTH); |
satok | 715514d | 2010-12-02 20:19:59 +0900 | [diff] [blame] | 276 | unsigned short *dest = mOutputChars + insertAt * MAX_WORD_LENGTH; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 277 | while (length--) { |
| 278 | *dest++ = *word++; |
| 279 | } |
| 280 | *dest = 0; // NULL terminate |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 281 | if (DEBUG_DICT) LOGI("Added word at %d", insertAt); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 282 | return true; |
| 283 | } |
| 284 | return false; |
| 285 | } |
| 286 | |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 287 | unsigned short UnigramDictionary::toBaseLowerCase(unsigned short c) { |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 288 | if (c < sizeof(BASE_CHARS) / sizeof(BASE_CHARS[0])) { |
| 289 | c = BASE_CHARS[c]; |
| 290 | } |
| 291 | if (c >='A' && c <= 'Z') { |
| 292 | c |= 32; |
| 293 | } else if (c > 127) { |
| 294 | c = latin_tolower(c); |
| 295 | } |
| 296 | return c; |
| 297 | } |
| 298 | |
satok | 28bd03b | 2010-12-03 16:39:16 +0900 | [diff] [blame] | 299 | bool UnigramDictionary::sameAsTyped(unsigned short *word, int length) { |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 300 | if (length != mInputLength) { |
| 301 | return false; |
| 302 | } |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame^] | 303 | const int *inputCodes = mInputCodes; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 304 | while (length--) { |
| 305 | if ((unsigned int) *inputCodes != (unsigned int) *word) { |
| 306 | return false; |
| 307 | } |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 308 | inputCodes += MAX_PROXIMITY_CHARS; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 309 | word++; |
| 310 | } |
| 311 | return true; |
| 312 | } |
| 313 | |
satok | 715514d | 2010-12-02 20:19:59 +0900 | [diff] [blame] | 314 | static const char QUOTE = '\''; |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 315 | static const char SPACE = ' '; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 316 | |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 317 | void UnigramDictionary::getSuggestionCandidates(const int skipPos, |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 318 | const int excessivePos, const int transposedPos, int *nextLetters, |
| 319 | const int nextLettersSize, const int maxDepth) { |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 320 | if (DEBUG_DICT) { |
| 321 | LOGI("getSuggestionCandidates %d", maxDepth); |
| 322 | assert(transposedPos + 1 < mInputLength); |
| 323 | assert(excessivePos < mInputLength); |
| 324 | assert(missingPos < mInputLength); |
| 325 | } |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 326 | int rootPosition = ROOT_POS; |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 327 | // Get the number of child of root, then increment the position |
| 328 | int childCount = Dictionary::getCount(DICT, &rootPosition); |
| 329 | int depth = 0; |
| 330 | |
| 331 | mStackChildCount[0] = childCount; |
| 332 | mStackTraverseAll[0] = (mInputLength <= 0); |
| 333 | mStackNodeFreq[0] = 1; |
| 334 | mStackInputIndex[0] = 0; |
| 335 | mStackDiffs[0] = 0; |
| 336 | mStackSiblingPos[0] = rootPosition; |
| 337 | |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 338 | // Depth first search |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 339 | while (depth >= 0) { |
| 340 | if (mStackChildCount[depth] > 0) { |
| 341 | --mStackChildCount[depth]; |
| 342 | bool traverseAllNodes = mStackTraverseAll[depth]; |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 343 | int matchWeight = mStackNodeFreq[depth]; |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 344 | int inputIndex = mStackInputIndex[depth]; |
| 345 | int diffs = mStackDiffs[depth]; |
| 346 | int siblingPos = mStackSiblingPos[depth]; |
| 347 | int firstChildPos; |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 348 | // depth will never be greater than maxDepth because in that case, |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 349 | // needsToTraverseChildrenNodes should be false |
| 350 | const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth, |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 351 | maxDepth, traverseAllNodes, matchWeight, inputIndex, diffs, skipPos, |
| 352 | excessivePos, transposedPos, nextLetters, nextLettersSize, &childCount, |
| 353 | &firstChildPos, &traverseAllNodes, &matchWeight, &inputIndex, &diffs, |
| 354 | &siblingPos); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 355 | // Update next sibling pos |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 356 | mStackSiblingPos[depth] = siblingPos; |
| 357 | if (needsToTraverseChildrenNodes) { |
| 358 | // Goes to child node |
| 359 | ++depth; |
| 360 | mStackChildCount[depth] = childCount; |
| 361 | mStackTraverseAll[depth] = traverseAllNodes; |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 362 | mStackNodeFreq[depth] = matchWeight; |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 363 | mStackInputIndex[depth] = inputIndex; |
| 364 | mStackDiffs[depth] = diffs; |
| 365 | mStackSiblingPos[depth] = firstChildPos; |
| 366 | } |
| 367 | } else { |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame] | 368 | // Goes to parent sibling node |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 369 | --depth; |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
satok | f7425bb | 2011-01-05 16:37:53 +0900 | [diff] [blame] | 374 | inline static void multiplyRate(const int rate, int *freq) { |
| 375 | if (rate > 1000000) { |
| 376 | *freq = (*freq / 100) * rate; |
| 377 | } else { |
| 378 | *freq = *freq * rate / 100; |
| 379 | } |
| 380 | } |
| 381 | |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 382 | bool UnigramDictionary::getMissingSpaceWords(const int inputLength, const int missingSpacePos) { |
satok | aee09dc | 2010-12-09 19:21:51 +0900 | [diff] [blame] | 383 | if (missingSpacePos <= 0 || missingSpacePos >= inputLength |
| 384 | || inputLength >= MAX_WORD_LENGTH) return false; |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 385 | const int newWordLength = inputLength + 1; |
| 386 | // Allocating variable length array on stack |
| 387 | unsigned short word[newWordLength]; |
satok | aee09dc | 2010-12-09 19:21:51 +0900 | [diff] [blame] | 388 | const int firstFreq = getBestWordFreq(0, missingSpacePos, mWord); |
| 389 | if (DEBUG_DICT) LOGI("First freq: %d", firstFreq); |
| 390 | if (firstFreq <= 0) return false; |
| 391 | |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 392 | for (int i = 0; i < missingSpacePos; ++i) { |
satok | aee09dc | 2010-12-09 19:21:51 +0900 | [diff] [blame] | 393 | word[i] = mWord[i]; |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 394 | } |
satok | aee09dc | 2010-12-09 19:21:51 +0900 | [diff] [blame] | 395 | |
| 396 | const int secondFreq = getBestWordFreq(missingSpacePos, inputLength - missingSpacePos, mWord); |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 397 | if (DEBUG_DICT) LOGI("Second freq: %d", secondFreq); |
satok | aee09dc | 2010-12-09 19:21:51 +0900 | [diff] [blame] | 398 | if (secondFreq <= 0) return false; |
| 399 | |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 400 | word[missingSpacePos] = SPACE; |
| 401 | for (int i = (missingSpacePos + 1); i < newWordLength; ++i) { |
satok | aee09dc | 2010-12-09 19:21:51 +0900 | [diff] [blame] | 402 | word[i] = mWord[i - missingSpacePos - 1]; |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 403 | } |
satok | aee09dc | 2010-12-09 19:21:51 +0900 | [diff] [blame] | 404 | |
| 405 | int pairFreq = ((firstFreq + secondFreq) / 2); |
| 406 | for (int i = 0; i < inputLength; ++i) pairFreq *= TYPED_LETTER_MULTIPLIER; |
satok | f7425bb | 2011-01-05 16:37:53 +0900 | [diff] [blame] | 407 | multiplyRate(WORDS_WITH_MISSING_SPACE_CHARACTER_DEMOTION_RATE, &pairFreq); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 408 | addWord(word, newWordLength, pairFreq); |
| 409 | return true; |
| 410 | } |
| 411 | |
| 412 | // Keep this for comparing spec to new getWords |
| 413 | void UnigramDictionary::getWordsOld(const int initialPos, const int inputLength, const int skipPos, |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 414 | const int excessivePos, const int transposedPos,int *nextLetters, |
| 415 | const int nextLettersSize) { |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 416 | int initialPosition = initialPos; |
| 417 | const int count = Dictionary::getCount(DICT, &initialPosition); |
| 418 | getWordsRec(count, initialPosition, 0, |
| 419 | min(inputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH), |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 420 | mInputLength <= 0, 1, 0, 0, skipPos, excessivePos, transposedPos, nextLetters, |
| 421 | nextLettersSize); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 422 | } |
| 423 | |
satok | 6831926 | 2010-12-03 19:38:08 +0900 | [diff] [blame] | 424 | void UnigramDictionary::getWordsRec(const int childrenCount, const int pos, const int depth, |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 425 | const int maxDepth, const bool traverseAllNodes, const int matchWeight, |
| 426 | const int inputIndex, const int diffs, const int skipPos, const int excessivePos, |
| 427 | const int transposedPos, int *nextLetters, const int nextLettersSize) { |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 428 | int siblingPos = pos; |
satok | 6831926 | 2010-12-03 19:38:08 +0900 | [diff] [blame] | 429 | for (int i = 0; i < childrenCount; ++i) { |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 430 | int newCount; |
| 431 | int newChildPosition; |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 432 | const int newDepth = depth + 1; |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 433 | bool newTraverseAllNodes; |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 434 | int newMatchRate; |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 435 | int newInputIndex; |
| 436 | int newDiffs; |
| 437 | int newSiblingPos; |
| 438 | const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth, maxDepth, |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 439 | traverseAllNodes, matchWeight, inputIndex, diffs, |
| 440 | skipPos, excessivePos, transposedPos, |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 441 | nextLetters, nextLettersSize, |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 442 | &newCount, &newChildPosition, &newTraverseAllNodes, &newMatchRate, |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 443 | &newInputIndex, &newDiffs, &newSiblingPos); |
| 444 | siblingPos = newSiblingPos; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 445 | |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 446 | if (needsToTraverseChildrenNodes) { |
| 447 | getWordsRec(newCount, newChildPosition, newDepth, maxDepth, newTraverseAllNodes, |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 448 | newMatchRate, newInputIndex, newDiffs, skipPos, excessivePos, transposedPos, |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 449 | nextLetters, nextLettersSize); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
Jean Chalard | a5d5849 | 2011-02-18 17:50:58 +0900 | [diff] [blame] | 454 | static const int TWO_31ST_DIV_255 = ((1 << 31) - 1) / 255; |
| 455 | static inline int capped255MultForFullMatchAccentsOrCapitalizationDifference(const int num) { |
| 456 | return (num < TWO_31ST_DIV_255 ? 255 * num : S_INT_MAX); |
| 457 | } |
satok | 58c49b9 | 2011-01-27 03:23:39 +0900 | [diff] [blame] | 458 | inline int UnigramDictionary::calculateFinalFreq(const int inputIndex, const int depth, |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 459 | const int matchWeight, const int skipPos, const int excessivePos, const int transposedPos, |
satok | 58c49b9 | 2011-01-27 03:23:39 +0900 | [diff] [blame] | 460 | const int freq, const bool sameLength) { |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 461 | // TODO: Demote by edit distance |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 462 | int finalFreq = freq * matchWeight; |
satok | f7425bb | 2011-01-05 16:37:53 +0900 | [diff] [blame] | 463 | if (skipPos >= 0) multiplyRate(WORDS_WITH_MISSING_CHARACTER_DEMOTION_RATE, &finalFreq); |
| 464 | if (transposedPos >= 0) multiplyRate( |
| 465 | WORDS_WITH_TRANSPOSED_CHARACTERS_DEMOTION_RATE, &finalFreq); |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 466 | if (excessivePos >= 0) { |
satok | f7425bb | 2011-01-05 16:37:53 +0900 | [diff] [blame] | 467 | multiplyRate(WORDS_WITH_EXCESSIVE_CHARACTER_DEMOTION_RATE, &finalFreq); |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 468 | if (!existsAdjacentProximityChars(inputIndex, mInputLength)) { |
satok | f7425bb | 2011-01-05 16:37:53 +0900 | [diff] [blame] | 469 | multiplyRate(WORDS_WITH_EXCESSIVE_CHARACTER_OUT_OF_PROXIMITY_DEMOTION_RATE, &finalFreq); |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 470 | } |
| 471 | } |
satok | 58c49b9 | 2011-01-27 03:23:39 +0900 | [diff] [blame] | 472 | int lengthFreq = TYPED_LETTER_MULTIPLIER; |
| 473 | for (int i = 0; i < depth; ++i) lengthFreq *= TYPED_LETTER_MULTIPLIER; |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 474 | if (lengthFreq == matchWeight) { |
Jean Chalard | 8dc754a | 2011-01-27 14:20:22 +0900 | [diff] [blame] | 475 | if (depth > 1) { |
| 476 | if (DEBUG_DICT) LOGI("Found full matched word."); |
| 477 | multiplyRate(FULL_MATCHED_WORDS_PROMOTION_RATE, &finalFreq); |
| 478 | } |
| 479 | if (sameLength && transposedPos < 0 && skipPos < 0 && excessivePos < 0) { |
Jean Chalard | a5d5849 | 2011-02-18 17:50:58 +0900 | [diff] [blame] | 480 | finalFreq = capped255MultForFullMatchAccentsOrCapitalizationDifference(finalFreq); |
Jean Chalard | 8dc754a | 2011-01-27 14:20:22 +0900 | [diff] [blame] | 481 | } |
satok | 58c49b9 | 2011-01-27 03:23:39 +0900 | [diff] [blame] | 482 | } |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 483 | if (sameLength && skipPos < 0) finalFreq *= FULL_WORD_MULTIPLIER; |
| 484 | return finalFreq; |
| 485 | } |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 486 | |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 487 | inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsGreaterThanInputLength( |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 488 | unsigned short *word, const int inputIndex, const int depth, const int matchWeight, |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 489 | int *nextLetters, const int nextLettersSize, const int skipPos, const int excessivePos, |
| 490 | const int transposedPos, const int freq) { |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 491 | const int finalFreq = calculateFinalFreq(inputIndex, depth, matchWeight, skipPos, excessivePos, |
satok | 58c49b9 | 2011-01-27 03:23:39 +0900 | [diff] [blame] | 492 | transposedPos, freq, false); |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 493 | if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq); |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 494 | if (depth >= mInputLength && skipPos < 0) { |
satok | 715514d | 2010-12-02 20:19:59 +0900 | [diff] [blame] | 495 | registerNextLetter(mWord[mInputLength], nextLetters, nextLettersSize); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsSameAsInputLength( |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 500 | unsigned short *word, const int inputIndex, const int depth, const int matchWeight, |
Jean Chalard | 8dc754a | 2011-01-27 14:20:22 +0900 | [diff] [blame] | 501 | const int skipPos, const int excessivePos, const int transposedPos, const int freq) { |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 502 | if (sameAsTyped(word, depth + 1)) return; |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 503 | const int finalFreq = calculateFinalFreq(inputIndex, depth, matchWeight, skipPos, |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 504 | excessivePos, transposedPos, freq, true); |
| 505 | // Proximity collection will promote a word of the same length as what user typed. |
| 506 | if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq); |
satok | 715514d | 2010-12-02 20:19:59 +0900 | [diff] [blame] | 507 | } |
satok | 28bd03b | 2010-12-03 16:39:16 +0900 | [diff] [blame] | 508 | |
| 509 | inline bool UnigramDictionary::needsToSkipCurrentNode(const unsigned short c, |
satok | 6831926 | 2010-12-03 19:38:08 +0900 | [diff] [blame] | 510 | const int inputIndex, const int skipPos, const int depth) { |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 511 | const unsigned short userTypedChar = getInputCharsAt(inputIndex)[0]; |
satok | 28bd03b | 2010-12-03 16:39:16 +0900 | [diff] [blame] | 512 | // Skip the ' or other letter and continue deeper |
| 513 | return (c == QUOTE && userTypedChar != QUOTE) || skipPos == depth; |
| 514 | } |
| 515 | |
satok | e07baa6 | 2010-12-09 21:55:40 +0900 | [diff] [blame] | 516 | inline bool UnigramDictionary::existsAdjacentProximityChars(const int inputIndex, |
| 517 | const int inputLength) { |
| 518 | if (inputIndex < 0 || inputIndex >= inputLength) return false; |
| 519 | const int currentChar = *getInputCharsAt(inputIndex); |
| 520 | const int leftIndex = inputIndex - 1; |
| 521 | if (leftIndex >= 0) { |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame^] | 522 | const int *leftChars = getInputCharsAt(leftIndex); |
satok | e07baa6 | 2010-12-09 21:55:40 +0900 | [diff] [blame] | 523 | int i = 0; |
| 524 | while (leftChars[i] > 0 && i < MAX_PROXIMITY_CHARS) { |
| 525 | if (leftChars[i++] == currentChar) return true; |
| 526 | } |
| 527 | } |
| 528 | const int rightIndex = inputIndex + 1; |
| 529 | if (rightIndex < inputLength) { |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame^] | 530 | const int *rightChars = getInputCharsAt(rightIndex); |
satok | e07baa6 | 2010-12-09 21:55:40 +0900 | [diff] [blame] | 531 | int i = 0; |
| 532 | while (rightChars[i] > 0 && i < MAX_PROXIMITY_CHARS) { |
| 533 | if (rightChars[i++] == currentChar) return true; |
| 534 | } |
| 535 | } |
| 536 | return false; |
| 537 | } |
| 538 | |
Jean Chalard | a5d5849 | 2011-02-18 17:50:58 +0900 | [diff] [blame] | 539 | |
| 540 | // In the following function, c is the current character of the dictionary word |
| 541 | // currently examined. |
| 542 | // currentChars is an array containing the keys close to the character the |
| 543 | // user actually typed at the same position. We want to see if c is in it: if so, |
| 544 | // then the word contains at that position a character close to what the user |
| 545 | // typed. |
| 546 | // What the user typed is actually the first character of the array. |
| 547 | // Notice : accented characters do not have a proximity list, so they are alone |
| 548 | // in their list. The non-accented version of the character should be considered |
| 549 | // "close", but not the other keys close to the non-accented version. |
Jean Chalard | 8dc754a | 2011-01-27 14:20:22 +0900 | [diff] [blame] | 550 | inline UnigramDictionary::ProximityType UnigramDictionary::getMatchedProximityId( |
| 551 | const int *currentChars, const unsigned short c, const int skipPos, |
| 552 | const int excessivePos, const int transposedPos) { |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 553 | const unsigned short baseLowerC = toBaseLowerCase(c); |
Jean Chalard | a5d5849 | 2011-02-18 17:50:58 +0900 | [diff] [blame] | 554 | |
| 555 | // The first char in the array is what user typed. If it matches right away, |
| 556 | // that means the user typed that same char for this pos. |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 557 | if (currentChars[0] == baseLowerC || currentChars[0] == c) |
Jean Chalard | a5d5849 | 2011-02-18 17:50:58 +0900 | [diff] [blame] | 558 | return SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR; |
| 559 | |
| 560 | // If one of those is true, we should not check for close characters at all. |
| 561 | if (skipPos >= 0 || excessivePos >= 0 || transposedPos >= 0) |
| 562 | return UNRELATED_CHAR; |
| 563 | |
| 564 | // If the non-accented, lowercased version of that first character matches c, |
| 565 | // then we have a non-accented version of the accented character the user |
| 566 | // typed. Treat it as a close char. |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 567 | if (toBaseLowerCase(currentChars[0]) == baseLowerC) |
Jean Chalard | a5d5849 | 2011-02-18 17:50:58 +0900 | [diff] [blame] | 568 | return NEAR_PROXIMITY_CHAR; |
| 569 | |
| 570 | // Not an exact nor an accent-alike match: search the list of close keys |
| 571 | int j = 1; |
satok | e07baa6 | 2010-12-09 21:55:40 +0900 | [diff] [blame] | 572 | while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS) { |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 573 | const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c); |
Jean Chalard | a5d5849 | 2011-02-18 17:50:58 +0900 | [diff] [blame] | 574 | if (matched) return NEAR_PROXIMITY_CHAR; |
satok | 28bd03b | 2010-12-03 16:39:16 +0900 | [diff] [blame] | 575 | ++j; |
| 576 | } |
Jean Chalard | a5d5849 | 2011-02-18 17:50:58 +0900 | [diff] [blame] | 577 | |
| 578 | // Was not included, signal this as an unrelated character. |
Jean Chalard | 8dc754a | 2011-01-27 14:20:22 +0900 | [diff] [blame] | 579 | return UNRELATED_CHAR; |
satok | 28bd03b | 2010-12-03 16:39:16 +0900 | [diff] [blame] | 580 | } |
| 581 | |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 582 | inline bool UnigramDictionary::processCurrentNode(const int pos, const int depth, |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 583 | const int maxDepth, const bool traverseAllNodes, int matchWeight, int inputIndex, |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 584 | const int diffs, const int skipPos, const int excessivePos, const int transposedPos, |
| 585 | int *nextLetters, const int nextLettersSize, int *newCount, int *newChildPosition, |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 586 | bool *newTraverseAllNodes, int *newMatchRate, int *newInputIndex, int *newDiffs, |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 587 | int *nextSiblingPosition) { |
| 588 | if (DEBUG_DICT) { |
| 589 | int inputCount = 0; |
| 590 | if (skipPos >= 0) ++inputCount; |
| 591 | if (excessivePos >= 0) ++inputCount; |
| 592 | if (transposedPos >= 0) ++inputCount; |
| 593 | assert(inputCount <= 1); |
| 594 | } |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 595 | unsigned short c; |
| 596 | int childPosition; |
| 597 | bool terminal; |
| 598 | int freq; |
satok | fd16f1d | 2011-01-27 16:25:16 +0900 | [diff] [blame] | 599 | bool isSameAsUserTypedLength = false; |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame] | 600 | |
satok | fd16f1d | 2011-01-27 16:25:16 +0900 | [diff] [blame] | 601 | if (excessivePos == depth && inputIndex < mInputLength - 1) ++inputIndex; |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame] | 602 | |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 603 | *nextSiblingPosition = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, pos, &c, |
| 604 | &childPosition, &terminal, &freq); |
| 605 | |
| 606 | const bool needsToTraverseChildrenNodes = childPosition != 0; |
| 607 | |
| 608 | // If we are only doing traverseAllNodes, no need to look at the typed characters. |
| 609 | if (traverseAllNodes || needsToSkipCurrentNode(c, inputIndex, skipPos, depth)) { |
| 610 | mWord[depth] = c; |
| 611 | if (traverseAllNodes && terminal) { |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 612 | onTerminalWhenUserTypedLengthIsGreaterThanInputLength(mWord, inputIndex, depth, |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 613 | matchWeight, nextLetters, nextLettersSize, skipPos, excessivePos, transposedPos, |
| 614 | freq); |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 615 | } |
| 616 | if (!needsToTraverseChildrenNodes) return false; |
| 617 | *newTraverseAllNodes = traverseAllNodes; |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 618 | *newMatchRate = matchWeight; |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 619 | *newDiffs = diffs; |
| 620 | *newInputIndex = inputIndex; |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 621 | } else { |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame^] | 622 | const int *currentChars = getInputCharsAt(inputIndex); |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 623 | |
| 624 | if (transposedPos >= 0) { |
| 625 | if (inputIndex == transposedPos) currentChars += MAX_PROXIMITY_CHARS; |
| 626 | if (inputIndex == (transposedPos + 1)) currentChars -= MAX_PROXIMITY_CHARS; |
| 627 | } |
| 628 | |
| 629 | int matchedProximityCharId = getMatchedProximityId(currentChars, c, skipPos, excessivePos, |
| 630 | transposedPos); |
Jean Chalard | 8dc754a | 2011-01-27 14:20:22 +0900 | [diff] [blame] | 631 | if (UNRELATED_CHAR == matchedProximityCharId) return false; |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 632 | mWord[depth] = c; |
| 633 | // If inputIndex is greater than mInputLength, that means there is no |
| 634 | // proximity chars. So, we don't need to check proximity. |
Jean Chalard | 8dc754a | 2011-01-27 14:20:22 +0900 | [diff] [blame] | 635 | if (SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR == matchedProximityCharId) { |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 636 | matchWeight = matchWeight * TYPED_LETTER_MULTIPLIER; |
Jean Chalard | 8dc754a | 2011-01-27 14:20:22 +0900 | [diff] [blame] | 637 | } |
satok | fd16f1d | 2011-01-27 16:25:16 +0900 | [diff] [blame] | 638 | bool isSameAsUserTypedLength = mInputLength == inputIndex + 1 |
| 639 | || (excessivePos == mInputLength - 1 && inputIndex == mInputLength - 2); |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 640 | if (isSameAsUserTypedLength && terminal) { |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 641 | onTerminalWhenUserTypedLengthIsSameAsInputLength(mWord, inputIndex, depth, matchWeight, |
Jean Chalard | 8dc754a | 2011-01-27 14:20:22 +0900 | [diff] [blame] | 642 | skipPos, excessivePos, transposedPos, freq); |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 643 | } |
| 644 | if (!needsToTraverseChildrenNodes) return false; |
| 645 | // Start traversing all nodes after the index exceeds the user typed length |
| 646 | *newTraverseAllNodes = isSameAsUserTypedLength; |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 647 | *newMatchRate = matchWeight; |
Jean Chalard | 8dc754a | 2011-01-27 14:20:22 +0900 | [diff] [blame] | 648 | *newDiffs = diffs + ((NEAR_PROXIMITY_CHAR == matchedProximityCharId) ? 1 : 0); |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 649 | *newInputIndex = inputIndex + 1; |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 650 | } |
| 651 | // Optimization: Prune out words that are too long compared to how much was typed. |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 652 | if (depth >= maxDepth || *newDiffs > mMaxEditDistance) { |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 653 | return false; |
| 654 | } |
| 655 | |
| 656 | // If inputIndex is greater than mInputLength, that means there are no proximity chars. |
satok | fd16f1d | 2011-01-27 16:25:16 +0900 | [diff] [blame] | 657 | // TODO: Check if this can be isSameAsUserTypedLength only. |
| 658 | if (isSameAsUserTypedLength || mInputLength <= *newInputIndex) { |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 659 | *newTraverseAllNodes = true; |
| 660 | } |
| 661 | // get the count of nodes and increment childAddress. |
| 662 | *newCount = Dictionary::getCount(DICT, &childPosition); |
| 663 | *newChildPosition = childPosition; |
| 664 | if (DEBUG_DICT) assert(needsToTraverseChildrenNodes); |
| 665 | return needsToTraverseChildrenNodes; |
| 666 | } |
| 667 | |
satok | aee09dc | 2010-12-09 19:21:51 +0900 | [diff] [blame] | 668 | inline int UnigramDictionary::getBestWordFreq(const int startInputIndex, const int inputLength, |
| 669 | unsigned short *word) { |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 670 | int pos = ROOT_POS; |
| 671 | int count = Dictionary::getCount(DICT, &pos); |
satok | aee09dc | 2010-12-09 19:21:51 +0900 | [diff] [blame] | 672 | int maxFreq = 0; |
| 673 | int depth = 0; |
| 674 | unsigned short newWord[MAX_WORD_LENGTH_INTERNAL]; |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 675 | bool terminal = false; |
| 676 | |
satok | aee09dc | 2010-12-09 19:21:51 +0900 | [diff] [blame] | 677 | mStackChildCount[0] = count; |
| 678 | mStackSiblingPos[0] = pos; |
| 679 | |
| 680 | while (depth >= 0) { |
| 681 | if (mStackChildCount[depth] > 0) { |
| 682 | --mStackChildCount[depth]; |
| 683 | int firstChildPos; |
| 684 | int newFreq; |
| 685 | int siblingPos = mStackSiblingPos[depth]; |
| 686 | const bool needsToTraverseChildrenNodes = processCurrentNodeForExactMatch(siblingPos, |
| 687 | startInputIndex, depth, newWord, &firstChildPos, &count, &terminal, &newFreq, |
| 688 | &siblingPos); |
| 689 | mStackSiblingPos[depth] = siblingPos; |
| 690 | if (depth == (inputLength - 1)) { |
| 691 | // Traverse sibling node |
| 692 | if (terminal) { |
| 693 | if (newFreq > maxFreq) { |
| 694 | for (int i = 0; i < inputLength; ++i) word[i] = newWord[i]; |
| 695 | if (DEBUG_DICT && DEBUG_NODE) { |
| 696 | char s[inputLength + 1]; |
| 697 | for (int i = 0; i < inputLength; ++i) s[i] = word[i]; |
| 698 | s[inputLength] = 0; |
| 699 | LOGI("New missing space word found: %d > %d (%s), %d, %d", |
| 700 | newFreq, maxFreq, s, inputLength, depth); |
| 701 | } |
| 702 | maxFreq = newFreq; |
| 703 | } |
| 704 | } |
| 705 | } else if (needsToTraverseChildrenNodes) { |
| 706 | // Traverse children nodes |
| 707 | ++depth; |
| 708 | mStackChildCount[depth] = count; |
| 709 | mStackSiblingPos[depth] = firstChildPos; |
| 710 | } |
| 711 | } else { |
| 712 | // Traverse parent node |
| 713 | --depth; |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 714 | } |
| 715 | } |
satok | aee09dc | 2010-12-09 19:21:51 +0900 | [diff] [blame] | 716 | |
| 717 | word[inputLength] = 0; |
| 718 | return maxFreq; |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | inline bool UnigramDictionary::processCurrentNodeForExactMatch(const int firstChildPos, |
satok | aee09dc | 2010-12-09 19:21:51 +0900 | [diff] [blame] | 722 | const int startInputIndex, const int depth, unsigned short *word, int *newChildPosition, |
| 723 | int *newCount, bool *newTerminal, int *newFreq, int *siblingPos) { |
| 724 | const int inputIndex = startInputIndex + depth; |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 725 | const int *currentChars = getInputCharsAt(inputIndex); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 726 | unsigned short c; |
satok | aee09dc | 2010-12-09 19:21:51 +0900 | [diff] [blame] | 727 | *siblingPos = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, firstChildPos, &c, |
| 728 | newChildPosition, newTerminal, newFreq); |
| 729 | const unsigned int inputC = currentChars[0]; |
| 730 | if (DEBUG_DICT) assert(inputC <= U_SHORT_MAX); |
Jean Chalard | f5f834a | 2011-02-22 15:12:46 +0900 | [diff] [blame] | 731 | const unsigned short baseLowerC = toBaseLowerCase(c); |
| 732 | const bool matched = (inputC == baseLowerC || inputC == c); |
satok | aee09dc | 2010-12-09 19:21:51 +0900 | [diff] [blame] | 733 | const bool hasChild = *newChildPosition != 0; |
| 734 | if (matched) { |
| 735 | word[depth] = c; |
| 736 | if (DEBUG_DICT && DEBUG_NODE) { |
| 737 | LOGI("Node(%c, %c)<%d>, %d, %d", inputC, c, matched, hasChild, *newFreq); |
| 738 | if (*newTerminal) LOGI("Terminal %d", *newFreq); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 739 | } |
satok | aee09dc | 2010-12-09 19:21:51 +0900 | [diff] [blame] | 740 | if (hasChild) { |
| 741 | *newCount = Dictionary::getCount(DICT, newChildPosition); |
| 742 | return true; |
| 743 | } else { |
| 744 | return false; |
| 745 | } |
| 746 | } else { |
| 747 | // If this node is not user typed character, this method treats this word as unmatched. |
| 748 | // Thus newTerminal shouldn't be true. |
| 749 | *newTerminal = false; |
| 750 | return false; |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 751 | } |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 752 | } |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 753 | } // namespace latinime |