blob: 7ecf1c9c0e1ed236c1df40ceecd82e773f0d9ae0 [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) {
satoka3d78f62010-12-09 22:08:33 +090039 if (DEBUG_DICT) 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);
satoka3d78f62010-12-09 22:08:33 +090048 const int MAX_DEPTH = min(mInputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH);
49 getSuggestionCandidates(codesSize, -1, -1, -1, nextLetters, nextLettersSize, MAX_DEPTH);
satok30088252010-12-01 21:22:15 +090050
satok662fe692010-12-08 17:05:39 +090051 // Suggestion with missing character
52 if (SUGGEST_WORDS_WITH_MISSING_CHARACTER) {
satok30088252010-12-01 21:22:15 +090053 for (int i = 0; i < codesSize; ++i) {
satokcdbbea72010-12-08 16:04:16 +090054 if (DEBUG_DICT) LOGI("--- Suggest missing characters %d", i);
satoka3d78f62010-12-09 22:08:33 +090055 getSuggestionCandidates(codesSize, i, -1, -1, NULL, 0, MAX_DEPTH);
satokcdbbea72010-12-08 16:04:16 +090056 }
57 }
58
satok662fe692010-12-08 17:05:39 +090059 // Suggestion with excessive character
satoka3d78f62010-12-09 22:08:33 +090060 if (SUGGEST_WORDS_WITH_EXCESSIVE_CHARACTER && mInputLength > MIN_SUGGEST_DEPTH) {
satokcdbbea72010-12-08 16:04:16 +090061 for (int i = 0; i < codesSize; ++i) {
satoke07baa62010-12-09 21:55:40 +090062 if (existsAdjacentProximityChars(i, codesSize)) {
63 if (DEBUG_DICT) LOGI("--- Suggest excessive characters %d", i);
satoka3d78f62010-12-09 22:08:33 +090064 getSuggestionCandidates(codesSize, -1, i, -1, NULL, 0, MAX_DEPTH);
satoke07baa62010-12-09 21:55:40 +090065 }
satok30088252010-12-01 21:22:15 +090066 }
67 }
68
satoka3d78f62010-12-09 22:08:33 +090069 // Suggestion with transposed characters
70 // Only suggest words that length is mInputLength
71 if (SUGGEST_WORDS_WITH_TRANSPOSED_CHARACTERS) {
72 for (int i = 0; i < codesSize; ++i) {
73 if (DEBUG_DICT) LOGI("--- Suggest transposed characters %d", i);
74 getSuggestionCandidates(codesSize, -1, -1, i, NULL, 0, mInputLength - 1);
75 }
76 }
77
satok662fe692010-12-08 17:05:39 +090078 // Suggestions with missing space
satokaee09dc2010-12-09 19:21:51 +090079 if (SUGGEST_WORDS_WITH_MISSING_SPACE_CHARACTER && mInputLength > MIN_SUGGEST_DEPTH) {
satok662fe692010-12-08 17:05:39 +090080 for (int i = 1; i < codesSize; ++i) {
81 if (DEBUG_DICT) LOGI("--- Suggest missing space characters %d", i);
82 getMissingSpaceWords(mInputLength, i);
83 }
84 }
85
86 // Get the word count
87 int suggestedWordsCount = 0;
88 while (suggestedWordsCount < MAX_WORDS && mFrequencies[suggestedWordsCount] > 0) {
89 suggestedWordsCount++;
90 }
91
satok30088252010-12-01 21:22:15 +090092 if (DEBUG_DICT) {
93 LOGI("Returning %d words", suggestedWordsCount);
94 LOGI("Next letters: ");
95 for (int k = 0; k < nextLettersSize; k++) {
96 if (nextLetters[k] > 0) {
97 LOGI("%c = %d,", k, nextLetters[k]);
98 }
99 }
100 LOGI("\n");
101 }
satok662fe692010-12-08 17:05:39 +0900102
satok30088252010-12-01 21:22:15 +0900103 return suggestedWordsCount;
104}
105
106void UnigramDictionary::initSuggestions(int *codes, int codesSize, unsigned short *outWords,
107 int *frequencies) {
satokf5cded12010-12-06 21:28:24 +0900108 if (DEBUG_DICT) LOGI("initSuggest");
satok30088252010-12-01 21:22:15 +0900109 mFrequencies = frequencies;
110 mOutputChars = outWords;
111 mInputCodes = codes;
112 mInputLength = codesSize;
113 mMaxEditDistance = mInputLength < 5 ? 2 : mInputLength / 2;
114}
115
satok715514d2010-12-02 20:19:59 +0900116void UnigramDictionary::registerNextLetter(
117 unsigned short c, int *nextLetters, int nextLettersSize) {
satok30088252010-12-01 21:22:15 +0900118 if (c < nextLettersSize) {
119 nextLetters[c]++;
120 }
121}
122
satok662fe692010-12-08 17:05:39 +0900123// TODO: We need to optimize addWord by using STL or something
satok28bd03b2010-12-03 16:39:16 +0900124bool UnigramDictionary::addWord(unsigned short *word, int length, int frequency) {
satok30088252010-12-01 21:22:15 +0900125 word[length] = 0;
satok662fe692010-12-08 17:05:39 +0900126 if (DEBUG_DICT && DEBUG_SHOW_FOUND_WORD) {
satok30088252010-12-01 21:22:15 +0900127 char s[length + 1];
128 for (int i = 0; i <= length; i++) s[i] = word[i];
satok662fe692010-12-08 17:05:39 +0900129 LOGI("Found word = %s, freq = %d", s, frequency);
satok30088252010-12-01 21:22:15 +0900130 }
satokf5cded12010-12-06 21:28:24 +0900131 if (length > MAX_WORD_LENGTH) {
132 if (DEBUG_DICT) LOGI("Exceeded max word length.");
133 return false;
134 }
satok30088252010-12-01 21:22:15 +0900135
136 // Find the right insertion point
137 int insertAt = 0;
138 while (insertAt < MAX_WORDS) {
satok715514d2010-12-02 20:19:59 +0900139 if (frequency > mFrequencies[insertAt] || (mFrequencies[insertAt] == frequency
140 && length < Dictionary::wideStrLen(mOutputChars + insertAt * MAX_WORD_LENGTH))) {
satok30088252010-12-01 21:22:15 +0900141 break;
142 }
143 insertAt++;
144 }
145 if (insertAt < MAX_WORDS) {
satokcdbbea72010-12-08 16:04:16 +0900146 if (DEBUG_DICT) {
147 char s[length + 1];
148 for (int i = 0; i <= length; i++) s[i] = word[i];
satok662fe692010-12-08 17:05:39 +0900149 LOGI("Added word = %s, freq = %d", s, frequency);
satokcdbbea72010-12-08 16:04:16 +0900150 }
satok30088252010-12-01 21:22:15 +0900151 memmove((char*) mFrequencies + (insertAt + 1) * sizeof(mFrequencies[0]),
152 (char*) mFrequencies + insertAt * sizeof(mFrequencies[0]),
153 (MAX_WORDS - insertAt - 1) * sizeof(mFrequencies[0]));
154 mFrequencies[insertAt] = frequency;
155 memmove((char*) mOutputChars + (insertAt + 1) * MAX_WORD_LENGTH * sizeof(short),
satok715514d2010-12-02 20:19:59 +0900156 (char*) mOutputChars + insertAt * MAX_WORD_LENGTH * sizeof(short),
satok30088252010-12-01 21:22:15 +0900157 (MAX_WORDS - insertAt - 1) * sizeof(short) * MAX_WORD_LENGTH);
satok715514d2010-12-02 20:19:59 +0900158 unsigned short *dest = mOutputChars + insertAt * MAX_WORD_LENGTH;
satok30088252010-12-01 21:22:15 +0900159 while (length--) {
160 *dest++ = *word++;
161 }
162 *dest = 0; // NULL terminate
satok662fe692010-12-08 17:05:39 +0900163 if (DEBUG_DICT) LOGI("Added word at %d", insertAt);
satok30088252010-12-01 21:22:15 +0900164 return true;
165 }
166 return false;
167}
168
satok28bd03b2010-12-03 16:39:16 +0900169unsigned short UnigramDictionary::toLowerCase(unsigned short c) {
satok30088252010-12-01 21:22:15 +0900170 if (c < sizeof(BASE_CHARS) / sizeof(BASE_CHARS[0])) {
171 c = BASE_CHARS[c];
172 }
173 if (c >='A' && c <= 'Z') {
174 c |= 32;
175 } else if (c > 127) {
176 c = latin_tolower(c);
177 }
178 return c;
179}
180
satok28bd03b2010-12-03 16:39:16 +0900181bool UnigramDictionary::sameAsTyped(unsigned short *word, int length) {
satok30088252010-12-01 21:22:15 +0900182 if (length != mInputLength) {
183 return false;
184 }
185 int *inputCodes = mInputCodes;
186 while (length--) {
187 if ((unsigned int) *inputCodes != (unsigned int) *word) {
188 return false;
189 }
satok662fe692010-12-08 17:05:39 +0900190 inputCodes += MAX_PROXIMITY_CHARS;
satok30088252010-12-01 21:22:15 +0900191 word++;
192 }
193 return true;
194}
195
satok715514d2010-12-02 20:19:59 +0900196static const char QUOTE = '\'';
satok662fe692010-12-08 17:05:39 +0900197static const char SPACE = ' ';
satok30088252010-12-01 21:22:15 +0900198
satok662fe692010-12-08 17:05:39 +0900199void UnigramDictionary::getSuggestionCandidates(const int inputLength, const int skipPos,
satoka3d78f62010-12-09 22:08:33 +0900200 const int excessivePos, const int transposedPos, int *nextLetters,
201 const int nextLettersSize, const int maxDepth) {
202 if (DEBUG_DICT) LOGI("getSuggestionCandidates %d", maxDepth);
203 if (DEBUG_DICT) assert(transposedPos + 1 < inputLength);
204 if (DEBUG_DICT) assert(excessivePos < inputLength);
205 if (DEBUG_DICT) assert(missingPos < inputLength);
satok662fe692010-12-08 17:05:39 +0900206 int rootPosition = ROOT_POS;
satokd2997922010-12-07 13:08:39 +0900207 // Get the number of child of root, then increment the position
208 int childCount = Dictionary::getCount(DICT, &rootPosition);
209 int depth = 0;
210
211 mStackChildCount[0] = childCount;
212 mStackTraverseAll[0] = (mInputLength <= 0);
213 mStackNodeFreq[0] = 1;
214 mStackInputIndex[0] = 0;
215 mStackDiffs[0] = 0;
216 mStackSiblingPos[0] = rootPosition;
217
satok662fe692010-12-08 17:05:39 +0900218 // Depth first search
satokd2997922010-12-07 13:08:39 +0900219 while (depth >= 0) {
220 if (mStackChildCount[depth] > 0) {
221 --mStackChildCount[depth];
222 bool traverseAllNodes = mStackTraverseAll[depth];
223 int snr = mStackNodeFreq[depth];
224 int inputIndex = mStackInputIndex[depth];
225 int diffs = mStackDiffs[depth];
226 int siblingPos = mStackSiblingPos[depth];
227 int firstChildPos;
satoka3d78f62010-12-09 22:08:33 +0900228 // depth will never be greater than maxDepth because in that case,
satokd2997922010-12-07 13:08:39 +0900229 // needsToTraverseChildrenNodes should be false
230 const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth,
satoka3d78f62010-12-09 22:08:33 +0900231 maxDepth, traverseAllNodes, snr, inputIndex, diffs, skipPos, excessivePos,
232 transposedPos, nextLetters, nextLettersSize, &childCount, &firstChildPos,
233 &traverseAllNodes, &snr, &inputIndex, &diffs, &siblingPos);
satok662fe692010-12-08 17:05:39 +0900234 // Update next sibling pos
satokd2997922010-12-07 13:08:39 +0900235 mStackSiblingPos[depth] = siblingPos;
236 if (needsToTraverseChildrenNodes) {
237 // Goes to child node
238 ++depth;
239 mStackChildCount[depth] = childCount;
240 mStackTraverseAll[depth] = traverseAllNodes;
241 mStackNodeFreq[depth] = snr;
242 mStackInputIndex[depth] = inputIndex;
243 mStackDiffs[depth] = diffs;
244 mStackSiblingPos[depth] = firstChildPos;
245 }
246 } else {
satokcdbbea72010-12-08 16:04:16 +0900247 // Goes to parent sibling node
satokd2997922010-12-07 13:08:39 +0900248 --depth;
249 }
250 }
251}
252
satok662fe692010-12-08 17:05:39 +0900253bool UnigramDictionary::getMissingSpaceWords(const int inputLength, const int missingSpacePos) {
satokaee09dc2010-12-09 19:21:51 +0900254 if (missingSpacePos <= 0 || missingSpacePos >= inputLength
255 || inputLength >= MAX_WORD_LENGTH) return false;
satok662fe692010-12-08 17:05:39 +0900256 const int newWordLength = inputLength + 1;
257 // Allocating variable length array on stack
258 unsigned short word[newWordLength];
satokaee09dc2010-12-09 19:21:51 +0900259 const int firstFreq = getBestWordFreq(0, missingSpacePos, mWord);
260 if (DEBUG_DICT) LOGI("First freq: %d", firstFreq);
261 if (firstFreq <= 0) return false;
262
satok662fe692010-12-08 17:05:39 +0900263 for (int i = 0; i < missingSpacePos; ++i) {
satokaee09dc2010-12-09 19:21:51 +0900264 word[i] = mWord[i];
satok662fe692010-12-08 17:05:39 +0900265 }
satokaee09dc2010-12-09 19:21:51 +0900266
267 const int secondFreq = getBestWordFreq(missingSpacePos, inputLength - missingSpacePos, mWord);
satoka3d78f62010-12-09 22:08:33 +0900268 if (DEBUG_DICT) LOGI("Second freq: %d", secondFreq);
satokaee09dc2010-12-09 19:21:51 +0900269 if (secondFreq <= 0) return false;
270
satok662fe692010-12-08 17:05:39 +0900271 word[missingSpacePos] = SPACE;
272 for (int i = (missingSpacePos + 1); i < newWordLength; ++i) {
satokaee09dc2010-12-09 19:21:51 +0900273 word[i] = mWord[i - missingSpacePos - 1];
satok662fe692010-12-08 17:05:39 +0900274 }
satokaee09dc2010-12-09 19:21:51 +0900275
276 int pairFreq = ((firstFreq + secondFreq) / 2);
277 for (int i = 0; i < inputLength; ++i) pairFreq *= TYPED_LETTER_MULTIPLIER;
satoka3d78f62010-12-09 22:08:33 +0900278 pairFreq = pairFreq * WORDS_WITH_MISSING_SPACE_CHARACTER_DEMOTION_RATE / 100;
satok662fe692010-12-08 17:05:39 +0900279 addWord(word, newWordLength, pairFreq);
280 return true;
281}
282
283// Keep this for comparing spec to new getWords
284void UnigramDictionary::getWordsOld(const int initialPos, const int inputLength, const int skipPos,
satoka3d78f62010-12-09 22:08:33 +0900285 const int excessivePos, const int transposedPos,int *nextLetters,
286 const int nextLettersSize) {
satok662fe692010-12-08 17:05:39 +0900287 int initialPosition = initialPos;
288 const int count = Dictionary::getCount(DICT, &initialPosition);
289 getWordsRec(count, initialPosition, 0,
290 min(inputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH),
satoka3d78f62010-12-09 22:08:33 +0900291 mInputLength <= 0, 1, 0, 0, skipPos, excessivePos, transposedPos, nextLetters,
292 nextLettersSize);
satok662fe692010-12-08 17:05:39 +0900293}
294
satok68319262010-12-03 19:38:08 +0900295void UnigramDictionary::getWordsRec(const int childrenCount, const int pos, const int depth,
296 const int maxDepth, const bool traverseAllNodes, const int snr, const int inputIndex,
satoka3d78f62010-12-09 22:08:33 +0900297 const int diffs, const int skipPos, const int excessivePos, const int transposedPos,
298 int *nextLetters, const int nextLettersSize) {
satok48e432c2010-12-06 17:38:58 +0900299 int siblingPos = pos;
satok68319262010-12-03 19:38:08 +0900300 for (int i = 0; i < childrenCount; ++i) {
satok48e432c2010-12-06 17:38:58 +0900301 int newCount;
302 int newChildPosition;
satokd2997922010-12-07 13:08:39 +0900303 const int newDepth = depth + 1;
satok48e432c2010-12-06 17:38:58 +0900304 bool newTraverseAllNodes;
305 int newSnr;
306 int newInputIndex;
307 int newDiffs;
308 int newSiblingPos;
309 const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth, maxDepth,
satoka3d78f62010-12-09 22:08:33 +0900310 traverseAllNodes, snr, inputIndex, diffs, skipPos, excessivePos, transposedPos,
311 nextLetters, nextLettersSize,
satokd2997922010-12-07 13:08:39 +0900312 &newCount, &newChildPosition, &newTraverseAllNodes, &newSnr,
satok48e432c2010-12-06 17:38:58 +0900313 &newInputIndex, &newDiffs, &newSiblingPos);
314 siblingPos = newSiblingPos;
satok30088252010-12-01 21:22:15 +0900315
satok48e432c2010-12-06 17:38:58 +0900316 if (needsToTraverseChildrenNodes) {
317 getWordsRec(newCount, newChildPosition, newDepth, maxDepth, newTraverseAllNodes,
satoka3d78f62010-12-09 22:08:33 +0900318 newSnr, newInputIndex, newDiffs, skipPos, excessivePos, transposedPos,
319 nextLetters, nextLettersSize);
satok30088252010-12-01 21:22:15 +0900320 }
321 }
322}
323
satok715514d2010-12-02 20:19:59 +0900324inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsGreaterThanInputLength(
325 unsigned short *word, const int inputLength, const int depth, const int snr,
satoka3d78f62010-12-09 22:08:33 +0900326 int *nextLetters, const int nextLettersSize, const int skipPos, const int excessivePos,
327 const int transposedPos, const int freq) {
328 int finalFreq = freq * snr;
329 // TODO: Demote by edit distance
330 if (skipPos >= 0) finalFreq = finalFreq * WORDS_WITH_MISSING_CHARACTER_DEMOTION_RATE / 100;
331 if (excessivePos >= 0) finalFreq = finalFreq
332 * WORDS_WITH_EXCESSIVE_CHARACTER_DEMOTION_RATE / 100;
333 if (transposedPos >= 0) finalFreq = finalFreq
334 * WORDS_WITH_TRANSPOSED_CHARACTERS_DEMOTION_RATE / 100;
335
336 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq);
satok715514d2010-12-02 20:19:59 +0900337 if (depth >= inputLength && skipPos < 0) {
338 registerNextLetter(mWord[mInputLength], nextLetters, nextLettersSize);
339 }
340}
341
342inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsSameAsInputLength(
satoka3d78f62010-12-09 22:08:33 +0900343 unsigned short *word, const int depth, const int snr, const int skipPos,
344 const int excessivePos, const int transposedPos, const int freq, const int addedWeight) {
satok715514d2010-12-02 20:19:59 +0900345 if (!sameAsTyped(word, depth + 1)) {
346 int finalFreq = freq * snr * addedWeight;
satoka3d78f62010-12-09 22:08:33 +0900347 // TODO: Demote by edit distance
348 if (skipPos >= 0) finalFreq = finalFreq * WORDS_WITH_MISSING_CHARACTER_DEMOTION_RATE / 100;
349 if (excessivePos >= 0) finalFreq = finalFreq
350 * WORDS_WITH_EXCESSIVE_CHARACTER_DEMOTION_RATE / 100;
351 if (transposedPos >= 0) finalFreq = finalFreq
352 * WORDS_WITH_TRANSPOSED_CHARACTERS_DEMOTION_RATE / 100;
353
satok715514d2010-12-02 20:19:59 +0900354 // Proximity collection will promote a word of the same length as
355 // what user typed.
356 if (skipPos < 0) finalFreq *= FULL_WORD_MULTIPLIER;
satok662fe692010-12-08 17:05:39 +0900357 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq);
satok715514d2010-12-02 20:19:59 +0900358 }
359}
satok28bd03b2010-12-03 16:39:16 +0900360
361inline bool UnigramDictionary::needsToSkipCurrentNode(const unsigned short c,
satok68319262010-12-03 19:38:08 +0900362 const int inputIndex, const int skipPos, const int depth) {
satok662fe692010-12-08 17:05:39 +0900363 const unsigned short userTypedChar = (mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS))[0];
satok28bd03b2010-12-03 16:39:16 +0900364 // Skip the ' or other letter and continue deeper
365 return (c == QUOTE && userTypedChar != QUOTE) || skipPos == depth;
366}
367
satoke07baa62010-12-09 21:55:40 +0900368inline bool UnigramDictionary::existsAdjacentProximityChars(const int inputIndex,
369 const int inputLength) {
370 if (inputIndex < 0 || inputIndex >= inputLength) return false;
371 const int currentChar = *getInputCharsAt(inputIndex);
372 const int leftIndex = inputIndex - 1;
373 if (leftIndex >= 0) {
374 int *leftChars = getInputCharsAt(leftIndex);
375 int i = 0;
376 while (leftChars[i] > 0 && i < MAX_PROXIMITY_CHARS) {
377 if (leftChars[i++] == currentChar) return true;
378 }
379 }
380 const int rightIndex = inputIndex + 1;
381 if (rightIndex < inputLength) {
382 int *rightChars = getInputCharsAt(rightIndex);
383 int i = 0;
384 while (rightChars[i] > 0 && i < MAX_PROXIMITY_CHARS) {
385 if (rightChars[i++] == currentChar) return true;
386 }
387 }
388 return false;
389}
390
satok28bd03b2010-12-03 16:39:16 +0900391inline int UnigramDictionary::getMatchedProximityId(const int *currentChars,
satoka3d78f62010-12-09 22:08:33 +0900392 const unsigned short c, const int skipPos, const int excessivePos,
393 const int transposedPos) {
satok48e432c2010-12-06 17:38:58 +0900394 const unsigned short lowerC = toLowerCase(c);
satok28bd03b2010-12-03 16:39:16 +0900395 int j = 0;
satoke07baa62010-12-09 21:55:40 +0900396 while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS) {
satok68319262010-12-03 19:38:08 +0900397 const bool matched = (currentChars[j] == lowerC || currentChars[j] == c);
satok28bd03b2010-12-03 16:39:16 +0900398 // If skipPos is defined, not to search proximity collections.
satoka3d78f62010-12-09 22:08:33 +0900399 // First char is what user typed.
satok28bd03b2010-12-03 16:39:16 +0900400 if (matched) {
401 return j;
satoka3d78f62010-12-09 22:08:33 +0900402 } else if (skipPos >= 0 || excessivePos >= 0 || transposedPos >= 0) {
403 // Not to check proximity characters
satok28bd03b2010-12-03 16:39:16 +0900404 return -1;
405 }
406 ++j;
407 }
408 return -1;
409}
410
satok48e432c2010-12-06 17:38:58 +0900411inline bool UnigramDictionary::processCurrentNode(const int pos, const int depth,
satokcdbbea72010-12-08 16:04:16 +0900412 const int maxDepth, const bool traverseAllNodes, const int snr, int inputIndex,
satoka3d78f62010-12-09 22:08:33 +0900413 const int diffs, const int skipPos, const int excessivePos, const int transposedPos,
414 int *nextLetters, const int nextLettersSize, int *newCount, int *newChildPosition,
415 bool *newTraverseAllNodes, int *newSnr, int*newInputIndex, int *newDiffs,
416 int *nextSiblingPosition) {
417 if (DEBUG_DICT) {
418 int inputCount = 0;
419 if (skipPos >= 0) ++inputCount;
420 if (excessivePos >= 0) ++inputCount;
421 if (transposedPos >= 0) ++inputCount;
422 assert(inputCount <= 1);
423 }
satok48e432c2010-12-06 17:38:58 +0900424 unsigned short c;
425 int childPosition;
426 bool terminal;
427 int freq;
satokcdbbea72010-12-08 16:04:16 +0900428
429 if (excessivePos == depth) ++inputIndex;
430
satok48e432c2010-12-06 17:38:58 +0900431 *nextSiblingPosition = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, pos, &c,
432 &childPosition, &terminal, &freq);
433
434 const bool needsToTraverseChildrenNodes = childPosition != 0;
435
436 // If we are only doing traverseAllNodes, no need to look at the typed characters.
437 if (traverseAllNodes || needsToSkipCurrentNode(c, inputIndex, skipPos, depth)) {
438 mWord[depth] = c;
439 if (traverseAllNodes && terminal) {
440 onTerminalWhenUserTypedLengthIsGreaterThanInputLength(mWord, mInputLength, depth,
satoka3d78f62010-12-09 22:08:33 +0900441 snr, nextLetters, nextLettersSize, skipPos, excessivePos, transposedPos, freq);
satok48e432c2010-12-06 17:38:58 +0900442 }
443 if (!needsToTraverseChildrenNodes) return false;
444 *newTraverseAllNodes = traverseAllNodes;
445 *newSnr = snr;
446 *newDiffs = diffs;
447 *newInputIndex = inputIndex;
satok48e432c2010-12-06 17:38:58 +0900448 } else {
satok662fe692010-12-08 17:05:39 +0900449 int *currentChars = mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS);
satoka3d78f62010-12-09 22:08:33 +0900450
451 if (transposedPos >= 0) {
452 if (inputIndex == transposedPos) currentChars += MAX_PROXIMITY_CHARS;
453 if (inputIndex == (transposedPos + 1)) currentChars -= MAX_PROXIMITY_CHARS;
454 }
455
456 int matchedProximityCharId = getMatchedProximityId(currentChars, c, skipPos, excessivePos,
457 transposedPos);
satok48e432c2010-12-06 17:38:58 +0900458 if (matchedProximityCharId < 0) return false;
459 mWord[depth] = c;
460 // If inputIndex is greater than mInputLength, that means there is no
461 // proximity chars. So, we don't need to check proximity.
462 const int addedWeight = matchedProximityCharId == 0 ? TYPED_LETTER_MULTIPLIER : 1;
463 const bool isSameAsUserTypedLength = mInputLength == inputIndex + 1;
464 if (isSameAsUserTypedLength && terminal) {
465 onTerminalWhenUserTypedLengthIsSameAsInputLength(mWord, depth, snr,
satoka3d78f62010-12-09 22:08:33 +0900466 skipPos, excessivePos, transposedPos, freq, addedWeight);
satok48e432c2010-12-06 17:38:58 +0900467 }
468 if (!needsToTraverseChildrenNodes) return false;
469 // Start traversing all nodes after the index exceeds the user typed length
470 *newTraverseAllNodes = isSameAsUserTypedLength;
471 *newSnr = snr * addedWeight;
satoka3d78f62010-12-09 22:08:33 +0900472 *newDiffs = diffs + ((matchedProximityCharId > 0) ? 1 : 0);
satok48e432c2010-12-06 17:38:58 +0900473 *newInputIndex = inputIndex + 1;
satok48e432c2010-12-06 17:38:58 +0900474 }
475 // Optimization: Prune out words that are too long compared to how much was typed.
satokd2997922010-12-07 13:08:39 +0900476 if (depth >= maxDepth || *newDiffs > mMaxEditDistance) {
satok48e432c2010-12-06 17:38:58 +0900477 return false;
478 }
479
480 // If inputIndex is greater than mInputLength, that means there are no proximity chars.
481 if (mInputLength <= *newInputIndex) {
482 *newTraverseAllNodes = true;
483 }
484 // get the count of nodes and increment childAddress.
485 *newCount = Dictionary::getCount(DICT, &childPosition);
486 *newChildPosition = childPosition;
487 if (DEBUG_DICT) assert(needsToTraverseChildrenNodes);
488 return needsToTraverseChildrenNodes;
489}
490
satokaee09dc2010-12-09 19:21:51 +0900491inline int UnigramDictionary::getBestWordFreq(const int startInputIndex, const int inputLength,
492 unsigned short *word) {
satok662fe692010-12-08 17:05:39 +0900493 int pos = ROOT_POS;
494 int count = Dictionary::getCount(DICT, &pos);
satokaee09dc2010-12-09 19:21:51 +0900495 int maxFreq = 0;
496 int depth = 0;
497 unsigned short newWord[MAX_WORD_LENGTH_INTERNAL];
satok662fe692010-12-08 17:05:39 +0900498 bool terminal = false;
499
satokaee09dc2010-12-09 19:21:51 +0900500 mStackChildCount[0] = count;
501 mStackSiblingPos[0] = pos;
502
503 while (depth >= 0) {
504 if (mStackChildCount[depth] > 0) {
505 --mStackChildCount[depth];
506 int firstChildPos;
507 int newFreq;
508 int siblingPos = mStackSiblingPos[depth];
509 const bool needsToTraverseChildrenNodes = processCurrentNodeForExactMatch(siblingPos,
510 startInputIndex, depth, newWord, &firstChildPos, &count, &terminal, &newFreq,
511 &siblingPos);
512 mStackSiblingPos[depth] = siblingPos;
513 if (depth == (inputLength - 1)) {
514 // Traverse sibling node
515 if (terminal) {
516 if (newFreq > maxFreq) {
517 for (int i = 0; i < inputLength; ++i) word[i] = newWord[i];
518 if (DEBUG_DICT && DEBUG_NODE) {
519 char s[inputLength + 1];
520 for (int i = 0; i < inputLength; ++i) s[i] = word[i];
521 s[inputLength] = 0;
522 LOGI("New missing space word found: %d > %d (%s), %d, %d",
523 newFreq, maxFreq, s, inputLength, depth);
524 }
525 maxFreq = newFreq;
526 }
527 }
528 } else if (needsToTraverseChildrenNodes) {
529 // Traverse children nodes
530 ++depth;
531 mStackChildCount[depth] = count;
532 mStackSiblingPos[depth] = firstChildPos;
533 }
534 } else {
535 // Traverse parent node
536 --depth;
satok662fe692010-12-08 17:05:39 +0900537 }
538 }
satokaee09dc2010-12-09 19:21:51 +0900539
540 word[inputLength] = 0;
541 return maxFreq;
satok662fe692010-12-08 17:05:39 +0900542}
543
544inline bool UnigramDictionary::processCurrentNodeForExactMatch(const int firstChildPos,
satokaee09dc2010-12-09 19:21:51 +0900545 const int startInputIndex, const int depth, unsigned short *word, int *newChildPosition,
546 int *newCount, bool *newTerminal, int *newFreq, int *siblingPos) {
547 const int inputIndex = startInputIndex + depth;
satok662fe692010-12-08 17:05:39 +0900548 const int *currentChars = mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS);
satok662fe692010-12-08 17:05:39 +0900549 unsigned short c;
satokaee09dc2010-12-09 19:21:51 +0900550 *siblingPos = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, firstChildPos, &c,
551 newChildPosition, newTerminal, newFreq);
552 const unsigned int inputC = currentChars[0];
553 if (DEBUG_DICT) assert(inputC <= U_SHORT_MAX);
554 const unsigned short lowerC = toLowerCase(c);
555 const bool matched = (inputC == lowerC || inputC == c);
556 const bool hasChild = *newChildPosition != 0;
557 if (matched) {
558 word[depth] = c;
559 if (DEBUG_DICT && DEBUG_NODE) {
560 LOGI("Node(%c, %c)<%d>, %d, %d", inputC, c, matched, hasChild, *newFreq);
561 if (*newTerminal) LOGI("Terminal %d", *newFreq);
satok662fe692010-12-08 17:05:39 +0900562 }
satokaee09dc2010-12-09 19:21:51 +0900563 if (hasChild) {
564 *newCount = Dictionary::getCount(DICT, newChildPosition);
565 return true;
566 } else {
567 return false;
568 }
569 } else {
570 // If this node is not user typed character, this method treats this word as unmatched.
571 // Thus newTerminal shouldn't be true.
572 *newTerminal = false;
573 return false;
satok662fe692010-12-08 17:05:39 +0900574 }
satok662fe692010-12-08 17:05:39 +0900575}
satok30088252010-12-01 21:22:15 +0900576} // namespace latinime