blob: 3d5683ed9d3cff12d803c4ac36e2bbbed3c93e36 [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)
Tadashi G. Takaoka887f11e2011-02-10 20:53:58 +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,
Tadashi G. Takaoka887f11e2011-02-10 20:53:58 +090045 int *frequencies) {
satok61e2f852011-01-05 14:13:07 +090046 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);
Tadashi G. Takaoka887f11e2011-02-10 20:53:58 +090055 getSuggestionCandidates(-1, -1, -1, mNextLettersFrequency, NEXT_LETTERS_SIZE, MAX_DEPTH);
satok61e2f852011-01-05 14:13:07 +090056 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: ");
Tadashi G. Takaoka887f11e2011-02-10 20:53:58 +0900111 for (int k = 0; k < NEXT_LETTERS_SIZE; k++) {
112 if (mNextLettersFrequency[k] > 0) {
113 LOGI("%c = %d,", k, mNextLettersFrequency[k]);
satok30088252010-12-01 21:22:15 +0900114 }
115 }
satok30088252010-12-01 21:22:15 +0900116 }
satok61e2f852011-01-05 14:13:07 +0900117 PROF_END(6);
118 PROF_CLOSE;
satok30088252010-12-01 21:22:15 +0900119 return suggestedWordsCount;
120}
121
122void UnigramDictionary::initSuggestions(int *codes, int codesSize, unsigned short *outWords,
123 int *frequencies) {
satokf5cded12010-12-06 21:28:24 +0900124 if (DEBUG_DICT) LOGI("initSuggest");
satok30088252010-12-01 21:22:15 +0900125 mFrequencies = frequencies;
126 mOutputChars = outWords;
127 mInputCodes = codes;
128 mInputLength = codesSize;
129 mMaxEditDistance = mInputLength < 5 ? 2 : mInputLength / 2;
130}
131
satok715514d2010-12-02 20:19:59 +0900132void UnigramDictionary::registerNextLetter(
133 unsigned short c, int *nextLetters, int nextLettersSize) {
satok30088252010-12-01 21:22:15 +0900134 if (c < nextLettersSize) {
135 nextLetters[c]++;
136 }
137}
138
satok662fe692010-12-08 17:05:39 +0900139// TODO: We need to optimize addWord by using STL or something
satok28bd03b2010-12-03 16:39:16 +0900140bool UnigramDictionary::addWord(unsigned short *word, int length, int frequency) {
satok30088252010-12-01 21:22:15 +0900141 word[length] = 0;
satok662fe692010-12-08 17:05:39 +0900142 if (DEBUG_DICT && DEBUG_SHOW_FOUND_WORD) {
satok30088252010-12-01 21:22:15 +0900143 char s[length + 1];
144 for (int i = 0; i <= length; i++) s[i] = word[i];
satok662fe692010-12-08 17:05:39 +0900145 LOGI("Found word = %s, freq = %d", s, frequency);
satok30088252010-12-01 21:22:15 +0900146 }
satokf5cded12010-12-06 21:28:24 +0900147 if (length > MAX_WORD_LENGTH) {
148 if (DEBUG_DICT) LOGI("Exceeded max word length.");
149 return false;
150 }
satok30088252010-12-01 21:22:15 +0900151
152 // Find the right insertion point
153 int insertAt = 0;
154 while (insertAt < MAX_WORDS) {
satok715514d2010-12-02 20:19:59 +0900155 if (frequency > mFrequencies[insertAt] || (mFrequencies[insertAt] == frequency
156 && length < Dictionary::wideStrLen(mOutputChars + insertAt * MAX_WORD_LENGTH))) {
satok30088252010-12-01 21:22:15 +0900157 break;
158 }
159 insertAt++;
160 }
161 if (insertAt < MAX_WORDS) {
satokcdbbea72010-12-08 16:04:16 +0900162 if (DEBUG_DICT) {
163 char s[length + 1];
164 for (int i = 0; i <= length; i++) s[i] = word[i];
satok662fe692010-12-08 17:05:39 +0900165 LOGI("Added word = %s, freq = %d", s, frequency);
satokcdbbea72010-12-08 16:04:16 +0900166 }
satok30088252010-12-01 21:22:15 +0900167 memmove((char*) mFrequencies + (insertAt + 1) * sizeof(mFrequencies[0]),
168 (char*) mFrequencies + insertAt * sizeof(mFrequencies[0]),
169 (MAX_WORDS - insertAt - 1) * sizeof(mFrequencies[0]));
170 mFrequencies[insertAt] = frequency;
171 memmove((char*) mOutputChars + (insertAt + 1) * MAX_WORD_LENGTH * sizeof(short),
satok715514d2010-12-02 20:19:59 +0900172 (char*) mOutputChars + insertAt * MAX_WORD_LENGTH * sizeof(short),
satok30088252010-12-01 21:22:15 +0900173 (MAX_WORDS - insertAt - 1) * sizeof(short) * MAX_WORD_LENGTH);
satok715514d2010-12-02 20:19:59 +0900174 unsigned short *dest = mOutputChars + insertAt * MAX_WORD_LENGTH;
satok30088252010-12-01 21:22:15 +0900175 while (length--) {
176 *dest++ = *word++;
177 }
178 *dest = 0; // NULL terminate
satok662fe692010-12-08 17:05:39 +0900179 if (DEBUG_DICT) LOGI("Added word at %d", insertAt);
satok30088252010-12-01 21:22:15 +0900180 return true;
181 }
182 return false;
183}
184
satok28bd03b2010-12-03 16:39:16 +0900185unsigned short UnigramDictionary::toLowerCase(unsigned short c) {
satok30088252010-12-01 21:22:15 +0900186 if (c < sizeof(BASE_CHARS) / sizeof(BASE_CHARS[0])) {
187 c = BASE_CHARS[c];
188 }
189 if (c >='A' && c <= 'Z') {
190 c |= 32;
191 } else if (c > 127) {
192 c = latin_tolower(c);
193 }
194 return c;
195}
196
satok28bd03b2010-12-03 16:39:16 +0900197bool UnigramDictionary::sameAsTyped(unsigned short *word, int length) {
satok30088252010-12-01 21:22:15 +0900198 if (length != mInputLength) {
199 return false;
200 }
201 int *inputCodes = mInputCodes;
202 while (length--) {
203 if ((unsigned int) *inputCodes != (unsigned int) *word) {
204 return false;
205 }
satok662fe692010-12-08 17:05:39 +0900206 inputCodes += MAX_PROXIMITY_CHARS;
satok30088252010-12-01 21:22:15 +0900207 word++;
208 }
209 return true;
210}
211
satok715514d2010-12-02 20:19:59 +0900212static const char QUOTE = '\'';
satok662fe692010-12-08 17:05:39 +0900213static const char SPACE = ' ';
satok30088252010-12-01 21:22:15 +0900214
satok54fe9e02010-12-13 14:42:35 +0900215void UnigramDictionary::getSuggestionCandidates(const int skipPos,
satoka3d78f62010-12-09 22:08:33 +0900216 const int excessivePos, const int transposedPos, int *nextLetters,
217 const int nextLettersSize, const int maxDepth) {
satok54fe9e02010-12-13 14:42:35 +0900218 if (DEBUG_DICT) {
219 LOGI("getSuggestionCandidates %d", maxDepth);
220 assert(transposedPos + 1 < mInputLength);
221 assert(excessivePos < mInputLength);
222 assert(missingPos < mInputLength);
223 }
satok662fe692010-12-08 17:05:39 +0900224 int rootPosition = ROOT_POS;
satokd2997922010-12-07 13:08:39 +0900225 // Get the number of child of root, then increment the position
226 int childCount = Dictionary::getCount(DICT, &rootPosition);
227 int depth = 0;
228
229 mStackChildCount[0] = childCount;
230 mStackTraverseAll[0] = (mInputLength <= 0);
231 mStackNodeFreq[0] = 1;
232 mStackInputIndex[0] = 0;
233 mStackDiffs[0] = 0;
234 mStackSiblingPos[0] = rootPosition;
235
satok662fe692010-12-08 17:05:39 +0900236 // Depth first search
satokd2997922010-12-07 13:08:39 +0900237 while (depth >= 0) {
238 if (mStackChildCount[depth] > 0) {
239 --mStackChildCount[depth];
240 bool traverseAllNodes = mStackTraverseAll[depth];
241 int snr = mStackNodeFreq[depth];
242 int inputIndex = mStackInputIndex[depth];
243 int diffs = mStackDiffs[depth];
244 int siblingPos = mStackSiblingPos[depth];
245 int firstChildPos;
satoka3d78f62010-12-09 22:08:33 +0900246 // depth will never be greater than maxDepth because in that case,
satokd2997922010-12-07 13:08:39 +0900247 // needsToTraverseChildrenNodes should be false
248 const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth,
satoka3d78f62010-12-09 22:08:33 +0900249 maxDepth, traverseAllNodes, snr, inputIndex, diffs, skipPos, excessivePos,
250 transposedPos, nextLetters, nextLettersSize, &childCount, &firstChildPos,
251 &traverseAllNodes, &snr, &inputIndex, &diffs, &siblingPos);
satok662fe692010-12-08 17:05:39 +0900252 // Update next sibling pos
satokd2997922010-12-07 13:08:39 +0900253 mStackSiblingPos[depth] = siblingPos;
254 if (needsToTraverseChildrenNodes) {
255 // Goes to child node
256 ++depth;
257 mStackChildCount[depth] = childCount;
258 mStackTraverseAll[depth] = traverseAllNodes;
259 mStackNodeFreq[depth] = snr;
260 mStackInputIndex[depth] = inputIndex;
261 mStackDiffs[depth] = diffs;
262 mStackSiblingPos[depth] = firstChildPos;
263 }
264 } else {
satokcdbbea72010-12-08 16:04:16 +0900265 // Goes to parent sibling node
satokd2997922010-12-07 13:08:39 +0900266 --depth;
267 }
268 }
269}
270
satokf7425bb2011-01-05 16:37:53 +0900271inline static void multiplyRate(const int rate, int *freq) {
272 if (rate > 1000000) {
273 *freq = (*freq / 100) * rate;
274 } else {
275 *freq = *freq * rate / 100;
276 }
277}
278
satok662fe692010-12-08 17:05:39 +0900279bool UnigramDictionary::getMissingSpaceWords(const int inputLength, const int missingSpacePos) {
satokaee09dc2010-12-09 19:21:51 +0900280 if (missingSpacePos <= 0 || missingSpacePos >= inputLength
281 || inputLength >= MAX_WORD_LENGTH) return false;
satok662fe692010-12-08 17:05:39 +0900282 const int newWordLength = inputLength + 1;
283 // Allocating variable length array on stack
284 unsigned short word[newWordLength];
satokaee09dc2010-12-09 19:21:51 +0900285 const int firstFreq = getBestWordFreq(0, missingSpacePos, mWord);
286 if (DEBUG_DICT) LOGI("First freq: %d", firstFreq);
287 if (firstFreq <= 0) return false;
288
satok662fe692010-12-08 17:05:39 +0900289 for (int i = 0; i < missingSpacePos; ++i) {
satokaee09dc2010-12-09 19:21:51 +0900290 word[i] = mWord[i];
satok662fe692010-12-08 17:05:39 +0900291 }
satokaee09dc2010-12-09 19:21:51 +0900292
293 const int secondFreq = getBestWordFreq(missingSpacePos, inputLength - missingSpacePos, mWord);
satoka3d78f62010-12-09 22:08:33 +0900294 if (DEBUG_DICT) LOGI("Second freq: %d", secondFreq);
satokaee09dc2010-12-09 19:21:51 +0900295 if (secondFreq <= 0) return false;
296
satok662fe692010-12-08 17:05:39 +0900297 word[missingSpacePos] = SPACE;
298 for (int i = (missingSpacePos + 1); i < newWordLength; ++i) {
satokaee09dc2010-12-09 19:21:51 +0900299 word[i] = mWord[i - missingSpacePos - 1];
satok662fe692010-12-08 17:05:39 +0900300 }
satokaee09dc2010-12-09 19:21:51 +0900301
302 int pairFreq = ((firstFreq + secondFreq) / 2);
303 for (int i = 0; i < inputLength; ++i) pairFreq *= TYPED_LETTER_MULTIPLIER;
satokf7425bb2011-01-05 16:37:53 +0900304 multiplyRate(WORDS_WITH_MISSING_SPACE_CHARACTER_DEMOTION_RATE, &pairFreq);
satok662fe692010-12-08 17:05:39 +0900305 addWord(word, newWordLength, pairFreq);
306 return true;
307}
308
309// Keep this for comparing spec to new getWords
310void UnigramDictionary::getWordsOld(const int initialPos, const int inputLength, const int skipPos,
satoka3d78f62010-12-09 22:08:33 +0900311 const int excessivePos, const int transposedPos,int *nextLetters,
312 const int nextLettersSize) {
satok662fe692010-12-08 17:05:39 +0900313 int initialPosition = initialPos;
314 const int count = Dictionary::getCount(DICT, &initialPosition);
315 getWordsRec(count, initialPosition, 0,
316 min(inputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH),
satoka3d78f62010-12-09 22:08:33 +0900317 mInputLength <= 0, 1, 0, 0, skipPos, excessivePos, transposedPos, nextLetters,
318 nextLettersSize);
satok662fe692010-12-08 17:05:39 +0900319}
320
satok68319262010-12-03 19:38:08 +0900321void UnigramDictionary::getWordsRec(const int childrenCount, const int pos, const int depth,
322 const int maxDepth, const bool traverseAllNodes, const int snr, const int inputIndex,
satoka3d78f62010-12-09 22:08:33 +0900323 const int diffs, const int skipPos, const int excessivePos, const int transposedPos,
324 int *nextLetters, const int nextLettersSize) {
satok48e432c2010-12-06 17:38:58 +0900325 int siblingPos = pos;
satok68319262010-12-03 19:38:08 +0900326 for (int i = 0; i < childrenCount; ++i) {
satok48e432c2010-12-06 17:38:58 +0900327 int newCount;
328 int newChildPosition;
satokd2997922010-12-07 13:08:39 +0900329 const int newDepth = depth + 1;
satok48e432c2010-12-06 17:38:58 +0900330 bool newTraverseAllNodes;
331 int newSnr;
332 int newInputIndex;
333 int newDiffs;
334 int newSiblingPos;
335 const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth, maxDepth,
satoka3d78f62010-12-09 22:08:33 +0900336 traverseAllNodes, snr, inputIndex, diffs, skipPos, excessivePos, transposedPos,
337 nextLetters, nextLettersSize,
satokd2997922010-12-07 13:08:39 +0900338 &newCount, &newChildPosition, &newTraverseAllNodes, &newSnr,
satok48e432c2010-12-06 17:38:58 +0900339 &newInputIndex, &newDiffs, &newSiblingPos);
340 siblingPos = newSiblingPos;
satok30088252010-12-01 21:22:15 +0900341
satok48e432c2010-12-06 17:38:58 +0900342 if (needsToTraverseChildrenNodes) {
343 getWordsRec(newCount, newChildPosition, newDepth, maxDepth, newTraverseAllNodes,
satoka3d78f62010-12-09 22:08:33 +0900344 newSnr, newInputIndex, newDiffs, skipPos, excessivePos, transposedPos,
345 nextLetters, nextLettersSize);
satok30088252010-12-01 21:22:15 +0900346 }
347 }
348}
349
satok58c49b92011-01-27 03:23:39 +0900350inline int UnigramDictionary::calculateFinalFreq(const int inputIndex, const int depth,
351 const int snr, const int skipPos, const int excessivePos, const int transposedPos,
352 const int freq, const bool sameLength) {
satoka3d78f62010-12-09 22:08:33 +0900353 // TODO: Demote by edit distance
satok54fe9e02010-12-13 14:42:35 +0900354 int finalFreq = freq * snr;
satokf7425bb2011-01-05 16:37:53 +0900355 if (skipPos >= 0) multiplyRate(WORDS_WITH_MISSING_CHARACTER_DEMOTION_RATE, &finalFreq);
356 if (transposedPos >= 0) multiplyRate(
357 WORDS_WITH_TRANSPOSED_CHARACTERS_DEMOTION_RATE, &finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900358 if (excessivePos >= 0) {
satokf7425bb2011-01-05 16:37:53 +0900359 multiplyRate(WORDS_WITH_EXCESSIVE_CHARACTER_DEMOTION_RATE, &finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900360 if (!existsAdjacentProximityChars(inputIndex, mInputLength)) {
satokf7425bb2011-01-05 16:37:53 +0900361 multiplyRate(WORDS_WITH_EXCESSIVE_CHARACTER_OUT_OF_PROXIMITY_DEMOTION_RATE, &finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900362 }
363 }
satok58c49b92011-01-27 03:23:39 +0900364 int lengthFreq = TYPED_LETTER_MULTIPLIER;
365 for (int i = 0; i < depth; ++i) lengthFreq *= TYPED_LETTER_MULTIPLIER;
Jean Chalard8dc754a2011-01-27 14:20:22 +0900366 if (lengthFreq == snr) {
367 if (depth > 1) {
368 if (DEBUG_DICT) LOGI("Found full matched word.");
369 multiplyRate(FULL_MATCHED_WORDS_PROMOTION_RATE, &finalFreq);
370 }
371 if (sameLength && transposedPos < 0 && skipPos < 0 && excessivePos < 0) {
372 finalFreq *= FULL_MATCH_ACCENTS_OR_CAPITALIZATION_DIFFER_MULTIPLIER;
373 }
satok58c49b92011-01-27 03:23:39 +0900374 }
satok54fe9e02010-12-13 14:42:35 +0900375 if (sameLength && skipPos < 0) finalFreq *= FULL_WORD_MULTIPLIER;
376 return finalFreq;
377}
satoka3d78f62010-12-09 22:08:33 +0900378
satok54fe9e02010-12-13 14:42:35 +0900379inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsGreaterThanInputLength(
380 unsigned short *word, const int inputIndex, const int depth, const int snr,
381 int *nextLetters, const int nextLettersSize, const int skipPos, const int excessivePos,
382 const int transposedPos, const int freq) {
satok58c49b92011-01-27 03:23:39 +0900383 const int finalFreq = calculateFinalFreq(inputIndex, depth, snr, skipPos, excessivePos,
384 transposedPos, freq, false);
satoka3d78f62010-12-09 22:08:33 +0900385 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900386 if (depth >= mInputLength && skipPos < 0) {
satok715514d2010-12-02 20:19:59 +0900387 registerNextLetter(mWord[mInputLength], nextLetters, nextLettersSize);
388 }
389}
390
391inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsSameAsInputLength(
satok54fe9e02010-12-13 14:42:35 +0900392 unsigned short *word, const int inputIndex, const int depth, const int snr,
Jean Chalard8dc754a2011-01-27 14:20:22 +0900393 const int skipPos, const int excessivePos, const int transposedPos, const int freq) {
satok54fe9e02010-12-13 14:42:35 +0900394 if (sameAsTyped(word, depth + 1)) return;
Jean Chalard8dc754a2011-01-27 14:20:22 +0900395 const int finalFreq = calculateFinalFreq(inputIndex, depth, snr, skipPos,
satok54fe9e02010-12-13 14:42:35 +0900396 excessivePos, transposedPos, freq, true);
397 // Proximity collection will promote a word of the same length as what user typed.
398 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq);
satok715514d2010-12-02 20:19:59 +0900399}
satok28bd03b2010-12-03 16:39:16 +0900400
401inline bool UnigramDictionary::needsToSkipCurrentNode(const unsigned short c,
satok68319262010-12-03 19:38:08 +0900402 const int inputIndex, const int skipPos, const int depth) {
satok662fe692010-12-08 17:05:39 +0900403 const unsigned short userTypedChar = (mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS))[0];
satok28bd03b2010-12-03 16:39:16 +0900404 // Skip the ' or other letter and continue deeper
405 return (c == QUOTE && userTypedChar != QUOTE) || skipPos == depth;
406}
407
satoke07baa62010-12-09 21:55:40 +0900408inline bool UnigramDictionary::existsAdjacentProximityChars(const int inputIndex,
409 const int inputLength) {
410 if (inputIndex < 0 || inputIndex >= inputLength) return false;
411 const int currentChar = *getInputCharsAt(inputIndex);
412 const int leftIndex = inputIndex - 1;
413 if (leftIndex >= 0) {
414 int *leftChars = getInputCharsAt(leftIndex);
415 int i = 0;
416 while (leftChars[i] > 0 && i < MAX_PROXIMITY_CHARS) {
417 if (leftChars[i++] == currentChar) return true;
418 }
419 }
420 const int rightIndex = inputIndex + 1;
421 if (rightIndex < inputLength) {
422 int *rightChars = getInputCharsAt(rightIndex);
423 int i = 0;
424 while (rightChars[i] > 0 && i < MAX_PROXIMITY_CHARS) {
425 if (rightChars[i++] == currentChar) return true;
426 }
427 }
428 return false;
429}
430
Jean Chalard8dc754a2011-01-27 14:20:22 +0900431inline UnigramDictionary::ProximityType UnigramDictionary::getMatchedProximityId(
432 const int *currentChars, const unsigned short c, const int skipPos,
433 const int excessivePos, const int transposedPos) {
satok48e432c2010-12-06 17:38:58 +0900434 const unsigned short lowerC = toLowerCase(c);
satok28bd03b2010-12-03 16:39:16 +0900435 int j = 0;
satoke07baa62010-12-09 21:55:40 +0900436 while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS) {
satok68319262010-12-03 19:38:08 +0900437 const bool matched = (currentChars[j] == lowerC || currentChars[j] == c);
satok28bd03b2010-12-03 16:39:16 +0900438 // If skipPos is defined, not to search proximity collections.
satoka3d78f62010-12-09 22:08:33 +0900439 // First char is what user typed.
satok28bd03b2010-12-03 16:39:16 +0900440 if (matched) {
Jean Chalard8dc754a2011-01-27 14:20:22 +0900441 if (j > 0) return NEAR_PROXIMITY_CHAR;
442 return SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR;
satoka3d78f62010-12-09 22:08:33 +0900443 } else if (skipPos >= 0 || excessivePos >= 0 || transposedPos >= 0) {
444 // Not to check proximity characters
Jean Chalard8dc754a2011-01-27 14:20:22 +0900445 return UNRELATED_CHAR;
satok28bd03b2010-12-03 16:39:16 +0900446 }
447 ++j;
448 }
Jean Chalard8dc754a2011-01-27 14:20:22 +0900449 return UNRELATED_CHAR;
satok28bd03b2010-12-03 16:39:16 +0900450}
451
satok48e432c2010-12-06 17:38:58 +0900452inline bool UnigramDictionary::processCurrentNode(const int pos, const int depth,
Jean Chalard8dc754a2011-01-27 14:20:22 +0900453 const int maxDepth, const bool traverseAllNodes, int snr, int inputIndex,
satoka3d78f62010-12-09 22:08:33 +0900454 const int diffs, const int skipPos, const int excessivePos, const int transposedPos,
455 int *nextLetters, const int nextLettersSize, int *newCount, int *newChildPosition,
456 bool *newTraverseAllNodes, int *newSnr, int*newInputIndex, int *newDiffs,
457 int *nextSiblingPosition) {
458 if (DEBUG_DICT) {
459 int inputCount = 0;
460 if (skipPos >= 0) ++inputCount;
461 if (excessivePos >= 0) ++inputCount;
462 if (transposedPos >= 0) ++inputCount;
463 assert(inputCount <= 1);
464 }
satok48e432c2010-12-06 17:38:58 +0900465 unsigned short c;
466 int childPosition;
467 bool terminal;
468 int freq;
satokfd16f1d2011-01-27 16:25:16 +0900469 bool isSameAsUserTypedLength = false;
satokcdbbea72010-12-08 16:04:16 +0900470
satokfd16f1d2011-01-27 16:25:16 +0900471 if (excessivePos == depth && inputIndex < mInputLength - 1) ++inputIndex;
satokcdbbea72010-12-08 16:04:16 +0900472
satok48e432c2010-12-06 17:38:58 +0900473 *nextSiblingPosition = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, pos, &c,
474 &childPosition, &terminal, &freq);
475
476 const bool needsToTraverseChildrenNodes = childPosition != 0;
477
478 // If we are only doing traverseAllNodes, no need to look at the typed characters.
479 if (traverseAllNodes || needsToSkipCurrentNode(c, inputIndex, skipPos, depth)) {
480 mWord[depth] = c;
481 if (traverseAllNodes && terminal) {
satok54fe9e02010-12-13 14:42:35 +0900482 onTerminalWhenUserTypedLengthIsGreaterThanInputLength(mWord, inputIndex, depth,
satoka3d78f62010-12-09 22:08:33 +0900483 snr, nextLetters, nextLettersSize, skipPos, excessivePos, transposedPos, freq);
satok48e432c2010-12-06 17:38:58 +0900484 }
485 if (!needsToTraverseChildrenNodes) return false;
486 *newTraverseAllNodes = traverseAllNodes;
487 *newSnr = snr;
488 *newDiffs = diffs;
489 *newInputIndex = inputIndex;
satok48e432c2010-12-06 17:38:58 +0900490 } else {
satok662fe692010-12-08 17:05:39 +0900491 int *currentChars = mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS);
satoka3d78f62010-12-09 22:08:33 +0900492
493 if (transposedPos >= 0) {
494 if (inputIndex == transposedPos) currentChars += MAX_PROXIMITY_CHARS;
495 if (inputIndex == (transposedPos + 1)) currentChars -= MAX_PROXIMITY_CHARS;
496 }
497
498 int matchedProximityCharId = getMatchedProximityId(currentChars, c, skipPos, excessivePos,
499 transposedPos);
Jean Chalard8dc754a2011-01-27 14:20:22 +0900500 if (UNRELATED_CHAR == matchedProximityCharId) return false;
satok48e432c2010-12-06 17:38:58 +0900501 mWord[depth] = c;
502 // If inputIndex is greater than mInputLength, that means there is no
503 // proximity chars. So, we don't need to check proximity.
Jean Chalard8dc754a2011-01-27 14:20:22 +0900504 if (SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR == matchedProximityCharId) {
505 snr = snr * TYPED_LETTER_MULTIPLIER;
506 }
satokfd16f1d2011-01-27 16:25:16 +0900507 bool isSameAsUserTypedLength = mInputLength == inputIndex + 1
508 || (excessivePos == mInputLength - 1 && inputIndex == mInputLength - 2);
satok48e432c2010-12-06 17:38:58 +0900509 if (isSameAsUserTypedLength && terminal) {
satok54fe9e02010-12-13 14:42:35 +0900510 onTerminalWhenUserTypedLengthIsSameAsInputLength(mWord, inputIndex, depth, snr,
Jean Chalard8dc754a2011-01-27 14:20:22 +0900511 skipPos, excessivePos, transposedPos, freq);
satok48e432c2010-12-06 17:38:58 +0900512 }
513 if (!needsToTraverseChildrenNodes) return false;
514 // Start traversing all nodes after the index exceeds the user typed length
515 *newTraverseAllNodes = isSameAsUserTypedLength;
Jean Chalard8dc754a2011-01-27 14:20:22 +0900516 *newSnr = snr;
517 *newDiffs = diffs + ((NEAR_PROXIMITY_CHAR == matchedProximityCharId) ? 1 : 0);
satok48e432c2010-12-06 17:38:58 +0900518 *newInputIndex = inputIndex + 1;
satok48e432c2010-12-06 17:38:58 +0900519 }
520 // Optimization: Prune out words that are too long compared to how much was typed.
satokd2997922010-12-07 13:08:39 +0900521 if (depth >= maxDepth || *newDiffs > mMaxEditDistance) {
satok48e432c2010-12-06 17:38:58 +0900522 return false;
523 }
524
525 // If inputIndex is greater than mInputLength, that means there are no proximity chars.
satokfd16f1d2011-01-27 16:25:16 +0900526 // TODO: Check if this can be isSameAsUserTypedLength only.
527 if (isSameAsUserTypedLength || mInputLength <= *newInputIndex) {
satok48e432c2010-12-06 17:38:58 +0900528 *newTraverseAllNodes = true;
529 }
530 // get the count of nodes and increment childAddress.
531 *newCount = Dictionary::getCount(DICT, &childPosition);
532 *newChildPosition = childPosition;
533 if (DEBUG_DICT) assert(needsToTraverseChildrenNodes);
534 return needsToTraverseChildrenNodes;
535}
536
satokaee09dc2010-12-09 19:21:51 +0900537inline int UnigramDictionary::getBestWordFreq(const int startInputIndex, const int inputLength,
538 unsigned short *word) {
satok662fe692010-12-08 17:05:39 +0900539 int pos = ROOT_POS;
540 int count = Dictionary::getCount(DICT, &pos);
satokaee09dc2010-12-09 19:21:51 +0900541 int maxFreq = 0;
542 int depth = 0;
543 unsigned short newWord[MAX_WORD_LENGTH_INTERNAL];
satok662fe692010-12-08 17:05:39 +0900544 bool terminal = false;
545
satokaee09dc2010-12-09 19:21:51 +0900546 mStackChildCount[0] = count;
547 mStackSiblingPos[0] = pos;
548
549 while (depth >= 0) {
550 if (mStackChildCount[depth] > 0) {
551 --mStackChildCount[depth];
552 int firstChildPos;
553 int newFreq;
554 int siblingPos = mStackSiblingPos[depth];
555 const bool needsToTraverseChildrenNodes = processCurrentNodeForExactMatch(siblingPos,
556 startInputIndex, depth, newWord, &firstChildPos, &count, &terminal, &newFreq,
557 &siblingPos);
558 mStackSiblingPos[depth] = siblingPos;
559 if (depth == (inputLength - 1)) {
560 // Traverse sibling node
561 if (terminal) {
562 if (newFreq > maxFreq) {
563 for (int i = 0; i < inputLength; ++i) word[i] = newWord[i];
564 if (DEBUG_DICT && DEBUG_NODE) {
565 char s[inputLength + 1];
566 for (int i = 0; i < inputLength; ++i) s[i] = word[i];
567 s[inputLength] = 0;
568 LOGI("New missing space word found: %d > %d (%s), %d, %d",
569 newFreq, maxFreq, s, inputLength, depth);
570 }
571 maxFreq = newFreq;
572 }
573 }
574 } else if (needsToTraverseChildrenNodes) {
575 // Traverse children nodes
576 ++depth;
577 mStackChildCount[depth] = count;
578 mStackSiblingPos[depth] = firstChildPos;
579 }
580 } else {
581 // Traverse parent node
582 --depth;
satok662fe692010-12-08 17:05:39 +0900583 }
584 }
satokaee09dc2010-12-09 19:21:51 +0900585
586 word[inputLength] = 0;
587 return maxFreq;
satok662fe692010-12-08 17:05:39 +0900588}
589
590inline bool UnigramDictionary::processCurrentNodeForExactMatch(const int firstChildPos,
satokaee09dc2010-12-09 19:21:51 +0900591 const int startInputIndex, const int depth, unsigned short *word, int *newChildPosition,
592 int *newCount, bool *newTerminal, int *newFreq, int *siblingPos) {
593 const int inputIndex = startInputIndex + depth;
satok662fe692010-12-08 17:05:39 +0900594 const int *currentChars = mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS);
satok662fe692010-12-08 17:05:39 +0900595 unsigned short c;
satokaee09dc2010-12-09 19:21:51 +0900596 *siblingPos = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, firstChildPos, &c,
597 newChildPosition, newTerminal, newFreq);
598 const unsigned int inputC = currentChars[0];
599 if (DEBUG_DICT) assert(inputC <= U_SHORT_MAX);
600 const unsigned short lowerC = toLowerCase(c);
601 const bool matched = (inputC == lowerC || inputC == c);
602 const bool hasChild = *newChildPosition != 0;
603 if (matched) {
604 word[depth] = c;
605 if (DEBUG_DICT && DEBUG_NODE) {
606 LOGI("Node(%c, %c)<%d>, %d, %d", inputC, c, matched, hasChild, *newFreq);
607 if (*newTerminal) LOGI("Terminal %d", *newFreq);
satok662fe692010-12-08 17:05:39 +0900608 }
satokaee09dc2010-12-09 19:21:51 +0900609 if (hasChild) {
610 *newCount = Dictionary::getCount(DICT, newChildPosition);
611 return true;
612 } else {
613 return false;
614 }
615 } else {
616 // If this node is not user typed character, this method treats this word as unmatched.
617 // Thus newTerminal shouldn't be true.
618 *newTerminal = false;
619 return false;
satok662fe692010-12-08 17:05:39 +0900620 }
satok662fe692010-12-08 17:05:39 +0900621}
satok30088252010-12-01 21:22:15 +0900622} // namespace latinime