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