blob: 274e1f6d3fb4de21ceea967d02c03cb95c02a85d [file] [log] [blame]
satok30088252010-12-01 21:22:15 +09001/*
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
satok48e432c2010-12-06 17:38:58 +090018#include <assert.h>
satok30088252010-12-01 21:22:15 +090019#include <fcntl.h>
satokf5cded12010-12-06 21:28:24 +090020#include <stdio.h>
satok30088252010-12-01 21:22:15 +090021#include <string.h>
22
satoke808e432010-12-02 14:53:24 +090023#define LOG_TAG "LatinIME: unigram_dictionary.cpp"
satok30088252010-12-01 21:22:15 +090024
satok30088252010-12-01 21:22:15 +090025#include "basechars.h"
26#include "char_utils.h"
satoke808e432010-12-02 14:53:24 +090027#include "dictionary.h"
28#include "unigram_dictionary.h"
satok30088252010-12-01 21:22:15 +090029
30namespace latinime {
31
Jean Chalardc2bbc6a2011-02-25 17:56:53 +090032const UnigramDictionary::digraph_t UnigramDictionary::GERMAN_UMLAUT_DIGRAPHS[] =
33 { { 'a', 'e' },
34 { 'o', 'e' },
35 { 'u', 'e' } };
36
satoke808e432010-12-02 14:53:24 +090037UnigramDictionary::UnigramDictionary(const unsigned char *dict, int typedLetterMultiplier,
satok662fe692010-12-08 17:05:39 +090038 int fullWordMultiplier, int maxWordLength, int maxWords, int maxProximityChars,
satok18c28f42010-12-02 18:11:54 +090039 const bool isLatestDictVersion)
Tadashi G. Takaoka887f11e2011-02-10 20:53:58 +090040 : DICT(dict), MAX_WORD_LENGTH(maxWordLength), MAX_WORDS(maxWords),
satok662fe692010-12-08 17:05:39 +090041 MAX_PROXIMITY_CHARS(maxProximityChars), IS_LATEST_DICT_VERSION(isLatestDictVersion),
42 TYPED_LETTER_MULTIPLIER(typedLetterMultiplier), FULL_WORD_MULTIPLIER(fullWordMultiplier),
Jean Chalardc2bbc6a2011-02-25 17:56:53 +090043 ROOT_POS(isLatestDictVersion ? DICTIONARY_HEADER_SIZE : 0),
Jean Chalarda787dba2011-03-04 12:17:48 +090044 BYTES_IN_ONE_CHAR(MAX_PROXIMITY_CHARS * sizeof(*mInputCodes)),
45 MAX_UMLAUT_SEARCH_DEPTH(DEFAULT_MAX_UMLAUT_SEARCH_DEPTH) {
satoka3d78f62010-12-09 22:08:33 +090046 if (DEBUG_DICT) LOGI("UnigramDictionary - constructor");
satok30088252010-12-01 21:22:15 +090047}
48
satok18c28f42010-12-02 18:11:54 +090049UnigramDictionary::~UnigramDictionary() {}
satok30088252010-12-01 21:22:15 +090050
Jean Chalardc2bbc6a2011-02-25 17:56:53 +090051static 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
56bool 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.
81void 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,
Jean Chalarda787dba2011-03-04 12:17:48 +090084 int currentDepth, int* codesDest, unsigned short* outWords, int* frequencies) {
Jean Chalardc2bbc6a2011-02-25 17:56:53 +090085
Jean Chalarda787dba2011-03-04 12:17:48 +090086 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 Chalardc2bbc6a2011-02-25 17:56:53 +090090
Jean Chalarda787dba2011-03-04 12:17:48 +090091 // 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 Chalardc2bbc6a2011-02-25 17:56:53 +0900103
Jean Chalarda787dba2011-03-04 12:17:48 +0900104 // 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 Chalardc2bbc6a2011-02-25 17:56:53 +0900115 }
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
131int 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 Chalarda787dba2011-03-04 12:17:48 +0900139 codesSize, flags, codes, codesSize, 0, codesBuffer, outWords, frequencies);
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900140 } else { // Normal processing
141 getWordSuggestions(proximityInfo, xcoordinates, ycoordinates, codes, codesSize,
142 outWords, frequencies);
143 }
144
satok817e5172011-03-04 06:06:45 -0800145 PROF_START(20);
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900146 // 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 }
satok817e5172011-03-04 06:06:45 -0800161 PROF_END(20);
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900162 PROF_CLOSE;
163 return suggestedWordsCount;
164}
165
166void 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
satok61e2f852011-01-05 14:13:07 +0900170 PROF_OPEN;
171 PROF_START(0);
satok30088252010-12-01 21:22:15 +0900172 initSuggestions(codes, codesSize, outWords, frequencies);
satok54fe9e02010-12-13 14:42:35 +0900173 if (DEBUG_DICT) assert(codesSize == mInputLength);
174
satoka3d78f62010-12-09 22:08:33 +0900175 const int MAX_DEPTH = min(mInputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH);
satok61e2f852011-01-05 14:13:07 +0900176 PROF_END(0);
satok30088252010-12-01 21:22:15 +0900177
satok61e2f852011-01-05 14:13:07 +0900178 PROF_START(1);
Tadashi G. Takaoka887f11e2011-02-10 20:53:58 +0900179 getSuggestionCandidates(-1, -1, -1, mNextLettersFrequency, NEXT_LETTERS_SIZE, MAX_DEPTH);
satok61e2f852011-01-05 14:13:07 +0900180 PROF_END(1);
181
182 PROF_START(2);
satok662fe692010-12-08 17:05:39 +0900183 // Suggestion with missing character
184 if (SUGGEST_WORDS_WITH_MISSING_CHARACTER) {
satok30088252010-12-01 21:22:15 +0900185 for (int i = 0; i < codesSize; ++i) {
satokcdbbea72010-12-08 16:04:16 +0900186 if (DEBUG_DICT) LOGI("--- Suggest missing characters %d", i);
satok54fe9e02010-12-13 14:42:35 +0900187 getSuggestionCandidates(i, -1, -1, NULL, 0, MAX_DEPTH);
satokcdbbea72010-12-08 16:04:16 +0900188 }
189 }
satok61e2f852011-01-05 14:13:07 +0900190 PROF_END(2);
satokcdbbea72010-12-08 16:04:16 +0900191
satok61e2f852011-01-05 14:13:07 +0900192 PROF_START(3);
satok662fe692010-12-08 17:05:39 +0900193 // Suggestion with excessive character
satok54fe9e02010-12-13 14:42:35 +0900194 if (SUGGEST_WORDS_WITH_EXCESSIVE_CHARACTER
195 && mInputLength >= MIN_USER_TYPED_LENGTH_FOR_EXCESSIVE_CHARACTER_SUGGESTION) {
satokcdbbea72010-12-08 16:04:16 +0900196 for (int i = 0; i < codesSize; ++i) {
satok54fe9e02010-12-13 14:42:35 +0900197 if (DEBUG_DICT) LOGI("--- Suggest excessive characters %d", i);
198 getSuggestionCandidates(-1, i, -1, NULL, 0, MAX_DEPTH);
satok30088252010-12-01 21:22:15 +0900199 }
200 }
satok61e2f852011-01-05 14:13:07 +0900201 PROF_END(3);
satok30088252010-12-01 21:22:15 +0900202
satok61e2f852011-01-05 14:13:07 +0900203 PROF_START(4);
satoka3d78f62010-12-09 22:08:33 +0900204 // 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);
satok54fe9e02010-12-13 14:42:35 +0900209 getSuggestionCandidates(-1, -1, i, NULL, 0, mInputLength - 1);
satoka3d78f62010-12-09 22:08:33 +0900210 }
211 }
satok61e2f852011-01-05 14:13:07 +0900212 PROF_END(4);
satoka3d78f62010-12-09 22:08:33 +0900213
satok61e2f852011-01-05 14:13:07 +0900214 PROF_START(5);
satok662fe692010-12-08 17:05:39 +0900215 // Suggestions with missing space
satok54fe9e02010-12-13 14:42:35 +0900216 if (SUGGEST_WORDS_WITH_MISSING_SPACE_CHARACTER
217 && mInputLength >= MIN_USER_TYPED_LENGTH_FOR_MISSING_SPACE_SUGGESTION) {
satok662fe692010-12-08 17:05:39 +0900218 for (int i = 1; i < codesSize; ++i) {
219 if (DEBUG_DICT) LOGI("--- Suggest missing space characters %d", i);
220 getMissingSpaceWords(mInputLength, i);
221 }
222 }
satok61e2f852011-01-05 14:13:07 +0900223 PROF_END(5);
satok817e5172011-03-04 06:06:45 -0800224
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));
235
236 if (proximityInfo->hasSpaceProximity(x, y)) {
237 getMistypedSpaceWords(mInputLength, i);
238 }
239
240 }
241 }
242 PROF_END(6);
satok30088252010-12-01 21:22:15 +0900243}
244
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900245void UnigramDictionary::initSuggestions(const int *codes, const int codesSize,
246 unsigned short *outWords, int *frequencies) {
satokf5cded12010-12-06 21:28:24 +0900247 if (DEBUG_DICT) LOGI("initSuggest");
satok30088252010-12-01 21:22:15 +0900248 mFrequencies = frequencies;
249 mOutputChars = outWords;
250 mInputCodes = codes;
251 mInputLength = codesSize;
252 mMaxEditDistance = mInputLength < 5 ? 2 : mInputLength / 2;
253}
254
satok715514d2010-12-02 20:19:59 +0900255void UnigramDictionary::registerNextLetter(
256 unsigned short c, int *nextLetters, int nextLettersSize) {
satok30088252010-12-01 21:22:15 +0900257 if (c < nextLettersSize) {
258 nextLetters[c]++;
259 }
260}
261
satok662fe692010-12-08 17:05:39 +0900262// TODO: We need to optimize addWord by using STL or something
satok28bd03b2010-12-03 16:39:16 +0900263bool UnigramDictionary::addWord(unsigned short *word, int length, int frequency) {
satok30088252010-12-01 21:22:15 +0900264 word[length] = 0;
satok662fe692010-12-08 17:05:39 +0900265 if (DEBUG_DICT && DEBUG_SHOW_FOUND_WORD) {
satok30088252010-12-01 21:22:15 +0900266 char s[length + 1];
267 for (int i = 0; i <= length; i++) s[i] = word[i];
satok662fe692010-12-08 17:05:39 +0900268 LOGI("Found word = %s, freq = %d", s, frequency);
satok30088252010-12-01 21:22:15 +0900269 }
satokf5cded12010-12-06 21:28:24 +0900270 if (length > MAX_WORD_LENGTH) {
271 if (DEBUG_DICT) LOGI("Exceeded max word length.");
272 return false;
273 }
satok30088252010-12-01 21:22:15 +0900274
275 // Find the right insertion point
276 int insertAt = 0;
277 while (insertAt < MAX_WORDS) {
satok715514d2010-12-02 20:19:59 +0900278 if (frequency > mFrequencies[insertAt] || (mFrequencies[insertAt] == frequency
279 && length < Dictionary::wideStrLen(mOutputChars + insertAt * MAX_WORD_LENGTH))) {
satok30088252010-12-01 21:22:15 +0900280 break;
281 }
282 insertAt++;
283 }
284 if (insertAt < MAX_WORDS) {
satokcdbbea72010-12-08 16:04:16 +0900285 if (DEBUG_DICT) {
286 char s[length + 1];
287 for (int i = 0; i <= length; i++) s[i] = word[i];
satok662fe692010-12-08 17:05:39 +0900288 LOGI("Added word = %s, freq = %d", s, frequency);
satokcdbbea72010-12-08 16:04:16 +0900289 }
satok30088252010-12-01 21:22:15 +0900290 memmove((char*) mFrequencies + (insertAt + 1) * sizeof(mFrequencies[0]),
291 (char*) mFrequencies + insertAt * sizeof(mFrequencies[0]),
292 (MAX_WORDS - insertAt - 1) * sizeof(mFrequencies[0]));
293 mFrequencies[insertAt] = frequency;
294 memmove((char*) mOutputChars + (insertAt + 1) * MAX_WORD_LENGTH * sizeof(short),
satok715514d2010-12-02 20:19:59 +0900295 (char*) mOutputChars + insertAt * MAX_WORD_LENGTH * sizeof(short),
satok30088252010-12-01 21:22:15 +0900296 (MAX_WORDS - insertAt - 1) * sizeof(short) * MAX_WORD_LENGTH);
satok715514d2010-12-02 20:19:59 +0900297 unsigned short *dest = mOutputChars + insertAt * MAX_WORD_LENGTH;
satok30088252010-12-01 21:22:15 +0900298 while (length--) {
299 *dest++ = *word++;
300 }
301 *dest = 0; // NULL terminate
satok662fe692010-12-08 17:05:39 +0900302 if (DEBUG_DICT) LOGI("Added word at %d", insertAt);
satok30088252010-12-01 21:22:15 +0900303 return true;
304 }
305 return false;
306}
307
Jean Chalardf5f834a2011-02-22 15:12:46 +0900308unsigned short UnigramDictionary::toBaseLowerCase(unsigned short c) {
satok30088252010-12-01 21:22:15 +0900309 if (c < sizeof(BASE_CHARS) / sizeof(BASE_CHARS[0])) {
310 c = BASE_CHARS[c];
311 }
312 if (c >='A' && c <= 'Z') {
313 c |= 32;
314 } else if (c > 127) {
315 c = latin_tolower(c);
316 }
317 return c;
318}
319
satok28bd03b2010-12-03 16:39:16 +0900320bool UnigramDictionary::sameAsTyped(unsigned short *word, int length) {
satok30088252010-12-01 21:22:15 +0900321 if (length != mInputLength) {
322 return false;
323 }
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900324 const int *inputCodes = mInputCodes;
satok30088252010-12-01 21:22:15 +0900325 while (length--) {
326 if ((unsigned int) *inputCodes != (unsigned int) *word) {
327 return false;
328 }
satok662fe692010-12-08 17:05:39 +0900329 inputCodes += MAX_PROXIMITY_CHARS;
satok30088252010-12-01 21:22:15 +0900330 word++;
331 }
332 return true;
333}
334
satok715514d2010-12-02 20:19:59 +0900335static const char QUOTE = '\'';
satok662fe692010-12-08 17:05:39 +0900336static const char SPACE = ' ';
satok30088252010-12-01 21:22:15 +0900337
satok54fe9e02010-12-13 14:42:35 +0900338void UnigramDictionary::getSuggestionCandidates(const int skipPos,
satoka3d78f62010-12-09 22:08:33 +0900339 const int excessivePos, const int transposedPos, int *nextLetters,
340 const int nextLettersSize, const int maxDepth) {
satok54fe9e02010-12-13 14:42:35 +0900341 if (DEBUG_DICT) {
342 LOGI("getSuggestionCandidates %d", maxDepth);
343 assert(transposedPos + 1 < mInputLength);
344 assert(excessivePos < mInputLength);
345 assert(missingPos < mInputLength);
346 }
satok662fe692010-12-08 17:05:39 +0900347 int rootPosition = ROOT_POS;
satokd2997922010-12-07 13:08:39 +0900348 // Get the number of child of root, then increment the position
349 int childCount = Dictionary::getCount(DICT, &rootPosition);
350 int depth = 0;
351
352 mStackChildCount[0] = childCount;
353 mStackTraverseAll[0] = (mInputLength <= 0);
354 mStackNodeFreq[0] = 1;
355 mStackInputIndex[0] = 0;
356 mStackDiffs[0] = 0;
357 mStackSiblingPos[0] = rootPosition;
358
satok662fe692010-12-08 17:05:39 +0900359 // Depth first search
satokd2997922010-12-07 13:08:39 +0900360 while (depth >= 0) {
361 if (mStackChildCount[depth] > 0) {
362 --mStackChildCount[depth];
363 bool traverseAllNodes = mStackTraverseAll[depth];
Jean Chalardf5f834a2011-02-22 15:12:46 +0900364 int matchWeight = mStackNodeFreq[depth];
satokd2997922010-12-07 13:08:39 +0900365 int inputIndex = mStackInputIndex[depth];
366 int diffs = mStackDiffs[depth];
367 int siblingPos = mStackSiblingPos[depth];
368 int firstChildPos;
satoka3d78f62010-12-09 22:08:33 +0900369 // depth will never be greater than maxDepth because in that case,
satokd2997922010-12-07 13:08:39 +0900370 // needsToTraverseChildrenNodes should be false
371 const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900372 maxDepth, traverseAllNodes, matchWeight, inputIndex, diffs, skipPos,
373 excessivePos, transposedPos, nextLetters, nextLettersSize, &childCount,
374 &firstChildPos, &traverseAllNodes, &matchWeight, &inputIndex, &diffs,
375 &siblingPos);
satok662fe692010-12-08 17:05:39 +0900376 // Update next sibling pos
satokd2997922010-12-07 13:08:39 +0900377 mStackSiblingPos[depth] = siblingPos;
378 if (needsToTraverseChildrenNodes) {
379 // Goes to child node
380 ++depth;
381 mStackChildCount[depth] = childCount;
382 mStackTraverseAll[depth] = traverseAllNodes;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900383 mStackNodeFreq[depth] = matchWeight;
satokd2997922010-12-07 13:08:39 +0900384 mStackInputIndex[depth] = inputIndex;
385 mStackDiffs[depth] = diffs;
386 mStackSiblingPos[depth] = firstChildPos;
387 }
388 } else {
satokcdbbea72010-12-08 16:04:16 +0900389 // Goes to parent sibling node
satokd2997922010-12-07 13:08:39 +0900390 --depth;
391 }
392 }
393}
394
satokf7425bb2011-01-05 16:37:53 +0900395inline static void multiplyRate(const int rate, int *freq) {
396 if (rate > 1000000) {
397 *freq = (*freq / 100) * rate;
398 } else {
399 *freq = *freq * rate / 100;
400 }
401}
402
satok817e5172011-03-04 06:06:45 -0800403bool UnigramDictionary::getSplitTwoWordsSuggestion(const int inputLength,
404 const int firstWordStartPos, const int firstWordLength, const int secondWordStartPos,
405 const int secondWordLength) {
406 if (inputLength >= MAX_WORD_LENGTH) return false;
407 if (0 >= firstWordLength || 0 >= secondWordLength || firstWordStartPos >= secondWordStartPos
408 || firstWordStartPos < 0 || secondWordStartPos >= inputLength)
409 return false;
410 const int newWordLength = firstWordLength + secondWordLength + 1;
satok662fe692010-12-08 17:05:39 +0900411 // Allocating variable length array on stack
412 unsigned short word[newWordLength];
satok817e5172011-03-04 06:06:45 -0800413 const int firstFreq = getBestWordFreq(firstWordStartPos, firstWordLength, mWord);
satokaee09dc2010-12-09 19:21:51 +0900414 if (DEBUG_DICT) LOGI("First freq: %d", firstFreq);
415 if (firstFreq <= 0) return false;
416
satok817e5172011-03-04 06:06:45 -0800417 for (int i = 0; i < firstWordLength; ++i) {
satokaee09dc2010-12-09 19:21:51 +0900418 word[i] = mWord[i];
satok662fe692010-12-08 17:05:39 +0900419 }
satokaee09dc2010-12-09 19:21:51 +0900420
satok817e5172011-03-04 06:06:45 -0800421 const int secondFreq = getBestWordFreq(secondWordStartPos, secondWordLength, mWord);
satoka3d78f62010-12-09 22:08:33 +0900422 if (DEBUG_DICT) LOGI("Second freq: %d", secondFreq);
satokaee09dc2010-12-09 19:21:51 +0900423 if (secondFreq <= 0) return false;
424
satok817e5172011-03-04 06:06:45 -0800425 word[firstWordLength] = SPACE;
426 for (int i = (firstWordLength + 1); i < newWordLength; ++i) {
427 word[i] = mWord[i - firstWordLength - 1];
satok662fe692010-12-08 17:05:39 +0900428 }
satokaee09dc2010-12-09 19:21:51 +0900429
430 int pairFreq = ((firstFreq + secondFreq) / 2);
431 for (int i = 0; i < inputLength; ++i) pairFreq *= TYPED_LETTER_MULTIPLIER;
satokf7425bb2011-01-05 16:37:53 +0900432 multiplyRate(WORDS_WITH_MISSING_SPACE_CHARACTER_DEMOTION_RATE, &pairFreq);
satok662fe692010-12-08 17:05:39 +0900433 addWord(word, newWordLength, pairFreq);
434 return true;
435}
436
satok817e5172011-03-04 06:06:45 -0800437bool UnigramDictionary::getMissingSpaceWords(const int inputLength, const int missingSpacePos) {
438 return getSplitTwoWordsSuggestion(
439 inputLength, 0, missingSpacePos, missingSpacePos, inputLength - missingSpacePos);
440}
441
442bool UnigramDictionary::getMistypedSpaceWords(const int inputLength, const int spaceProximityPos) {
443 return getSplitTwoWordsSuggestion(
444 inputLength, 0, spaceProximityPos, spaceProximityPos + 1,
445 inputLength - spaceProximityPos - 1);
446}
447
satok662fe692010-12-08 17:05:39 +0900448// Keep this for comparing spec to new getWords
449void UnigramDictionary::getWordsOld(const int initialPos, const int inputLength, const int skipPos,
satoka3d78f62010-12-09 22:08:33 +0900450 const int excessivePos, const int transposedPos,int *nextLetters,
451 const int nextLettersSize) {
satok662fe692010-12-08 17:05:39 +0900452 int initialPosition = initialPos;
453 const int count = Dictionary::getCount(DICT, &initialPosition);
454 getWordsRec(count, initialPosition, 0,
455 min(inputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH),
satoka3d78f62010-12-09 22:08:33 +0900456 mInputLength <= 0, 1, 0, 0, skipPos, excessivePos, transposedPos, nextLetters,
457 nextLettersSize);
satok662fe692010-12-08 17:05:39 +0900458}
459
satok68319262010-12-03 19:38:08 +0900460void UnigramDictionary::getWordsRec(const int childrenCount, const int pos, const int depth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900461 const int maxDepth, const bool traverseAllNodes, const int matchWeight,
462 const int inputIndex, const int diffs, const int skipPos, const int excessivePos,
463 const int transposedPos, int *nextLetters, const int nextLettersSize) {
satok48e432c2010-12-06 17:38:58 +0900464 int siblingPos = pos;
satok68319262010-12-03 19:38:08 +0900465 for (int i = 0; i < childrenCount; ++i) {
satok48e432c2010-12-06 17:38:58 +0900466 int newCount;
467 int newChildPosition;
satokd2997922010-12-07 13:08:39 +0900468 const int newDepth = depth + 1;
satok48e432c2010-12-06 17:38:58 +0900469 bool newTraverseAllNodes;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900470 int newMatchRate;
satok48e432c2010-12-06 17:38:58 +0900471 int newInputIndex;
472 int newDiffs;
473 int newSiblingPos;
474 const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth, maxDepth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900475 traverseAllNodes, matchWeight, inputIndex, diffs,
476 skipPos, excessivePos, transposedPos,
satoka3d78f62010-12-09 22:08:33 +0900477 nextLetters, nextLettersSize,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900478 &newCount, &newChildPosition, &newTraverseAllNodes, &newMatchRate,
satok48e432c2010-12-06 17:38:58 +0900479 &newInputIndex, &newDiffs, &newSiblingPos);
480 siblingPos = newSiblingPos;
satok30088252010-12-01 21:22:15 +0900481
satok48e432c2010-12-06 17:38:58 +0900482 if (needsToTraverseChildrenNodes) {
483 getWordsRec(newCount, newChildPosition, newDepth, maxDepth, newTraverseAllNodes,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900484 newMatchRate, newInputIndex, newDiffs, skipPos, excessivePos, transposedPos,
satoka3d78f62010-12-09 22:08:33 +0900485 nextLetters, nextLettersSize);
satok30088252010-12-01 21:22:15 +0900486 }
487 }
488}
489
Jean Chalarda5d58492011-02-18 17:50:58 +0900490static const int TWO_31ST_DIV_255 = ((1 << 31) - 1) / 255;
491static inline int capped255MultForFullMatchAccentsOrCapitalizationDifference(const int num) {
492 return (num < TWO_31ST_DIV_255 ? 255 * num : S_INT_MAX);
493}
satok58c49b92011-01-27 03:23:39 +0900494inline int UnigramDictionary::calculateFinalFreq(const int inputIndex, const int depth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900495 const int matchWeight, const int skipPos, const int excessivePos, const int transposedPos,
satok58c49b92011-01-27 03:23:39 +0900496 const int freq, const bool sameLength) {
satoka3d78f62010-12-09 22:08:33 +0900497 // TODO: Demote by edit distance
Jean Chalardf5f834a2011-02-22 15:12:46 +0900498 int finalFreq = freq * matchWeight;
satokf7425bb2011-01-05 16:37:53 +0900499 if (skipPos >= 0) multiplyRate(WORDS_WITH_MISSING_CHARACTER_DEMOTION_RATE, &finalFreq);
500 if (transposedPos >= 0) multiplyRate(
501 WORDS_WITH_TRANSPOSED_CHARACTERS_DEMOTION_RATE, &finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900502 if (excessivePos >= 0) {
satokf7425bb2011-01-05 16:37:53 +0900503 multiplyRate(WORDS_WITH_EXCESSIVE_CHARACTER_DEMOTION_RATE, &finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900504 if (!existsAdjacentProximityChars(inputIndex, mInputLength)) {
satokf7425bb2011-01-05 16:37:53 +0900505 multiplyRate(WORDS_WITH_EXCESSIVE_CHARACTER_OUT_OF_PROXIMITY_DEMOTION_RATE, &finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900506 }
507 }
satok58c49b92011-01-27 03:23:39 +0900508 int lengthFreq = TYPED_LETTER_MULTIPLIER;
509 for (int i = 0; i < depth; ++i) lengthFreq *= TYPED_LETTER_MULTIPLIER;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900510 if (lengthFreq == matchWeight) {
Jean Chalard8dc754a2011-01-27 14:20:22 +0900511 if (depth > 1) {
512 if (DEBUG_DICT) LOGI("Found full matched word.");
513 multiplyRate(FULL_MATCHED_WORDS_PROMOTION_RATE, &finalFreq);
514 }
515 if (sameLength && transposedPos < 0 && skipPos < 0 && excessivePos < 0) {
Jean Chalarda5d58492011-02-18 17:50:58 +0900516 finalFreq = capped255MultForFullMatchAccentsOrCapitalizationDifference(finalFreq);
Jean Chalard8dc754a2011-01-27 14:20:22 +0900517 }
satok58c49b92011-01-27 03:23:39 +0900518 }
satok54fe9e02010-12-13 14:42:35 +0900519 if (sameLength && skipPos < 0) finalFreq *= FULL_WORD_MULTIPLIER;
520 return finalFreq;
521}
satoka3d78f62010-12-09 22:08:33 +0900522
satok54fe9e02010-12-13 14:42:35 +0900523inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsGreaterThanInputLength(
Jean Chalardf5f834a2011-02-22 15:12:46 +0900524 unsigned short *word, const int inputIndex, const int depth, const int matchWeight,
satok54fe9e02010-12-13 14:42:35 +0900525 int *nextLetters, const int nextLettersSize, const int skipPos, const int excessivePos,
526 const int transposedPos, const int freq) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900527 const int finalFreq = calculateFinalFreq(inputIndex, depth, matchWeight, skipPos, excessivePos,
satok58c49b92011-01-27 03:23:39 +0900528 transposedPos, freq, false);
satoka3d78f62010-12-09 22:08:33 +0900529 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900530 if (depth >= mInputLength && skipPos < 0) {
satok715514d2010-12-02 20:19:59 +0900531 registerNextLetter(mWord[mInputLength], nextLetters, nextLettersSize);
532 }
533}
534
535inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsSameAsInputLength(
Jean Chalardf5f834a2011-02-22 15:12:46 +0900536 unsigned short *word, const int inputIndex, const int depth, const int matchWeight,
Jean Chalard8dc754a2011-01-27 14:20:22 +0900537 const int skipPos, const int excessivePos, const int transposedPos, const int freq) {
satok54fe9e02010-12-13 14:42:35 +0900538 if (sameAsTyped(word, depth + 1)) return;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900539 const int finalFreq = calculateFinalFreq(inputIndex, depth, matchWeight, skipPos,
satok54fe9e02010-12-13 14:42:35 +0900540 excessivePos, transposedPos, freq, true);
541 // Proximity collection will promote a word of the same length as what user typed.
542 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq);
satok715514d2010-12-02 20:19:59 +0900543}
satok28bd03b2010-12-03 16:39:16 +0900544
545inline bool UnigramDictionary::needsToSkipCurrentNode(const unsigned short c,
satok68319262010-12-03 19:38:08 +0900546 const int inputIndex, const int skipPos, const int depth) {
satok8fbd5522011-02-22 17:28:55 +0900547 const unsigned short userTypedChar = getInputCharsAt(inputIndex)[0];
satok28bd03b2010-12-03 16:39:16 +0900548 // Skip the ' or other letter and continue deeper
549 return (c == QUOTE && userTypedChar != QUOTE) || skipPos == depth;
550}
551
satoke07baa62010-12-09 21:55:40 +0900552inline bool UnigramDictionary::existsAdjacentProximityChars(const int inputIndex,
553 const int inputLength) {
554 if (inputIndex < 0 || inputIndex >= inputLength) return false;
555 const int currentChar = *getInputCharsAt(inputIndex);
556 const int leftIndex = inputIndex - 1;
557 if (leftIndex >= 0) {
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900558 const int *leftChars = getInputCharsAt(leftIndex);
satoke07baa62010-12-09 21:55:40 +0900559 int i = 0;
560 while (leftChars[i] > 0 && i < MAX_PROXIMITY_CHARS) {
561 if (leftChars[i++] == currentChar) return true;
562 }
563 }
564 const int rightIndex = inputIndex + 1;
565 if (rightIndex < inputLength) {
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900566 const int *rightChars = getInputCharsAt(rightIndex);
satoke07baa62010-12-09 21:55:40 +0900567 int i = 0;
568 while (rightChars[i] > 0 && i < MAX_PROXIMITY_CHARS) {
569 if (rightChars[i++] == currentChar) return true;
570 }
571 }
572 return false;
573}
574
Jean Chalarda5d58492011-02-18 17:50:58 +0900575
576// In the following function, c is the current character of the dictionary word
577// currently examined.
578// currentChars is an array containing the keys close to the character the
579// user actually typed at the same position. We want to see if c is in it: if so,
580// then the word contains at that position a character close to what the user
581// typed.
582// What the user typed is actually the first character of the array.
583// Notice : accented characters do not have a proximity list, so they are alone
584// in their list. The non-accented version of the character should be considered
585// "close", but not the other keys close to the non-accented version.
Jean Chalard8dc754a2011-01-27 14:20:22 +0900586inline UnigramDictionary::ProximityType UnigramDictionary::getMatchedProximityId(
587 const int *currentChars, const unsigned short c, const int skipPos,
588 const int excessivePos, const int transposedPos) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900589 const unsigned short baseLowerC = toBaseLowerCase(c);
Jean Chalarda5d58492011-02-18 17:50:58 +0900590
591 // The first char in the array is what user typed. If it matches right away,
592 // that means the user typed that same char for this pos.
Jean Chalardf5f834a2011-02-22 15:12:46 +0900593 if (currentChars[0] == baseLowerC || currentChars[0] == c)
Jean Chalarda5d58492011-02-18 17:50:58 +0900594 return SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR;
595
596 // If one of those is true, we should not check for close characters at all.
597 if (skipPos >= 0 || excessivePos >= 0 || transposedPos >= 0)
598 return UNRELATED_CHAR;
599
600 // If the non-accented, lowercased version of that first character matches c,
601 // then we have a non-accented version of the accented character the user
602 // typed. Treat it as a close char.
Jean Chalardf5f834a2011-02-22 15:12:46 +0900603 if (toBaseLowerCase(currentChars[0]) == baseLowerC)
Jean Chalarda5d58492011-02-18 17:50:58 +0900604 return NEAR_PROXIMITY_CHAR;
605
606 // Not an exact nor an accent-alike match: search the list of close keys
607 int j = 1;
satoke07baa62010-12-09 21:55:40 +0900608 while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900609 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
Jean Chalarda5d58492011-02-18 17:50:58 +0900610 if (matched) return NEAR_PROXIMITY_CHAR;
satok28bd03b2010-12-03 16:39:16 +0900611 ++j;
612 }
Jean Chalarda5d58492011-02-18 17:50:58 +0900613
614 // Was not included, signal this as an unrelated character.
Jean Chalard8dc754a2011-01-27 14:20:22 +0900615 return UNRELATED_CHAR;
satok28bd03b2010-12-03 16:39:16 +0900616}
617
satok48e432c2010-12-06 17:38:58 +0900618inline bool UnigramDictionary::processCurrentNode(const int pos, const int depth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900619 const int maxDepth, const bool traverseAllNodes, int matchWeight, int inputIndex,
satoka3d78f62010-12-09 22:08:33 +0900620 const int diffs, const int skipPos, const int excessivePos, const int transposedPos,
621 int *nextLetters, const int nextLettersSize, int *newCount, int *newChildPosition,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900622 bool *newTraverseAllNodes, int *newMatchRate, int *newInputIndex, int *newDiffs,
satoka3d78f62010-12-09 22:08:33 +0900623 int *nextSiblingPosition) {
624 if (DEBUG_DICT) {
625 int inputCount = 0;
626 if (skipPos >= 0) ++inputCount;
627 if (excessivePos >= 0) ++inputCount;
628 if (transposedPos >= 0) ++inputCount;
629 assert(inputCount <= 1);
630 }
satok48e432c2010-12-06 17:38:58 +0900631 unsigned short c;
632 int childPosition;
633 bool terminal;
634 int freq;
satokfd16f1d2011-01-27 16:25:16 +0900635 bool isSameAsUserTypedLength = false;
satokcdbbea72010-12-08 16:04:16 +0900636
satokfd16f1d2011-01-27 16:25:16 +0900637 if (excessivePos == depth && inputIndex < mInputLength - 1) ++inputIndex;
satokcdbbea72010-12-08 16:04:16 +0900638
satok48e432c2010-12-06 17:38:58 +0900639 *nextSiblingPosition = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, pos, &c,
640 &childPosition, &terminal, &freq);
641
642 const bool needsToTraverseChildrenNodes = childPosition != 0;
643
644 // If we are only doing traverseAllNodes, no need to look at the typed characters.
645 if (traverseAllNodes || needsToSkipCurrentNode(c, inputIndex, skipPos, depth)) {
646 mWord[depth] = c;
647 if (traverseAllNodes && terminal) {
satok54fe9e02010-12-13 14:42:35 +0900648 onTerminalWhenUserTypedLengthIsGreaterThanInputLength(mWord, inputIndex, depth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900649 matchWeight, nextLetters, nextLettersSize, skipPos, excessivePos, transposedPos,
650 freq);
satok48e432c2010-12-06 17:38:58 +0900651 }
652 if (!needsToTraverseChildrenNodes) return false;
653 *newTraverseAllNodes = traverseAllNodes;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900654 *newMatchRate = matchWeight;
satok48e432c2010-12-06 17:38:58 +0900655 *newDiffs = diffs;
656 *newInputIndex = inputIndex;
satok48e432c2010-12-06 17:38:58 +0900657 } else {
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900658 const int *currentChars = getInputCharsAt(inputIndex);
satoka3d78f62010-12-09 22:08:33 +0900659
660 if (transposedPos >= 0) {
661 if (inputIndex == transposedPos) currentChars += MAX_PROXIMITY_CHARS;
662 if (inputIndex == (transposedPos + 1)) currentChars -= MAX_PROXIMITY_CHARS;
663 }
664
665 int matchedProximityCharId = getMatchedProximityId(currentChars, c, skipPos, excessivePos,
666 transposedPos);
Jean Chalard8dc754a2011-01-27 14:20:22 +0900667 if (UNRELATED_CHAR == matchedProximityCharId) return false;
satok48e432c2010-12-06 17:38:58 +0900668 mWord[depth] = c;
669 // If inputIndex is greater than mInputLength, that means there is no
670 // proximity chars. So, we don't need to check proximity.
Jean Chalard8dc754a2011-01-27 14:20:22 +0900671 if (SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR == matchedProximityCharId) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900672 matchWeight = matchWeight * TYPED_LETTER_MULTIPLIER;
Jean Chalard8dc754a2011-01-27 14:20:22 +0900673 }
satokfd16f1d2011-01-27 16:25:16 +0900674 bool isSameAsUserTypedLength = mInputLength == inputIndex + 1
675 || (excessivePos == mInputLength - 1 && inputIndex == mInputLength - 2);
satok48e432c2010-12-06 17:38:58 +0900676 if (isSameAsUserTypedLength && terminal) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900677 onTerminalWhenUserTypedLengthIsSameAsInputLength(mWord, inputIndex, depth, matchWeight,
Jean Chalard8dc754a2011-01-27 14:20:22 +0900678 skipPos, excessivePos, transposedPos, freq);
satok48e432c2010-12-06 17:38:58 +0900679 }
680 if (!needsToTraverseChildrenNodes) return false;
681 // Start traversing all nodes after the index exceeds the user typed length
682 *newTraverseAllNodes = isSameAsUserTypedLength;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900683 *newMatchRate = matchWeight;
Jean Chalard8dc754a2011-01-27 14:20:22 +0900684 *newDiffs = diffs + ((NEAR_PROXIMITY_CHAR == matchedProximityCharId) ? 1 : 0);
satok48e432c2010-12-06 17:38:58 +0900685 *newInputIndex = inputIndex + 1;
satok48e432c2010-12-06 17:38:58 +0900686 }
687 // Optimization: Prune out words that are too long compared to how much was typed.
satokd2997922010-12-07 13:08:39 +0900688 if (depth >= maxDepth || *newDiffs > mMaxEditDistance) {
satok48e432c2010-12-06 17:38:58 +0900689 return false;
690 }
691
692 // If inputIndex is greater than mInputLength, that means there are no proximity chars.
satokfd16f1d2011-01-27 16:25:16 +0900693 // TODO: Check if this can be isSameAsUserTypedLength only.
694 if (isSameAsUserTypedLength || mInputLength <= *newInputIndex) {
satok48e432c2010-12-06 17:38:58 +0900695 *newTraverseAllNodes = true;
696 }
697 // get the count of nodes and increment childAddress.
698 *newCount = Dictionary::getCount(DICT, &childPosition);
699 *newChildPosition = childPosition;
700 if (DEBUG_DICT) assert(needsToTraverseChildrenNodes);
701 return needsToTraverseChildrenNodes;
702}
703
satokaee09dc2010-12-09 19:21:51 +0900704inline int UnigramDictionary::getBestWordFreq(const int startInputIndex, const int inputLength,
705 unsigned short *word) {
satok662fe692010-12-08 17:05:39 +0900706 int pos = ROOT_POS;
707 int count = Dictionary::getCount(DICT, &pos);
satokaee09dc2010-12-09 19:21:51 +0900708 int maxFreq = 0;
709 int depth = 0;
710 unsigned short newWord[MAX_WORD_LENGTH_INTERNAL];
satok662fe692010-12-08 17:05:39 +0900711 bool terminal = false;
712
satokaee09dc2010-12-09 19:21:51 +0900713 mStackChildCount[0] = count;
714 mStackSiblingPos[0] = pos;
715
716 while (depth >= 0) {
717 if (mStackChildCount[depth] > 0) {
718 --mStackChildCount[depth];
719 int firstChildPos;
720 int newFreq;
721 int siblingPos = mStackSiblingPos[depth];
722 const bool needsToTraverseChildrenNodes = processCurrentNodeForExactMatch(siblingPos,
723 startInputIndex, depth, newWord, &firstChildPos, &count, &terminal, &newFreq,
724 &siblingPos);
725 mStackSiblingPos[depth] = siblingPos;
726 if (depth == (inputLength - 1)) {
727 // Traverse sibling node
728 if (terminal) {
729 if (newFreq > maxFreq) {
730 for (int i = 0; i < inputLength; ++i) word[i] = newWord[i];
731 if (DEBUG_DICT && DEBUG_NODE) {
732 char s[inputLength + 1];
733 for (int i = 0; i < inputLength; ++i) s[i] = word[i];
734 s[inputLength] = 0;
735 LOGI("New missing space word found: %d > %d (%s), %d, %d",
736 newFreq, maxFreq, s, inputLength, depth);
737 }
738 maxFreq = newFreq;
739 }
740 }
741 } else if (needsToTraverseChildrenNodes) {
742 // Traverse children nodes
743 ++depth;
744 mStackChildCount[depth] = count;
745 mStackSiblingPos[depth] = firstChildPos;
746 }
747 } else {
748 // Traverse parent node
749 --depth;
satok662fe692010-12-08 17:05:39 +0900750 }
751 }
satokaee09dc2010-12-09 19:21:51 +0900752
753 word[inputLength] = 0;
754 return maxFreq;
satok662fe692010-12-08 17:05:39 +0900755}
756
757inline bool UnigramDictionary::processCurrentNodeForExactMatch(const int firstChildPos,
satokaee09dc2010-12-09 19:21:51 +0900758 const int startInputIndex, const int depth, unsigned short *word, int *newChildPosition,
759 int *newCount, bool *newTerminal, int *newFreq, int *siblingPos) {
760 const int inputIndex = startInputIndex + depth;
satok8fbd5522011-02-22 17:28:55 +0900761 const int *currentChars = getInputCharsAt(inputIndex);
satok662fe692010-12-08 17:05:39 +0900762 unsigned short c;
satokaee09dc2010-12-09 19:21:51 +0900763 *siblingPos = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, firstChildPos, &c,
764 newChildPosition, newTerminal, newFreq);
765 const unsigned int inputC = currentChars[0];
766 if (DEBUG_DICT) assert(inputC <= U_SHORT_MAX);
Jean Chalardf5f834a2011-02-22 15:12:46 +0900767 const unsigned short baseLowerC = toBaseLowerCase(c);
768 const bool matched = (inputC == baseLowerC || inputC == c);
satokaee09dc2010-12-09 19:21:51 +0900769 const bool hasChild = *newChildPosition != 0;
770 if (matched) {
771 word[depth] = c;
772 if (DEBUG_DICT && DEBUG_NODE) {
773 LOGI("Node(%c, %c)<%d>, %d, %d", inputC, c, matched, hasChild, *newFreq);
774 if (*newTerminal) LOGI("Terminal %d", *newFreq);
satok662fe692010-12-08 17:05:39 +0900775 }
satokaee09dc2010-12-09 19:21:51 +0900776 if (hasChild) {
777 *newCount = Dictionary::getCount(DICT, newChildPosition);
778 return true;
779 } else {
780 return false;
781 }
782 } else {
783 // If this node is not user typed character, this method treats this word as unmatched.
784 // Thus newTerminal shouldn't be true.
785 *newTerminal = false;
786 return false;
satok662fe692010-12-08 17:05:39 +0900787 }
satok662fe692010-12-08 17:05:39 +0900788}
satok30088252010-12-01 21:22:15 +0900789} // namespace latinime