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