blob: b82910a40103887a47ed1e19686e408b227edc9a [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
145 PROF_START(6);
146 // Get the word count
147 int suggestedWordsCount = 0;
148 while (suggestedWordsCount < MAX_WORDS && mFrequencies[suggestedWordsCount] > 0) {
149 suggestedWordsCount++;
150 }
151
152 if (DEBUG_DICT) {
153 LOGI("Returning %d words", suggestedWordsCount);
154 LOGI("Next letters: ");
155 for (int k = 0; k < NEXT_LETTERS_SIZE; k++) {
156 if (mNextLettersFrequency[k] > 0) {
157 LOGI("%c = %d,", k, mNextLettersFrequency[k]);
158 }
159 }
160 }
161 PROF_END(6);
162 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
satok8fbd5522011-02-22 17:28:55 +0900175 if (DEBUG_PROXIMITY_INFO) {
176 for (int i = 0; i < codesSize; ++i) {
177 LOGI("Input[%d] x = %d, y = %d", i, xcoordinates[i], ycoordinates[i]);
178 }
179 }
180
satoka3d78f62010-12-09 22:08:33 +0900181 const int MAX_DEPTH = min(mInputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH);
satok61e2f852011-01-05 14:13:07 +0900182 PROF_END(0);
satok30088252010-12-01 21:22:15 +0900183
satok61e2f852011-01-05 14:13:07 +0900184 PROF_START(1);
Tadashi G. Takaoka887f11e2011-02-10 20:53:58 +0900185 getSuggestionCandidates(-1, -1, -1, mNextLettersFrequency, NEXT_LETTERS_SIZE, MAX_DEPTH);
satok61e2f852011-01-05 14:13:07 +0900186 PROF_END(1);
187
188 PROF_START(2);
satok662fe692010-12-08 17:05:39 +0900189 // Suggestion with missing character
190 if (SUGGEST_WORDS_WITH_MISSING_CHARACTER) {
satok30088252010-12-01 21:22:15 +0900191 for (int i = 0; i < codesSize; ++i) {
satokcdbbea72010-12-08 16:04:16 +0900192 if (DEBUG_DICT) LOGI("--- Suggest missing characters %d", i);
satok54fe9e02010-12-13 14:42:35 +0900193 getSuggestionCandidates(i, -1, -1, NULL, 0, MAX_DEPTH);
satokcdbbea72010-12-08 16:04:16 +0900194 }
195 }
satok61e2f852011-01-05 14:13:07 +0900196 PROF_END(2);
satokcdbbea72010-12-08 16:04:16 +0900197
satok61e2f852011-01-05 14:13:07 +0900198 PROF_START(3);
satok662fe692010-12-08 17:05:39 +0900199 // Suggestion with excessive character
satok54fe9e02010-12-13 14:42:35 +0900200 if (SUGGEST_WORDS_WITH_EXCESSIVE_CHARACTER
201 && mInputLength >= MIN_USER_TYPED_LENGTH_FOR_EXCESSIVE_CHARACTER_SUGGESTION) {
satokcdbbea72010-12-08 16:04:16 +0900202 for (int i = 0; i < codesSize; ++i) {
satok54fe9e02010-12-13 14:42:35 +0900203 if (DEBUG_DICT) LOGI("--- Suggest excessive characters %d", i);
204 getSuggestionCandidates(-1, i, -1, NULL, 0, MAX_DEPTH);
satok30088252010-12-01 21:22:15 +0900205 }
206 }
satok61e2f852011-01-05 14:13:07 +0900207 PROF_END(3);
satok30088252010-12-01 21:22:15 +0900208
satok61e2f852011-01-05 14:13:07 +0900209 PROF_START(4);
satoka3d78f62010-12-09 22:08:33 +0900210 // Suggestion with transposed characters
211 // Only suggest words that length is mInputLength
212 if (SUGGEST_WORDS_WITH_TRANSPOSED_CHARACTERS) {
213 for (int i = 0; i < codesSize; ++i) {
214 if (DEBUG_DICT) LOGI("--- Suggest transposed characters %d", i);
satok54fe9e02010-12-13 14:42:35 +0900215 getSuggestionCandidates(-1, -1, i, NULL, 0, mInputLength - 1);
satoka3d78f62010-12-09 22:08:33 +0900216 }
217 }
satok61e2f852011-01-05 14:13:07 +0900218 PROF_END(4);
satoka3d78f62010-12-09 22:08:33 +0900219
satok61e2f852011-01-05 14:13:07 +0900220 PROF_START(5);
satok662fe692010-12-08 17:05:39 +0900221 // Suggestions with missing space
satok54fe9e02010-12-13 14:42:35 +0900222 if (SUGGEST_WORDS_WITH_MISSING_SPACE_CHARACTER
223 && mInputLength >= MIN_USER_TYPED_LENGTH_FOR_MISSING_SPACE_SUGGESTION) {
satok662fe692010-12-08 17:05:39 +0900224 for (int i = 1; i < codesSize; ++i) {
225 if (DEBUG_DICT) LOGI("--- Suggest missing space characters %d", i);
226 getMissingSpaceWords(mInputLength, i);
227 }
228 }
satok61e2f852011-01-05 14:13:07 +0900229 PROF_END(5);
satok30088252010-12-01 21:22:15 +0900230}
231
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900232void UnigramDictionary::initSuggestions(const int *codes, const int codesSize,
233 unsigned short *outWords, int *frequencies) {
satokf5cded12010-12-06 21:28:24 +0900234 if (DEBUG_DICT) LOGI("initSuggest");
satok30088252010-12-01 21:22:15 +0900235 mFrequencies = frequencies;
236 mOutputChars = outWords;
237 mInputCodes = codes;
238 mInputLength = codesSize;
239 mMaxEditDistance = mInputLength < 5 ? 2 : mInputLength / 2;
240}
241
satok715514d2010-12-02 20:19:59 +0900242void UnigramDictionary::registerNextLetter(
243 unsigned short c, int *nextLetters, int nextLettersSize) {
satok30088252010-12-01 21:22:15 +0900244 if (c < nextLettersSize) {
245 nextLetters[c]++;
246 }
247}
248
satok662fe692010-12-08 17:05:39 +0900249// TODO: We need to optimize addWord by using STL or something
satok28bd03b2010-12-03 16:39:16 +0900250bool UnigramDictionary::addWord(unsigned short *word, int length, int frequency) {
satok30088252010-12-01 21:22:15 +0900251 word[length] = 0;
satok662fe692010-12-08 17:05:39 +0900252 if (DEBUG_DICT && DEBUG_SHOW_FOUND_WORD) {
satok30088252010-12-01 21:22:15 +0900253 char s[length + 1];
254 for (int i = 0; i <= length; i++) s[i] = word[i];
satok662fe692010-12-08 17:05:39 +0900255 LOGI("Found word = %s, freq = %d", s, frequency);
satok30088252010-12-01 21:22:15 +0900256 }
satokf5cded12010-12-06 21:28:24 +0900257 if (length > MAX_WORD_LENGTH) {
258 if (DEBUG_DICT) LOGI("Exceeded max word length.");
259 return false;
260 }
satok30088252010-12-01 21:22:15 +0900261
262 // Find the right insertion point
263 int insertAt = 0;
264 while (insertAt < MAX_WORDS) {
satok715514d2010-12-02 20:19:59 +0900265 if (frequency > mFrequencies[insertAt] || (mFrequencies[insertAt] == frequency
266 && length < Dictionary::wideStrLen(mOutputChars + insertAt * MAX_WORD_LENGTH))) {
satok30088252010-12-01 21:22:15 +0900267 break;
268 }
269 insertAt++;
270 }
271 if (insertAt < MAX_WORDS) {
satokcdbbea72010-12-08 16:04:16 +0900272 if (DEBUG_DICT) {
273 char s[length + 1];
274 for (int i = 0; i <= length; i++) s[i] = word[i];
satok662fe692010-12-08 17:05:39 +0900275 LOGI("Added word = %s, freq = %d", s, frequency);
satokcdbbea72010-12-08 16:04:16 +0900276 }
satok30088252010-12-01 21:22:15 +0900277 memmove((char*) mFrequencies + (insertAt + 1) * sizeof(mFrequencies[0]),
278 (char*) mFrequencies + insertAt * sizeof(mFrequencies[0]),
279 (MAX_WORDS - insertAt - 1) * sizeof(mFrequencies[0]));
280 mFrequencies[insertAt] = frequency;
281 memmove((char*) mOutputChars + (insertAt + 1) * MAX_WORD_LENGTH * sizeof(short),
satok715514d2010-12-02 20:19:59 +0900282 (char*) mOutputChars + insertAt * MAX_WORD_LENGTH * sizeof(short),
satok30088252010-12-01 21:22:15 +0900283 (MAX_WORDS - insertAt - 1) * sizeof(short) * MAX_WORD_LENGTH);
satok715514d2010-12-02 20:19:59 +0900284 unsigned short *dest = mOutputChars + insertAt * MAX_WORD_LENGTH;
satok30088252010-12-01 21:22:15 +0900285 while (length--) {
286 *dest++ = *word++;
287 }
288 *dest = 0; // NULL terminate
satok662fe692010-12-08 17:05:39 +0900289 if (DEBUG_DICT) LOGI("Added word at %d", insertAt);
satok30088252010-12-01 21:22:15 +0900290 return true;
291 }
292 return false;
293}
294
Jean Chalardf5f834a2011-02-22 15:12:46 +0900295unsigned short UnigramDictionary::toBaseLowerCase(unsigned short c) {
satok30088252010-12-01 21:22:15 +0900296 if (c < sizeof(BASE_CHARS) / sizeof(BASE_CHARS[0])) {
297 c = BASE_CHARS[c];
298 }
299 if (c >='A' && c <= 'Z') {
300 c |= 32;
301 } else if (c > 127) {
302 c = latin_tolower(c);
303 }
304 return c;
305}
306
satok28bd03b2010-12-03 16:39:16 +0900307bool UnigramDictionary::sameAsTyped(unsigned short *word, int length) {
satok30088252010-12-01 21:22:15 +0900308 if (length != mInputLength) {
309 return false;
310 }
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900311 const int *inputCodes = mInputCodes;
satok30088252010-12-01 21:22:15 +0900312 while (length--) {
313 if ((unsigned int) *inputCodes != (unsigned int) *word) {
314 return false;
315 }
satok662fe692010-12-08 17:05:39 +0900316 inputCodes += MAX_PROXIMITY_CHARS;
satok30088252010-12-01 21:22:15 +0900317 word++;
318 }
319 return true;
320}
321
satok715514d2010-12-02 20:19:59 +0900322static const char QUOTE = '\'';
satok662fe692010-12-08 17:05:39 +0900323static const char SPACE = ' ';
satok30088252010-12-01 21:22:15 +0900324
satok54fe9e02010-12-13 14:42:35 +0900325void UnigramDictionary::getSuggestionCandidates(const int skipPos,
satoka3d78f62010-12-09 22:08:33 +0900326 const int excessivePos, const int transposedPos, int *nextLetters,
327 const int nextLettersSize, const int maxDepth) {
satok54fe9e02010-12-13 14:42:35 +0900328 if (DEBUG_DICT) {
329 LOGI("getSuggestionCandidates %d", maxDepth);
330 assert(transposedPos + 1 < mInputLength);
331 assert(excessivePos < mInputLength);
332 assert(missingPos < mInputLength);
333 }
satok662fe692010-12-08 17:05:39 +0900334 int rootPosition = ROOT_POS;
satokd2997922010-12-07 13:08:39 +0900335 // Get the number of child of root, then increment the position
336 int childCount = Dictionary::getCount(DICT, &rootPosition);
337 int depth = 0;
338
339 mStackChildCount[0] = childCount;
340 mStackTraverseAll[0] = (mInputLength <= 0);
341 mStackNodeFreq[0] = 1;
342 mStackInputIndex[0] = 0;
343 mStackDiffs[0] = 0;
344 mStackSiblingPos[0] = rootPosition;
345
satok662fe692010-12-08 17:05:39 +0900346 // Depth first search
satokd2997922010-12-07 13:08:39 +0900347 while (depth >= 0) {
348 if (mStackChildCount[depth] > 0) {
349 --mStackChildCount[depth];
350 bool traverseAllNodes = mStackTraverseAll[depth];
Jean Chalardf5f834a2011-02-22 15:12:46 +0900351 int matchWeight = mStackNodeFreq[depth];
satokd2997922010-12-07 13:08:39 +0900352 int inputIndex = mStackInputIndex[depth];
353 int diffs = mStackDiffs[depth];
354 int siblingPos = mStackSiblingPos[depth];
355 int firstChildPos;
satoka3d78f62010-12-09 22:08:33 +0900356 // depth will never be greater than maxDepth because in that case,
satokd2997922010-12-07 13:08:39 +0900357 // needsToTraverseChildrenNodes should be false
358 const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900359 maxDepth, traverseAllNodes, matchWeight, inputIndex, diffs, skipPos,
360 excessivePos, transposedPos, nextLetters, nextLettersSize, &childCount,
361 &firstChildPos, &traverseAllNodes, &matchWeight, &inputIndex, &diffs,
362 &siblingPos);
satok662fe692010-12-08 17:05:39 +0900363 // Update next sibling pos
satokd2997922010-12-07 13:08:39 +0900364 mStackSiblingPos[depth] = siblingPos;
365 if (needsToTraverseChildrenNodes) {
366 // Goes to child node
367 ++depth;
368 mStackChildCount[depth] = childCount;
369 mStackTraverseAll[depth] = traverseAllNodes;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900370 mStackNodeFreq[depth] = matchWeight;
satokd2997922010-12-07 13:08:39 +0900371 mStackInputIndex[depth] = inputIndex;
372 mStackDiffs[depth] = diffs;
373 mStackSiblingPos[depth] = firstChildPos;
374 }
375 } else {
satokcdbbea72010-12-08 16:04:16 +0900376 // Goes to parent sibling node
satokd2997922010-12-07 13:08:39 +0900377 --depth;
378 }
379 }
380}
381
satokf7425bb2011-01-05 16:37:53 +0900382inline static void multiplyRate(const int rate, int *freq) {
383 if (rate > 1000000) {
384 *freq = (*freq / 100) * rate;
385 } else {
386 *freq = *freq * rate / 100;
387 }
388}
389
satok662fe692010-12-08 17:05:39 +0900390bool UnigramDictionary::getMissingSpaceWords(const int inputLength, const int missingSpacePos) {
satokaee09dc2010-12-09 19:21:51 +0900391 if (missingSpacePos <= 0 || missingSpacePos >= inputLength
392 || inputLength >= MAX_WORD_LENGTH) return false;
satok662fe692010-12-08 17:05:39 +0900393 const int newWordLength = inputLength + 1;
394 // Allocating variable length array on stack
395 unsigned short word[newWordLength];
satokaee09dc2010-12-09 19:21:51 +0900396 const int firstFreq = getBestWordFreq(0, missingSpacePos, mWord);
397 if (DEBUG_DICT) LOGI("First freq: %d", firstFreq);
398 if (firstFreq <= 0) return false;
399
satok662fe692010-12-08 17:05:39 +0900400 for (int i = 0; i < missingSpacePos; ++i) {
satokaee09dc2010-12-09 19:21:51 +0900401 word[i] = mWord[i];
satok662fe692010-12-08 17:05:39 +0900402 }
satokaee09dc2010-12-09 19:21:51 +0900403
404 const int secondFreq = getBestWordFreq(missingSpacePos, inputLength - missingSpacePos, mWord);
satoka3d78f62010-12-09 22:08:33 +0900405 if (DEBUG_DICT) LOGI("Second freq: %d", secondFreq);
satokaee09dc2010-12-09 19:21:51 +0900406 if (secondFreq <= 0) return false;
407
satok662fe692010-12-08 17:05:39 +0900408 word[missingSpacePos] = SPACE;
409 for (int i = (missingSpacePos + 1); i < newWordLength; ++i) {
satokaee09dc2010-12-09 19:21:51 +0900410 word[i] = mWord[i - missingSpacePos - 1];
satok662fe692010-12-08 17:05:39 +0900411 }
satokaee09dc2010-12-09 19:21:51 +0900412
413 int pairFreq = ((firstFreq + secondFreq) / 2);
414 for (int i = 0; i < inputLength; ++i) pairFreq *= TYPED_LETTER_MULTIPLIER;
satokf7425bb2011-01-05 16:37:53 +0900415 multiplyRate(WORDS_WITH_MISSING_SPACE_CHARACTER_DEMOTION_RATE, &pairFreq);
satok662fe692010-12-08 17:05:39 +0900416 addWord(word, newWordLength, pairFreq);
417 return true;
418}
419
420// Keep this for comparing spec to new getWords
421void UnigramDictionary::getWordsOld(const int initialPos, const int inputLength, const int skipPos,
satoka3d78f62010-12-09 22:08:33 +0900422 const int excessivePos, const int transposedPos,int *nextLetters,
423 const int nextLettersSize) {
satok662fe692010-12-08 17:05:39 +0900424 int initialPosition = initialPos;
425 const int count = Dictionary::getCount(DICT, &initialPosition);
426 getWordsRec(count, initialPosition, 0,
427 min(inputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH),
satoka3d78f62010-12-09 22:08:33 +0900428 mInputLength <= 0, 1, 0, 0, skipPos, excessivePos, transposedPos, nextLetters,
429 nextLettersSize);
satok662fe692010-12-08 17:05:39 +0900430}
431
satok68319262010-12-03 19:38:08 +0900432void UnigramDictionary::getWordsRec(const int childrenCount, const int pos, const int depth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900433 const int maxDepth, const bool traverseAllNodes, const int matchWeight,
434 const int inputIndex, const int diffs, const int skipPos, const int excessivePos,
435 const int transposedPos, int *nextLetters, const int nextLettersSize) {
satok48e432c2010-12-06 17:38:58 +0900436 int siblingPos = pos;
satok68319262010-12-03 19:38:08 +0900437 for (int i = 0; i < childrenCount; ++i) {
satok48e432c2010-12-06 17:38:58 +0900438 int newCount;
439 int newChildPosition;
satokd2997922010-12-07 13:08:39 +0900440 const int newDepth = depth + 1;
satok48e432c2010-12-06 17:38:58 +0900441 bool newTraverseAllNodes;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900442 int newMatchRate;
satok48e432c2010-12-06 17:38:58 +0900443 int newInputIndex;
444 int newDiffs;
445 int newSiblingPos;
446 const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth, maxDepth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900447 traverseAllNodes, matchWeight, inputIndex, diffs,
448 skipPos, excessivePos, transposedPos,
satoka3d78f62010-12-09 22:08:33 +0900449 nextLetters, nextLettersSize,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900450 &newCount, &newChildPosition, &newTraverseAllNodes, &newMatchRate,
satok48e432c2010-12-06 17:38:58 +0900451 &newInputIndex, &newDiffs, &newSiblingPos);
452 siblingPos = newSiblingPos;
satok30088252010-12-01 21:22:15 +0900453
satok48e432c2010-12-06 17:38:58 +0900454 if (needsToTraverseChildrenNodes) {
455 getWordsRec(newCount, newChildPosition, newDepth, maxDepth, newTraverseAllNodes,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900456 newMatchRate, newInputIndex, newDiffs, skipPos, excessivePos, transposedPos,
satoka3d78f62010-12-09 22:08:33 +0900457 nextLetters, nextLettersSize);
satok30088252010-12-01 21:22:15 +0900458 }
459 }
460}
461
Jean Chalarda5d58492011-02-18 17:50:58 +0900462static const int TWO_31ST_DIV_255 = ((1 << 31) - 1) / 255;
463static inline int capped255MultForFullMatchAccentsOrCapitalizationDifference(const int num) {
464 return (num < TWO_31ST_DIV_255 ? 255 * num : S_INT_MAX);
465}
satok58c49b92011-01-27 03:23:39 +0900466inline int UnigramDictionary::calculateFinalFreq(const int inputIndex, const int depth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900467 const int matchWeight, const int skipPos, const int excessivePos, const int transposedPos,
satok58c49b92011-01-27 03:23:39 +0900468 const int freq, const bool sameLength) {
satoka3d78f62010-12-09 22:08:33 +0900469 // TODO: Demote by edit distance
Jean Chalardf5f834a2011-02-22 15:12:46 +0900470 int finalFreq = freq * matchWeight;
satokf7425bb2011-01-05 16:37:53 +0900471 if (skipPos >= 0) multiplyRate(WORDS_WITH_MISSING_CHARACTER_DEMOTION_RATE, &finalFreq);
472 if (transposedPos >= 0) multiplyRate(
473 WORDS_WITH_TRANSPOSED_CHARACTERS_DEMOTION_RATE, &finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900474 if (excessivePos >= 0) {
satokf7425bb2011-01-05 16:37:53 +0900475 multiplyRate(WORDS_WITH_EXCESSIVE_CHARACTER_DEMOTION_RATE, &finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900476 if (!existsAdjacentProximityChars(inputIndex, mInputLength)) {
satokf7425bb2011-01-05 16:37:53 +0900477 multiplyRate(WORDS_WITH_EXCESSIVE_CHARACTER_OUT_OF_PROXIMITY_DEMOTION_RATE, &finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900478 }
479 }
satok58c49b92011-01-27 03:23:39 +0900480 int lengthFreq = TYPED_LETTER_MULTIPLIER;
481 for (int i = 0; i < depth; ++i) lengthFreq *= TYPED_LETTER_MULTIPLIER;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900482 if (lengthFreq == matchWeight) {
Jean Chalard8dc754a2011-01-27 14:20:22 +0900483 if (depth > 1) {
484 if (DEBUG_DICT) LOGI("Found full matched word.");
485 multiplyRate(FULL_MATCHED_WORDS_PROMOTION_RATE, &finalFreq);
486 }
487 if (sameLength && transposedPos < 0 && skipPos < 0 && excessivePos < 0) {
Jean Chalarda5d58492011-02-18 17:50:58 +0900488 finalFreq = capped255MultForFullMatchAccentsOrCapitalizationDifference(finalFreq);
Jean Chalard8dc754a2011-01-27 14:20:22 +0900489 }
satok58c49b92011-01-27 03:23:39 +0900490 }
satok54fe9e02010-12-13 14:42:35 +0900491 if (sameLength && skipPos < 0) finalFreq *= FULL_WORD_MULTIPLIER;
492 return finalFreq;
493}
satoka3d78f62010-12-09 22:08:33 +0900494
satok54fe9e02010-12-13 14:42:35 +0900495inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsGreaterThanInputLength(
Jean Chalardf5f834a2011-02-22 15:12:46 +0900496 unsigned short *word, const int inputIndex, const int depth, const int matchWeight,
satok54fe9e02010-12-13 14:42:35 +0900497 int *nextLetters, const int nextLettersSize, const int skipPos, const int excessivePos,
498 const int transposedPos, const int freq) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900499 const int finalFreq = calculateFinalFreq(inputIndex, depth, matchWeight, skipPos, excessivePos,
satok58c49b92011-01-27 03:23:39 +0900500 transposedPos, freq, false);
satoka3d78f62010-12-09 22:08:33 +0900501 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900502 if (depth >= mInputLength && skipPos < 0) {
satok715514d2010-12-02 20:19:59 +0900503 registerNextLetter(mWord[mInputLength], nextLetters, nextLettersSize);
504 }
505}
506
507inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsSameAsInputLength(
Jean Chalardf5f834a2011-02-22 15:12:46 +0900508 unsigned short *word, const int inputIndex, const int depth, const int matchWeight,
Jean Chalard8dc754a2011-01-27 14:20:22 +0900509 const int skipPos, const int excessivePos, const int transposedPos, const int freq) {
satok54fe9e02010-12-13 14:42:35 +0900510 if (sameAsTyped(word, depth + 1)) return;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900511 const int finalFreq = calculateFinalFreq(inputIndex, depth, matchWeight, skipPos,
satok54fe9e02010-12-13 14:42:35 +0900512 excessivePos, transposedPos, freq, true);
513 // Proximity collection will promote a word of the same length as what user typed.
514 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq);
satok715514d2010-12-02 20:19:59 +0900515}
satok28bd03b2010-12-03 16:39:16 +0900516
517inline bool UnigramDictionary::needsToSkipCurrentNode(const unsigned short c,
satok68319262010-12-03 19:38:08 +0900518 const int inputIndex, const int skipPos, const int depth) {
satok8fbd5522011-02-22 17:28:55 +0900519 const unsigned short userTypedChar = getInputCharsAt(inputIndex)[0];
satok28bd03b2010-12-03 16:39:16 +0900520 // Skip the ' or other letter and continue deeper
521 return (c == QUOTE && userTypedChar != QUOTE) || skipPos == depth;
522}
523
satoke07baa62010-12-09 21:55:40 +0900524inline bool UnigramDictionary::existsAdjacentProximityChars(const int inputIndex,
525 const int inputLength) {
526 if (inputIndex < 0 || inputIndex >= inputLength) return false;
527 const int currentChar = *getInputCharsAt(inputIndex);
528 const int leftIndex = inputIndex - 1;
529 if (leftIndex >= 0) {
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900530 const int *leftChars = getInputCharsAt(leftIndex);
satoke07baa62010-12-09 21:55:40 +0900531 int i = 0;
532 while (leftChars[i] > 0 && i < MAX_PROXIMITY_CHARS) {
533 if (leftChars[i++] == currentChar) return true;
534 }
535 }
536 const int rightIndex = inputIndex + 1;
537 if (rightIndex < inputLength) {
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900538 const int *rightChars = getInputCharsAt(rightIndex);
satoke07baa62010-12-09 21:55:40 +0900539 int i = 0;
540 while (rightChars[i] > 0 && i < MAX_PROXIMITY_CHARS) {
541 if (rightChars[i++] == currentChar) return true;
542 }
543 }
544 return false;
545}
546
Jean Chalarda5d58492011-02-18 17:50:58 +0900547
548// In the following function, c is the current character of the dictionary word
549// currently examined.
550// currentChars is an array containing the keys close to the character the
551// user actually typed at the same position. We want to see if c is in it: if so,
552// then the word contains at that position a character close to what the user
553// typed.
554// What the user typed is actually the first character of the array.
555// Notice : accented characters do not have a proximity list, so they are alone
556// in their list. The non-accented version of the character should be considered
557// "close", but not the other keys close to the non-accented version.
Jean Chalard8dc754a2011-01-27 14:20:22 +0900558inline UnigramDictionary::ProximityType UnigramDictionary::getMatchedProximityId(
559 const int *currentChars, const unsigned short c, const int skipPos,
560 const int excessivePos, const int transposedPos) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900561 const unsigned short baseLowerC = toBaseLowerCase(c);
Jean Chalarda5d58492011-02-18 17:50:58 +0900562
563 // The first char in the array is what user typed. If it matches right away,
564 // that means the user typed that same char for this pos.
Jean Chalardf5f834a2011-02-22 15:12:46 +0900565 if (currentChars[0] == baseLowerC || currentChars[0] == c)
Jean Chalarda5d58492011-02-18 17:50:58 +0900566 return SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR;
567
568 // If one of those is true, we should not check for close characters at all.
569 if (skipPos >= 0 || excessivePos >= 0 || transposedPos >= 0)
570 return UNRELATED_CHAR;
571
572 // If the non-accented, lowercased version of that first character matches c,
573 // then we have a non-accented version of the accented character the user
574 // typed. Treat it as a close char.
Jean Chalardf5f834a2011-02-22 15:12:46 +0900575 if (toBaseLowerCase(currentChars[0]) == baseLowerC)
Jean Chalarda5d58492011-02-18 17:50:58 +0900576 return NEAR_PROXIMITY_CHAR;
577
578 // Not an exact nor an accent-alike match: search the list of close keys
579 int j = 1;
satoke07baa62010-12-09 21:55:40 +0900580 while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900581 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
Jean Chalarda5d58492011-02-18 17:50:58 +0900582 if (matched) return NEAR_PROXIMITY_CHAR;
satok28bd03b2010-12-03 16:39:16 +0900583 ++j;
584 }
Jean Chalarda5d58492011-02-18 17:50:58 +0900585
586 // Was not included, signal this as an unrelated character.
Jean Chalard8dc754a2011-01-27 14:20:22 +0900587 return UNRELATED_CHAR;
satok28bd03b2010-12-03 16:39:16 +0900588}
589
satok48e432c2010-12-06 17:38:58 +0900590inline bool UnigramDictionary::processCurrentNode(const int pos, const int depth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900591 const int maxDepth, const bool traverseAllNodes, int matchWeight, int inputIndex,
satoka3d78f62010-12-09 22:08:33 +0900592 const int diffs, const int skipPos, const int excessivePos, const int transposedPos,
593 int *nextLetters, const int nextLettersSize, int *newCount, int *newChildPosition,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900594 bool *newTraverseAllNodes, int *newMatchRate, int *newInputIndex, int *newDiffs,
satoka3d78f62010-12-09 22:08:33 +0900595 int *nextSiblingPosition) {
596 if (DEBUG_DICT) {
597 int inputCount = 0;
598 if (skipPos >= 0) ++inputCount;
599 if (excessivePos >= 0) ++inputCount;
600 if (transposedPos >= 0) ++inputCount;
601 assert(inputCount <= 1);
602 }
satok48e432c2010-12-06 17:38:58 +0900603 unsigned short c;
604 int childPosition;
605 bool terminal;
606 int freq;
satokfd16f1d2011-01-27 16:25:16 +0900607 bool isSameAsUserTypedLength = false;
satokcdbbea72010-12-08 16:04:16 +0900608
satokfd16f1d2011-01-27 16:25:16 +0900609 if (excessivePos == depth && inputIndex < mInputLength - 1) ++inputIndex;
satokcdbbea72010-12-08 16:04:16 +0900610
satok48e432c2010-12-06 17:38:58 +0900611 *nextSiblingPosition = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, pos, &c,
612 &childPosition, &terminal, &freq);
613
614 const bool needsToTraverseChildrenNodes = childPosition != 0;
615
616 // If we are only doing traverseAllNodes, no need to look at the typed characters.
617 if (traverseAllNodes || needsToSkipCurrentNode(c, inputIndex, skipPos, depth)) {
618 mWord[depth] = c;
619 if (traverseAllNodes && terminal) {
satok54fe9e02010-12-13 14:42:35 +0900620 onTerminalWhenUserTypedLengthIsGreaterThanInputLength(mWord, inputIndex, depth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900621 matchWeight, nextLetters, nextLettersSize, skipPos, excessivePos, transposedPos,
622 freq);
satok48e432c2010-12-06 17:38:58 +0900623 }
624 if (!needsToTraverseChildrenNodes) return false;
625 *newTraverseAllNodes = traverseAllNodes;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900626 *newMatchRate = matchWeight;
satok48e432c2010-12-06 17:38:58 +0900627 *newDiffs = diffs;
628 *newInputIndex = inputIndex;
satok48e432c2010-12-06 17:38:58 +0900629 } else {
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900630 const int *currentChars = getInputCharsAt(inputIndex);
satoka3d78f62010-12-09 22:08:33 +0900631
632 if (transposedPos >= 0) {
633 if (inputIndex == transposedPos) currentChars += MAX_PROXIMITY_CHARS;
634 if (inputIndex == (transposedPos + 1)) currentChars -= MAX_PROXIMITY_CHARS;
635 }
636
637 int matchedProximityCharId = getMatchedProximityId(currentChars, c, skipPos, excessivePos,
638 transposedPos);
Jean Chalard8dc754a2011-01-27 14:20:22 +0900639 if (UNRELATED_CHAR == matchedProximityCharId) return false;
satok48e432c2010-12-06 17:38:58 +0900640 mWord[depth] = c;
641 // If inputIndex is greater than mInputLength, that means there is no
642 // proximity chars. So, we don't need to check proximity.
Jean Chalard8dc754a2011-01-27 14:20:22 +0900643 if (SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR == matchedProximityCharId) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900644 matchWeight = matchWeight * TYPED_LETTER_MULTIPLIER;
Jean Chalard8dc754a2011-01-27 14:20:22 +0900645 }
satokfd16f1d2011-01-27 16:25:16 +0900646 bool isSameAsUserTypedLength = mInputLength == inputIndex + 1
647 || (excessivePos == mInputLength - 1 && inputIndex == mInputLength - 2);
satok48e432c2010-12-06 17:38:58 +0900648 if (isSameAsUserTypedLength && terminal) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900649 onTerminalWhenUserTypedLengthIsSameAsInputLength(mWord, inputIndex, depth, matchWeight,
Jean Chalard8dc754a2011-01-27 14:20:22 +0900650 skipPos, excessivePos, transposedPos, freq);
satok48e432c2010-12-06 17:38:58 +0900651 }
652 if (!needsToTraverseChildrenNodes) return false;
653 // Start traversing all nodes after the index exceeds the user typed length
654 *newTraverseAllNodes = isSameAsUserTypedLength;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900655 *newMatchRate = matchWeight;
Jean Chalard8dc754a2011-01-27 14:20:22 +0900656 *newDiffs = diffs + ((NEAR_PROXIMITY_CHAR == matchedProximityCharId) ? 1 : 0);
satok48e432c2010-12-06 17:38:58 +0900657 *newInputIndex = inputIndex + 1;
satok48e432c2010-12-06 17:38:58 +0900658 }
659 // Optimization: Prune out words that are too long compared to how much was typed.
satokd2997922010-12-07 13:08:39 +0900660 if (depth >= maxDepth || *newDiffs > mMaxEditDistance) {
satok48e432c2010-12-06 17:38:58 +0900661 return false;
662 }
663
664 // If inputIndex is greater than mInputLength, that means there are no proximity chars.
satokfd16f1d2011-01-27 16:25:16 +0900665 // TODO: Check if this can be isSameAsUserTypedLength only.
666 if (isSameAsUserTypedLength || mInputLength <= *newInputIndex) {
satok48e432c2010-12-06 17:38:58 +0900667 *newTraverseAllNodes = true;
668 }
669 // get the count of nodes and increment childAddress.
670 *newCount = Dictionary::getCount(DICT, &childPosition);
671 *newChildPosition = childPosition;
672 if (DEBUG_DICT) assert(needsToTraverseChildrenNodes);
673 return needsToTraverseChildrenNodes;
674}
675
satokaee09dc2010-12-09 19:21:51 +0900676inline int UnigramDictionary::getBestWordFreq(const int startInputIndex, const int inputLength,
677 unsigned short *word) {
satok662fe692010-12-08 17:05:39 +0900678 int pos = ROOT_POS;
679 int count = Dictionary::getCount(DICT, &pos);
satokaee09dc2010-12-09 19:21:51 +0900680 int maxFreq = 0;
681 int depth = 0;
682 unsigned short newWord[MAX_WORD_LENGTH_INTERNAL];
satok662fe692010-12-08 17:05:39 +0900683 bool terminal = false;
684
satokaee09dc2010-12-09 19:21:51 +0900685 mStackChildCount[0] = count;
686 mStackSiblingPos[0] = pos;
687
688 while (depth >= 0) {
689 if (mStackChildCount[depth] > 0) {
690 --mStackChildCount[depth];
691 int firstChildPos;
692 int newFreq;
693 int siblingPos = mStackSiblingPos[depth];
694 const bool needsToTraverseChildrenNodes = processCurrentNodeForExactMatch(siblingPos,
695 startInputIndex, depth, newWord, &firstChildPos, &count, &terminal, &newFreq,
696 &siblingPos);
697 mStackSiblingPos[depth] = siblingPos;
698 if (depth == (inputLength - 1)) {
699 // Traverse sibling node
700 if (terminal) {
701 if (newFreq > maxFreq) {
702 for (int i = 0; i < inputLength; ++i) word[i] = newWord[i];
703 if (DEBUG_DICT && DEBUG_NODE) {
704 char s[inputLength + 1];
705 for (int i = 0; i < inputLength; ++i) s[i] = word[i];
706 s[inputLength] = 0;
707 LOGI("New missing space word found: %d > %d (%s), %d, %d",
708 newFreq, maxFreq, s, inputLength, depth);
709 }
710 maxFreq = newFreq;
711 }
712 }
713 } else if (needsToTraverseChildrenNodes) {
714 // Traverse children nodes
715 ++depth;
716 mStackChildCount[depth] = count;
717 mStackSiblingPos[depth] = firstChildPos;
718 }
719 } else {
720 // Traverse parent node
721 --depth;
satok662fe692010-12-08 17:05:39 +0900722 }
723 }
satokaee09dc2010-12-09 19:21:51 +0900724
725 word[inputLength] = 0;
726 return maxFreq;
satok662fe692010-12-08 17:05:39 +0900727}
728
729inline bool UnigramDictionary::processCurrentNodeForExactMatch(const int firstChildPos,
satokaee09dc2010-12-09 19:21:51 +0900730 const int startInputIndex, const int depth, unsigned short *word, int *newChildPosition,
731 int *newCount, bool *newTerminal, int *newFreq, int *siblingPos) {
732 const int inputIndex = startInputIndex + depth;
satok8fbd5522011-02-22 17:28:55 +0900733 const int *currentChars = getInputCharsAt(inputIndex);
satok662fe692010-12-08 17:05:39 +0900734 unsigned short c;
satokaee09dc2010-12-09 19:21:51 +0900735 *siblingPos = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, firstChildPos, &c,
736 newChildPosition, newTerminal, newFreq);
737 const unsigned int inputC = currentChars[0];
738 if (DEBUG_DICT) assert(inputC <= U_SHORT_MAX);
Jean Chalardf5f834a2011-02-22 15:12:46 +0900739 const unsigned short baseLowerC = toBaseLowerCase(c);
740 const bool matched = (inputC == baseLowerC || inputC == c);
satokaee09dc2010-12-09 19:21:51 +0900741 const bool hasChild = *newChildPosition != 0;
742 if (matched) {
743 word[depth] = c;
744 if (DEBUG_DICT && DEBUG_NODE) {
745 LOGI("Node(%c, %c)<%d>, %d, %d", inputC, c, matched, hasChild, *newFreq);
746 if (*newTerminal) LOGI("Terminal %d", *newFreq);
satok662fe692010-12-08 17:05:39 +0900747 }
satokaee09dc2010-12-09 19:21:51 +0900748 if (hasChild) {
749 *newCount = Dictionary::getCount(DICT, newChildPosition);
750 return true;
751 } else {
752 return false;
753 }
754 } else {
755 // If this node is not user typed character, this method treats this word as unmatched.
756 // Thus newTerminal shouldn't be true.
757 *newTerminal = false;
758 return false;
satok662fe692010-12-08 17:05:39 +0900759 }
satok662fe692010-12-08 17:05:39 +0900760}
satok30088252010-12-01 21:22:15 +0900761} // namespace latinime