blob: 46332c74d597fc115c17e48c577a328a3ba9ef9c [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) {
satoke07baa62010-12-09 21:55:40 +090061 if (existsAdjacentProximityChars(i, codesSize)) {
62 if (DEBUG_DICT) LOGI("--- Suggest excessive characters %d", i);
63 getSuggestionCandidates(codesSize, -1, i, NULL, 0);
64 }
satok30088252010-12-01 21:22:15 +090065 }
66 }
67
satok662fe692010-12-08 17:05:39 +090068 // Suggestions with missing space
satokaee09dc2010-12-09 19:21:51 +090069 if (SUGGEST_WORDS_WITH_MISSING_SPACE_CHARACTER && mInputLength > MIN_SUGGEST_DEPTH) {
satok662fe692010-12-08 17:05:39 +090070 for (int i = 1; i < codesSize; ++i) {
71 if (DEBUG_DICT) LOGI("--- Suggest missing space characters %d", i);
72 getMissingSpaceWords(mInputLength, i);
73 }
74 }
75
76 // Get the word count
77 int suggestedWordsCount = 0;
78 while (suggestedWordsCount < MAX_WORDS && mFrequencies[suggestedWordsCount] > 0) {
79 suggestedWordsCount++;
80 }
81
satok30088252010-12-01 21:22:15 +090082 if (DEBUG_DICT) {
83 LOGI("Returning %d words", suggestedWordsCount);
84 LOGI("Next letters: ");
85 for (int k = 0; k < nextLettersSize; k++) {
86 if (nextLetters[k] > 0) {
87 LOGI("%c = %d,", k, nextLetters[k]);
88 }
89 }
90 LOGI("\n");
91 }
satok662fe692010-12-08 17:05:39 +090092
satok30088252010-12-01 21:22:15 +090093 return suggestedWordsCount;
94}
95
96void UnigramDictionary::initSuggestions(int *codes, int codesSize, unsigned short *outWords,
97 int *frequencies) {
satokf5cded12010-12-06 21:28:24 +090098 if (DEBUG_DICT) LOGI("initSuggest");
satok30088252010-12-01 21:22:15 +090099 mFrequencies = frequencies;
100 mOutputChars = outWords;
101 mInputCodes = codes;
102 mInputLength = codesSize;
103 mMaxEditDistance = mInputLength < 5 ? 2 : mInputLength / 2;
104}
105
satok715514d2010-12-02 20:19:59 +0900106void UnigramDictionary::registerNextLetter(
107 unsigned short c, int *nextLetters, int nextLettersSize) {
satok30088252010-12-01 21:22:15 +0900108 if (c < nextLettersSize) {
109 nextLetters[c]++;
110 }
111}
112
satok662fe692010-12-08 17:05:39 +0900113// TODO: We need to optimize addWord by using STL or something
satok28bd03b2010-12-03 16:39:16 +0900114bool UnigramDictionary::addWord(unsigned short *word, int length, int frequency) {
satok30088252010-12-01 21:22:15 +0900115 word[length] = 0;
satok662fe692010-12-08 17:05:39 +0900116 if (DEBUG_DICT && DEBUG_SHOW_FOUND_WORD) {
satok30088252010-12-01 21:22:15 +0900117 char s[length + 1];
118 for (int i = 0; i <= length; i++) s[i] = word[i];
satok662fe692010-12-08 17:05:39 +0900119 LOGI("Found word = %s, freq = %d", s, frequency);
satok30088252010-12-01 21:22:15 +0900120 }
satokf5cded12010-12-06 21:28:24 +0900121 if (length > MAX_WORD_LENGTH) {
122 if (DEBUG_DICT) LOGI("Exceeded max word length.");
123 return false;
124 }
satok30088252010-12-01 21:22:15 +0900125
126 // Find the right insertion point
127 int insertAt = 0;
128 while (insertAt < MAX_WORDS) {
satok715514d2010-12-02 20:19:59 +0900129 if (frequency > mFrequencies[insertAt] || (mFrequencies[insertAt] == frequency
130 && length < Dictionary::wideStrLen(mOutputChars + insertAt * MAX_WORD_LENGTH))) {
satok30088252010-12-01 21:22:15 +0900131 break;
132 }
133 insertAt++;
134 }
135 if (insertAt < MAX_WORDS) {
satokcdbbea72010-12-08 16:04:16 +0900136 if (DEBUG_DICT) {
137 char s[length + 1];
138 for (int i = 0; i <= length; i++) s[i] = word[i];
satok662fe692010-12-08 17:05:39 +0900139 LOGI("Added word = %s, freq = %d", s, frequency);
satokcdbbea72010-12-08 16:04:16 +0900140 }
satok30088252010-12-01 21:22:15 +0900141 memmove((char*) mFrequencies + (insertAt + 1) * sizeof(mFrequencies[0]),
142 (char*) mFrequencies + insertAt * sizeof(mFrequencies[0]),
143 (MAX_WORDS - insertAt - 1) * sizeof(mFrequencies[0]));
144 mFrequencies[insertAt] = frequency;
145 memmove((char*) mOutputChars + (insertAt + 1) * MAX_WORD_LENGTH * sizeof(short),
satok715514d2010-12-02 20:19:59 +0900146 (char*) mOutputChars + insertAt * MAX_WORD_LENGTH * sizeof(short),
satok30088252010-12-01 21:22:15 +0900147 (MAX_WORDS - insertAt - 1) * sizeof(short) * MAX_WORD_LENGTH);
satok715514d2010-12-02 20:19:59 +0900148 unsigned short *dest = mOutputChars + insertAt * MAX_WORD_LENGTH;
satok30088252010-12-01 21:22:15 +0900149 while (length--) {
150 *dest++ = *word++;
151 }
152 *dest = 0; // NULL terminate
satok662fe692010-12-08 17:05:39 +0900153 if (DEBUG_DICT) LOGI("Added word at %d", insertAt);
satok30088252010-12-01 21:22:15 +0900154 return true;
155 }
156 return false;
157}
158
satok28bd03b2010-12-03 16:39:16 +0900159unsigned short UnigramDictionary::toLowerCase(unsigned short c) {
satok30088252010-12-01 21:22:15 +0900160 if (c < sizeof(BASE_CHARS) / sizeof(BASE_CHARS[0])) {
161 c = BASE_CHARS[c];
162 }
163 if (c >='A' && c <= 'Z') {
164 c |= 32;
165 } else if (c > 127) {
166 c = latin_tolower(c);
167 }
168 return c;
169}
170
satok28bd03b2010-12-03 16:39:16 +0900171bool UnigramDictionary::sameAsTyped(unsigned short *word, int length) {
satok30088252010-12-01 21:22:15 +0900172 if (length != mInputLength) {
173 return false;
174 }
175 int *inputCodes = mInputCodes;
176 while (length--) {
177 if ((unsigned int) *inputCodes != (unsigned int) *word) {
178 return false;
179 }
satok662fe692010-12-08 17:05:39 +0900180 inputCodes += MAX_PROXIMITY_CHARS;
satok30088252010-12-01 21:22:15 +0900181 word++;
182 }
183 return true;
184}
185
satok715514d2010-12-02 20:19:59 +0900186static const char QUOTE = '\'';
satok662fe692010-12-08 17:05:39 +0900187static const char SPACE = ' ';
satok30088252010-12-01 21:22:15 +0900188
satok662fe692010-12-08 17:05:39 +0900189void UnigramDictionary::getSuggestionCandidates(const int inputLength, const int skipPos,
satokcdbbea72010-12-08 16:04:16 +0900190 const int excessivePos, int *nextLetters, const int nextLettersSize) {
satok662fe692010-12-08 17:05:39 +0900191 if (DEBUG_DICT) LOGI("getSuggestionCandidates");
192 int rootPosition = ROOT_POS;
satokd2997922010-12-07 13:08:39 +0900193 const int MAX_DEPTH = min(inputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH);
194 // Get the number of child of root, then increment the position
195 int childCount = Dictionary::getCount(DICT, &rootPosition);
196 int depth = 0;
197
198 mStackChildCount[0] = childCount;
199 mStackTraverseAll[0] = (mInputLength <= 0);
200 mStackNodeFreq[0] = 1;
201 mStackInputIndex[0] = 0;
202 mStackDiffs[0] = 0;
203 mStackSiblingPos[0] = rootPosition;
204
satok662fe692010-12-08 17:05:39 +0900205 // Depth first search
satokd2997922010-12-07 13:08:39 +0900206 while (depth >= 0) {
207 if (mStackChildCount[depth] > 0) {
208 --mStackChildCount[depth];
209 bool traverseAllNodes = mStackTraverseAll[depth];
210 int snr = mStackNodeFreq[depth];
211 int inputIndex = mStackInputIndex[depth];
212 int diffs = mStackDiffs[depth];
213 int siblingPos = mStackSiblingPos[depth];
214 int firstChildPos;
215 // depth will never be greater than MAX_DEPTH because in that case,
216 // needsToTraverseChildrenNodes should be false
217 const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth,
satokcdbbea72010-12-08 16:04:16 +0900218 MAX_DEPTH, traverseAllNodes, snr, inputIndex, diffs, skipPos, excessivePos,
219 nextLetters, nextLettersSize, &childCount, &firstChildPos, &traverseAllNodes,
220 &snr, &inputIndex, &diffs, &siblingPos);
satok662fe692010-12-08 17:05:39 +0900221 // Update next sibling pos
satokd2997922010-12-07 13:08:39 +0900222 mStackSiblingPos[depth] = siblingPos;
223 if (needsToTraverseChildrenNodes) {
224 // Goes to child node
225 ++depth;
226 mStackChildCount[depth] = childCount;
227 mStackTraverseAll[depth] = traverseAllNodes;
228 mStackNodeFreq[depth] = snr;
229 mStackInputIndex[depth] = inputIndex;
230 mStackDiffs[depth] = diffs;
231 mStackSiblingPos[depth] = firstChildPos;
232 }
233 } else {
satokcdbbea72010-12-08 16:04:16 +0900234 // Goes to parent sibling node
satokd2997922010-12-07 13:08:39 +0900235 --depth;
236 }
237 }
238}
239
satok662fe692010-12-08 17:05:39 +0900240bool UnigramDictionary::getMissingSpaceWords(const int inputLength, const int missingSpacePos) {
satokaee09dc2010-12-09 19:21:51 +0900241 if (missingSpacePos <= 0 || missingSpacePos >= inputLength
242 || inputLength >= MAX_WORD_LENGTH) return false;
satok662fe692010-12-08 17:05:39 +0900243 const int newWordLength = inputLength + 1;
244 // Allocating variable length array on stack
245 unsigned short word[newWordLength];
satokaee09dc2010-12-09 19:21:51 +0900246 const int firstFreq = getBestWordFreq(0, missingSpacePos, mWord);
247 if (DEBUG_DICT) LOGI("First freq: %d", firstFreq);
248 if (firstFreq <= 0) return false;
249
satok662fe692010-12-08 17:05:39 +0900250 for (int i = 0; i < missingSpacePos; ++i) {
satokaee09dc2010-12-09 19:21:51 +0900251 word[i] = mWord[i];
satok662fe692010-12-08 17:05:39 +0900252 }
satokaee09dc2010-12-09 19:21:51 +0900253
254 const int secondFreq = getBestWordFreq(missingSpacePos, inputLength - missingSpacePos, mWord);
255 if (DEBUG_DICT) LOGI("Second freq: %d", secondFreq);
256 if (secondFreq <= 0) return false;
257
satok662fe692010-12-08 17:05:39 +0900258 word[missingSpacePos] = SPACE;
259 for (int i = (missingSpacePos + 1); i < newWordLength; ++i) {
satokaee09dc2010-12-09 19:21:51 +0900260 word[i] = mWord[i - missingSpacePos - 1];
satok662fe692010-12-08 17:05:39 +0900261 }
satokaee09dc2010-12-09 19:21:51 +0900262
263 int pairFreq = ((firstFreq + secondFreq) / 2);
264 for (int i = 0; i < inputLength; ++i) pairFreq *= TYPED_LETTER_MULTIPLIER;
satok662fe692010-12-08 17:05:39 +0900265 addWord(word, newWordLength, pairFreq);
266 return true;
267}
268
269// Keep this for comparing spec to new getWords
270void UnigramDictionary::getWordsOld(const int initialPos, const int inputLength, const int skipPos,
271 const int excessivePos, int *nextLetters, const int nextLettersSize) {
272 int initialPosition = initialPos;
273 const int count = Dictionary::getCount(DICT, &initialPosition);
274 getWordsRec(count, initialPosition, 0,
275 min(inputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH),
276 mInputLength <= 0, 1, 0, 0, skipPos, excessivePos, nextLetters, nextLettersSize);
277}
278
satok68319262010-12-03 19:38:08 +0900279void UnigramDictionary::getWordsRec(const int childrenCount, const int pos, const int depth,
280 const int maxDepth, const bool traverseAllNodes, const int snr, const int inputIndex,
satokcdbbea72010-12-08 16:04:16 +0900281 const int diffs, const int skipPos, const int excessivePos, int *nextLetters,
282 const int nextLettersSize) {
satok48e432c2010-12-06 17:38:58 +0900283 int siblingPos = pos;
satok68319262010-12-03 19:38:08 +0900284 for (int i = 0; i < childrenCount; ++i) {
satok48e432c2010-12-06 17:38:58 +0900285 int newCount;
286 int newChildPosition;
satokd2997922010-12-07 13:08:39 +0900287 const int newDepth = depth + 1;
satok48e432c2010-12-06 17:38:58 +0900288 bool newTraverseAllNodes;
289 int newSnr;
290 int newInputIndex;
291 int newDiffs;
292 int newSiblingPos;
293 const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth, maxDepth,
satokcdbbea72010-12-08 16:04:16 +0900294 traverseAllNodes, snr, inputIndex, diffs, skipPos, excessivePos, nextLetters,
295 nextLettersSize,
satokd2997922010-12-07 13:08:39 +0900296 &newCount, &newChildPosition, &newTraverseAllNodes, &newSnr,
satok48e432c2010-12-06 17:38:58 +0900297 &newInputIndex, &newDiffs, &newSiblingPos);
298 siblingPos = newSiblingPos;
satok30088252010-12-01 21:22:15 +0900299
satok48e432c2010-12-06 17:38:58 +0900300 if (needsToTraverseChildrenNodes) {
301 getWordsRec(newCount, newChildPosition, newDepth, maxDepth, newTraverseAllNodes,
satokcdbbea72010-12-08 16:04:16 +0900302 newSnr, newInputIndex, newDiffs, skipPos, excessivePos, nextLetters,
303 nextLettersSize);
satok30088252010-12-01 21:22:15 +0900304 }
305 }
306}
307
satok715514d2010-12-02 20:19:59 +0900308inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsGreaterThanInputLength(
309 unsigned short *word, const int inputLength, const int depth, const int snr,
310 int *nextLetters, const int nextLettersSize, const int skipPos, const int freq) {
satok662fe692010-12-08 17:05:39 +0900311 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, freq * snr);
satok715514d2010-12-02 20:19:59 +0900312 if (depth >= inputLength && skipPos < 0) {
313 registerNextLetter(mWord[mInputLength], nextLetters, nextLettersSize);
314 }
315}
316
317inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsSameAsInputLength(
318 unsigned short *word, const int depth, const int snr, const int skipPos, const int freq,
319 const int addedWeight) {
320 if (!sameAsTyped(word, depth + 1)) {
321 int finalFreq = freq * snr * addedWeight;
322 // Proximity collection will promote a word of the same length as
323 // what user typed.
324 if (skipPos < 0) finalFreq *= FULL_WORD_MULTIPLIER;
satok662fe692010-12-08 17:05:39 +0900325 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq);
satok715514d2010-12-02 20:19:59 +0900326 }
327}
satok28bd03b2010-12-03 16:39:16 +0900328
329inline bool UnigramDictionary::needsToSkipCurrentNode(const unsigned short c,
satok68319262010-12-03 19:38:08 +0900330 const int inputIndex, const int skipPos, const int depth) {
satok662fe692010-12-08 17:05:39 +0900331 const unsigned short userTypedChar = (mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS))[0];
satok28bd03b2010-12-03 16:39:16 +0900332 // Skip the ' or other letter and continue deeper
333 return (c == QUOTE && userTypedChar != QUOTE) || skipPos == depth;
334}
335
satoke07baa62010-12-09 21:55:40 +0900336inline bool UnigramDictionary::existsAdjacentProximityChars(const int inputIndex,
337 const int inputLength) {
338 if (inputIndex < 0 || inputIndex >= inputLength) return false;
339 const int currentChar = *getInputCharsAt(inputIndex);
340 const int leftIndex = inputIndex - 1;
341 if (leftIndex >= 0) {
342 int *leftChars = getInputCharsAt(leftIndex);
343 int i = 0;
344 while (leftChars[i] > 0 && i < MAX_PROXIMITY_CHARS) {
345 if (leftChars[i++] == currentChar) return true;
346 }
347 }
348 const int rightIndex = inputIndex + 1;
349 if (rightIndex < inputLength) {
350 int *rightChars = getInputCharsAt(rightIndex);
351 int i = 0;
352 while (rightChars[i] > 0 && i < MAX_PROXIMITY_CHARS) {
353 if (rightChars[i++] == currentChar) return true;
354 }
355 }
356 return false;
357}
358
satok28bd03b2010-12-03 16:39:16 +0900359inline int UnigramDictionary::getMatchedProximityId(const int *currentChars,
satok48e432c2010-12-06 17:38:58 +0900360 const unsigned short c, const int skipPos) {
361 const unsigned short lowerC = toLowerCase(c);
satok28bd03b2010-12-03 16:39:16 +0900362 int j = 0;
satoke07baa62010-12-09 21:55:40 +0900363 while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS) {
satok68319262010-12-03 19:38:08 +0900364 const bool matched = (currentChars[j] == lowerC || currentChars[j] == c);
satok28bd03b2010-12-03 16:39:16 +0900365 // If skipPos is defined, not to search proximity collections.
366 // First char is what user typed.
367 if (matched) {
368 return j;
369 } else if (skipPos >= 0) {
370 return -1;
371 }
372 ++j;
373 }
374 return -1;
375}
376
satok48e432c2010-12-06 17:38:58 +0900377inline bool UnigramDictionary::processCurrentNode(const int pos, const int depth,
satokcdbbea72010-12-08 16:04:16 +0900378 const int maxDepth, const bool traverseAllNodes, const int snr, int inputIndex,
379 const int diffs, const int skipPos, const int excessivePos, int *nextLetters,
380 const int nextLettersSize, int *newCount, int *newChildPosition, bool *newTraverseAllNodes,
satok48e432c2010-12-06 17:38:58 +0900381 int *newSnr, int*newInputIndex, int *newDiffs, int *nextSiblingPosition) {
satokcdbbea72010-12-08 16:04:16 +0900382 if (DEBUG_DICT) assert(skipPos < 0 || excessivePos < 0);
satok48e432c2010-12-06 17:38:58 +0900383 unsigned short c;
384 int childPosition;
385 bool terminal;
386 int freq;
satokcdbbea72010-12-08 16:04:16 +0900387
388 if (excessivePos == depth) ++inputIndex;
389
satok48e432c2010-12-06 17:38:58 +0900390 *nextSiblingPosition = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, pos, &c,
391 &childPosition, &terminal, &freq);
392
393 const bool needsToTraverseChildrenNodes = childPosition != 0;
394
395 // If we are only doing traverseAllNodes, no need to look at the typed characters.
396 if (traverseAllNodes || needsToSkipCurrentNode(c, inputIndex, skipPos, depth)) {
397 mWord[depth] = c;
398 if (traverseAllNodes && terminal) {
399 onTerminalWhenUserTypedLengthIsGreaterThanInputLength(mWord, mInputLength, depth,
400 snr, nextLetters, nextLettersSize, skipPos, freq);
401 }
402 if (!needsToTraverseChildrenNodes) return false;
403 *newTraverseAllNodes = traverseAllNodes;
404 *newSnr = snr;
405 *newDiffs = diffs;
406 *newInputIndex = inputIndex;
satok48e432c2010-12-06 17:38:58 +0900407 } else {
satok662fe692010-12-08 17:05:39 +0900408 int *currentChars = mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS);
satok48e432c2010-12-06 17:38:58 +0900409 int matchedProximityCharId = getMatchedProximityId(currentChars, c, skipPos);
410 if (matchedProximityCharId < 0) return false;
411 mWord[depth] = c;
412 // If inputIndex is greater than mInputLength, that means there is no
413 // proximity chars. So, we don't need to check proximity.
414 const int addedWeight = matchedProximityCharId == 0 ? TYPED_LETTER_MULTIPLIER : 1;
415 const bool isSameAsUserTypedLength = mInputLength == inputIndex + 1;
416 if (isSameAsUserTypedLength && terminal) {
417 onTerminalWhenUserTypedLengthIsSameAsInputLength(mWord, depth, snr,
418 skipPos, freq, addedWeight);
419 }
420 if (!needsToTraverseChildrenNodes) return false;
421 // Start traversing all nodes after the index exceeds the user typed length
422 *newTraverseAllNodes = isSameAsUserTypedLength;
423 *newSnr = snr * addedWeight;
424 *newDiffs = diffs + (matchedProximityCharId > 0);
425 *newInputIndex = inputIndex + 1;
satok48e432c2010-12-06 17:38:58 +0900426 }
427 // Optimization: Prune out words that are too long compared to how much was typed.
satokd2997922010-12-07 13:08:39 +0900428 if (depth >= maxDepth || *newDiffs > mMaxEditDistance) {
satok48e432c2010-12-06 17:38:58 +0900429 return false;
430 }
431
432 // If inputIndex is greater than mInputLength, that means there are no proximity chars.
433 if (mInputLength <= *newInputIndex) {
434 *newTraverseAllNodes = true;
435 }
436 // get the count of nodes and increment childAddress.
437 *newCount = Dictionary::getCount(DICT, &childPosition);
438 *newChildPosition = childPosition;
439 if (DEBUG_DICT) assert(needsToTraverseChildrenNodes);
440 return needsToTraverseChildrenNodes;
441}
442
satokaee09dc2010-12-09 19:21:51 +0900443inline int UnigramDictionary::getBestWordFreq(const int startInputIndex, const int inputLength,
444 unsigned short *word) {
satok662fe692010-12-08 17:05:39 +0900445 int pos = ROOT_POS;
446 int count = Dictionary::getCount(DICT, &pos);
satokaee09dc2010-12-09 19:21:51 +0900447 int maxFreq = 0;
448 int depth = 0;
449 unsigned short newWord[MAX_WORD_LENGTH_INTERNAL];
satok662fe692010-12-08 17:05:39 +0900450 bool terminal = false;
451
satokaee09dc2010-12-09 19:21:51 +0900452 mStackChildCount[0] = count;
453 mStackSiblingPos[0] = pos;
454
455 while (depth >= 0) {
456 if (mStackChildCount[depth] > 0) {
457 --mStackChildCount[depth];
458 int firstChildPos;
459 int newFreq;
460 int siblingPos = mStackSiblingPos[depth];
461 const bool needsToTraverseChildrenNodes = processCurrentNodeForExactMatch(siblingPos,
462 startInputIndex, depth, newWord, &firstChildPos, &count, &terminal, &newFreq,
463 &siblingPos);
464 mStackSiblingPos[depth] = siblingPos;
465 if (depth == (inputLength - 1)) {
466 // Traverse sibling node
467 if (terminal) {
468 if (newFreq > maxFreq) {
469 for (int i = 0; i < inputLength; ++i) word[i] = newWord[i];
470 if (DEBUG_DICT && DEBUG_NODE) {
471 char s[inputLength + 1];
472 for (int i = 0; i < inputLength; ++i) s[i] = word[i];
473 s[inputLength] = 0;
474 LOGI("New missing space word found: %d > %d (%s), %d, %d",
475 newFreq, maxFreq, s, inputLength, depth);
476 }
477 maxFreq = newFreq;
478 }
479 }
480 } else if (needsToTraverseChildrenNodes) {
481 // Traverse children nodes
482 ++depth;
483 mStackChildCount[depth] = count;
484 mStackSiblingPos[depth] = firstChildPos;
485 }
486 } else {
487 // Traverse parent node
488 --depth;
satok662fe692010-12-08 17:05:39 +0900489 }
490 }
satokaee09dc2010-12-09 19:21:51 +0900491
492 word[inputLength] = 0;
493 return maxFreq;
satok662fe692010-12-08 17:05:39 +0900494}
495
496inline bool UnigramDictionary::processCurrentNodeForExactMatch(const int firstChildPos,
satokaee09dc2010-12-09 19:21:51 +0900497 const int startInputIndex, const int depth, unsigned short *word, int *newChildPosition,
498 int *newCount, bool *newTerminal, int *newFreq, int *siblingPos) {
499 const int inputIndex = startInputIndex + depth;
satok662fe692010-12-08 17:05:39 +0900500 const int *currentChars = mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS);
satok662fe692010-12-08 17:05:39 +0900501 unsigned short c;
satokaee09dc2010-12-09 19:21:51 +0900502 *siblingPos = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, firstChildPos, &c,
503 newChildPosition, newTerminal, newFreq);
504 const unsigned int inputC = currentChars[0];
505 if (DEBUG_DICT) assert(inputC <= U_SHORT_MAX);
506 const unsigned short lowerC = toLowerCase(c);
507 const bool matched = (inputC == lowerC || inputC == c);
508 const bool hasChild = *newChildPosition != 0;
509 if (matched) {
510 word[depth] = c;
511 if (DEBUG_DICT && DEBUG_NODE) {
512 LOGI("Node(%c, %c)<%d>, %d, %d", inputC, c, matched, hasChild, *newFreq);
513 if (*newTerminal) LOGI("Terminal %d", *newFreq);
satok662fe692010-12-08 17:05:39 +0900514 }
satokaee09dc2010-12-09 19:21:51 +0900515 if (hasChild) {
516 *newCount = Dictionary::getCount(DICT, newChildPosition);
517 return true;
518 } else {
519 return false;
520 }
521 } else {
522 // If this node is not user typed character, this method treats this word as unmatched.
523 // Thus newTerminal shouldn't be true.
524 *newTerminal = false;
525 return false;
satok662fe692010-12-08 17:05:39 +0900526 }
satok662fe692010-12-08 17:05:39 +0900527}
satok30088252010-12-01 21:22:15 +0900528} // namespace latinime