blob: 3487d4f1182307bd59ba8d582cb2019b5a41746f [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,
Jean Chalard07a84062011-03-03 10:22:10 +0900496 const int freq, const bool sameLength) const {
satoka3d78f62010-12-09 22:08:33 +0900497 // TODO: Demote by edit distance
Jean Chalardf5f834a2011-02-22 15:12:46 +0900498 int finalFreq = freq * matchWeight;
Jean Chalard07a84062011-03-03 10:22:10 +0900499 if (skipPos >= 0) {
500 if (mInputLength >= 3) {
501 multiplyRate(WORDS_WITH_MISSING_CHARACTER_DEMOTION_RATE *
502 (mInputLength - 2) / (mInputLength - 1), &finalFreq);
503 } else {
504 finalFreq = 0;
505 }
506 }
satokf7425bb2011-01-05 16:37:53 +0900507 if (transposedPos >= 0) multiplyRate(
508 WORDS_WITH_TRANSPOSED_CHARACTERS_DEMOTION_RATE, &finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900509 if (excessivePos >= 0) {
satokf7425bb2011-01-05 16:37:53 +0900510 multiplyRate(WORDS_WITH_EXCESSIVE_CHARACTER_DEMOTION_RATE, &finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900511 if (!existsAdjacentProximityChars(inputIndex, mInputLength)) {
satokf7425bb2011-01-05 16:37:53 +0900512 multiplyRate(WORDS_WITH_EXCESSIVE_CHARACTER_OUT_OF_PROXIMITY_DEMOTION_RATE, &finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900513 }
514 }
satok58c49b92011-01-27 03:23:39 +0900515 int lengthFreq = TYPED_LETTER_MULTIPLIER;
516 for (int i = 0; i < depth; ++i) lengthFreq *= TYPED_LETTER_MULTIPLIER;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900517 if (lengthFreq == matchWeight) {
Jean Chalard8dc754a2011-01-27 14:20:22 +0900518 if (depth > 1) {
519 if (DEBUG_DICT) LOGI("Found full matched word.");
520 multiplyRate(FULL_MATCHED_WORDS_PROMOTION_RATE, &finalFreq);
521 }
522 if (sameLength && transposedPos < 0 && skipPos < 0 && excessivePos < 0) {
Jean Chalarda5d58492011-02-18 17:50:58 +0900523 finalFreq = capped255MultForFullMatchAccentsOrCapitalizationDifference(finalFreq);
Jean Chalard8dc754a2011-01-27 14:20:22 +0900524 }
satok58c49b92011-01-27 03:23:39 +0900525 }
satok54fe9e02010-12-13 14:42:35 +0900526 if (sameLength && skipPos < 0) finalFreq *= FULL_WORD_MULTIPLIER;
527 return finalFreq;
528}
satoka3d78f62010-12-09 22:08:33 +0900529
satok54fe9e02010-12-13 14:42:35 +0900530inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsGreaterThanInputLength(
Jean Chalardf5f834a2011-02-22 15:12:46 +0900531 unsigned short *word, const int inputIndex, const int depth, const int matchWeight,
satok54fe9e02010-12-13 14:42:35 +0900532 int *nextLetters, const int nextLettersSize, const int skipPos, const int excessivePos,
533 const int transposedPos, const int freq) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900534 const int finalFreq = calculateFinalFreq(inputIndex, depth, matchWeight, skipPos, excessivePos,
satok58c49b92011-01-27 03:23:39 +0900535 transposedPos, freq, false);
satoka3d78f62010-12-09 22:08:33 +0900536 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900537 if (depth >= mInputLength && skipPos < 0) {
satok715514d2010-12-02 20:19:59 +0900538 registerNextLetter(mWord[mInputLength], nextLetters, nextLettersSize);
539 }
540}
541
542inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsSameAsInputLength(
Jean Chalardf5f834a2011-02-22 15:12:46 +0900543 unsigned short *word, const int inputIndex, const int depth, const int matchWeight,
Jean Chalard8dc754a2011-01-27 14:20:22 +0900544 const int skipPos, const int excessivePos, const int transposedPos, const int freq) {
satok54fe9e02010-12-13 14:42:35 +0900545 if (sameAsTyped(word, depth + 1)) return;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900546 const int finalFreq = calculateFinalFreq(inputIndex, depth, matchWeight, skipPos,
satok54fe9e02010-12-13 14:42:35 +0900547 excessivePos, transposedPos, freq, true);
548 // Proximity collection will promote a word of the same length as what user typed.
549 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq);
satok715514d2010-12-02 20:19:59 +0900550}
satok28bd03b2010-12-03 16:39:16 +0900551
552inline bool UnigramDictionary::needsToSkipCurrentNode(const unsigned short c,
satok68319262010-12-03 19:38:08 +0900553 const int inputIndex, const int skipPos, const int depth) {
satok8fbd5522011-02-22 17:28:55 +0900554 const unsigned short userTypedChar = getInputCharsAt(inputIndex)[0];
satok28bd03b2010-12-03 16:39:16 +0900555 // Skip the ' or other letter and continue deeper
556 return (c == QUOTE && userTypedChar != QUOTE) || skipPos == depth;
557}
558
satoke07baa62010-12-09 21:55:40 +0900559inline bool UnigramDictionary::existsAdjacentProximityChars(const int inputIndex,
Jean Chalard07a84062011-03-03 10:22:10 +0900560 const int inputLength) const {
satoke07baa62010-12-09 21:55:40 +0900561 if (inputIndex < 0 || inputIndex >= inputLength) return false;
562 const int currentChar = *getInputCharsAt(inputIndex);
563 const int leftIndex = inputIndex - 1;
564 if (leftIndex >= 0) {
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900565 const int *leftChars = getInputCharsAt(leftIndex);
satoke07baa62010-12-09 21:55:40 +0900566 int i = 0;
567 while (leftChars[i] > 0 && i < MAX_PROXIMITY_CHARS) {
568 if (leftChars[i++] == currentChar) return true;
569 }
570 }
571 const int rightIndex = inputIndex + 1;
572 if (rightIndex < inputLength) {
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900573 const int *rightChars = getInputCharsAt(rightIndex);
satoke07baa62010-12-09 21:55:40 +0900574 int i = 0;
575 while (rightChars[i] > 0 && i < MAX_PROXIMITY_CHARS) {
576 if (rightChars[i++] == currentChar) return true;
577 }
578 }
579 return false;
580}
581
Jean Chalarda5d58492011-02-18 17:50:58 +0900582
583// In the following function, c is the current character of the dictionary word
584// currently examined.
585// currentChars is an array containing the keys close to the character the
586// user actually typed at the same position. We want to see if c is in it: if so,
587// then the word contains at that position a character close to what the user
588// typed.
589// What the user typed is actually the first character of the array.
590// Notice : accented characters do not have a proximity list, so they are alone
591// in their list. The non-accented version of the character should be considered
592// "close", but not the other keys close to the non-accented version.
Jean Chalard8dc754a2011-01-27 14:20:22 +0900593inline UnigramDictionary::ProximityType UnigramDictionary::getMatchedProximityId(
594 const int *currentChars, const unsigned short c, const int skipPos,
595 const int excessivePos, const int transposedPos) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900596 const unsigned short baseLowerC = toBaseLowerCase(c);
Jean Chalarda5d58492011-02-18 17:50:58 +0900597
598 // The first char in the array is what user typed. If it matches right away,
599 // that means the user typed that same char for this pos.
Jean Chalardf5f834a2011-02-22 15:12:46 +0900600 if (currentChars[0] == baseLowerC || currentChars[0] == c)
Jean Chalarda5d58492011-02-18 17:50:58 +0900601 return SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR;
602
603 // If one of those is true, we should not check for close characters at all.
604 if (skipPos >= 0 || excessivePos >= 0 || transposedPos >= 0)
605 return UNRELATED_CHAR;
606
607 // If the non-accented, lowercased version of that first character matches c,
608 // then we have a non-accented version of the accented character the user
609 // typed. Treat it as a close char.
Jean Chalardf5f834a2011-02-22 15:12:46 +0900610 if (toBaseLowerCase(currentChars[0]) == baseLowerC)
Jean Chalarda5d58492011-02-18 17:50:58 +0900611 return NEAR_PROXIMITY_CHAR;
612
613 // Not an exact nor an accent-alike match: search the list of close keys
614 int j = 1;
satoke07baa62010-12-09 21:55:40 +0900615 while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900616 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
Jean Chalarda5d58492011-02-18 17:50:58 +0900617 if (matched) return NEAR_PROXIMITY_CHAR;
satok28bd03b2010-12-03 16:39:16 +0900618 ++j;
619 }
Jean Chalarda5d58492011-02-18 17:50:58 +0900620
621 // Was not included, signal this as an unrelated character.
Jean Chalard8dc754a2011-01-27 14:20:22 +0900622 return UNRELATED_CHAR;
satok28bd03b2010-12-03 16:39:16 +0900623}
624
satok48e432c2010-12-06 17:38:58 +0900625inline bool UnigramDictionary::processCurrentNode(const int pos, const int depth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900626 const int maxDepth, const bool traverseAllNodes, int matchWeight, int inputIndex,
satoka3d78f62010-12-09 22:08:33 +0900627 const int diffs, const int skipPos, const int excessivePos, const int transposedPos,
628 int *nextLetters, const int nextLettersSize, int *newCount, int *newChildPosition,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900629 bool *newTraverseAllNodes, int *newMatchRate, int *newInputIndex, int *newDiffs,
satoka3d78f62010-12-09 22:08:33 +0900630 int *nextSiblingPosition) {
631 if (DEBUG_DICT) {
632 int inputCount = 0;
633 if (skipPos >= 0) ++inputCount;
634 if (excessivePos >= 0) ++inputCount;
635 if (transposedPos >= 0) ++inputCount;
636 assert(inputCount <= 1);
637 }
satok48e432c2010-12-06 17:38:58 +0900638 unsigned short c;
639 int childPosition;
640 bool terminal;
641 int freq;
satokfd16f1d2011-01-27 16:25:16 +0900642 bool isSameAsUserTypedLength = false;
satokcdbbea72010-12-08 16:04:16 +0900643
satokfd16f1d2011-01-27 16:25:16 +0900644 if (excessivePos == depth && inputIndex < mInputLength - 1) ++inputIndex;
satokcdbbea72010-12-08 16:04:16 +0900645
satok48e432c2010-12-06 17:38:58 +0900646 *nextSiblingPosition = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, pos, &c,
647 &childPosition, &terminal, &freq);
648
649 const bool needsToTraverseChildrenNodes = childPosition != 0;
650
651 // If we are only doing traverseAllNodes, no need to look at the typed characters.
652 if (traverseAllNodes || needsToSkipCurrentNode(c, inputIndex, skipPos, depth)) {
653 mWord[depth] = c;
654 if (traverseAllNodes && terminal) {
satok54fe9e02010-12-13 14:42:35 +0900655 onTerminalWhenUserTypedLengthIsGreaterThanInputLength(mWord, inputIndex, depth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900656 matchWeight, nextLetters, nextLettersSize, skipPos, excessivePos, transposedPos,
657 freq);
satok48e432c2010-12-06 17:38:58 +0900658 }
659 if (!needsToTraverseChildrenNodes) return false;
660 *newTraverseAllNodes = traverseAllNodes;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900661 *newMatchRate = matchWeight;
satok48e432c2010-12-06 17:38:58 +0900662 *newDiffs = diffs;
663 *newInputIndex = inputIndex;
satok48e432c2010-12-06 17:38:58 +0900664 } else {
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900665 const int *currentChars = getInputCharsAt(inputIndex);
satoka3d78f62010-12-09 22:08:33 +0900666
667 if (transposedPos >= 0) {
668 if (inputIndex == transposedPos) currentChars += MAX_PROXIMITY_CHARS;
669 if (inputIndex == (transposedPos + 1)) currentChars -= MAX_PROXIMITY_CHARS;
670 }
671
672 int matchedProximityCharId = getMatchedProximityId(currentChars, c, skipPos, excessivePos,
673 transposedPos);
Jean Chalard8dc754a2011-01-27 14:20:22 +0900674 if (UNRELATED_CHAR == matchedProximityCharId) return false;
satok48e432c2010-12-06 17:38:58 +0900675 mWord[depth] = c;
676 // If inputIndex is greater than mInputLength, that means there is no
677 // proximity chars. So, we don't need to check proximity.
Jean Chalard8dc754a2011-01-27 14:20:22 +0900678 if (SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR == matchedProximityCharId) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900679 matchWeight = matchWeight * TYPED_LETTER_MULTIPLIER;
Jean Chalard8dc754a2011-01-27 14:20:22 +0900680 }
satokfd16f1d2011-01-27 16:25:16 +0900681 bool isSameAsUserTypedLength = mInputLength == inputIndex + 1
682 || (excessivePos == mInputLength - 1 && inputIndex == mInputLength - 2);
satok48e432c2010-12-06 17:38:58 +0900683 if (isSameAsUserTypedLength && terminal) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900684 onTerminalWhenUserTypedLengthIsSameAsInputLength(mWord, inputIndex, depth, matchWeight,
Jean Chalard8dc754a2011-01-27 14:20:22 +0900685 skipPos, excessivePos, transposedPos, freq);
satok48e432c2010-12-06 17:38:58 +0900686 }
687 if (!needsToTraverseChildrenNodes) return false;
688 // Start traversing all nodes after the index exceeds the user typed length
689 *newTraverseAllNodes = isSameAsUserTypedLength;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900690 *newMatchRate = matchWeight;
Jean Chalard8dc754a2011-01-27 14:20:22 +0900691 *newDiffs = diffs + ((NEAR_PROXIMITY_CHAR == matchedProximityCharId) ? 1 : 0);
satok48e432c2010-12-06 17:38:58 +0900692 *newInputIndex = inputIndex + 1;
satok48e432c2010-12-06 17:38:58 +0900693 }
694 // Optimization: Prune out words that are too long compared to how much was typed.
satokd2997922010-12-07 13:08:39 +0900695 if (depth >= maxDepth || *newDiffs > mMaxEditDistance) {
satok48e432c2010-12-06 17:38:58 +0900696 return false;
697 }
698
699 // If inputIndex is greater than mInputLength, that means there are no proximity chars.
satokfd16f1d2011-01-27 16:25:16 +0900700 // TODO: Check if this can be isSameAsUserTypedLength only.
701 if (isSameAsUserTypedLength || mInputLength <= *newInputIndex) {
satok48e432c2010-12-06 17:38:58 +0900702 *newTraverseAllNodes = true;
703 }
704 // get the count of nodes and increment childAddress.
705 *newCount = Dictionary::getCount(DICT, &childPosition);
706 *newChildPosition = childPosition;
707 if (DEBUG_DICT) assert(needsToTraverseChildrenNodes);
708 return needsToTraverseChildrenNodes;
709}
710
satokaee09dc2010-12-09 19:21:51 +0900711inline int UnigramDictionary::getBestWordFreq(const int startInputIndex, const int inputLength,
712 unsigned short *word) {
satok662fe692010-12-08 17:05:39 +0900713 int pos = ROOT_POS;
714 int count = Dictionary::getCount(DICT, &pos);
satokaee09dc2010-12-09 19:21:51 +0900715 int maxFreq = 0;
716 int depth = 0;
717 unsigned short newWord[MAX_WORD_LENGTH_INTERNAL];
satok662fe692010-12-08 17:05:39 +0900718 bool terminal = false;
719
satokaee09dc2010-12-09 19:21:51 +0900720 mStackChildCount[0] = count;
721 mStackSiblingPos[0] = pos;
722
723 while (depth >= 0) {
724 if (mStackChildCount[depth] > 0) {
725 --mStackChildCount[depth];
726 int firstChildPos;
727 int newFreq;
728 int siblingPos = mStackSiblingPos[depth];
729 const bool needsToTraverseChildrenNodes = processCurrentNodeForExactMatch(siblingPos,
730 startInputIndex, depth, newWord, &firstChildPos, &count, &terminal, &newFreq,
731 &siblingPos);
732 mStackSiblingPos[depth] = siblingPos;
733 if (depth == (inputLength - 1)) {
734 // Traverse sibling node
735 if (terminal) {
736 if (newFreq > maxFreq) {
737 for (int i = 0; i < inputLength; ++i) word[i] = newWord[i];
738 if (DEBUG_DICT && DEBUG_NODE) {
739 char s[inputLength + 1];
740 for (int i = 0; i < inputLength; ++i) s[i] = word[i];
741 s[inputLength] = 0;
742 LOGI("New missing space word found: %d > %d (%s), %d, %d",
743 newFreq, maxFreq, s, inputLength, depth);
744 }
745 maxFreq = newFreq;
746 }
747 }
748 } else if (needsToTraverseChildrenNodes) {
749 // Traverse children nodes
750 ++depth;
751 mStackChildCount[depth] = count;
752 mStackSiblingPos[depth] = firstChildPos;
753 }
754 } else {
755 // Traverse parent node
756 --depth;
satok662fe692010-12-08 17:05:39 +0900757 }
758 }
satokaee09dc2010-12-09 19:21:51 +0900759
760 word[inputLength] = 0;
761 return maxFreq;
satok662fe692010-12-08 17:05:39 +0900762}
763
764inline bool UnigramDictionary::processCurrentNodeForExactMatch(const int firstChildPos,
satokaee09dc2010-12-09 19:21:51 +0900765 const int startInputIndex, const int depth, unsigned short *word, int *newChildPosition,
766 int *newCount, bool *newTerminal, int *newFreq, int *siblingPos) {
767 const int inputIndex = startInputIndex + depth;
satok8fbd5522011-02-22 17:28:55 +0900768 const int *currentChars = getInputCharsAt(inputIndex);
satok662fe692010-12-08 17:05:39 +0900769 unsigned short c;
satokaee09dc2010-12-09 19:21:51 +0900770 *siblingPos = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, firstChildPos, &c,
771 newChildPosition, newTerminal, newFreq);
772 const unsigned int inputC = currentChars[0];
773 if (DEBUG_DICT) assert(inputC <= U_SHORT_MAX);
Jean Chalardf5f834a2011-02-22 15:12:46 +0900774 const unsigned short baseLowerC = toBaseLowerCase(c);
775 const bool matched = (inputC == baseLowerC || inputC == c);
satokaee09dc2010-12-09 19:21:51 +0900776 const bool hasChild = *newChildPosition != 0;
777 if (matched) {
778 word[depth] = c;
779 if (DEBUG_DICT && DEBUG_NODE) {
780 LOGI("Node(%c, %c)<%d>, %d, %d", inputC, c, matched, hasChild, *newFreq);
781 if (*newTerminal) LOGI("Terminal %d", *newFreq);
satok662fe692010-12-08 17:05:39 +0900782 }
satokaee09dc2010-12-09 19:21:51 +0900783 if (hasChild) {
784 *newCount = Dictionary::getCount(DICT, newChildPosition);
785 return true;
786 } else {
787 return false;
788 }
789 } else {
790 // If this node is not user typed character, this method treats this word as unmatched.
791 // Thus newTerminal shouldn't be true.
792 *newTerminal = false;
793 return false;
satok662fe692010-12-08 17:05:39 +0900794 }
satok662fe692010-12-08 17:05:39 +0900795}
satok30088252010-12-01 21:22:15 +0900796} // namespace latinime