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, |
| 33 | int fullWordMultiplier, int maxWordLength, int maxWords, int maxAlternatives, |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 34 | const bool isLatestDictVersion) |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 35 | : DICT(dict), MAX_WORD_LENGTH(maxWordLength),MAX_WORDS(maxWords), |
| 36 | MAX_ALTERNATIVES(maxAlternatives), IS_LATEST_DICT_VERSION(isLatestDictVersion), |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 37 | TYPED_LETTER_MULTIPLIER(typedLetterMultiplier), FULL_WORD_MULTIPLIER(fullWordMultiplier) { |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 38 | LOGI("UnigramDictionary - constructor"); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 39 | } |
| 40 | |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 41 | UnigramDictionary::~UnigramDictionary() {} |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 42 | |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 43 | int UnigramDictionary::getSuggestions(int *codes, int codesSize, unsigned short *outWords, |
| 44 | int *frequencies, int *nextLetters, int nextLettersSize) |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 45 | { |
| 46 | |
| 47 | initSuggestions(codes, codesSize, outWords, frequencies); |
| 48 | |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame^] | 49 | int suggestedWordsCount = getSuggestionCandidates(codesSize, -1, -1, nextLetters, |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 50 | nextLettersSize); |
| 51 | |
| 52 | // If there aren't sufficient suggestions, search for words by allowing wild cards at |
| 53 | // the different character positions. This feature is not ready for prime-time as we need |
| 54 | // to figure out the best ranking for such words compared to proximity corrections and |
| 55 | // completions. |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame^] | 56 | if (SUGGEST_MISSING_CHARACTERS) { |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 57 | for (int i = 0; i < codesSize; ++i) { |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame^] | 58 | if (DEBUG_DICT) LOGI("--- Suggest missing characters %d", i); |
| 59 | const int tempCount = getSuggestionCandidates(codesSize, i, -1, NULL, 0); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 60 | if (tempCount > suggestedWordsCount) { |
| 61 | suggestedWordsCount = tempCount; |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame^] | 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Suggest excessive characters |
| 67 | if (SUGGEST_EXCESSIVE_CHARACTERS) { |
| 68 | for (int i = 0; i < codesSize; ++i) { |
| 69 | if (DEBUG_DICT) LOGI("--- Suggest excessive characters %d", i); |
| 70 | const int tempCount = getSuggestionCandidates(codesSize, -1, i, NULL, 0); |
| 71 | if (tempCount > suggestedWordsCount) { |
| 72 | suggestedWordsCount = tempCount; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if (DEBUG_DICT) { |
| 78 | LOGI("Returning %d words", suggestedWordsCount); |
| 79 | LOGI("Next letters: "); |
| 80 | for (int k = 0; k < nextLettersSize; k++) { |
| 81 | if (nextLetters[k] > 0) { |
| 82 | LOGI("%c = %d,", k, nextLetters[k]); |
| 83 | } |
| 84 | } |
| 85 | LOGI("\n"); |
| 86 | } |
| 87 | return suggestedWordsCount; |
| 88 | } |
| 89 | |
| 90 | void UnigramDictionary::initSuggestions(int *codes, int codesSize, unsigned short *outWords, |
| 91 | int *frequencies) { |
satok | f5cded1 | 2010-12-06 21:28:24 +0900 | [diff] [blame] | 92 | if (DEBUG_DICT) LOGI("initSuggest"); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 93 | mFrequencies = frequencies; |
| 94 | mOutputChars = outWords; |
| 95 | mInputCodes = codes; |
| 96 | mInputLength = codesSize; |
| 97 | mMaxEditDistance = mInputLength < 5 ? 2 : mInputLength / 2; |
| 98 | } |
| 99 | |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame^] | 100 | int UnigramDictionary::getSuggestionCandidates(int inputLength, int skipPos, int excessivePos, |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 101 | int *nextLetters, int nextLettersSize) { |
satok | f5cded1 | 2010-12-06 21:28:24 +0900 | [diff] [blame] | 102 | if (DEBUG_DICT) LOGI("getSuggestionCandidates"); |
satok | 6831926 | 2010-12-03 19:38:08 +0900 | [diff] [blame] | 103 | int initialPos = 0; |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 104 | if (IS_LATEST_DICT_VERSION) { |
satok | 6831926 | 2010-12-03 19:38:08 +0900 | [diff] [blame] | 105 | initialPos = DICTIONARY_HEADER_SIZE; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 106 | } |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame^] | 107 | getWords(initialPos, inputLength, skipPos, excessivePos, nextLetters, nextLettersSize); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 108 | |
| 109 | // Get the word count |
| 110 | int suggestedWordsCount = 0; |
| 111 | while (suggestedWordsCount < MAX_WORDS && mFrequencies[suggestedWordsCount] > 0) { |
| 112 | suggestedWordsCount++; |
| 113 | } |
| 114 | return suggestedWordsCount; |
| 115 | } |
| 116 | |
satok | 715514d | 2010-12-02 20:19:59 +0900 | [diff] [blame] | 117 | void UnigramDictionary::registerNextLetter( |
| 118 | unsigned short c, int *nextLetters, int nextLettersSize) { |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 119 | if (c < nextLettersSize) { |
| 120 | nextLetters[c]++; |
| 121 | } |
| 122 | } |
| 123 | |
satok | 28bd03b | 2010-12-03 16:39:16 +0900 | [diff] [blame] | 124 | bool UnigramDictionary::addWord(unsigned short *word, int length, int frequency) { |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 125 | word[length] = 0; |
| 126 | if (DEBUG_DICT) { |
| 127 | char s[length + 1]; |
| 128 | for (int i = 0; i <= length; i++) s[i] = word[i]; |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame^] | 129 | if (DEBUG_SHOW_FOUND_WORD) LOGI("Found word = %s, freq = %d : \n", s, frequency); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 130 | } |
satok | f5cded1 | 2010-12-06 21:28:24 +0900 | [diff] [blame] | 131 | if (length > MAX_WORD_LENGTH) { |
| 132 | if (DEBUG_DICT) LOGI("Exceeded max word length."); |
| 133 | return false; |
| 134 | } |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 135 | |
| 136 | // Find the right insertion point |
| 137 | int insertAt = 0; |
| 138 | while (insertAt < MAX_WORDS) { |
satok | 715514d | 2010-12-02 20:19:59 +0900 | [diff] [blame] | 139 | if (frequency > mFrequencies[insertAt] || (mFrequencies[insertAt] == frequency |
| 140 | && length < Dictionary::wideStrLen(mOutputChars + insertAt * MAX_WORD_LENGTH))) { |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 141 | break; |
| 142 | } |
| 143 | insertAt++; |
| 144 | } |
| 145 | if (insertAt < MAX_WORDS) { |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame^] | 146 | if (DEBUG_DICT) { |
| 147 | char s[length + 1]; |
| 148 | for (int i = 0; i <= length; i++) s[i] = word[i]; |
| 149 | LOGI("Added word = %s, freq = %d : \n", s, frequency); |
| 150 | } |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 151 | memmove((char*) mFrequencies + (insertAt + 1) * sizeof(mFrequencies[0]), |
| 152 | (char*) mFrequencies + insertAt * sizeof(mFrequencies[0]), |
| 153 | (MAX_WORDS - insertAt - 1) * sizeof(mFrequencies[0])); |
| 154 | mFrequencies[insertAt] = frequency; |
| 155 | memmove((char*) mOutputChars + (insertAt + 1) * MAX_WORD_LENGTH * sizeof(short), |
satok | 715514d | 2010-12-02 20:19:59 +0900 | [diff] [blame] | 156 | (char*) mOutputChars + insertAt * MAX_WORD_LENGTH * sizeof(short), |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 157 | (MAX_WORDS - insertAt - 1) * sizeof(short) * MAX_WORD_LENGTH); |
satok | 715514d | 2010-12-02 20:19:59 +0900 | [diff] [blame] | 158 | unsigned short *dest = mOutputChars + insertAt * MAX_WORD_LENGTH; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 159 | while (length--) { |
| 160 | *dest++ = *word++; |
| 161 | } |
| 162 | *dest = 0; // NULL terminate |
| 163 | if (DEBUG_DICT) LOGI("Added word at %d\n", insertAt); |
| 164 | return true; |
| 165 | } |
| 166 | return false; |
| 167 | } |
| 168 | |
satok | 28bd03b | 2010-12-03 16:39:16 +0900 | [diff] [blame] | 169 | unsigned short UnigramDictionary::toLowerCase(unsigned short c) { |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 170 | if (c < sizeof(BASE_CHARS) / sizeof(BASE_CHARS[0])) { |
| 171 | c = BASE_CHARS[c]; |
| 172 | } |
| 173 | if (c >='A' && c <= 'Z') { |
| 174 | c |= 32; |
| 175 | } else if (c > 127) { |
| 176 | c = latin_tolower(c); |
| 177 | } |
| 178 | return c; |
| 179 | } |
| 180 | |
satok | 28bd03b | 2010-12-03 16:39:16 +0900 | [diff] [blame] | 181 | bool UnigramDictionary::sameAsTyped(unsigned short *word, int length) { |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 182 | if (length != mInputLength) { |
| 183 | return false; |
| 184 | } |
| 185 | int *inputCodes = mInputCodes; |
| 186 | while (length--) { |
| 187 | if ((unsigned int) *inputCodes != (unsigned int) *word) { |
| 188 | return false; |
| 189 | } |
| 190 | inputCodes += MAX_ALTERNATIVES; |
| 191 | word++; |
| 192 | } |
| 193 | return true; |
| 194 | } |
| 195 | |
satok | 715514d | 2010-12-02 20:19:59 +0900 | [diff] [blame] | 196 | static const char QUOTE = '\''; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 197 | |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 198 | // Keep this for comparing spec to new getWords |
| 199 | void UnigramDictionary::getWordsOld(const int initialPos, const int inputLength, const int skipPos, |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame^] | 200 | const int excessivePos, int *nextLetters, const int nextLettersSize) { |
satok | 6831926 | 2010-12-03 19:38:08 +0900 | [diff] [blame] | 201 | int initialPosition = initialPos; |
| 202 | const int count = Dictionary::getCount(DICT, &initialPosition); |
satok | f5cded1 | 2010-12-06 21:28:24 +0900 | [diff] [blame] | 203 | getWordsRec(count, initialPosition, 0, |
| 204 | min(inputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH), |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame^] | 205 | mInputLength <= 0, 1, 0, 0, skipPos, excessivePos, nextLetters, nextLettersSize); |
satok | 6831926 | 2010-12-03 19:38:08 +0900 | [diff] [blame] | 206 | } |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 207 | |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 208 | void UnigramDictionary::getWords(const int rootPos, const int inputLength, const int skipPos, |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame^] | 209 | const int excessivePos, int *nextLetters, const int nextLettersSize) { |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 210 | int rootPosition = rootPos; |
| 211 | const int MAX_DEPTH = min(inputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH); |
| 212 | // Get the number of child of root, then increment the position |
| 213 | int childCount = Dictionary::getCount(DICT, &rootPosition); |
| 214 | int depth = 0; |
| 215 | |
| 216 | mStackChildCount[0] = childCount; |
| 217 | mStackTraverseAll[0] = (mInputLength <= 0); |
| 218 | mStackNodeFreq[0] = 1; |
| 219 | mStackInputIndex[0] = 0; |
| 220 | mStackDiffs[0] = 0; |
| 221 | mStackSiblingPos[0] = rootPosition; |
| 222 | |
| 223 | while (depth >= 0) { |
| 224 | if (mStackChildCount[depth] > 0) { |
| 225 | --mStackChildCount[depth]; |
| 226 | bool traverseAllNodes = mStackTraverseAll[depth]; |
| 227 | int snr = mStackNodeFreq[depth]; |
| 228 | int inputIndex = mStackInputIndex[depth]; |
| 229 | int diffs = mStackDiffs[depth]; |
| 230 | int siblingPos = mStackSiblingPos[depth]; |
| 231 | int firstChildPos; |
| 232 | // depth will never be greater than MAX_DEPTH because in that case, |
| 233 | // needsToTraverseChildrenNodes should be false |
| 234 | const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth, |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame^] | 235 | MAX_DEPTH, traverseAllNodes, snr, inputIndex, diffs, skipPos, excessivePos, |
| 236 | nextLetters, nextLettersSize, &childCount, &firstChildPos, &traverseAllNodes, |
| 237 | &snr, &inputIndex, &diffs, &siblingPos); |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 238 | // Next sibling pos |
| 239 | mStackSiblingPos[depth] = siblingPos; |
| 240 | if (needsToTraverseChildrenNodes) { |
| 241 | // Goes to child node |
| 242 | ++depth; |
| 243 | mStackChildCount[depth] = childCount; |
| 244 | mStackTraverseAll[depth] = traverseAllNodes; |
| 245 | mStackNodeFreq[depth] = snr; |
| 246 | mStackInputIndex[depth] = inputIndex; |
| 247 | mStackDiffs[depth] = diffs; |
| 248 | mStackSiblingPos[depth] = firstChildPos; |
| 249 | } |
| 250 | } else { |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame^] | 251 | // Goes to parent sibling node |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 252 | --depth; |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
satok | 6831926 | 2010-12-03 19:38:08 +0900 | [diff] [blame] | 257 | // snr : frequency? |
| 258 | void UnigramDictionary::getWordsRec(const int childrenCount, const int pos, const int depth, |
| 259 | const int maxDepth, const bool traverseAllNodes, const int snr, const int inputIndex, |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame^] | 260 | const int diffs, const int skipPos, const int excessivePos, int *nextLetters, |
| 261 | const int nextLettersSize) { |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 262 | int siblingPos = pos; |
satok | 6831926 | 2010-12-03 19:38:08 +0900 | [diff] [blame] | 263 | for (int i = 0; i < childrenCount; ++i) { |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 264 | int newCount; |
| 265 | int newChildPosition; |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 266 | const int newDepth = depth + 1; |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 267 | bool newTraverseAllNodes; |
| 268 | int newSnr; |
| 269 | int newInputIndex; |
| 270 | int newDiffs; |
| 271 | int newSiblingPos; |
| 272 | const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth, maxDepth, |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame^] | 273 | traverseAllNodes, snr, inputIndex, diffs, skipPos, excessivePos, nextLetters, |
| 274 | nextLettersSize, |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 275 | &newCount, &newChildPosition, &newTraverseAllNodes, &newSnr, |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 276 | &newInputIndex, &newDiffs, &newSiblingPos); |
| 277 | siblingPos = newSiblingPos; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 278 | |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 279 | if (needsToTraverseChildrenNodes) { |
| 280 | getWordsRec(newCount, newChildPosition, newDepth, maxDepth, newTraverseAllNodes, |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame^] | 281 | newSnr, newInputIndex, newDiffs, skipPos, excessivePos, nextLetters, |
| 282 | nextLettersSize); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
satok | 715514d | 2010-12-02 20:19:59 +0900 | [diff] [blame] | 287 | inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsGreaterThanInputLength( |
| 288 | unsigned short *word, const int inputLength, const int depth, const int snr, |
| 289 | int *nextLetters, const int nextLettersSize, const int skipPos, const int freq) { |
| 290 | addWord(word, depth + 1, freq * snr); |
| 291 | if (depth >= inputLength && skipPos < 0) { |
| 292 | registerNextLetter(mWord[mInputLength], nextLetters, nextLettersSize); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsSameAsInputLength( |
| 297 | unsigned short *word, const int depth, const int snr, const int skipPos, const int freq, |
| 298 | const int addedWeight) { |
| 299 | if (!sameAsTyped(word, depth + 1)) { |
| 300 | int finalFreq = freq * snr * addedWeight; |
| 301 | // Proximity collection will promote a word of the same length as |
| 302 | // what user typed. |
| 303 | if (skipPos < 0) finalFreq *= FULL_WORD_MULTIPLIER; |
| 304 | addWord(word, depth + 1, finalFreq); |
| 305 | } |
| 306 | } |
satok | 28bd03b | 2010-12-03 16:39:16 +0900 | [diff] [blame] | 307 | |
| 308 | inline bool UnigramDictionary::needsToSkipCurrentNode(const unsigned short c, |
satok | 6831926 | 2010-12-03 19:38:08 +0900 | [diff] [blame] | 309 | const int inputIndex, const int skipPos, const int depth) { |
| 310 | const unsigned short userTypedChar = (mInputCodes + (inputIndex * MAX_ALTERNATIVES))[0]; |
satok | 28bd03b | 2010-12-03 16:39:16 +0900 | [diff] [blame] | 311 | // Skip the ' or other letter and continue deeper |
| 312 | return (c == QUOTE && userTypedChar != QUOTE) || skipPos == depth; |
| 313 | } |
| 314 | |
| 315 | inline int UnigramDictionary::getMatchedProximityId(const int *currentChars, |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 316 | const unsigned short c, const int skipPos) { |
| 317 | const unsigned short lowerC = toLowerCase(c); |
satok | 28bd03b | 2010-12-03 16:39:16 +0900 | [diff] [blame] | 318 | int j = 0; |
| 319 | while (currentChars[j] > 0) { |
satok | 6831926 | 2010-12-03 19:38:08 +0900 | [diff] [blame] | 320 | const bool matched = (currentChars[j] == lowerC || currentChars[j] == c); |
satok | 28bd03b | 2010-12-03 16:39:16 +0900 | [diff] [blame] | 321 | // If skipPos is defined, not to search proximity collections. |
| 322 | // First char is what user typed. |
| 323 | if (matched) { |
| 324 | return j; |
| 325 | } else if (skipPos >= 0) { |
| 326 | return -1; |
| 327 | } |
| 328 | ++j; |
| 329 | } |
| 330 | return -1; |
| 331 | } |
| 332 | |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 333 | inline bool UnigramDictionary::processCurrentNode(const int pos, const int depth, |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame^] | 334 | const int maxDepth, const bool traverseAllNodes, const int snr, int inputIndex, |
| 335 | const int diffs, const int skipPos, const int excessivePos, int *nextLetters, |
| 336 | const int nextLettersSize, int *newCount, int *newChildPosition, bool *newTraverseAllNodes, |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 337 | int *newSnr, int*newInputIndex, int *newDiffs, int *nextSiblingPosition) { |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame^] | 338 | if (DEBUG_DICT) assert(skipPos < 0 || excessivePos < 0); |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 339 | unsigned short c; |
| 340 | int childPosition; |
| 341 | bool terminal; |
| 342 | int freq; |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame^] | 343 | |
| 344 | if (excessivePos == depth) ++inputIndex; |
| 345 | |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 346 | *nextSiblingPosition = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, pos, &c, |
| 347 | &childPosition, &terminal, &freq); |
| 348 | |
| 349 | const bool needsToTraverseChildrenNodes = childPosition != 0; |
| 350 | |
| 351 | // If we are only doing traverseAllNodes, no need to look at the typed characters. |
| 352 | if (traverseAllNodes || needsToSkipCurrentNode(c, inputIndex, skipPos, depth)) { |
| 353 | mWord[depth] = c; |
| 354 | if (traverseAllNodes && terminal) { |
| 355 | onTerminalWhenUserTypedLengthIsGreaterThanInputLength(mWord, mInputLength, depth, |
| 356 | snr, nextLetters, nextLettersSize, skipPos, freq); |
| 357 | } |
| 358 | if (!needsToTraverseChildrenNodes) return false; |
| 359 | *newTraverseAllNodes = traverseAllNodes; |
| 360 | *newSnr = snr; |
| 361 | *newDiffs = diffs; |
| 362 | *newInputIndex = inputIndex; |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 363 | } else { |
| 364 | int *currentChars = mInputCodes + (inputIndex * MAX_ALTERNATIVES); |
| 365 | int matchedProximityCharId = getMatchedProximityId(currentChars, c, skipPos); |
| 366 | if (matchedProximityCharId < 0) return false; |
| 367 | mWord[depth] = c; |
| 368 | // If inputIndex is greater than mInputLength, that means there is no |
| 369 | // proximity chars. So, we don't need to check proximity. |
| 370 | const int addedWeight = matchedProximityCharId == 0 ? TYPED_LETTER_MULTIPLIER : 1; |
| 371 | const bool isSameAsUserTypedLength = mInputLength == inputIndex + 1; |
| 372 | if (isSameAsUserTypedLength && terminal) { |
| 373 | onTerminalWhenUserTypedLengthIsSameAsInputLength(mWord, depth, snr, |
| 374 | skipPos, freq, addedWeight); |
| 375 | } |
| 376 | if (!needsToTraverseChildrenNodes) return false; |
| 377 | // Start traversing all nodes after the index exceeds the user typed length |
| 378 | *newTraverseAllNodes = isSameAsUserTypedLength; |
| 379 | *newSnr = snr * addedWeight; |
| 380 | *newDiffs = diffs + (matchedProximityCharId > 0); |
| 381 | *newInputIndex = inputIndex + 1; |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 382 | } |
| 383 | // 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] | 384 | if (depth >= maxDepth || *newDiffs > mMaxEditDistance) { |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 385 | return false; |
| 386 | } |
| 387 | |
| 388 | // If inputIndex is greater than mInputLength, that means there are no proximity chars. |
| 389 | if (mInputLength <= *newInputIndex) { |
| 390 | *newTraverseAllNodes = true; |
| 391 | } |
| 392 | // get the count of nodes and increment childAddress. |
| 393 | *newCount = Dictionary::getCount(DICT, &childPosition); |
| 394 | *newChildPosition = childPosition; |
| 395 | if (DEBUG_DICT) assert(needsToTraverseChildrenNodes); |
| 396 | return needsToTraverseChildrenNodes; |
| 397 | } |
| 398 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 399 | } // namespace latinime |