blob: c2cd76084f52b688056e5b0f16ee0d6860cce787 [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,
satok3c4bb772011-03-04 22:50:19 -080084 const 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));
satok817e5172011-03-04 06:06:45 -0800235 if (proximityInfo->hasSpaceProximity(x, y)) {
236 getMistypedSpaceWords(mInputLength, i);
237 }
satok817e5172011-03-04 06:06:45 -0800238 }
239 }
240 PROF_END(6);
satok30088252010-12-01 21:22:15 +0900241}
242
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900243void UnigramDictionary::initSuggestions(const int *codes, const int codesSize,
244 unsigned short *outWords, int *frequencies) {
satokf5cded12010-12-06 21:28:24 +0900245 if (DEBUG_DICT) LOGI("initSuggest");
satok30088252010-12-01 21:22:15 +0900246 mFrequencies = frequencies;
247 mOutputChars = outWords;
248 mInputCodes = codes;
249 mInputLength = codesSize;
250 mMaxEditDistance = mInputLength < 5 ? 2 : mInputLength / 2;
251}
252
satok715514d2010-12-02 20:19:59 +0900253void UnigramDictionary::registerNextLetter(
254 unsigned short c, int *nextLetters, int nextLettersSize) {
satok30088252010-12-01 21:22:15 +0900255 if (c < nextLettersSize) {
256 nextLetters[c]++;
257 }
258}
259
satok662fe692010-12-08 17:05:39 +0900260// TODO: We need to optimize addWord by using STL or something
satok28bd03b2010-12-03 16:39:16 +0900261bool UnigramDictionary::addWord(unsigned short *word, int length, int frequency) {
satok30088252010-12-01 21:22:15 +0900262 word[length] = 0;
satok662fe692010-12-08 17:05:39 +0900263 if (DEBUG_DICT && DEBUG_SHOW_FOUND_WORD) {
satok30088252010-12-01 21:22:15 +0900264 char s[length + 1];
265 for (int i = 0; i <= length; i++) s[i] = word[i];
satok662fe692010-12-08 17:05:39 +0900266 LOGI("Found word = %s, freq = %d", s, frequency);
satok30088252010-12-01 21:22:15 +0900267 }
satokf5cded12010-12-06 21:28:24 +0900268 if (length > MAX_WORD_LENGTH) {
269 if (DEBUG_DICT) LOGI("Exceeded max word length.");
270 return false;
271 }
satok30088252010-12-01 21:22:15 +0900272
273 // Find the right insertion point
274 int insertAt = 0;
275 while (insertAt < MAX_WORDS) {
satok715514d2010-12-02 20:19:59 +0900276 if (frequency > mFrequencies[insertAt] || (mFrequencies[insertAt] == frequency
277 && length < Dictionary::wideStrLen(mOutputChars + insertAt * MAX_WORD_LENGTH))) {
satok30088252010-12-01 21:22:15 +0900278 break;
279 }
280 insertAt++;
281 }
282 if (insertAt < MAX_WORDS) {
satokcdbbea72010-12-08 16:04:16 +0900283 if (DEBUG_DICT) {
284 char s[length + 1];
285 for (int i = 0; i <= length; i++) s[i] = word[i];
satok662fe692010-12-08 17:05:39 +0900286 LOGI("Added word = %s, freq = %d", s, frequency);
satokcdbbea72010-12-08 16:04:16 +0900287 }
satok30088252010-12-01 21:22:15 +0900288 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),
satok715514d2010-12-02 20:19:59 +0900293 (char*) mOutputChars + insertAt * MAX_WORD_LENGTH * sizeof(short),
satok30088252010-12-01 21:22:15 +0900294 (MAX_WORDS - insertAt - 1) * sizeof(short) * MAX_WORD_LENGTH);
satok715514d2010-12-02 20:19:59 +0900295 unsigned short *dest = mOutputChars + insertAt * MAX_WORD_LENGTH;
satok30088252010-12-01 21:22:15 +0900296 while (length--) {
297 *dest++ = *word++;
298 }
299 *dest = 0; // NULL terminate
satok662fe692010-12-08 17:05:39 +0900300 if (DEBUG_DICT) LOGI("Added word at %d", insertAt);
satok30088252010-12-01 21:22:15 +0900301 return true;
302 }
303 return false;
304}
305
Jean Chalardf5f834a2011-02-22 15:12:46 +0900306unsigned short UnigramDictionary::toBaseLowerCase(unsigned short c) {
satok30088252010-12-01 21:22:15 +0900307 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
satok28bd03b2010-12-03 16:39:16 +0900318bool UnigramDictionary::sameAsTyped(unsigned short *word, int length) {
satok30088252010-12-01 21:22:15 +0900319 if (length != mInputLength) {
320 return false;
321 }
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900322 const int *inputCodes = mInputCodes;
satok30088252010-12-01 21:22:15 +0900323 while (length--) {
324 if ((unsigned int) *inputCodes != (unsigned int) *word) {
325 return false;
326 }
satok662fe692010-12-08 17:05:39 +0900327 inputCodes += MAX_PROXIMITY_CHARS;
satok30088252010-12-01 21:22:15 +0900328 word++;
329 }
330 return true;
331}
332
satok715514d2010-12-02 20:19:59 +0900333static const char QUOTE = '\'';
satok662fe692010-12-08 17:05:39 +0900334static const char SPACE = ' ';
satok30088252010-12-01 21:22:15 +0900335
satok54fe9e02010-12-13 14:42:35 +0900336void UnigramDictionary::getSuggestionCandidates(const int skipPos,
satoka3d78f62010-12-09 22:08:33 +0900337 const int excessivePos, const int transposedPos, int *nextLetters,
338 const int nextLettersSize, const int maxDepth) {
satok54fe9e02010-12-13 14:42:35 +0900339 if (DEBUG_DICT) {
340 LOGI("getSuggestionCandidates %d", maxDepth);
341 assert(transposedPos + 1 < mInputLength);
342 assert(excessivePos < mInputLength);
343 assert(missingPos < mInputLength);
344 }
satok662fe692010-12-08 17:05:39 +0900345 int rootPosition = ROOT_POS;
satokd2997922010-12-07 13:08:39 +0900346 // 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
satok662fe692010-12-08 17:05:39 +0900357 // Depth first search
satokd2997922010-12-07 13:08:39 +0900358 while (depth >= 0) {
359 if (mStackChildCount[depth] > 0) {
360 --mStackChildCount[depth];
361 bool traverseAllNodes = mStackTraverseAll[depth];
Jean Chalardf5f834a2011-02-22 15:12:46 +0900362 int matchWeight = mStackNodeFreq[depth];
satokd2997922010-12-07 13:08:39 +0900363 int inputIndex = mStackInputIndex[depth];
364 int diffs = mStackDiffs[depth];
365 int siblingPos = mStackSiblingPos[depth];
366 int firstChildPos;
satoka3d78f62010-12-09 22:08:33 +0900367 // depth will never be greater than maxDepth because in that case,
satokd2997922010-12-07 13:08:39 +0900368 // needsToTraverseChildrenNodes should be false
369 const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900370 maxDepth, traverseAllNodes, matchWeight, inputIndex, diffs, skipPos,
371 excessivePos, transposedPos, nextLetters, nextLettersSize, &childCount,
372 &firstChildPos, &traverseAllNodes, &matchWeight, &inputIndex, &diffs,
373 &siblingPos);
satok662fe692010-12-08 17:05:39 +0900374 // Update next sibling pos
satokd2997922010-12-07 13:08:39 +0900375 mStackSiblingPos[depth] = siblingPos;
376 if (needsToTraverseChildrenNodes) {
377 // Goes to child node
378 ++depth;
379 mStackChildCount[depth] = childCount;
380 mStackTraverseAll[depth] = traverseAllNodes;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900381 mStackNodeFreq[depth] = matchWeight;
satokd2997922010-12-07 13:08:39 +0900382 mStackInputIndex[depth] = inputIndex;
383 mStackDiffs[depth] = diffs;
384 mStackSiblingPos[depth] = firstChildPos;
385 }
386 } else {
satokcdbbea72010-12-08 16:04:16 +0900387 // Goes to parent sibling node
satokd2997922010-12-07 13:08:39 +0900388 --depth;
389 }
390 }
391}
392
satokf7425bb2011-01-05 16:37:53 +0900393inline 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
satok817e5172011-03-04 06:06:45 -0800401bool 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
satok3c4bb772011-03-04 22:50:19 -0800406 || firstWordStartPos < 0 || secondWordStartPos + secondWordLength > inputLength)
satok817e5172011-03-04 06:06:45 -0800407 return false;
408 const int newWordLength = firstWordLength + secondWordLength + 1;
satok662fe692010-12-08 17:05:39 +0900409 // Allocating variable length array on stack
410 unsigned short word[newWordLength];
satok817e5172011-03-04 06:06:45 -0800411 const int firstFreq = getBestWordFreq(firstWordStartPos, firstWordLength, mWord);
satokaee09dc2010-12-09 19:21:51 +0900412 if (DEBUG_DICT) LOGI("First freq: %d", firstFreq);
413 if (firstFreq <= 0) return false;
414
satok817e5172011-03-04 06:06:45 -0800415 for (int i = 0; i < firstWordLength; ++i) {
satokaee09dc2010-12-09 19:21:51 +0900416 word[i] = mWord[i];
satok662fe692010-12-08 17:05:39 +0900417 }
satokaee09dc2010-12-09 19:21:51 +0900418
satok817e5172011-03-04 06:06:45 -0800419 const int secondFreq = getBestWordFreq(secondWordStartPos, secondWordLength, mWord);
satoka3d78f62010-12-09 22:08:33 +0900420 if (DEBUG_DICT) LOGI("Second freq: %d", secondFreq);
satokaee09dc2010-12-09 19:21:51 +0900421 if (secondFreq <= 0) return false;
422
satok817e5172011-03-04 06:06:45 -0800423 word[firstWordLength] = SPACE;
424 for (int i = (firstWordLength + 1); i < newWordLength; ++i) {
425 word[i] = mWord[i - firstWordLength - 1];
satok662fe692010-12-08 17:05:39 +0900426 }
satokaee09dc2010-12-09 19:21:51 +0900427
428 int pairFreq = ((firstFreq + secondFreq) / 2);
429 for (int i = 0; i < inputLength; ++i) pairFreq *= TYPED_LETTER_MULTIPLIER;
satokf7425bb2011-01-05 16:37:53 +0900430 multiplyRate(WORDS_WITH_MISSING_SPACE_CHARACTER_DEMOTION_RATE, &pairFreq);
satok662fe692010-12-08 17:05:39 +0900431 addWord(word, newWordLength, pairFreq);
432 return true;
433}
434
satok817e5172011-03-04 06:06:45 -0800435bool UnigramDictionary::getMissingSpaceWords(const int inputLength, const int missingSpacePos) {
436 return getSplitTwoWordsSuggestion(
437 inputLength, 0, missingSpacePos, missingSpacePos, inputLength - missingSpacePos);
438}
439
440bool UnigramDictionary::getMistypedSpaceWords(const int inputLength, const int spaceProximityPos) {
441 return getSplitTwoWordsSuggestion(
442 inputLength, 0, spaceProximityPos, spaceProximityPos + 1,
443 inputLength - spaceProximityPos - 1);
444}
445
satok662fe692010-12-08 17:05:39 +0900446// Keep this for comparing spec to new getWords
447void UnigramDictionary::getWordsOld(const int initialPos, const int inputLength, const int skipPos,
satoka3d78f62010-12-09 22:08:33 +0900448 const int excessivePos, const int transposedPos,int *nextLetters,
449 const int nextLettersSize) {
satok662fe692010-12-08 17:05:39 +0900450 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),
satoka3d78f62010-12-09 22:08:33 +0900454 mInputLength <= 0, 1, 0, 0, skipPos, excessivePos, transposedPos, nextLetters,
455 nextLettersSize);
satok662fe692010-12-08 17:05:39 +0900456}
457
satok68319262010-12-03 19:38:08 +0900458void UnigramDictionary::getWordsRec(const int childrenCount, const int pos, const int depth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900459 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) {
satok48e432c2010-12-06 17:38:58 +0900462 int siblingPos = pos;
satok68319262010-12-03 19:38:08 +0900463 for (int i = 0; i < childrenCount; ++i) {
satok48e432c2010-12-06 17:38:58 +0900464 int newCount;
465 int newChildPosition;
satokd2997922010-12-07 13:08:39 +0900466 const int newDepth = depth + 1;
satok48e432c2010-12-06 17:38:58 +0900467 bool newTraverseAllNodes;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900468 int newMatchRate;
satok48e432c2010-12-06 17:38:58 +0900469 int newInputIndex;
470 int newDiffs;
471 int newSiblingPos;
472 const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth, maxDepth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900473 traverseAllNodes, matchWeight, inputIndex, diffs,
474 skipPos, excessivePos, transposedPos,
satoka3d78f62010-12-09 22:08:33 +0900475 nextLetters, nextLettersSize,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900476 &newCount, &newChildPosition, &newTraverseAllNodes, &newMatchRate,
satok48e432c2010-12-06 17:38:58 +0900477 &newInputIndex, &newDiffs, &newSiblingPos);
478 siblingPos = newSiblingPos;
satok30088252010-12-01 21:22:15 +0900479
satok48e432c2010-12-06 17:38:58 +0900480 if (needsToTraverseChildrenNodes) {
481 getWordsRec(newCount, newChildPosition, newDepth, maxDepth, newTraverseAllNodes,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900482 newMatchRate, newInputIndex, newDiffs, skipPos, excessivePos, transposedPos,
satoka3d78f62010-12-09 22:08:33 +0900483 nextLetters, nextLettersSize);
satok30088252010-12-01 21:22:15 +0900484 }
485 }
486}
487
satok3c4bb772011-03-04 22:50:19 -0800488static const int TWO_31ST_DIV_255 = S_INT_MAX / 255;
Jean Chalarda5d58492011-02-18 17:50:58 +0900489static inline int capped255MultForFullMatchAccentsOrCapitalizationDifference(const int num) {
490 return (num < TWO_31ST_DIV_255 ? 255 * num : S_INT_MAX);
491}
satok58c49b92011-01-27 03:23:39 +0900492inline int UnigramDictionary::calculateFinalFreq(const int inputIndex, const int depth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900493 const int matchWeight, const int skipPos, const int excessivePos, const int transposedPos,
Jean Chalard07a84062011-03-03 10:22:10 +0900494 const int freq, const bool sameLength) const {
satoka3d78f62010-12-09 22:08:33 +0900495 // TODO: Demote by edit distance
Jean Chalardf5f834a2011-02-22 15:12:46 +0900496 int finalFreq = freq * matchWeight;
Jean Chalard07a84062011-03-03 10:22:10 +0900497 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 }
satokf7425bb2011-01-05 16:37:53 +0900505 if (transposedPos >= 0) multiplyRate(
506 WORDS_WITH_TRANSPOSED_CHARACTERS_DEMOTION_RATE, &finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900507 if (excessivePos >= 0) {
satokf7425bb2011-01-05 16:37:53 +0900508 multiplyRate(WORDS_WITH_EXCESSIVE_CHARACTER_DEMOTION_RATE, &finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900509 if (!existsAdjacentProximityChars(inputIndex, mInputLength)) {
satokf7425bb2011-01-05 16:37:53 +0900510 multiplyRate(WORDS_WITH_EXCESSIVE_CHARACTER_OUT_OF_PROXIMITY_DEMOTION_RATE, &finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900511 }
512 }
satok58c49b92011-01-27 03:23:39 +0900513 int lengthFreq = TYPED_LETTER_MULTIPLIER;
514 for (int i = 0; i < depth; ++i) lengthFreq *= TYPED_LETTER_MULTIPLIER;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900515 if (lengthFreq == matchWeight) {
Jean Chalard8dc754a2011-01-27 14:20:22 +0900516 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 Chalarda5d58492011-02-18 17:50:58 +0900521 finalFreq = capped255MultForFullMatchAccentsOrCapitalizationDifference(finalFreq);
Jean Chalard8dc754a2011-01-27 14:20:22 +0900522 }
satok58c49b92011-01-27 03:23:39 +0900523 }
satok54fe9e02010-12-13 14:42:35 +0900524 if (sameLength && skipPos < 0) finalFreq *= FULL_WORD_MULTIPLIER;
525 return finalFreq;
526}
satoka3d78f62010-12-09 22:08:33 +0900527
satok54fe9e02010-12-13 14:42:35 +0900528inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsGreaterThanInputLength(
Jean Chalardf5f834a2011-02-22 15:12:46 +0900529 unsigned short *word, const int inputIndex, const int depth, const int matchWeight,
satok54fe9e02010-12-13 14:42:35 +0900530 int *nextLetters, const int nextLettersSize, const int skipPos, const int excessivePos,
531 const int transposedPos, const int freq) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900532 const int finalFreq = calculateFinalFreq(inputIndex, depth, matchWeight, skipPos, excessivePos,
satok58c49b92011-01-27 03:23:39 +0900533 transposedPos, freq, false);
satoka3d78f62010-12-09 22:08:33 +0900534 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900535 if (depth >= mInputLength && skipPos < 0) {
satok715514d2010-12-02 20:19:59 +0900536 registerNextLetter(mWord[mInputLength], nextLetters, nextLettersSize);
537 }
538}
539
540inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsSameAsInputLength(
Jean Chalardf5f834a2011-02-22 15:12:46 +0900541 unsigned short *word, const int inputIndex, const int depth, const int matchWeight,
Jean Chalard8dc754a2011-01-27 14:20:22 +0900542 const int skipPos, const int excessivePos, const int transposedPos, const int freq) {
satok54fe9e02010-12-13 14:42:35 +0900543 if (sameAsTyped(word, depth + 1)) return;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900544 const int finalFreq = calculateFinalFreq(inputIndex, depth, matchWeight, skipPos,
satok54fe9e02010-12-13 14:42:35 +0900545 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);
satok715514d2010-12-02 20:19:59 +0900548}
satok28bd03b2010-12-03 16:39:16 +0900549
550inline bool UnigramDictionary::needsToSkipCurrentNode(const unsigned short c,
satok68319262010-12-03 19:38:08 +0900551 const int inputIndex, const int skipPos, const int depth) {
satok8fbd5522011-02-22 17:28:55 +0900552 const unsigned short userTypedChar = getInputCharsAt(inputIndex)[0];
satok28bd03b2010-12-03 16:39:16 +0900553 // Skip the ' or other letter and continue deeper
554 return (c == QUOTE && userTypedChar != QUOTE) || skipPos == depth;
555}
556
satoke07baa62010-12-09 21:55:40 +0900557inline bool UnigramDictionary::existsAdjacentProximityChars(const int inputIndex,
Jean Chalard07a84062011-03-03 10:22:10 +0900558 const int inputLength) const {
satoke07baa62010-12-09 21:55:40 +0900559 if (inputIndex < 0 || inputIndex >= inputLength) return false;
560 const int currentChar = *getInputCharsAt(inputIndex);
561 const int leftIndex = inputIndex - 1;
562 if (leftIndex >= 0) {
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900563 const int *leftChars = getInputCharsAt(leftIndex);
satoke07baa62010-12-09 21:55:40 +0900564 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 Chalardc2bbc6a2011-02-25 17:56:53 +0900571 const int *rightChars = getInputCharsAt(rightIndex);
satoke07baa62010-12-09 21:55:40 +0900572 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 Chalarda5d58492011-02-18 17:50:58 +0900580
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 Chalard8dc754a2011-01-27 14:20:22 +0900591inline UnigramDictionary::ProximityType UnigramDictionary::getMatchedProximityId(
592 const int *currentChars, const unsigned short c, const int skipPos,
593 const int excessivePos, const int transposedPos) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900594 const unsigned short baseLowerC = toBaseLowerCase(c);
Jean Chalarda5d58492011-02-18 17:50:58 +0900595
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 Chalardf5f834a2011-02-22 15:12:46 +0900598 if (currentChars[0] == baseLowerC || currentChars[0] == c)
Jean Chalarda5d58492011-02-18 17:50:58 +0900599 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 Chalardf5f834a2011-02-22 15:12:46 +0900608 if (toBaseLowerCase(currentChars[0]) == baseLowerC)
Jean Chalarda5d58492011-02-18 17:50:58 +0900609 return NEAR_PROXIMITY_CHAR;
610
611 // Not an exact nor an accent-alike match: search the list of close keys
612 int j = 1;
satoke07baa62010-12-09 21:55:40 +0900613 while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900614 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
Jean Chalarda5d58492011-02-18 17:50:58 +0900615 if (matched) return NEAR_PROXIMITY_CHAR;
satok28bd03b2010-12-03 16:39:16 +0900616 ++j;
617 }
Jean Chalarda5d58492011-02-18 17:50:58 +0900618
619 // Was not included, signal this as an unrelated character.
Jean Chalard8dc754a2011-01-27 14:20:22 +0900620 return UNRELATED_CHAR;
satok28bd03b2010-12-03 16:39:16 +0900621}
622
satok48e432c2010-12-06 17:38:58 +0900623inline bool UnigramDictionary::processCurrentNode(const int pos, const int depth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900624 const int maxDepth, const bool traverseAllNodes, int matchWeight, int inputIndex,
satoka3d78f62010-12-09 22:08:33 +0900625 const int diffs, const int skipPos, const int excessivePos, const int transposedPos,
626 int *nextLetters, const int nextLettersSize, int *newCount, int *newChildPosition,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900627 bool *newTraverseAllNodes, int *newMatchRate, int *newInputIndex, int *newDiffs,
satoka3d78f62010-12-09 22:08:33 +0900628 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 }
satok48e432c2010-12-06 17:38:58 +0900636 unsigned short c;
637 int childPosition;
638 bool terminal;
639 int freq;
satokfd16f1d2011-01-27 16:25:16 +0900640 bool isSameAsUserTypedLength = false;
satokcdbbea72010-12-08 16:04:16 +0900641
satokfd16f1d2011-01-27 16:25:16 +0900642 if (excessivePos == depth && inputIndex < mInputLength - 1) ++inputIndex;
satokcdbbea72010-12-08 16:04:16 +0900643
satok48e432c2010-12-06 17:38:58 +0900644 *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) {
satok54fe9e02010-12-13 14:42:35 +0900653 onTerminalWhenUserTypedLengthIsGreaterThanInputLength(mWord, inputIndex, depth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900654 matchWeight, nextLetters, nextLettersSize, skipPos, excessivePos, transposedPos,
655 freq);
satok48e432c2010-12-06 17:38:58 +0900656 }
657 if (!needsToTraverseChildrenNodes) return false;
658 *newTraverseAllNodes = traverseAllNodes;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900659 *newMatchRate = matchWeight;
satok48e432c2010-12-06 17:38:58 +0900660 *newDiffs = diffs;
661 *newInputIndex = inputIndex;
satok48e432c2010-12-06 17:38:58 +0900662 } else {
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900663 const int *currentChars = getInputCharsAt(inputIndex);
satoka3d78f62010-12-09 22:08:33 +0900664
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 Chalard8dc754a2011-01-27 14:20:22 +0900672 if (UNRELATED_CHAR == matchedProximityCharId) return false;
satok48e432c2010-12-06 17:38:58 +0900673 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 Chalard8dc754a2011-01-27 14:20:22 +0900676 if (SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR == matchedProximityCharId) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900677 matchWeight = matchWeight * TYPED_LETTER_MULTIPLIER;
Jean Chalard8dc754a2011-01-27 14:20:22 +0900678 }
satokfd16f1d2011-01-27 16:25:16 +0900679 bool isSameAsUserTypedLength = mInputLength == inputIndex + 1
680 || (excessivePos == mInputLength - 1 && inputIndex == mInputLength - 2);
satok48e432c2010-12-06 17:38:58 +0900681 if (isSameAsUserTypedLength && terminal) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900682 onTerminalWhenUserTypedLengthIsSameAsInputLength(mWord, inputIndex, depth, matchWeight,
Jean Chalard8dc754a2011-01-27 14:20:22 +0900683 skipPos, excessivePos, transposedPos, freq);
satok48e432c2010-12-06 17:38:58 +0900684 }
685 if (!needsToTraverseChildrenNodes) return false;
686 // Start traversing all nodes after the index exceeds the user typed length
687 *newTraverseAllNodes = isSameAsUserTypedLength;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900688 *newMatchRate = matchWeight;
Jean Chalard8dc754a2011-01-27 14:20:22 +0900689 *newDiffs = diffs + ((NEAR_PROXIMITY_CHAR == matchedProximityCharId) ? 1 : 0);
satok48e432c2010-12-06 17:38:58 +0900690 *newInputIndex = inputIndex + 1;
satok48e432c2010-12-06 17:38:58 +0900691 }
692 // Optimization: Prune out words that are too long compared to how much was typed.
satokd2997922010-12-07 13:08:39 +0900693 if (depth >= maxDepth || *newDiffs > mMaxEditDistance) {
satok48e432c2010-12-06 17:38:58 +0900694 return false;
695 }
696
697 // If inputIndex is greater than mInputLength, that means there are no proximity chars.
satokfd16f1d2011-01-27 16:25:16 +0900698 // TODO: Check if this can be isSameAsUserTypedLength only.
699 if (isSameAsUserTypedLength || mInputLength <= *newInputIndex) {
satok48e432c2010-12-06 17:38:58 +0900700 *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
satokaee09dc2010-12-09 19:21:51 +0900709inline int UnigramDictionary::getBestWordFreq(const int startInputIndex, const int inputLength,
710 unsigned short *word) {
satok662fe692010-12-08 17:05:39 +0900711 int pos = ROOT_POS;
712 int count = Dictionary::getCount(DICT, &pos);
satokaee09dc2010-12-09 19:21:51 +0900713 int maxFreq = 0;
714 int depth = 0;
715 unsigned short newWord[MAX_WORD_LENGTH_INTERNAL];
satok662fe692010-12-08 17:05:39 +0900716 bool terminal = false;
717
satokaee09dc2010-12-09 19:21:51 +0900718 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;
satok662fe692010-12-08 17:05:39 +0900755 }
756 }
satokaee09dc2010-12-09 19:21:51 +0900757
758 word[inputLength] = 0;
759 return maxFreq;
satok662fe692010-12-08 17:05:39 +0900760}
761
762inline bool UnigramDictionary::processCurrentNodeForExactMatch(const int firstChildPos,
satokaee09dc2010-12-09 19:21:51 +0900763 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;
satok8fbd5522011-02-22 17:28:55 +0900766 const int *currentChars = getInputCharsAt(inputIndex);
satok662fe692010-12-08 17:05:39 +0900767 unsigned short c;
satokaee09dc2010-12-09 19:21:51 +0900768 *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 Chalardf5f834a2011-02-22 15:12:46 +0900772 const unsigned short baseLowerC = toBaseLowerCase(c);
773 const bool matched = (inputC == baseLowerC || inputC == c);
satokaee09dc2010-12-09 19:21:51 +0900774 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);
satok662fe692010-12-08 17:05:39 +0900780 }
satokaee09dc2010-12-09 19:21:51 +0900781 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;
satok662fe692010-12-08 17:05:39 +0900792 }
satok662fe692010-12-08 17:05:39 +0900793}
satok30088252010-12-01 21:22:15 +0900794} // namespace latinime