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