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