blob: 5e1212a5d70e179d637c0b3ac57aba9e47b51d96 [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,
satok61e2f852011-01-05 14:13:07 +090045 int *frequencies, int *nextLetters, int nextLettersSize) {
46 PROF_OPEN;
47 PROF_START(0);
satok30088252010-12-01 21:22:15 +090048 initSuggestions(codes, codesSize, outWords, frequencies);
satok54fe9e02010-12-13 14:42:35 +090049 if (DEBUG_DICT) assert(codesSize == mInputLength);
50
satoka3d78f62010-12-09 22:08:33 +090051 const int MAX_DEPTH = min(mInputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH);
satok61e2f852011-01-05 14:13:07 +090052 PROF_END(0);
satok30088252010-12-01 21:22:15 +090053
satok61e2f852011-01-05 14:13:07 +090054 PROF_START(1);
55 getSuggestionCandidates(-1, -1, -1, nextLetters, nextLettersSize, MAX_DEPTH);
56 PROF_END(1);
57
58 PROF_START(2);
satok662fe692010-12-08 17:05:39 +090059 // Suggestion with missing character
60 if (SUGGEST_WORDS_WITH_MISSING_CHARACTER) {
satok30088252010-12-01 21:22:15 +090061 for (int i = 0; i < codesSize; ++i) {
satokcdbbea72010-12-08 16:04:16 +090062 if (DEBUG_DICT) LOGI("--- Suggest missing characters %d", i);
satok54fe9e02010-12-13 14:42:35 +090063 getSuggestionCandidates(i, -1, -1, NULL, 0, MAX_DEPTH);
satokcdbbea72010-12-08 16:04:16 +090064 }
65 }
satok61e2f852011-01-05 14:13:07 +090066 PROF_END(2);
satokcdbbea72010-12-08 16:04:16 +090067
satok61e2f852011-01-05 14:13:07 +090068 PROF_START(3);
satok662fe692010-12-08 17:05:39 +090069 // Suggestion with excessive character
satok54fe9e02010-12-13 14:42:35 +090070 if (SUGGEST_WORDS_WITH_EXCESSIVE_CHARACTER
71 && mInputLength >= MIN_USER_TYPED_LENGTH_FOR_EXCESSIVE_CHARACTER_SUGGESTION) {
satokcdbbea72010-12-08 16:04:16 +090072 for (int i = 0; i < codesSize; ++i) {
satok54fe9e02010-12-13 14:42:35 +090073 if (DEBUG_DICT) LOGI("--- Suggest excessive characters %d", i);
74 getSuggestionCandidates(-1, i, -1, NULL, 0, MAX_DEPTH);
satok30088252010-12-01 21:22:15 +090075 }
76 }
satok61e2f852011-01-05 14:13:07 +090077 PROF_END(3);
satok30088252010-12-01 21:22:15 +090078
satok61e2f852011-01-05 14:13:07 +090079 PROF_START(4);
satoka3d78f62010-12-09 22:08:33 +090080 // Suggestion with transposed characters
81 // Only suggest words that length is mInputLength
82 if (SUGGEST_WORDS_WITH_TRANSPOSED_CHARACTERS) {
83 for (int i = 0; i < codesSize; ++i) {
84 if (DEBUG_DICT) LOGI("--- Suggest transposed characters %d", i);
satok54fe9e02010-12-13 14:42:35 +090085 getSuggestionCandidates(-1, -1, i, NULL, 0, mInputLength - 1);
satoka3d78f62010-12-09 22:08:33 +090086 }
87 }
satok61e2f852011-01-05 14:13:07 +090088 PROF_END(4);
satoka3d78f62010-12-09 22:08:33 +090089
satok61e2f852011-01-05 14:13:07 +090090 PROF_START(5);
satok662fe692010-12-08 17:05:39 +090091 // Suggestions with missing space
satok54fe9e02010-12-13 14:42:35 +090092 if (SUGGEST_WORDS_WITH_MISSING_SPACE_CHARACTER
93 && mInputLength >= MIN_USER_TYPED_LENGTH_FOR_MISSING_SPACE_SUGGESTION) {
satok662fe692010-12-08 17:05:39 +090094 for (int i = 1; i < codesSize; ++i) {
95 if (DEBUG_DICT) LOGI("--- Suggest missing space characters %d", i);
96 getMissingSpaceWords(mInputLength, i);
97 }
98 }
satok61e2f852011-01-05 14:13:07 +090099 PROF_END(5);
satok662fe692010-12-08 17:05:39 +0900100
satok61e2f852011-01-05 14:13:07 +0900101 PROF_START(6);
satok662fe692010-12-08 17:05:39 +0900102 // Get the word count
103 int suggestedWordsCount = 0;
104 while (suggestedWordsCount < MAX_WORDS && mFrequencies[suggestedWordsCount] > 0) {
105 suggestedWordsCount++;
106 }
107
satok30088252010-12-01 21:22:15 +0900108 if (DEBUG_DICT) {
109 LOGI("Returning %d words", suggestedWordsCount);
110 LOGI("Next letters: ");
111 for (int k = 0; k < nextLettersSize; k++) {
112 if (nextLetters[k] > 0) {
113 LOGI("%c = %d,", k, nextLetters[k]);
114 }
115 }
116 LOGI("\n");
117 }
satok61e2f852011-01-05 14:13:07 +0900118 PROF_END(6);
119 PROF_CLOSE;
satok30088252010-12-01 21:22:15 +0900120 return suggestedWordsCount;
121}
122
123void UnigramDictionary::initSuggestions(int *codes, int codesSize, unsigned short *outWords,
124 int *frequencies) {
satokf5cded12010-12-06 21:28:24 +0900125 if (DEBUG_DICT) LOGI("initSuggest");
satok30088252010-12-01 21:22:15 +0900126 mFrequencies = frequencies;
127 mOutputChars = outWords;
128 mInputCodes = codes;
129 mInputLength = codesSize;
130 mMaxEditDistance = mInputLength < 5 ? 2 : mInputLength / 2;
131}
132
satok715514d2010-12-02 20:19:59 +0900133void UnigramDictionary::registerNextLetter(
134 unsigned short c, int *nextLetters, int nextLettersSize) {
satok30088252010-12-01 21:22:15 +0900135 if (c < nextLettersSize) {
136 nextLetters[c]++;
137 }
138}
139
satok662fe692010-12-08 17:05:39 +0900140// TODO: We need to optimize addWord by using STL or something
satok28bd03b2010-12-03 16:39:16 +0900141bool UnigramDictionary::addWord(unsigned short *word, int length, int frequency) {
satok30088252010-12-01 21:22:15 +0900142 word[length] = 0;
satok662fe692010-12-08 17:05:39 +0900143 if (DEBUG_DICT && DEBUG_SHOW_FOUND_WORD) {
satok30088252010-12-01 21:22:15 +0900144 char s[length + 1];
145 for (int i = 0; i <= length; i++) s[i] = word[i];
satok662fe692010-12-08 17:05:39 +0900146 LOGI("Found word = %s, freq = %d", s, frequency);
satok30088252010-12-01 21:22:15 +0900147 }
satokf5cded12010-12-06 21:28:24 +0900148 if (length > MAX_WORD_LENGTH) {
149 if (DEBUG_DICT) LOGI("Exceeded max word length.");
150 return false;
151 }
satok30088252010-12-01 21:22:15 +0900152
153 // Find the right insertion point
154 int insertAt = 0;
155 while (insertAt < MAX_WORDS) {
satok715514d2010-12-02 20:19:59 +0900156 if (frequency > mFrequencies[insertAt] || (mFrequencies[insertAt] == frequency
157 && length < Dictionary::wideStrLen(mOutputChars + insertAt * MAX_WORD_LENGTH))) {
satok30088252010-12-01 21:22:15 +0900158 break;
159 }
160 insertAt++;
161 }
162 if (insertAt < MAX_WORDS) {
satokcdbbea72010-12-08 16:04:16 +0900163 if (DEBUG_DICT) {
164 char s[length + 1];
165 for (int i = 0; i <= length; i++) s[i] = word[i];
satok662fe692010-12-08 17:05:39 +0900166 LOGI("Added word = %s, freq = %d", s, frequency);
satokcdbbea72010-12-08 16:04:16 +0900167 }
satok30088252010-12-01 21:22:15 +0900168 memmove((char*) mFrequencies + (insertAt + 1) * sizeof(mFrequencies[0]),
169 (char*) mFrequencies + insertAt * sizeof(mFrequencies[0]),
170 (MAX_WORDS - insertAt - 1) * sizeof(mFrequencies[0]));
171 mFrequencies[insertAt] = frequency;
172 memmove((char*) mOutputChars + (insertAt + 1) * MAX_WORD_LENGTH * sizeof(short),
satok715514d2010-12-02 20:19:59 +0900173 (char*) mOutputChars + insertAt * MAX_WORD_LENGTH * sizeof(short),
satok30088252010-12-01 21:22:15 +0900174 (MAX_WORDS - insertAt - 1) * sizeof(short) * MAX_WORD_LENGTH);
satok715514d2010-12-02 20:19:59 +0900175 unsigned short *dest = mOutputChars + insertAt * MAX_WORD_LENGTH;
satok30088252010-12-01 21:22:15 +0900176 while (length--) {
177 *dest++ = *word++;
178 }
179 *dest = 0; // NULL terminate
satok662fe692010-12-08 17:05:39 +0900180 if (DEBUG_DICT) LOGI("Added word at %d", insertAt);
satok30088252010-12-01 21:22:15 +0900181 return true;
182 }
183 return false;
184}
185
satok28bd03b2010-12-03 16:39:16 +0900186unsigned short UnigramDictionary::toLowerCase(unsigned short c) {
satok30088252010-12-01 21:22:15 +0900187 if (c < sizeof(BASE_CHARS) / sizeof(BASE_CHARS[0])) {
188 c = BASE_CHARS[c];
189 }
190 if (c >='A' && c <= 'Z') {
191 c |= 32;
192 } else if (c > 127) {
193 c = latin_tolower(c);
194 }
195 return c;
196}
197
satok28bd03b2010-12-03 16:39:16 +0900198bool UnigramDictionary::sameAsTyped(unsigned short *word, int length) {
satok30088252010-12-01 21:22:15 +0900199 if (length != mInputLength) {
200 return false;
201 }
202 int *inputCodes = mInputCodes;
203 while (length--) {
204 if ((unsigned int) *inputCodes != (unsigned int) *word) {
205 return false;
206 }
satok662fe692010-12-08 17:05:39 +0900207 inputCodes += MAX_PROXIMITY_CHARS;
satok30088252010-12-01 21:22:15 +0900208 word++;
209 }
210 return true;
211}
212
satok715514d2010-12-02 20:19:59 +0900213static const char QUOTE = '\'';
satok662fe692010-12-08 17:05:39 +0900214static const char SPACE = ' ';
satok30088252010-12-01 21:22:15 +0900215
satok54fe9e02010-12-13 14:42:35 +0900216void UnigramDictionary::getSuggestionCandidates(const int skipPos,
satoka3d78f62010-12-09 22:08:33 +0900217 const int excessivePos, const int transposedPos, int *nextLetters,
218 const int nextLettersSize, const int maxDepth) {
satok54fe9e02010-12-13 14:42:35 +0900219 if (DEBUG_DICT) {
220 LOGI("getSuggestionCandidates %d", maxDepth);
221 assert(transposedPos + 1 < mInputLength);
222 assert(excessivePos < mInputLength);
223 assert(missingPos < mInputLength);
224 }
satok662fe692010-12-08 17:05:39 +0900225 int rootPosition = ROOT_POS;
satokd2997922010-12-07 13:08:39 +0900226 // Get the number of child of root, then increment the position
227 int childCount = Dictionary::getCount(DICT, &rootPosition);
228 int depth = 0;
229
230 mStackChildCount[0] = childCount;
231 mStackTraverseAll[0] = (mInputLength <= 0);
232 mStackNodeFreq[0] = 1;
233 mStackInputIndex[0] = 0;
234 mStackDiffs[0] = 0;
235 mStackSiblingPos[0] = rootPosition;
236
satok662fe692010-12-08 17:05:39 +0900237 // Depth first search
satokd2997922010-12-07 13:08:39 +0900238 while (depth >= 0) {
239 if (mStackChildCount[depth] > 0) {
240 --mStackChildCount[depth];
241 bool traverseAllNodes = mStackTraverseAll[depth];
242 int snr = mStackNodeFreq[depth];
243 int inputIndex = mStackInputIndex[depth];
244 int diffs = mStackDiffs[depth];
245 int siblingPos = mStackSiblingPos[depth];
246 int firstChildPos;
satoka3d78f62010-12-09 22:08:33 +0900247 // depth will never be greater than maxDepth because in that case,
satokd2997922010-12-07 13:08:39 +0900248 // needsToTraverseChildrenNodes should be false
249 const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth,
satoka3d78f62010-12-09 22:08:33 +0900250 maxDepth, traverseAllNodes, snr, inputIndex, diffs, skipPos, excessivePos,
251 transposedPos, nextLetters, nextLettersSize, &childCount, &firstChildPos,
252 &traverseAllNodes, &snr, &inputIndex, &diffs, &siblingPos);
satok662fe692010-12-08 17:05:39 +0900253 // Update next sibling pos
satokd2997922010-12-07 13:08:39 +0900254 mStackSiblingPos[depth] = siblingPos;
255 if (needsToTraverseChildrenNodes) {
256 // Goes to child node
257 ++depth;
258 mStackChildCount[depth] = childCount;
259 mStackTraverseAll[depth] = traverseAllNodes;
260 mStackNodeFreq[depth] = snr;
261 mStackInputIndex[depth] = inputIndex;
262 mStackDiffs[depth] = diffs;
263 mStackSiblingPos[depth] = firstChildPos;
264 }
265 } else {
satokcdbbea72010-12-08 16:04:16 +0900266 // Goes to parent sibling node
satokd2997922010-12-07 13:08:39 +0900267 --depth;
268 }
269 }
270}
271
satok662fe692010-12-08 17:05:39 +0900272bool UnigramDictionary::getMissingSpaceWords(const int inputLength, const int missingSpacePos) {
satokaee09dc2010-12-09 19:21:51 +0900273 if (missingSpacePos <= 0 || missingSpacePos >= inputLength
274 || inputLength >= MAX_WORD_LENGTH) return false;
satok662fe692010-12-08 17:05:39 +0900275 const int newWordLength = inputLength + 1;
276 // Allocating variable length array on stack
277 unsigned short word[newWordLength];
satokaee09dc2010-12-09 19:21:51 +0900278 const int firstFreq = getBestWordFreq(0, missingSpacePos, mWord);
279 if (DEBUG_DICT) LOGI("First freq: %d", firstFreq);
280 if (firstFreq <= 0) return false;
281
satok662fe692010-12-08 17:05:39 +0900282 for (int i = 0; i < missingSpacePos; ++i) {
satokaee09dc2010-12-09 19:21:51 +0900283 word[i] = mWord[i];
satok662fe692010-12-08 17:05:39 +0900284 }
satokaee09dc2010-12-09 19:21:51 +0900285
286 const int secondFreq = getBestWordFreq(missingSpacePos, inputLength - missingSpacePos, mWord);
satoka3d78f62010-12-09 22:08:33 +0900287 if (DEBUG_DICT) LOGI("Second freq: %d", secondFreq);
satokaee09dc2010-12-09 19:21:51 +0900288 if (secondFreq <= 0) return false;
289
satok662fe692010-12-08 17:05:39 +0900290 word[missingSpacePos] = SPACE;
291 for (int i = (missingSpacePos + 1); i < newWordLength; ++i) {
satokaee09dc2010-12-09 19:21:51 +0900292 word[i] = mWord[i - missingSpacePos - 1];
satok662fe692010-12-08 17:05:39 +0900293 }
satokaee09dc2010-12-09 19:21:51 +0900294
295 int pairFreq = ((firstFreq + secondFreq) / 2);
296 for (int i = 0; i < inputLength; ++i) pairFreq *= TYPED_LETTER_MULTIPLIER;
satoka3d78f62010-12-09 22:08:33 +0900297 pairFreq = pairFreq * WORDS_WITH_MISSING_SPACE_CHARACTER_DEMOTION_RATE / 100;
satok662fe692010-12-08 17:05:39 +0900298 addWord(word, newWordLength, pairFreq);
299 return true;
300}
301
302// Keep this for comparing spec to new getWords
303void UnigramDictionary::getWordsOld(const int initialPos, const int inputLength, const int skipPos,
satoka3d78f62010-12-09 22:08:33 +0900304 const int excessivePos, const int transposedPos,int *nextLetters,
305 const int nextLettersSize) {
satok662fe692010-12-08 17:05:39 +0900306 int initialPosition = initialPos;
307 const int count = Dictionary::getCount(DICT, &initialPosition);
308 getWordsRec(count, initialPosition, 0,
309 min(inputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH),
satoka3d78f62010-12-09 22:08:33 +0900310 mInputLength <= 0, 1, 0, 0, skipPos, excessivePos, transposedPos, nextLetters,
311 nextLettersSize);
satok662fe692010-12-08 17:05:39 +0900312}
313
satok68319262010-12-03 19:38:08 +0900314void UnigramDictionary::getWordsRec(const int childrenCount, const int pos, const int depth,
315 const int maxDepth, const bool traverseAllNodes, const int snr, const int inputIndex,
satoka3d78f62010-12-09 22:08:33 +0900316 const int diffs, const int skipPos, const int excessivePos, const int transposedPos,
317 int *nextLetters, const int nextLettersSize) {
satok48e432c2010-12-06 17:38:58 +0900318 int siblingPos = pos;
satok68319262010-12-03 19:38:08 +0900319 for (int i = 0; i < childrenCount; ++i) {
satok48e432c2010-12-06 17:38:58 +0900320 int newCount;
321 int newChildPosition;
satokd2997922010-12-07 13:08:39 +0900322 const int newDepth = depth + 1;
satok48e432c2010-12-06 17:38:58 +0900323 bool newTraverseAllNodes;
324 int newSnr;
325 int newInputIndex;
326 int newDiffs;
327 int newSiblingPos;
328 const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth, maxDepth,
satoka3d78f62010-12-09 22:08:33 +0900329 traverseAllNodes, snr, inputIndex, diffs, skipPos, excessivePos, transposedPos,
330 nextLetters, nextLettersSize,
satokd2997922010-12-07 13:08:39 +0900331 &newCount, &newChildPosition, &newTraverseAllNodes, &newSnr,
satok48e432c2010-12-06 17:38:58 +0900332 &newInputIndex, &newDiffs, &newSiblingPos);
333 siblingPos = newSiblingPos;
satok30088252010-12-01 21:22:15 +0900334
satok48e432c2010-12-06 17:38:58 +0900335 if (needsToTraverseChildrenNodes) {
336 getWordsRec(newCount, newChildPosition, newDepth, maxDepth, newTraverseAllNodes,
satoka3d78f62010-12-09 22:08:33 +0900337 newSnr, newInputIndex, newDiffs, skipPos, excessivePos, transposedPos,
338 nextLetters, nextLettersSize);
satok30088252010-12-01 21:22:15 +0900339 }
340 }
341}
342
satok54fe9e02010-12-13 14:42:35 +0900343inline int UnigramDictionary::calculateFinalFreq(const int inputIndex, const int snr,
344 const int skipPos, const int excessivePos, const int transposedPos, const int freq,
345 const bool sameLength) {
satoka3d78f62010-12-09 22:08:33 +0900346 // TODO: Demote by edit distance
satok54fe9e02010-12-13 14:42:35 +0900347 int finalFreq = freq * snr;
satoka3d78f62010-12-09 22:08:33 +0900348 if (skipPos >= 0) finalFreq = finalFreq * WORDS_WITH_MISSING_CHARACTER_DEMOTION_RATE / 100;
satoka3d78f62010-12-09 22:08:33 +0900349 if (transposedPos >= 0) finalFreq = finalFreq
350 * WORDS_WITH_TRANSPOSED_CHARACTERS_DEMOTION_RATE / 100;
satok54fe9e02010-12-13 14:42:35 +0900351 if (excessivePos >= 0) {
352 finalFreq = finalFreq * WORDS_WITH_EXCESSIVE_CHARACTER_DEMOTION_RATE / 100;
353 if (!existsAdjacentProximityChars(inputIndex, mInputLength)) {
354 finalFreq = finalFreq
355 * WORDS_WITH_EXCESSIVE_CHARACTER_OUT_OF_PROXIMITY_DEMOTION_RATE / 100;
356 }
357 }
358 if (sameLength && skipPos < 0) finalFreq *= FULL_WORD_MULTIPLIER;
359 return finalFreq;
360}
satoka3d78f62010-12-09 22:08:33 +0900361
satok54fe9e02010-12-13 14:42:35 +0900362inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsGreaterThanInputLength(
363 unsigned short *word, const int inputIndex, const int depth, const int snr,
364 int *nextLetters, const int nextLettersSize, const int skipPos, const int excessivePos,
365 const int transposedPos, const int freq) {
366 const int finalFreq = calculateFinalFreq(inputIndex, snr, skipPos, excessivePos, transposedPos,
367 freq, false);
satoka3d78f62010-12-09 22:08:33 +0900368 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900369 if (depth >= mInputLength && skipPos < 0) {
satok715514d2010-12-02 20:19:59 +0900370 registerNextLetter(mWord[mInputLength], nextLetters, nextLettersSize);
371 }
372}
373
374inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsSameAsInputLength(
satok54fe9e02010-12-13 14:42:35 +0900375 unsigned short *word, const int inputIndex, const int depth, const int snr,
376 const int skipPos, const int excessivePos, const int transposedPos, const int freq,
377 const int addedWeight) {
378 if (sameAsTyped(word, depth + 1)) return;
379 const int finalFreq = calculateFinalFreq(inputIndex, snr * addedWeight, skipPos,
380 excessivePos, transposedPos, freq, true);
381 // Proximity collection will promote a word of the same length as what user typed.
382 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq);
satok715514d2010-12-02 20:19:59 +0900383}
satok28bd03b2010-12-03 16:39:16 +0900384
385inline bool UnigramDictionary::needsToSkipCurrentNode(const unsigned short c,
satok68319262010-12-03 19:38:08 +0900386 const int inputIndex, const int skipPos, const int depth) {
satok662fe692010-12-08 17:05:39 +0900387 const unsigned short userTypedChar = (mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS))[0];
satok28bd03b2010-12-03 16:39:16 +0900388 // Skip the ' or other letter and continue deeper
389 return (c == QUOTE && userTypedChar != QUOTE) || skipPos == depth;
390}
391
satoke07baa62010-12-09 21:55:40 +0900392inline bool UnigramDictionary::existsAdjacentProximityChars(const int inputIndex,
393 const int inputLength) {
394 if (inputIndex < 0 || inputIndex >= inputLength) return false;
395 const int currentChar = *getInputCharsAt(inputIndex);
396 const int leftIndex = inputIndex - 1;
397 if (leftIndex >= 0) {
398 int *leftChars = getInputCharsAt(leftIndex);
399 int i = 0;
400 while (leftChars[i] > 0 && i < MAX_PROXIMITY_CHARS) {
401 if (leftChars[i++] == currentChar) return true;
402 }
403 }
404 const int rightIndex = inputIndex + 1;
405 if (rightIndex < inputLength) {
406 int *rightChars = getInputCharsAt(rightIndex);
407 int i = 0;
408 while (rightChars[i] > 0 && i < MAX_PROXIMITY_CHARS) {
409 if (rightChars[i++] == currentChar) return true;
410 }
411 }
412 return false;
413}
414
satok28bd03b2010-12-03 16:39:16 +0900415inline int UnigramDictionary::getMatchedProximityId(const int *currentChars,
satoka3d78f62010-12-09 22:08:33 +0900416 const unsigned short c, const int skipPos, const int excessivePos,
417 const int transposedPos) {
satok48e432c2010-12-06 17:38:58 +0900418 const unsigned short lowerC = toLowerCase(c);
satok28bd03b2010-12-03 16:39:16 +0900419 int j = 0;
satoke07baa62010-12-09 21:55:40 +0900420 while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS) {
satok68319262010-12-03 19:38:08 +0900421 const bool matched = (currentChars[j] == lowerC || currentChars[j] == c);
satok28bd03b2010-12-03 16:39:16 +0900422 // If skipPos is defined, not to search proximity collections.
satoka3d78f62010-12-09 22:08:33 +0900423 // First char is what user typed.
satok28bd03b2010-12-03 16:39:16 +0900424 if (matched) {
425 return j;
satoka3d78f62010-12-09 22:08:33 +0900426 } else if (skipPos >= 0 || excessivePos >= 0 || transposedPos >= 0) {
427 // Not to check proximity characters
satok28bd03b2010-12-03 16:39:16 +0900428 return -1;
429 }
430 ++j;
431 }
432 return -1;
433}
434
satok48e432c2010-12-06 17:38:58 +0900435inline bool UnigramDictionary::processCurrentNode(const int pos, const int depth,
satokcdbbea72010-12-08 16:04:16 +0900436 const int maxDepth, const bool traverseAllNodes, const int snr, int inputIndex,
satoka3d78f62010-12-09 22:08:33 +0900437 const int diffs, const int skipPos, const int excessivePos, const int transposedPos,
438 int *nextLetters, const int nextLettersSize, int *newCount, int *newChildPosition,
439 bool *newTraverseAllNodes, int *newSnr, int*newInputIndex, int *newDiffs,
440 int *nextSiblingPosition) {
441 if (DEBUG_DICT) {
442 int inputCount = 0;
443 if (skipPos >= 0) ++inputCount;
444 if (excessivePos >= 0) ++inputCount;
445 if (transposedPos >= 0) ++inputCount;
446 assert(inputCount <= 1);
447 }
satok48e432c2010-12-06 17:38:58 +0900448 unsigned short c;
449 int childPosition;
450 bool terminal;
451 int freq;
satokcdbbea72010-12-08 16:04:16 +0900452
453 if (excessivePos == depth) ++inputIndex;
454
satok48e432c2010-12-06 17:38:58 +0900455 *nextSiblingPosition = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, pos, &c,
456 &childPosition, &terminal, &freq);
457
458 const bool needsToTraverseChildrenNodes = childPosition != 0;
459
460 // If we are only doing traverseAllNodes, no need to look at the typed characters.
461 if (traverseAllNodes || needsToSkipCurrentNode(c, inputIndex, skipPos, depth)) {
462 mWord[depth] = c;
463 if (traverseAllNodes && terminal) {
satok54fe9e02010-12-13 14:42:35 +0900464 onTerminalWhenUserTypedLengthIsGreaterThanInputLength(mWord, inputIndex, depth,
satoka3d78f62010-12-09 22:08:33 +0900465 snr, nextLetters, nextLettersSize, skipPos, excessivePos, transposedPos, freq);
satok48e432c2010-12-06 17:38:58 +0900466 }
467 if (!needsToTraverseChildrenNodes) return false;
468 *newTraverseAllNodes = traverseAllNodes;
469 *newSnr = snr;
470 *newDiffs = diffs;
471 *newInputIndex = inputIndex;
satok48e432c2010-12-06 17:38:58 +0900472 } else {
satok662fe692010-12-08 17:05:39 +0900473 int *currentChars = mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS);
satoka3d78f62010-12-09 22:08:33 +0900474
475 if (transposedPos >= 0) {
476 if (inputIndex == transposedPos) currentChars += MAX_PROXIMITY_CHARS;
477 if (inputIndex == (transposedPos + 1)) currentChars -= MAX_PROXIMITY_CHARS;
478 }
479
480 int matchedProximityCharId = getMatchedProximityId(currentChars, c, skipPos, excessivePos,
481 transposedPos);
satok48e432c2010-12-06 17:38:58 +0900482 if (matchedProximityCharId < 0) return false;
483 mWord[depth] = c;
484 // If inputIndex is greater than mInputLength, that means there is no
485 // proximity chars. So, we don't need to check proximity.
486 const int addedWeight = matchedProximityCharId == 0 ? TYPED_LETTER_MULTIPLIER : 1;
487 const bool isSameAsUserTypedLength = mInputLength == inputIndex + 1;
488 if (isSameAsUserTypedLength && terminal) {
satok54fe9e02010-12-13 14:42:35 +0900489 onTerminalWhenUserTypedLengthIsSameAsInputLength(mWord, inputIndex, depth, snr,
satoka3d78f62010-12-09 22:08:33 +0900490 skipPos, excessivePos, transposedPos, freq, addedWeight);
satok48e432c2010-12-06 17:38:58 +0900491 }
492 if (!needsToTraverseChildrenNodes) return false;
493 // Start traversing all nodes after the index exceeds the user typed length
494 *newTraverseAllNodes = isSameAsUserTypedLength;
495 *newSnr = snr * addedWeight;
satoka3d78f62010-12-09 22:08:33 +0900496 *newDiffs = diffs + ((matchedProximityCharId > 0) ? 1 : 0);
satok48e432c2010-12-06 17:38:58 +0900497 *newInputIndex = inputIndex + 1;
satok48e432c2010-12-06 17:38:58 +0900498 }
499 // Optimization: Prune out words that are too long compared to how much was typed.
satokd2997922010-12-07 13:08:39 +0900500 if (depth >= maxDepth || *newDiffs > mMaxEditDistance) {
satok48e432c2010-12-06 17:38:58 +0900501 return false;
502 }
503
504 // If inputIndex is greater than mInputLength, that means there are no proximity chars.
505 if (mInputLength <= *newInputIndex) {
506 *newTraverseAllNodes = true;
507 }
508 // get the count of nodes and increment childAddress.
509 *newCount = Dictionary::getCount(DICT, &childPosition);
510 *newChildPosition = childPosition;
511 if (DEBUG_DICT) assert(needsToTraverseChildrenNodes);
512 return needsToTraverseChildrenNodes;
513}
514
satokaee09dc2010-12-09 19:21:51 +0900515inline int UnigramDictionary::getBestWordFreq(const int startInputIndex, const int inputLength,
516 unsigned short *word) {
satok662fe692010-12-08 17:05:39 +0900517 int pos = ROOT_POS;
518 int count = Dictionary::getCount(DICT, &pos);
satokaee09dc2010-12-09 19:21:51 +0900519 int maxFreq = 0;
520 int depth = 0;
521 unsigned short newWord[MAX_WORD_LENGTH_INTERNAL];
satok662fe692010-12-08 17:05:39 +0900522 bool terminal = false;
523
satokaee09dc2010-12-09 19:21:51 +0900524 mStackChildCount[0] = count;
525 mStackSiblingPos[0] = pos;
526
527 while (depth >= 0) {
528 if (mStackChildCount[depth] > 0) {
529 --mStackChildCount[depth];
530 int firstChildPos;
531 int newFreq;
532 int siblingPos = mStackSiblingPos[depth];
533 const bool needsToTraverseChildrenNodes = processCurrentNodeForExactMatch(siblingPos,
534 startInputIndex, depth, newWord, &firstChildPos, &count, &terminal, &newFreq,
535 &siblingPos);
536 mStackSiblingPos[depth] = siblingPos;
537 if (depth == (inputLength - 1)) {
538 // Traverse sibling node
539 if (terminal) {
540 if (newFreq > maxFreq) {
541 for (int i = 0; i < inputLength; ++i) word[i] = newWord[i];
542 if (DEBUG_DICT && DEBUG_NODE) {
543 char s[inputLength + 1];
544 for (int i = 0; i < inputLength; ++i) s[i] = word[i];
545 s[inputLength] = 0;
546 LOGI("New missing space word found: %d > %d (%s), %d, %d",
547 newFreq, maxFreq, s, inputLength, depth);
548 }
549 maxFreq = newFreq;
550 }
551 }
552 } else if (needsToTraverseChildrenNodes) {
553 // Traverse children nodes
554 ++depth;
555 mStackChildCount[depth] = count;
556 mStackSiblingPos[depth] = firstChildPos;
557 }
558 } else {
559 // Traverse parent node
560 --depth;
satok662fe692010-12-08 17:05:39 +0900561 }
562 }
satokaee09dc2010-12-09 19:21:51 +0900563
564 word[inputLength] = 0;
565 return maxFreq;
satok662fe692010-12-08 17:05:39 +0900566}
567
568inline bool UnigramDictionary::processCurrentNodeForExactMatch(const int firstChildPos,
satokaee09dc2010-12-09 19:21:51 +0900569 const int startInputIndex, const int depth, unsigned short *word, int *newChildPosition,
570 int *newCount, bool *newTerminal, int *newFreq, int *siblingPos) {
571 const int inputIndex = startInputIndex + depth;
satok662fe692010-12-08 17:05:39 +0900572 const int *currentChars = mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS);
satok662fe692010-12-08 17:05:39 +0900573 unsigned short c;
satokaee09dc2010-12-09 19:21:51 +0900574 *siblingPos = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, firstChildPos, &c,
575 newChildPosition, newTerminal, newFreq);
576 const unsigned int inputC = currentChars[0];
577 if (DEBUG_DICT) assert(inputC <= U_SHORT_MAX);
578 const unsigned short lowerC = toLowerCase(c);
579 const bool matched = (inputC == lowerC || inputC == c);
580 const bool hasChild = *newChildPosition != 0;
581 if (matched) {
582 word[depth] = c;
583 if (DEBUG_DICT && DEBUG_NODE) {
584 LOGI("Node(%c, %c)<%d>, %d, %d", inputC, c, matched, hasChild, *newFreq);
585 if (*newTerminal) LOGI("Terminal %d", *newFreq);
satok662fe692010-12-08 17:05:39 +0900586 }
satokaee09dc2010-12-09 19:21:51 +0900587 if (hasChild) {
588 *newCount = Dictionary::getCount(DICT, newChildPosition);
589 return true;
590 } else {
591 return false;
592 }
593 } else {
594 // If this node is not user typed character, this method treats this word as unmatched.
595 // Thus newTerminal shouldn't be true.
596 *newTerminal = false;
597 return false;
satok662fe692010-12-08 17:05:39 +0900598 }
satok662fe692010-12-08 17:05:39 +0900599}
satok30088252010-12-01 21:22:15 +0900600} // namespace latinime