blob: 55d879fd2ff268d8c802206ff94fac591bccb1ea [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
satoke808e432010-12-02 14:53:24 +090032UnigramDictionary::UnigramDictionary(const unsigned char *dict, int typedLetterMultiplier,
satok662fe692010-12-08 17:05:39 +090033 int fullWordMultiplier, int maxWordLength, int maxWords, int maxProximityChars,
satok18c28f42010-12-02 18:11:54 +090034 const bool isLatestDictVersion)
satoke808e432010-12-02 14:53:24 +090035 : DICT(dict), MAX_WORD_LENGTH(maxWordLength),MAX_WORDS(maxWords),
satok662fe692010-12-08 17:05:39 +090036 MAX_PROXIMITY_CHARS(maxProximityChars), IS_LATEST_DICT_VERSION(isLatestDictVersion),
37 TYPED_LETTER_MULTIPLIER(typedLetterMultiplier), FULL_WORD_MULTIPLIER(fullWordMultiplier),
38 ROOT_POS(isLatestDictVersion ? DICTIONARY_HEADER_SIZE : 0) {
satok30088252010-12-01 21:22:15 +090039 LOGI("UnigramDictionary - constructor");
satok30088252010-12-01 21:22:15 +090040}
41
satok18c28f42010-12-02 18:11:54 +090042UnigramDictionary::~UnigramDictionary() {}
satok30088252010-12-01 21:22:15 +090043
satok18c28f42010-12-02 18:11:54 +090044int UnigramDictionary::getSuggestions(int *codes, int codesSize, unsigned short *outWords,
45 int *frequencies, int *nextLetters, int nextLettersSize)
satok30088252010-12-01 21:22:15 +090046{
satok30088252010-12-01 21:22:15 +090047 initSuggestions(codes, codesSize, outWords, frequencies);
satok662fe692010-12-08 17:05:39 +090048 getSuggestionCandidates(codesSize, -1, -1, nextLetters, nextLettersSize);
satok30088252010-12-01 21:22:15 +090049
satok662fe692010-12-08 17:05:39 +090050 // Suggestion with missing character
51 if (SUGGEST_WORDS_WITH_MISSING_CHARACTER) {
satok30088252010-12-01 21:22:15 +090052 for (int i = 0; i < codesSize; ++i) {
satokcdbbea72010-12-08 16:04:16 +090053 if (DEBUG_DICT) LOGI("--- Suggest missing characters %d", i);
satok662fe692010-12-08 17:05:39 +090054 getSuggestionCandidates(codesSize, i, -1, NULL, 0);
satokcdbbea72010-12-08 16:04:16 +090055 }
56 }
57
satok662fe692010-12-08 17:05:39 +090058 // Suggestion with excessive character
59 if (SUGGEST_WORDS_WITH_EXCESSIVE_CHARACTER) {
satokcdbbea72010-12-08 16:04:16 +090060 for (int i = 0; i < codesSize; ++i) {
61 if (DEBUG_DICT) LOGI("--- Suggest excessive characters %d", i);
satok662fe692010-12-08 17:05:39 +090062 getSuggestionCandidates(codesSize, -1, i, NULL, 0);
satok30088252010-12-01 21:22:15 +090063 }
64 }
65
satok662fe692010-12-08 17:05:39 +090066 // Suggestions with missing space
satokaee09dc2010-12-09 19:21:51 +090067 if (SUGGEST_WORDS_WITH_MISSING_SPACE_CHARACTER && mInputLength > MIN_SUGGEST_DEPTH) {
satok662fe692010-12-08 17:05:39 +090068 for (int i = 1; i < codesSize; ++i) {
69 if (DEBUG_DICT) LOGI("--- Suggest missing space characters %d", i);
70 getMissingSpaceWords(mInputLength, i);
71 }
72 }
73
74 // Get the word count
75 int suggestedWordsCount = 0;
76 while (suggestedWordsCount < MAX_WORDS && mFrequencies[suggestedWordsCount] > 0) {
77 suggestedWordsCount++;
78 }
79
satok30088252010-12-01 21:22:15 +090080 if (DEBUG_DICT) {
81 LOGI("Returning %d words", suggestedWordsCount);
82 LOGI("Next letters: ");
83 for (int k = 0; k < nextLettersSize; k++) {
84 if (nextLetters[k] > 0) {
85 LOGI("%c = %d,", k, nextLetters[k]);
86 }
87 }
88 LOGI("\n");
89 }
satok662fe692010-12-08 17:05:39 +090090
satok30088252010-12-01 21:22:15 +090091 return suggestedWordsCount;
92}
93
94void UnigramDictionary::initSuggestions(int *codes, int codesSize, unsigned short *outWords,
95 int *frequencies) {
satokf5cded12010-12-06 21:28:24 +090096 if (DEBUG_DICT) LOGI("initSuggest");
satok30088252010-12-01 21:22:15 +090097 mFrequencies = frequencies;
98 mOutputChars = outWords;
99 mInputCodes = codes;
100 mInputLength = codesSize;
101 mMaxEditDistance = mInputLength < 5 ? 2 : mInputLength / 2;
102}
103
satok715514d2010-12-02 20:19:59 +0900104void UnigramDictionary::registerNextLetter(
105 unsigned short c, int *nextLetters, int nextLettersSize) {
satok30088252010-12-01 21:22:15 +0900106 if (c < nextLettersSize) {
107 nextLetters[c]++;
108 }
109}
110
satok662fe692010-12-08 17:05:39 +0900111// TODO: We need to optimize addWord by using STL or something
satok28bd03b2010-12-03 16:39:16 +0900112bool UnigramDictionary::addWord(unsigned short *word, int length, int frequency) {
satok30088252010-12-01 21:22:15 +0900113 word[length] = 0;
satok662fe692010-12-08 17:05:39 +0900114 if (DEBUG_DICT && DEBUG_SHOW_FOUND_WORD) {
satok30088252010-12-01 21:22:15 +0900115 char s[length + 1];
116 for (int i = 0; i <= length; i++) s[i] = word[i];
satok662fe692010-12-08 17:05:39 +0900117 LOGI("Found word = %s, freq = %d", s, frequency);
satok30088252010-12-01 21:22:15 +0900118 }
satokf5cded12010-12-06 21:28:24 +0900119 if (length > MAX_WORD_LENGTH) {
120 if (DEBUG_DICT) LOGI("Exceeded max word length.");
121 return false;
122 }
satok30088252010-12-01 21:22:15 +0900123
124 // Find the right insertion point
125 int insertAt = 0;
126 while (insertAt < MAX_WORDS) {
satok715514d2010-12-02 20:19:59 +0900127 if (frequency > mFrequencies[insertAt] || (mFrequencies[insertAt] == frequency
128 && length < Dictionary::wideStrLen(mOutputChars + insertAt * MAX_WORD_LENGTH))) {
satok30088252010-12-01 21:22:15 +0900129 break;
130 }
131 insertAt++;
132 }
133 if (insertAt < MAX_WORDS) {
satokcdbbea72010-12-08 16:04:16 +0900134 if (DEBUG_DICT) {
135 char s[length + 1];
136 for (int i = 0; i <= length; i++) s[i] = word[i];
satok662fe692010-12-08 17:05:39 +0900137 LOGI("Added word = %s, freq = %d", s, frequency);
satokcdbbea72010-12-08 16:04:16 +0900138 }
satok30088252010-12-01 21:22:15 +0900139 memmove((char*) mFrequencies + (insertAt + 1) * sizeof(mFrequencies[0]),
140 (char*) mFrequencies + insertAt * sizeof(mFrequencies[0]),
141 (MAX_WORDS - insertAt - 1) * sizeof(mFrequencies[0]));
142 mFrequencies[insertAt] = frequency;
143 memmove((char*) mOutputChars + (insertAt + 1) * MAX_WORD_LENGTH * sizeof(short),
satok715514d2010-12-02 20:19:59 +0900144 (char*) mOutputChars + insertAt * MAX_WORD_LENGTH * sizeof(short),
satok30088252010-12-01 21:22:15 +0900145 (MAX_WORDS - insertAt - 1) * sizeof(short) * MAX_WORD_LENGTH);
satok715514d2010-12-02 20:19:59 +0900146 unsigned short *dest = mOutputChars + insertAt * MAX_WORD_LENGTH;
satok30088252010-12-01 21:22:15 +0900147 while (length--) {
148 *dest++ = *word++;
149 }
150 *dest = 0; // NULL terminate
satok662fe692010-12-08 17:05:39 +0900151 if (DEBUG_DICT) LOGI("Added word at %d", insertAt);
satok30088252010-12-01 21:22:15 +0900152 return true;
153 }
154 return false;
155}
156
satok28bd03b2010-12-03 16:39:16 +0900157unsigned short UnigramDictionary::toLowerCase(unsigned short c) {
satok30088252010-12-01 21:22:15 +0900158 if (c < sizeof(BASE_CHARS) / sizeof(BASE_CHARS[0])) {
159 c = BASE_CHARS[c];
160 }
161 if (c >='A' && c <= 'Z') {
162 c |= 32;
163 } else if (c > 127) {
164 c = latin_tolower(c);
165 }
166 return c;
167}
168
satok28bd03b2010-12-03 16:39:16 +0900169bool UnigramDictionary::sameAsTyped(unsigned short *word, int length) {
satok30088252010-12-01 21:22:15 +0900170 if (length != mInputLength) {
171 return false;
172 }
173 int *inputCodes = mInputCodes;
174 while (length--) {
175 if ((unsigned int) *inputCodes != (unsigned int) *word) {
176 return false;
177 }
satok662fe692010-12-08 17:05:39 +0900178 inputCodes += MAX_PROXIMITY_CHARS;
satok30088252010-12-01 21:22:15 +0900179 word++;
180 }
181 return true;
182}
183
satok715514d2010-12-02 20:19:59 +0900184static const char QUOTE = '\'';
satok662fe692010-12-08 17:05:39 +0900185static const char SPACE = ' ';
satok30088252010-12-01 21:22:15 +0900186
satok662fe692010-12-08 17:05:39 +0900187void UnigramDictionary::getSuggestionCandidates(const int inputLength, const int skipPos,
satokcdbbea72010-12-08 16:04:16 +0900188 const int excessivePos, int *nextLetters, const int nextLettersSize) {
satok662fe692010-12-08 17:05:39 +0900189 if (DEBUG_DICT) LOGI("getSuggestionCandidates");
190 int rootPosition = ROOT_POS;
satokd2997922010-12-07 13:08:39 +0900191 const int MAX_DEPTH = min(inputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH);
192 // Get the number of child of root, then increment the position
193 int childCount = Dictionary::getCount(DICT, &rootPosition);
194 int depth = 0;
195
196 mStackChildCount[0] = childCount;
197 mStackTraverseAll[0] = (mInputLength <= 0);
198 mStackNodeFreq[0] = 1;
199 mStackInputIndex[0] = 0;
200 mStackDiffs[0] = 0;
201 mStackSiblingPos[0] = rootPosition;
202
satok662fe692010-12-08 17:05:39 +0900203 // Depth first search
satokd2997922010-12-07 13:08:39 +0900204 while (depth >= 0) {
205 if (mStackChildCount[depth] > 0) {
206 --mStackChildCount[depth];
207 bool traverseAllNodes = mStackTraverseAll[depth];
208 int snr = mStackNodeFreq[depth];
209 int inputIndex = mStackInputIndex[depth];
210 int diffs = mStackDiffs[depth];
211 int siblingPos = mStackSiblingPos[depth];
212 int firstChildPos;
213 // depth will never be greater than MAX_DEPTH because in that case,
214 // needsToTraverseChildrenNodes should be false
215 const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth,
satokcdbbea72010-12-08 16:04:16 +0900216 MAX_DEPTH, traverseAllNodes, snr, inputIndex, diffs, skipPos, excessivePos,
217 nextLetters, nextLettersSize, &childCount, &firstChildPos, &traverseAllNodes,
218 &snr, &inputIndex, &diffs, &siblingPos);
satok662fe692010-12-08 17:05:39 +0900219 // Update next sibling pos
satokd2997922010-12-07 13:08:39 +0900220 mStackSiblingPos[depth] = siblingPos;
221 if (needsToTraverseChildrenNodes) {
222 // Goes to child node
223 ++depth;
224 mStackChildCount[depth] = childCount;
225 mStackTraverseAll[depth] = traverseAllNodes;
226 mStackNodeFreq[depth] = snr;
227 mStackInputIndex[depth] = inputIndex;
228 mStackDiffs[depth] = diffs;
229 mStackSiblingPos[depth] = firstChildPos;
230 }
231 } else {
satokcdbbea72010-12-08 16:04:16 +0900232 // Goes to parent sibling node
satokd2997922010-12-07 13:08:39 +0900233 --depth;
234 }
235 }
236}
237
satok662fe692010-12-08 17:05:39 +0900238bool UnigramDictionary::getMissingSpaceWords(const int inputLength, const int missingSpacePos) {
satokaee09dc2010-12-09 19:21:51 +0900239 if (missingSpacePos <= 0 || missingSpacePos >= inputLength
240 || inputLength >= MAX_WORD_LENGTH) return false;
satok662fe692010-12-08 17:05:39 +0900241 const int newWordLength = inputLength + 1;
242 // Allocating variable length array on stack
243 unsigned short word[newWordLength];
satokaee09dc2010-12-09 19:21:51 +0900244 const int firstFreq = getBestWordFreq(0, missingSpacePos, mWord);
245 if (DEBUG_DICT) LOGI("First freq: %d", firstFreq);
246 if (firstFreq <= 0) return false;
247
satok662fe692010-12-08 17:05:39 +0900248 for (int i = 0; i < missingSpacePos; ++i) {
satokaee09dc2010-12-09 19:21:51 +0900249 word[i] = mWord[i];
satok662fe692010-12-08 17:05:39 +0900250 }
satokaee09dc2010-12-09 19:21:51 +0900251
252 const int secondFreq = getBestWordFreq(missingSpacePos, inputLength - missingSpacePos, mWord);
253 if (DEBUG_DICT) LOGI("Second freq: %d", secondFreq);
254 if (secondFreq <= 0) return false;
255
satok662fe692010-12-08 17:05:39 +0900256 word[missingSpacePos] = SPACE;
257 for (int i = (missingSpacePos + 1); i < newWordLength; ++i) {
satokaee09dc2010-12-09 19:21:51 +0900258 word[i] = mWord[i - missingSpacePos - 1];
satok662fe692010-12-08 17:05:39 +0900259 }
satokaee09dc2010-12-09 19:21:51 +0900260
261 int pairFreq = ((firstFreq + secondFreq) / 2);
262 for (int i = 0; i < inputLength; ++i) pairFreq *= TYPED_LETTER_MULTIPLIER;
satok662fe692010-12-08 17:05:39 +0900263 addWord(word, newWordLength, pairFreq);
264 return true;
265}
266
267// Keep this for comparing spec to new getWords
268void UnigramDictionary::getWordsOld(const int initialPos, const int inputLength, const int skipPos,
269 const int excessivePos, int *nextLetters, const int nextLettersSize) {
270 int initialPosition = initialPos;
271 const int count = Dictionary::getCount(DICT, &initialPosition);
272 getWordsRec(count, initialPosition, 0,
273 min(inputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH),
274 mInputLength <= 0, 1, 0, 0, skipPos, excessivePos, nextLetters, nextLettersSize);
275}
276
satok68319262010-12-03 19:38:08 +0900277void UnigramDictionary::getWordsRec(const int childrenCount, const int pos, const int depth,
278 const int maxDepth, const bool traverseAllNodes, const int snr, const int inputIndex,
satokcdbbea72010-12-08 16:04:16 +0900279 const int diffs, const int skipPos, const int excessivePos, int *nextLetters,
280 const int nextLettersSize) {
satok48e432c2010-12-06 17:38:58 +0900281 int siblingPos = pos;
satok68319262010-12-03 19:38:08 +0900282 for (int i = 0; i < childrenCount; ++i) {
satok48e432c2010-12-06 17:38:58 +0900283 int newCount;
284 int newChildPosition;
satokd2997922010-12-07 13:08:39 +0900285 const int newDepth = depth + 1;
satok48e432c2010-12-06 17:38:58 +0900286 bool newTraverseAllNodes;
287 int newSnr;
288 int newInputIndex;
289 int newDiffs;
290 int newSiblingPos;
291 const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth, maxDepth,
satokcdbbea72010-12-08 16:04:16 +0900292 traverseAllNodes, snr, inputIndex, diffs, skipPos, excessivePos, nextLetters,
293 nextLettersSize,
satokd2997922010-12-07 13:08:39 +0900294 &newCount, &newChildPosition, &newTraverseAllNodes, &newSnr,
satok48e432c2010-12-06 17:38:58 +0900295 &newInputIndex, &newDiffs, &newSiblingPos);
296 siblingPos = newSiblingPos;
satok30088252010-12-01 21:22:15 +0900297
satok48e432c2010-12-06 17:38:58 +0900298 if (needsToTraverseChildrenNodes) {
299 getWordsRec(newCount, newChildPosition, newDepth, maxDepth, newTraverseAllNodes,
satokcdbbea72010-12-08 16:04:16 +0900300 newSnr, newInputIndex, newDiffs, skipPos, excessivePos, nextLetters,
301 nextLettersSize);
satok30088252010-12-01 21:22:15 +0900302 }
303 }
304}
305
satok715514d2010-12-02 20:19:59 +0900306inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsGreaterThanInputLength(
307 unsigned short *word, const int inputLength, const int depth, const int snr,
308 int *nextLetters, const int nextLettersSize, const int skipPos, const int freq) {
satok662fe692010-12-08 17:05:39 +0900309 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, freq * snr);
satok715514d2010-12-02 20:19:59 +0900310 if (depth >= inputLength && skipPos < 0) {
311 registerNextLetter(mWord[mInputLength], nextLetters, nextLettersSize);
312 }
313}
314
315inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsSameAsInputLength(
316 unsigned short *word, const int depth, const int snr, const int skipPos, const int freq,
317 const int addedWeight) {
318 if (!sameAsTyped(word, depth + 1)) {
319 int finalFreq = freq * snr * addedWeight;
320 // Proximity collection will promote a word of the same length as
321 // what user typed.
322 if (skipPos < 0) finalFreq *= FULL_WORD_MULTIPLIER;
satok662fe692010-12-08 17:05:39 +0900323 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq);
satok715514d2010-12-02 20:19:59 +0900324 }
325}
satok28bd03b2010-12-03 16:39:16 +0900326
327inline bool UnigramDictionary::needsToSkipCurrentNode(const unsigned short c,
satok68319262010-12-03 19:38:08 +0900328 const int inputIndex, const int skipPos, const int depth) {
satok662fe692010-12-08 17:05:39 +0900329 const unsigned short userTypedChar = (mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS))[0];
satok28bd03b2010-12-03 16:39:16 +0900330 // Skip the ' or other letter and continue deeper
331 return (c == QUOTE && userTypedChar != QUOTE) || skipPos == depth;
332}
333
334inline int UnigramDictionary::getMatchedProximityId(const int *currentChars,
satok48e432c2010-12-06 17:38:58 +0900335 const unsigned short c, const int skipPos) {
336 const unsigned short lowerC = toLowerCase(c);
satok28bd03b2010-12-03 16:39:16 +0900337 int j = 0;
338 while (currentChars[j] > 0) {
satok68319262010-12-03 19:38:08 +0900339 const bool matched = (currentChars[j] == lowerC || currentChars[j] == c);
satok28bd03b2010-12-03 16:39:16 +0900340 // If skipPos is defined, not to search proximity collections.
341 // First char is what user typed.
342 if (matched) {
343 return j;
344 } else if (skipPos >= 0) {
345 return -1;
346 }
347 ++j;
348 }
349 return -1;
350}
351
satok48e432c2010-12-06 17:38:58 +0900352inline bool UnigramDictionary::processCurrentNode(const int pos, const int depth,
satokcdbbea72010-12-08 16:04:16 +0900353 const int maxDepth, const bool traverseAllNodes, const int snr, int inputIndex,
354 const int diffs, const int skipPos, const int excessivePos, int *nextLetters,
355 const int nextLettersSize, int *newCount, int *newChildPosition, bool *newTraverseAllNodes,
satok48e432c2010-12-06 17:38:58 +0900356 int *newSnr, int*newInputIndex, int *newDiffs, int *nextSiblingPosition) {
satokcdbbea72010-12-08 16:04:16 +0900357 if (DEBUG_DICT) assert(skipPos < 0 || excessivePos < 0);
satok48e432c2010-12-06 17:38:58 +0900358 unsigned short c;
359 int childPosition;
360 bool terminal;
361 int freq;
satokcdbbea72010-12-08 16:04:16 +0900362
363 if (excessivePos == depth) ++inputIndex;
364
satok48e432c2010-12-06 17:38:58 +0900365 *nextSiblingPosition = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, pos, &c,
366 &childPosition, &terminal, &freq);
367
368 const bool needsToTraverseChildrenNodes = childPosition != 0;
369
370 // If we are only doing traverseAllNodes, no need to look at the typed characters.
371 if (traverseAllNodes || needsToSkipCurrentNode(c, inputIndex, skipPos, depth)) {
372 mWord[depth] = c;
373 if (traverseAllNodes && terminal) {
374 onTerminalWhenUserTypedLengthIsGreaterThanInputLength(mWord, mInputLength, depth,
375 snr, nextLetters, nextLettersSize, skipPos, freq);
376 }
377 if (!needsToTraverseChildrenNodes) return false;
378 *newTraverseAllNodes = traverseAllNodes;
379 *newSnr = snr;
380 *newDiffs = diffs;
381 *newInputIndex = inputIndex;
satok48e432c2010-12-06 17:38:58 +0900382 } else {
satok662fe692010-12-08 17:05:39 +0900383 int *currentChars = mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS);
satok48e432c2010-12-06 17:38:58 +0900384 int matchedProximityCharId = getMatchedProximityId(currentChars, c, skipPos);
385 if (matchedProximityCharId < 0) return false;
386 mWord[depth] = c;
387 // If inputIndex is greater than mInputLength, that means there is no
388 // proximity chars. So, we don't need to check proximity.
389 const int addedWeight = matchedProximityCharId == 0 ? TYPED_LETTER_MULTIPLIER : 1;
390 const bool isSameAsUserTypedLength = mInputLength == inputIndex + 1;
391 if (isSameAsUserTypedLength && terminal) {
392 onTerminalWhenUserTypedLengthIsSameAsInputLength(mWord, depth, snr,
393 skipPos, freq, addedWeight);
394 }
395 if (!needsToTraverseChildrenNodes) return false;
396 // Start traversing all nodes after the index exceeds the user typed length
397 *newTraverseAllNodes = isSameAsUserTypedLength;
398 *newSnr = snr * addedWeight;
399 *newDiffs = diffs + (matchedProximityCharId > 0);
400 *newInputIndex = inputIndex + 1;
satok48e432c2010-12-06 17:38:58 +0900401 }
402 // Optimization: Prune out words that are too long compared to how much was typed.
satokd2997922010-12-07 13:08:39 +0900403 if (depth >= maxDepth || *newDiffs > mMaxEditDistance) {
satok48e432c2010-12-06 17:38:58 +0900404 return false;
405 }
406
407 // If inputIndex is greater than mInputLength, that means there are no proximity chars.
408 if (mInputLength <= *newInputIndex) {
409 *newTraverseAllNodes = true;
410 }
411 // get the count of nodes and increment childAddress.
412 *newCount = Dictionary::getCount(DICT, &childPosition);
413 *newChildPosition = childPosition;
414 if (DEBUG_DICT) assert(needsToTraverseChildrenNodes);
415 return needsToTraverseChildrenNodes;
416}
417
satokaee09dc2010-12-09 19:21:51 +0900418inline int UnigramDictionary::getBestWordFreq(const int startInputIndex, const int inputLength,
419 unsigned short *word) {
satok662fe692010-12-08 17:05:39 +0900420 int pos = ROOT_POS;
421 int count = Dictionary::getCount(DICT, &pos);
satokaee09dc2010-12-09 19:21:51 +0900422 int maxFreq = 0;
423 int depth = 0;
424 unsigned short newWord[MAX_WORD_LENGTH_INTERNAL];
satok662fe692010-12-08 17:05:39 +0900425 bool terminal = false;
426
satokaee09dc2010-12-09 19:21:51 +0900427 mStackChildCount[0] = count;
428 mStackSiblingPos[0] = pos;
429
430 while (depth >= 0) {
431 if (mStackChildCount[depth] > 0) {
432 --mStackChildCount[depth];
433 int firstChildPos;
434 int newFreq;
435 int siblingPos = mStackSiblingPos[depth];
436 const bool needsToTraverseChildrenNodes = processCurrentNodeForExactMatch(siblingPos,
437 startInputIndex, depth, newWord, &firstChildPos, &count, &terminal, &newFreq,
438 &siblingPos);
439 mStackSiblingPos[depth] = siblingPos;
440 if (depth == (inputLength - 1)) {
441 // Traverse sibling node
442 if (terminal) {
443 if (newFreq > maxFreq) {
444 for (int i = 0; i < inputLength; ++i) word[i] = newWord[i];
445 if (DEBUG_DICT && DEBUG_NODE) {
446 char s[inputLength + 1];
447 for (int i = 0; i < inputLength; ++i) s[i] = word[i];
448 s[inputLength] = 0;
449 LOGI("New missing space word found: %d > %d (%s), %d, %d",
450 newFreq, maxFreq, s, inputLength, depth);
451 }
452 maxFreq = newFreq;
453 }
454 }
455 } else if (needsToTraverseChildrenNodes) {
456 // Traverse children nodes
457 ++depth;
458 mStackChildCount[depth] = count;
459 mStackSiblingPos[depth] = firstChildPos;
460 }
461 } else {
462 // Traverse parent node
463 --depth;
satok662fe692010-12-08 17:05:39 +0900464 }
465 }
satokaee09dc2010-12-09 19:21:51 +0900466
467 word[inputLength] = 0;
468 return maxFreq;
satok662fe692010-12-08 17:05:39 +0900469}
470
471inline bool UnigramDictionary::processCurrentNodeForExactMatch(const int firstChildPos,
satokaee09dc2010-12-09 19:21:51 +0900472 const int startInputIndex, const int depth, unsigned short *word, int *newChildPosition,
473 int *newCount, bool *newTerminal, int *newFreq, int *siblingPos) {
474 const int inputIndex = startInputIndex + depth;
satok662fe692010-12-08 17:05:39 +0900475 const int *currentChars = mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS);
satok662fe692010-12-08 17:05:39 +0900476 unsigned short c;
satokaee09dc2010-12-09 19:21:51 +0900477 *siblingPos = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, firstChildPos, &c,
478 newChildPosition, newTerminal, newFreq);
479 const unsigned int inputC = currentChars[0];
480 if (DEBUG_DICT) assert(inputC <= U_SHORT_MAX);
481 const unsigned short lowerC = toLowerCase(c);
482 const bool matched = (inputC == lowerC || inputC == c);
483 const bool hasChild = *newChildPosition != 0;
484 if (matched) {
485 word[depth] = c;
486 if (DEBUG_DICT && DEBUG_NODE) {
487 LOGI("Node(%c, %c)<%d>, %d, %d", inputC, c, matched, hasChild, *newFreq);
488 if (*newTerminal) LOGI("Terminal %d", *newFreq);
satok662fe692010-12-08 17:05:39 +0900489 }
satokaee09dc2010-12-09 19:21:51 +0900490 if (hasChild) {
491 *newCount = Dictionary::getCount(DICT, newChildPosition);
492 return true;
493 } else {
494 return false;
495 }
496 } else {
497 // If this node is not user typed character, this method treats this word as unmatched.
498 // Thus newTerminal shouldn't be true.
499 *newTerminal = false;
500 return false;
satok662fe692010-12-08 17:05:39 +0900501 }
satok662fe692010-12-08 17:05:39 +0900502}
satok30088252010-12-01 21:22:15 +0900503} // namespace latinime