blob: 3856cb7ec6e87c20ee79b697e6cab54d4ab815f8 [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
67 if (SUGGEST_WORDS_WITH_MISSING_SPACE_CHARACTER) {
68 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) {
239 if (missingSpacePos <= 0 || missingSpacePos >= inputLength) return false;
240 const int firstFreq = getWordFreq(0, missingSpacePos);
241 const int secondFreq = getWordFreq(missingSpacePos, inputLength - missingSpacePos);
242 if (DEBUG_DICT) LOGI("First freq: %d, Second freq: %d", firstFreq, secondFreq);
243
244 if (firstFreq <= 0 || secondFreq <= 0) return false;
245 int pairFreq = (firstFreq + secondFreq) / 2;
246 for (int i = 0; i < inputLength; ++i) pairFreq *= TYPED_LETTER_MULTIPLIER;
247 const int newWordLength = inputLength + 1;
248 // Allocating variable length array on stack
249 unsigned short word[newWordLength];
250 int j = 0;
251 for (int i = 0; i < missingSpacePos; ++i) {
252 // Down-casting
253 if (DEBUG_DICT) {
254 assert((*(mInputCodes + i * MAX_PROXIMITY_CHARS)) <= U_SHORT_MAX);
255 }
256 word[i] = (unsigned short) *(mInputCodes + i * MAX_PROXIMITY_CHARS);
257 }
258 word[missingSpacePos] = SPACE;
259 for (int i = (missingSpacePos + 1); i < newWordLength; ++i) {
260 // Down-casting
261 if (DEBUG_DICT) {
262 assert((*(mInputCodes + (i - 1) * MAX_PROXIMITY_CHARS)) <= U_SHORT_MAX);
263 }
264 word[i] = (unsigned short) *(mInputCodes + (i - 1) * MAX_PROXIMITY_CHARS);
265 }
266 addWord(word, newWordLength, pairFreq);
267 return true;
268}
269
270// Keep this for comparing spec to new getWords
271void UnigramDictionary::getWordsOld(const int initialPos, const int inputLength, const int skipPos,
272 const int excessivePos, int *nextLetters, const int nextLettersSize) {
273 int initialPosition = initialPos;
274 const int count = Dictionary::getCount(DICT, &initialPosition);
275 getWordsRec(count, initialPosition, 0,
276 min(inputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH),
277 mInputLength <= 0, 1, 0, 0, skipPos, excessivePos, nextLetters, nextLettersSize);
278}
279
satok68319262010-12-03 19:38:08 +0900280void UnigramDictionary::getWordsRec(const int childrenCount, const int pos, const int depth,
281 const int maxDepth, const bool traverseAllNodes, const int snr, const int inputIndex,
satokcdbbea72010-12-08 16:04:16 +0900282 const int diffs, const int skipPos, const int excessivePos, int *nextLetters,
283 const int nextLettersSize) {
satok48e432c2010-12-06 17:38:58 +0900284 int siblingPos = pos;
satok68319262010-12-03 19:38:08 +0900285 for (int i = 0; i < childrenCount; ++i) {
satok48e432c2010-12-06 17:38:58 +0900286 int newCount;
287 int newChildPosition;
satokd2997922010-12-07 13:08:39 +0900288 const int newDepth = depth + 1;
satok48e432c2010-12-06 17:38:58 +0900289 bool newTraverseAllNodes;
290 int newSnr;
291 int newInputIndex;
292 int newDiffs;
293 int newSiblingPos;
294 const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth, maxDepth,
satokcdbbea72010-12-08 16:04:16 +0900295 traverseAllNodes, snr, inputIndex, diffs, skipPos, excessivePos, nextLetters,
296 nextLettersSize,
satokd2997922010-12-07 13:08:39 +0900297 &newCount, &newChildPosition, &newTraverseAllNodes, &newSnr,
satok48e432c2010-12-06 17:38:58 +0900298 &newInputIndex, &newDiffs, &newSiblingPos);
299 siblingPos = newSiblingPos;
satok30088252010-12-01 21:22:15 +0900300
satok48e432c2010-12-06 17:38:58 +0900301 if (needsToTraverseChildrenNodes) {
302 getWordsRec(newCount, newChildPosition, newDepth, maxDepth, newTraverseAllNodes,
satokcdbbea72010-12-08 16:04:16 +0900303 newSnr, newInputIndex, newDiffs, skipPos, excessivePos, nextLetters,
304 nextLettersSize);
satok30088252010-12-01 21:22:15 +0900305 }
306 }
307}
308
satok715514d2010-12-02 20:19:59 +0900309inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsGreaterThanInputLength(
310 unsigned short *word, const int inputLength, const int depth, const int snr,
311 int *nextLetters, const int nextLettersSize, const int skipPos, const int freq) {
satok662fe692010-12-08 17:05:39 +0900312 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, freq * snr);
satok715514d2010-12-02 20:19:59 +0900313 if (depth >= inputLength && skipPos < 0) {
314 registerNextLetter(mWord[mInputLength], nextLetters, nextLettersSize);
315 }
316}
317
318inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsSameAsInputLength(
319 unsigned short *word, const int depth, const int snr, const int skipPos, const int freq,
320 const int addedWeight) {
321 if (!sameAsTyped(word, depth + 1)) {
322 int finalFreq = freq * snr * addedWeight;
323 // Proximity collection will promote a word of the same length as
324 // what user typed.
325 if (skipPos < 0) finalFreq *= FULL_WORD_MULTIPLIER;
satok662fe692010-12-08 17:05:39 +0900326 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq);
satok715514d2010-12-02 20:19:59 +0900327 }
328}
satok28bd03b2010-12-03 16:39:16 +0900329
330inline bool UnigramDictionary::needsToSkipCurrentNode(const unsigned short c,
satok68319262010-12-03 19:38:08 +0900331 const int inputIndex, const int skipPos, const int depth) {
satok662fe692010-12-08 17:05:39 +0900332 const unsigned short userTypedChar = (mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS))[0];
satok28bd03b2010-12-03 16:39:16 +0900333 // Skip the ' or other letter and continue deeper
334 return (c == QUOTE && userTypedChar != QUOTE) || skipPos == depth;
335}
336
337inline int UnigramDictionary::getMatchedProximityId(const int *currentChars,
satok48e432c2010-12-06 17:38:58 +0900338 const unsigned short c, const int skipPos) {
339 const unsigned short lowerC = toLowerCase(c);
satok28bd03b2010-12-03 16:39:16 +0900340 int j = 0;
341 while (currentChars[j] > 0) {
satok68319262010-12-03 19:38:08 +0900342 const bool matched = (currentChars[j] == lowerC || currentChars[j] == c);
satok28bd03b2010-12-03 16:39:16 +0900343 // If skipPos is defined, not to search proximity collections.
344 // First char is what user typed.
345 if (matched) {
346 return j;
347 } else if (skipPos >= 0) {
348 return -1;
349 }
350 ++j;
351 }
352 return -1;
353}
354
satok48e432c2010-12-06 17:38:58 +0900355inline bool UnigramDictionary::processCurrentNode(const int pos, const int depth,
satokcdbbea72010-12-08 16:04:16 +0900356 const int maxDepth, const bool traverseAllNodes, const int snr, int inputIndex,
357 const int diffs, const int skipPos, const int excessivePos, int *nextLetters,
358 const int nextLettersSize, int *newCount, int *newChildPosition, bool *newTraverseAllNodes,
satok48e432c2010-12-06 17:38:58 +0900359 int *newSnr, int*newInputIndex, int *newDiffs, int *nextSiblingPosition) {
satokcdbbea72010-12-08 16:04:16 +0900360 if (DEBUG_DICT) assert(skipPos < 0 || excessivePos < 0);
satok48e432c2010-12-06 17:38:58 +0900361 unsigned short c;
362 int childPosition;
363 bool terminal;
364 int freq;
satokcdbbea72010-12-08 16:04:16 +0900365
366 if (excessivePos == depth) ++inputIndex;
367
satok48e432c2010-12-06 17:38:58 +0900368 *nextSiblingPosition = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, pos, &c,
369 &childPosition, &terminal, &freq);
370
371 const bool needsToTraverseChildrenNodes = childPosition != 0;
372
373 // If we are only doing traverseAllNodes, no need to look at the typed characters.
374 if (traverseAllNodes || needsToSkipCurrentNode(c, inputIndex, skipPos, depth)) {
375 mWord[depth] = c;
376 if (traverseAllNodes && terminal) {
377 onTerminalWhenUserTypedLengthIsGreaterThanInputLength(mWord, mInputLength, depth,
378 snr, nextLetters, nextLettersSize, skipPos, freq);
379 }
380 if (!needsToTraverseChildrenNodes) return false;
381 *newTraverseAllNodes = traverseAllNodes;
382 *newSnr = snr;
383 *newDiffs = diffs;
384 *newInputIndex = inputIndex;
satok48e432c2010-12-06 17:38:58 +0900385 } else {
satok662fe692010-12-08 17:05:39 +0900386 int *currentChars = mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS);
satok48e432c2010-12-06 17:38:58 +0900387 int matchedProximityCharId = getMatchedProximityId(currentChars, c, skipPos);
388 if (matchedProximityCharId < 0) return false;
389 mWord[depth] = c;
390 // If inputIndex is greater than mInputLength, that means there is no
391 // proximity chars. So, we don't need to check proximity.
392 const int addedWeight = matchedProximityCharId == 0 ? TYPED_LETTER_MULTIPLIER : 1;
393 const bool isSameAsUserTypedLength = mInputLength == inputIndex + 1;
394 if (isSameAsUserTypedLength && terminal) {
395 onTerminalWhenUserTypedLengthIsSameAsInputLength(mWord, depth, snr,
396 skipPos, freq, addedWeight);
397 }
398 if (!needsToTraverseChildrenNodes) return false;
399 // Start traversing all nodes after the index exceeds the user typed length
400 *newTraverseAllNodes = isSameAsUserTypedLength;
401 *newSnr = snr * addedWeight;
402 *newDiffs = diffs + (matchedProximityCharId > 0);
403 *newInputIndex = inputIndex + 1;
satok48e432c2010-12-06 17:38:58 +0900404 }
405 // Optimization: Prune out words that are too long compared to how much was typed.
satokd2997922010-12-07 13:08:39 +0900406 if (depth >= maxDepth || *newDiffs > mMaxEditDistance) {
satok48e432c2010-12-06 17:38:58 +0900407 return false;
408 }
409
410 // If inputIndex is greater than mInputLength, that means there are no proximity chars.
411 if (mInputLength <= *newInputIndex) {
412 *newTraverseAllNodes = true;
413 }
414 // get the count of nodes and increment childAddress.
415 *newCount = Dictionary::getCount(DICT, &childPosition);
416 *newChildPosition = childPosition;
417 if (DEBUG_DICT) assert(needsToTraverseChildrenNodes);
418 return needsToTraverseChildrenNodes;
419}
420
satok662fe692010-12-08 17:05:39 +0900421inline int UnigramDictionary::getWordFreq(const int startInputIndex, const int inputLength) {
422 int pos = ROOT_POS;
423 int count = Dictionary::getCount(DICT, &pos);
424 int freq = 0;
425 bool terminal = false;
426
427 for (int i = 0; i < inputLength; ++i) {
428 bool needsToTraverseChildrenNodes = processCurrentNodeForExactMatch(pos, count,
429 startInputIndex + i, &pos, &count, &terminal, &freq);
430 if (!needsToTraverseChildrenNodes && (i < inputLength - 1)) {
431 return 0;
432 }
433 }
434 if (terminal) {
435 return freq;
436 } else {
437 return 0;
438 }
439}
440
441inline bool UnigramDictionary::processCurrentNodeForExactMatch(const int firstChildPos,
442 const int count, const int inputIndex, int *newChildPosition, int *newCount,
443 bool *newTerminal, int *newFreq) {
444 const int *currentChars = mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS);
445 int pos = firstChildPos;
446 unsigned short c;
447 for (int i = 0; i < count; ++i) {
448 pos = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, pos, &c,
449 newChildPosition, newTerminal, newFreq);
450 const unsigned int inputC = currentChars[0];
451 const unsigned short lowerC = toLowerCase(c);
452 const bool matched = (inputC == lowerC || inputC == c);
453 const bool hasChild = *newChildPosition != 0;
454 if (matched) {
455 if (hasChild) {
456 *newCount = Dictionary::getCount(DICT, newChildPosition);
457 return true;
458 } else {
459 return false;
460 }
461 }
462 }
463 return false;
464}
satok30088252010-12-01 21:22:15 +0900465} // namespace latinime