blob: 72b0f361af462d03eae4d7daf68b6110a4fc5da7 [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
satok8fbd5522011-02-22 17:28:55 +090044int UnigramDictionary::getSuggestions(ProximityInfo *proximityInfo, int *xcoordinates,
45 int *ycoordinates, int *codes, int codesSize, unsigned short *outWords, 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
satok8fbd5522011-02-22 17:28:55 +090051 if (DEBUG_PROXIMITY_INFO) {
52 for (int i = 0; i < codesSize; ++i) {
53 LOGI("Input[%d] x = %d, y = %d", i, xcoordinates[i], ycoordinates[i]);
54 }
55 }
56
satoka3d78f62010-12-09 22:08:33 +090057 const int MAX_DEPTH = min(mInputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH);
satok61e2f852011-01-05 14:13:07 +090058 PROF_END(0);
satok30088252010-12-01 21:22:15 +090059
satok61e2f852011-01-05 14:13:07 +090060 PROF_START(1);
Tadashi G. Takaoka887f11e2011-02-10 20:53:58 +090061 getSuggestionCandidates(-1, -1, -1, mNextLettersFrequency, NEXT_LETTERS_SIZE, MAX_DEPTH);
satok61e2f852011-01-05 14:13:07 +090062 PROF_END(1);
63
64 PROF_START(2);
satok662fe692010-12-08 17:05:39 +090065 // Suggestion with missing character
66 if (SUGGEST_WORDS_WITH_MISSING_CHARACTER) {
satok30088252010-12-01 21:22:15 +090067 for (int i = 0; i < codesSize; ++i) {
satokcdbbea72010-12-08 16:04:16 +090068 if (DEBUG_DICT) LOGI("--- Suggest missing characters %d", i);
satok54fe9e02010-12-13 14:42:35 +090069 getSuggestionCandidates(i, -1, -1, NULL, 0, MAX_DEPTH);
satokcdbbea72010-12-08 16:04:16 +090070 }
71 }
satok61e2f852011-01-05 14:13:07 +090072 PROF_END(2);
satokcdbbea72010-12-08 16:04:16 +090073
satok61e2f852011-01-05 14:13:07 +090074 PROF_START(3);
satok662fe692010-12-08 17:05:39 +090075 // Suggestion with excessive character
satok54fe9e02010-12-13 14:42:35 +090076 if (SUGGEST_WORDS_WITH_EXCESSIVE_CHARACTER
77 && mInputLength >= MIN_USER_TYPED_LENGTH_FOR_EXCESSIVE_CHARACTER_SUGGESTION) {
satokcdbbea72010-12-08 16:04:16 +090078 for (int i = 0; i < codesSize; ++i) {
satok54fe9e02010-12-13 14:42:35 +090079 if (DEBUG_DICT) LOGI("--- Suggest excessive characters %d", i);
80 getSuggestionCandidates(-1, i, -1, NULL, 0, MAX_DEPTH);
satok30088252010-12-01 21:22:15 +090081 }
82 }
satok61e2f852011-01-05 14:13:07 +090083 PROF_END(3);
satok30088252010-12-01 21:22:15 +090084
satok61e2f852011-01-05 14:13:07 +090085 PROF_START(4);
satoka3d78f62010-12-09 22:08:33 +090086 // Suggestion with transposed characters
87 // Only suggest words that length is mInputLength
88 if (SUGGEST_WORDS_WITH_TRANSPOSED_CHARACTERS) {
89 for (int i = 0; i < codesSize; ++i) {
90 if (DEBUG_DICT) LOGI("--- Suggest transposed characters %d", i);
satok54fe9e02010-12-13 14:42:35 +090091 getSuggestionCandidates(-1, -1, i, NULL, 0, mInputLength - 1);
satoka3d78f62010-12-09 22:08:33 +090092 }
93 }
satok61e2f852011-01-05 14:13:07 +090094 PROF_END(4);
satoka3d78f62010-12-09 22:08:33 +090095
satok61e2f852011-01-05 14:13:07 +090096 PROF_START(5);
satok662fe692010-12-08 17:05:39 +090097 // Suggestions with missing space
satok54fe9e02010-12-13 14:42:35 +090098 if (SUGGEST_WORDS_WITH_MISSING_SPACE_CHARACTER
99 && mInputLength >= MIN_USER_TYPED_LENGTH_FOR_MISSING_SPACE_SUGGESTION) {
satok662fe692010-12-08 17:05:39 +0900100 for (int i = 1; i < codesSize; ++i) {
101 if (DEBUG_DICT) LOGI("--- Suggest missing space characters %d", i);
102 getMissingSpaceWords(mInputLength, i);
103 }
104 }
satok61e2f852011-01-05 14:13:07 +0900105 PROF_END(5);
satok662fe692010-12-08 17:05:39 +0900106
satok61e2f852011-01-05 14:13:07 +0900107 PROF_START(6);
satok662fe692010-12-08 17:05:39 +0900108 // Get the word count
109 int suggestedWordsCount = 0;
110 while (suggestedWordsCount < MAX_WORDS && mFrequencies[suggestedWordsCount] > 0) {
111 suggestedWordsCount++;
112 }
113
satok30088252010-12-01 21:22:15 +0900114 if (DEBUG_DICT) {
115 LOGI("Returning %d words", suggestedWordsCount);
116 LOGI("Next letters: ");
Tadashi G. Takaoka887f11e2011-02-10 20:53:58 +0900117 for (int k = 0; k < NEXT_LETTERS_SIZE; k++) {
118 if (mNextLettersFrequency[k] > 0) {
119 LOGI("%c = %d,", k, mNextLettersFrequency[k]);
satok30088252010-12-01 21:22:15 +0900120 }
121 }
satok30088252010-12-01 21:22:15 +0900122 }
satok61e2f852011-01-05 14:13:07 +0900123 PROF_END(6);
124 PROF_CLOSE;
satok30088252010-12-01 21:22:15 +0900125 return suggestedWordsCount;
126}
127
128void UnigramDictionary::initSuggestions(int *codes, int codesSize, unsigned short *outWords,
129 int *frequencies) {
satokf5cded12010-12-06 21:28:24 +0900130 if (DEBUG_DICT) LOGI("initSuggest");
satok30088252010-12-01 21:22:15 +0900131 mFrequencies = frequencies;
132 mOutputChars = outWords;
133 mInputCodes = codes;
134 mInputLength = codesSize;
135 mMaxEditDistance = mInputLength < 5 ? 2 : mInputLength / 2;
136}
137
satok715514d2010-12-02 20:19:59 +0900138void UnigramDictionary::registerNextLetter(
139 unsigned short c, int *nextLetters, int nextLettersSize) {
satok30088252010-12-01 21:22:15 +0900140 if (c < nextLettersSize) {
141 nextLetters[c]++;
142 }
143}
144
satok662fe692010-12-08 17:05:39 +0900145// TODO: We need to optimize addWord by using STL or something
satok28bd03b2010-12-03 16:39:16 +0900146bool UnigramDictionary::addWord(unsigned short *word, int length, int frequency) {
satok30088252010-12-01 21:22:15 +0900147 word[length] = 0;
satok662fe692010-12-08 17:05:39 +0900148 if (DEBUG_DICT && DEBUG_SHOW_FOUND_WORD) {
satok30088252010-12-01 21:22:15 +0900149 char s[length + 1];
150 for (int i = 0; i <= length; i++) s[i] = word[i];
satok662fe692010-12-08 17:05:39 +0900151 LOGI("Found word = %s, freq = %d", s, frequency);
satok30088252010-12-01 21:22:15 +0900152 }
satokf5cded12010-12-06 21:28:24 +0900153 if (length > MAX_WORD_LENGTH) {
154 if (DEBUG_DICT) LOGI("Exceeded max word length.");
155 return false;
156 }
satok30088252010-12-01 21:22:15 +0900157
158 // Find the right insertion point
159 int insertAt = 0;
160 while (insertAt < MAX_WORDS) {
satok715514d2010-12-02 20:19:59 +0900161 if (frequency > mFrequencies[insertAt] || (mFrequencies[insertAt] == frequency
162 && length < Dictionary::wideStrLen(mOutputChars + insertAt * MAX_WORD_LENGTH))) {
satok30088252010-12-01 21:22:15 +0900163 break;
164 }
165 insertAt++;
166 }
167 if (insertAt < MAX_WORDS) {
satokcdbbea72010-12-08 16:04:16 +0900168 if (DEBUG_DICT) {
169 char s[length + 1];
170 for (int i = 0; i <= length; i++) s[i] = word[i];
satok662fe692010-12-08 17:05:39 +0900171 LOGI("Added word = %s, freq = %d", s, frequency);
satokcdbbea72010-12-08 16:04:16 +0900172 }
satok30088252010-12-01 21:22:15 +0900173 memmove((char*) mFrequencies + (insertAt + 1) * sizeof(mFrequencies[0]),
174 (char*) mFrequencies + insertAt * sizeof(mFrequencies[0]),
175 (MAX_WORDS - insertAt - 1) * sizeof(mFrequencies[0]));
176 mFrequencies[insertAt] = frequency;
177 memmove((char*) mOutputChars + (insertAt + 1) * MAX_WORD_LENGTH * sizeof(short),
satok715514d2010-12-02 20:19:59 +0900178 (char*) mOutputChars + insertAt * MAX_WORD_LENGTH * sizeof(short),
satok30088252010-12-01 21:22:15 +0900179 (MAX_WORDS - insertAt - 1) * sizeof(short) * MAX_WORD_LENGTH);
satok715514d2010-12-02 20:19:59 +0900180 unsigned short *dest = mOutputChars + insertAt * MAX_WORD_LENGTH;
satok30088252010-12-01 21:22:15 +0900181 while (length--) {
182 *dest++ = *word++;
183 }
184 *dest = 0; // NULL terminate
satok662fe692010-12-08 17:05:39 +0900185 if (DEBUG_DICT) LOGI("Added word at %d", insertAt);
satok30088252010-12-01 21:22:15 +0900186 return true;
187 }
188 return false;
189}
190
Jean Chalardf5f834a2011-02-22 15:12:46 +0900191unsigned short UnigramDictionary::toBaseLowerCase(unsigned short c) {
satok30088252010-12-01 21:22:15 +0900192 if (c < sizeof(BASE_CHARS) / sizeof(BASE_CHARS[0])) {
193 c = BASE_CHARS[c];
194 }
195 if (c >='A' && c <= 'Z') {
196 c |= 32;
197 } else if (c > 127) {
198 c = latin_tolower(c);
199 }
200 return c;
201}
202
satok28bd03b2010-12-03 16:39:16 +0900203bool UnigramDictionary::sameAsTyped(unsigned short *word, int length) {
satok30088252010-12-01 21:22:15 +0900204 if (length != mInputLength) {
205 return false;
206 }
207 int *inputCodes = mInputCodes;
208 while (length--) {
209 if ((unsigned int) *inputCodes != (unsigned int) *word) {
210 return false;
211 }
satok662fe692010-12-08 17:05:39 +0900212 inputCodes += MAX_PROXIMITY_CHARS;
satok30088252010-12-01 21:22:15 +0900213 word++;
214 }
215 return true;
216}
217
satok715514d2010-12-02 20:19:59 +0900218static const char QUOTE = '\'';
satok662fe692010-12-08 17:05:39 +0900219static const char SPACE = ' ';
satok30088252010-12-01 21:22:15 +0900220
satok54fe9e02010-12-13 14:42:35 +0900221void UnigramDictionary::getSuggestionCandidates(const int skipPos,
satoka3d78f62010-12-09 22:08:33 +0900222 const int excessivePos, const int transposedPos, int *nextLetters,
223 const int nextLettersSize, const int maxDepth) {
satok54fe9e02010-12-13 14:42:35 +0900224 if (DEBUG_DICT) {
225 LOGI("getSuggestionCandidates %d", maxDepth);
226 assert(transposedPos + 1 < mInputLength);
227 assert(excessivePos < mInputLength);
228 assert(missingPos < mInputLength);
229 }
satok662fe692010-12-08 17:05:39 +0900230 int rootPosition = ROOT_POS;
satokd2997922010-12-07 13:08:39 +0900231 // Get the number of child of root, then increment the position
232 int childCount = Dictionary::getCount(DICT, &rootPosition);
233 int depth = 0;
234
235 mStackChildCount[0] = childCount;
236 mStackTraverseAll[0] = (mInputLength <= 0);
237 mStackNodeFreq[0] = 1;
238 mStackInputIndex[0] = 0;
239 mStackDiffs[0] = 0;
240 mStackSiblingPos[0] = rootPosition;
241
satok662fe692010-12-08 17:05:39 +0900242 // Depth first search
satokd2997922010-12-07 13:08:39 +0900243 while (depth >= 0) {
244 if (mStackChildCount[depth] > 0) {
245 --mStackChildCount[depth];
246 bool traverseAllNodes = mStackTraverseAll[depth];
Jean Chalardf5f834a2011-02-22 15:12:46 +0900247 int matchWeight = mStackNodeFreq[depth];
satokd2997922010-12-07 13:08:39 +0900248 int inputIndex = mStackInputIndex[depth];
249 int diffs = mStackDiffs[depth];
250 int siblingPos = mStackSiblingPos[depth];
251 int firstChildPos;
satoka3d78f62010-12-09 22:08:33 +0900252 // depth will never be greater than maxDepth because in that case,
satokd2997922010-12-07 13:08:39 +0900253 // needsToTraverseChildrenNodes should be false
254 const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900255 maxDepth, traverseAllNodes, matchWeight, inputIndex, diffs, skipPos,
256 excessivePos, transposedPos, nextLetters, nextLettersSize, &childCount,
257 &firstChildPos, &traverseAllNodes, &matchWeight, &inputIndex, &diffs,
258 &siblingPos);
satok662fe692010-12-08 17:05:39 +0900259 // Update next sibling pos
satokd2997922010-12-07 13:08:39 +0900260 mStackSiblingPos[depth] = siblingPos;
261 if (needsToTraverseChildrenNodes) {
262 // Goes to child node
263 ++depth;
264 mStackChildCount[depth] = childCount;
265 mStackTraverseAll[depth] = traverseAllNodes;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900266 mStackNodeFreq[depth] = matchWeight;
satokd2997922010-12-07 13:08:39 +0900267 mStackInputIndex[depth] = inputIndex;
268 mStackDiffs[depth] = diffs;
269 mStackSiblingPos[depth] = firstChildPos;
270 }
271 } else {
satokcdbbea72010-12-08 16:04:16 +0900272 // Goes to parent sibling node
satokd2997922010-12-07 13:08:39 +0900273 --depth;
274 }
275 }
276}
277
satokf7425bb2011-01-05 16:37:53 +0900278inline static void multiplyRate(const int rate, int *freq) {
279 if (rate > 1000000) {
280 *freq = (*freq / 100) * rate;
281 } else {
282 *freq = *freq * rate / 100;
283 }
284}
285
satok662fe692010-12-08 17:05:39 +0900286bool UnigramDictionary::getMissingSpaceWords(const int inputLength, const int missingSpacePos) {
satokaee09dc2010-12-09 19:21:51 +0900287 if (missingSpacePos <= 0 || missingSpacePos >= inputLength
288 || inputLength >= MAX_WORD_LENGTH) return false;
satok662fe692010-12-08 17:05:39 +0900289 const int newWordLength = inputLength + 1;
290 // Allocating variable length array on stack
291 unsigned short word[newWordLength];
satokaee09dc2010-12-09 19:21:51 +0900292 const int firstFreq = getBestWordFreq(0, missingSpacePos, mWord);
293 if (DEBUG_DICT) LOGI("First freq: %d", firstFreq);
294 if (firstFreq <= 0) return false;
295
satok662fe692010-12-08 17:05:39 +0900296 for (int i = 0; i < missingSpacePos; ++i) {
satokaee09dc2010-12-09 19:21:51 +0900297 word[i] = mWord[i];
satok662fe692010-12-08 17:05:39 +0900298 }
satokaee09dc2010-12-09 19:21:51 +0900299
300 const int secondFreq = getBestWordFreq(missingSpacePos, inputLength - missingSpacePos, mWord);
satoka3d78f62010-12-09 22:08:33 +0900301 if (DEBUG_DICT) LOGI("Second freq: %d", secondFreq);
satokaee09dc2010-12-09 19:21:51 +0900302 if (secondFreq <= 0) return false;
303
satok662fe692010-12-08 17:05:39 +0900304 word[missingSpacePos] = SPACE;
305 for (int i = (missingSpacePos + 1); i < newWordLength; ++i) {
satokaee09dc2010-12-09 19:21:51 +0900306 word[i] = mWord[i - missingSpacePos - 1];
satok662fe692010-12-08 17:05:39 +0900307 }
satokaee09dc2010-12-09 19:21:51 +0900308
309 int pairFreq = ((firstFreq + secondFreq) / 2);
310 for (int i = 0; i < inputLength; ++i) pairFreq *= TYPED_LETTER_MULTIPLIER;
satokf7425bb2011-01-05 16:37:53 +0900311 multiplyRate(WORDS_WITH_MISSING_SPACE_CHARACTER_DEMOTION_RATE, &pairFreq);
satok662fe692010-12-08 17:05:39 +0900312 addWord(word, newWordLength, pairFreq);
313 return true;
314}
315
316// Keep this for comparing spec to new getWords
317void UnigramDictionary::getWordsOld(const int initialPos, const int inputLength, const int skipPos,
satoka3d78f62010-12-09 22:08:33 +0900318 const int excessivePos, const int transposedPos,int *nextLetters,
319 const int nextLettersSize) {
satok662fe692010-12-08 17:05:39 +0900320 int initialPosition = initialPos;
321 const int count = Dictionary::getCount(DICT, &initialPosition);
322 getWordsRec(count, initialPosition, 0,
323 min(inputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH),
satoka3d78f62010-12-09 22:08:33 +0900324 mInputLength <= 0, 1, 0, 0, skipPos, excessivePos, transposedPos, nextLetters,
325 nextLettersSize);
satok662fe692010-12-08 17:05:39 +0900326}
327
satok68319262010-12-03 19:38:08 +0900328void UnigramDictionary::getWordsRec(const int childrenCount, const int pos, const int depth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900329 const int maxDepth, const bool traverseAllNodes, const int matchWeight,
330 const int inputIndex, const int diffs, const int skipPos, const int excessivePos,
331 const int transposedPos, int *nextLetters, const int nextLettersSize) {
satok48e432c2010-12-06 17:38:58 +0900332 int siblingPos = pos;
satok68319262010-12-03 19:38:08 +0900333 for (int i = 0; i < childrenCount; ++i) {
satok48e432c2010-12-06 17:38:58 +0900334 int newCount;
335 int newChildPosition;
satokd2997922010-12-07 13:08:39 +0900336 const int newDepth = depth + 1;
satok48e432c2010-12-06 17:38:58 +0900337 bool newTraverseAllNodes;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900338 int newMatchRate;
satok48e432c2010-12-06 17:38:58 +0900339 int newInputIndex;
340 int newDiffs;
341 int newSiblingPos;
342 const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth, maxDepth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900343 traverseAllNodes, matchWeight, inputIndex, diffs,
344 skipPos, excessivePos, transposedPos,
satoka3d78f62010-12-09 22:08:33 +0900345 nextLetters, nextLettersSize,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900346 &newCount, &newChildPosition, &newTraverseAllNodes, &newMatchRate,
satok48e432c2010-12-06 17:38:58 +0900347 &newInputIndex, &newDiffs, &newSiblingPos);
348 siblingPos = newSiblingPos;
satok30088252010-12-01 21:22:15 +0900349
satok48e432c2010-12-06 17:38:58 +0900350 if (needsToTraverseChildrenNodes) {
351 getWordsRec(newCount, newChildPosition, newDepth, maxDepth, newTraverseAllNodes,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900352 newMatchRate, newInputIndex, newDiffs, skipPos, excessivePos, transposedPos,
satoka3d78f62010-12-09 22:08:33 +0900353 nextLetters, nextLettersSize);
satok30088252010-12-01 21:22:15 +0900354 }
355 }
356}
357
Jean Chalarda5d58492011-02-18 17:50:58 +0900358static const int TWO_31ST_DIV_255 = ((1 << 31) - 1) / 255;
359static inline int capped255MultForFullMatchAccentsOrCapitalizationDifference(const int num) {
360 return (num < TWO_31ST_DIV_255 ? 255 * num : S_INT_MAX);
361}
satok58c49b92011-01-27 03:23:39 +0900362inline int UnigramDictionary::calculateFinalFreq(const int inputIndex, const int depth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900363 const int matchWeight, const int skipPos, const int excessivePos, const int transposedPos,
satok58c49b92011-01-27 03:23:39 +0900364 const int freq, const bool sameLength) {
satoka3d78f62010-12-09 22:08:33 +0900365 // TODO: Demote by edit distance
Jean Chalardf5f834a2011-02-22 15:12:46 +0900366 int finalFreq = freq * matchWeight;
satokf7425bb2011-01-05 16:37:53 +0900367 if (skipPos >= 0) multiplyRate(WORDS_WITH_MISSING_CHARACTER_DEMOTION_RATE, &finalFreq);
368 if (transposedPos >= 0) multiplyRate(
369 WORDS_WITH_TRANSPOSED_CHARACTERS_DEMOTION_RATE, &finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900370 if (excessivePos >= 0) {
satokf7425bb2011-01-05 16:37:53 +0900371 multiplyRate(WORDS_WITH_EXCESSIVE_CHARACTER_DEMOTION_RATE, &finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900372 if (!existsAdjacentProximityChars(inputIndex, mInputLength)) {
satokf7425bb2011-01-05 16:37:53 +0900373 multiplyRate(WORDS_WITH_EXCESSIVE_CHARACTER_OUT_OF_PROXIMITY_DEMOTION_RATE, &finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900374 }
375 }
satok58c49b92011-01-27 03:23:39 +0900376 int lengthFreq = TYPED_LETTER_MULTIPLIER;
377 for (int i = 0; i < depth; ++i) lengthFreq *= TYPED_LETTER_MULTIPLIER;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900378 if (lengthFreq == matchWeight) {
Jean Chalard8dc754a2011-01-27 14:20:22 +0900379 if (depth > 1) {
380 if (DEBUG_DICT) LOGI("Found full matched word.");
381 multiplyRate(FULL_MATCHED_WORDS_PROMOTION_RATE, &finalFreq);
382 }
383 if (sameLength && transposedPos < 0 && skipPos < 0 && excessivePos < 0) {
Jean Chalarda5d58492011-02-18 17:50:58 +0900384 finalFreq = capped255MultForFullMatchAccentsOrCapitalizationDifference(finalFreq);
Jean Chalard8dc754a2011-01-27 14:20:22 +0900385 }
satok58c49b92011-01-27 03:23:39 +0900386 }
satok54fe9e02010-12-13 14:42:35 +0900387 if (sameLength && skipPos < 0) finalFreq *= FULL_WORD_MULTIPLIER;
388 return finalFreq;
389}
satoka3d78f62010-12-09 22:08:33 +0900390
satok54fe9e02010-12-13 14:42:35 +0900391inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsGreaterThanInputLength(
Jean Chalardf5f834a2011-02-22 15:12:46 +0900392 unsigned short *word, const int inputIndex, const int depth, const int matchWeight,
satok54fe9e02010-12-13 14:42:35 +0900393 int *nextLetters, const int nextLettersSize, const int skipPos, const int excessivePos,
394 const int transposedPos, const int freq) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900395 const int finalFreq = calculateFinalFreq(inputIndex, depth, matchWeight, skipPos, excessivePos,
satok58c49b92011-01-27 03:23:39 +0900396 transposedPos, freq, false);
satoka3d78f62010-12-09 22:08:33 +0900397 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900398 if (depth >= mInputLength && skipPos < 0) {
satok715514d2010-12-02 20:19:59 +0900399 registerNextLetter(mWord[mInputLength], nextLetters, nextLettersSize);
400 }
401}
402
403inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsSameAsInputLength(
Jean Chalardf5f834a2011-02-22 15:12:46 +0900404 unsigned short *word, const int inputIndex, const int depth, const int matchWeight,
Jean Chalard8dc754a2011-01-27 14:20:22 +0900405 const int skipPos, const int excessivePos, const int transposedPos, const int freq) {
satok54fe9e02010-12-13 14:42:35 +0900406 if (sameAsTyped(word, depth + 1)) return;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900407 const int finalFreq = calculateFinalFreq(inputIndex, depth, matchWeight, skipPos,
satok54fe9e02010-12-13 14:42:35 +0900408 excessivePos, transposedPos, freq, true);
409 // Proximity collection will promote a word of the same length as what user typed.
410 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq);
satok715514d2010-12-02 20:19:59 +0900411}
satok28bd03b2010-12-03 16:39:16 +0900412
413inline bool UnigramDictionary::needsToSkipCurrentNode(const unsigned short c,
satok68319262010-12-03 19:38:08 +0900414 const int inputIndex, const int skipPos, const int depth) {
satok8fbd5522011-02-22 17:28:55 +0900415 const unsigned short userTypedChar = getInputCharsAt(inputIndex)[0];
satok28bd03b2010-12-03 16:39:16 +0900416 // Skip the ' or other letter and continue deeper
417 return (c == QUOTE && userTypedChar != QUOTE) || skipPos == depth;
418}
419
satoke07baa62010-12-09 21:55:40 +0900420inline bool UnigramDictionary::existsAdjacentProximityChars(const int inputIndex,
421 const int inputLength) {
422 if (inputIndex < 0 || inputIndex >= inputLength) return false;
423 const int currentChar = *getInputCharsAt(inputIndex);
424 const int leftIndex = inputIndex - 1;
425 if (leftIndex >= 0) {
426 int *leftChars = getInputCharsAt(leftIndex);
427 int i = 0;
428 while (leftChars[i] > 0 && i < MAX_PROXIMITY_CHARS) {
429 if (leftChars[i++] == currentChar) return true;
430 }
431 }
432 const int rightIndex = inputIndex + 1;
433 if (rightIndex < inputLength) {
434 int *rightChars = getInputCharsAt(rightIndex);
435 int i = 0;
436 while (rightChars[i] > 0 && i < MAX_PROXIMITY_CHARS) {
437 if (rightChars[i++] == currentChar) return true;
438 }
439 }
440 return false;
441}
442
Jean Chalarda5d58492011-02-18 17:50:58 +0900443
444// In the following function, c is the current character of the dictionary word
445// currently examined.
446// currentChars is an array containing the keys close to the character the
447// user actually typed at the same position. We want to see if c is in it: if so,
448// then the word contains at that position a character close to what the user
449// typed.
450// What the user typed is actually the first character of the array.
451// Notice : accented characters do not have a proximity list, so they are alone
452// in their list. The non-accented version of the character should be considered
453// "close", but not the other keys close to the non-accented version.
Jean Chalard8dc754a2011-01-27 14:20:22 +0900454inline UnigramDictionary::ProximityType UnigramDictionary::getMatchedProximityId(
455 const int *currentChars, const unsigned short c, const int skipPos,
456 const int excessivePos, const int transposedPos) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900457 const unsigned short baseLowerC = toBaseLowerCase(c);
Jean Chalarda5d58492011-02-18 17:50:58 +0900458
459 // The first char in the array is what user typed. If it matches right away,
460 // that means the user typed that same char for this pos.
Jean Chalardf5f834a2011-02-22 15:12:46 +0900461 if (currentChars[0] == baseLowerC || currentChars[0] == c)
Jean Chalarda5d58492011-02-18 17:50:58 +0900462 return SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR;
463
464 // If one of those is true, we should not check for close characters at all.
465 if (skipPos >= 0 || excessivePos >= 0 || transposedPos >= 0)
466 return UNRELATED_CHAR;
467
468 // If the non-accented, lowercased version of that first character matches c,
469 // then we have a non-accented version of the accented character the user
470 // typed. Treat it as a close char.
Jean Chalardf5f834a2011-02-22 15:12:46 +0900471 if (toBaseLowerCase(currentChars[0]) == baseLowerC)
Jean Chalarda5d58492011-02-18 17:50:58 +0900472 return NEAR_PROXIMITY_CHAR;
473
474 // Not an exact nor an accent-alike match: search the list of close keys
475 int j = 1;
satoke07baa62010-12-09 21:55:40 +0900476 while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900477 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
Jean Chalarda5d58492011-02-18 17:50:58 +0900478 if (matched) return NEAR_PROXIMITY_CHAR;
satok28bd03b2010-12-03 16:39:16 +0900479 ++j;
480 }
Jean Chalarda5d58492011-02-18 17:50:58 +0900481
482 // Was not included, signal this as an unrelated character.
Jean Chalard8dc754a2011-01-27 14:20:22 +0900483 return UNRELATED_CHAR;
satok28bd03b2010-12-03 16:39:16 +0900484}
485
satok48e432c2010-12-06 17:38:58 +0900486inline bool UnigramDictionary::processCurrentNode(const int pos, const int depth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900487 const int maxDepth, const bool traverseAllNodes, int matchWeight, int inputIndex,
satoka3d78f62010-12-09 22:08:33 +0900488 const int diffs, const int skipPos, const int excessivePos, const int transposedPos,
489 int *nextLetters, const int nextLettersSize, int *newCount, int *newChildPosition,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900490 bool *newTraverseAllNodes, int *newMatchRate, int *newInputIndex, int *newDiffs,
satoka3d78f62010-12-09 22:08:33 +0900491 int *nextSiblingPosition) {
492 if (DEBUG_DICT) {
493 int inputCount = 0;
494 if (skipPos >= 0) ++inputCount;
495 if (excessivePos >= 0) ++inputCount;
496 if (transposedPos >= 0) ++inputCount;
497 assert(inputCount <= 1);
498 }
satok48e432c2010-12-06 17:38:58 +0900499 unsigned short c;
500 int childPosition;
501 bool terminal;
502 int freq;
satokfd16f1d2011-01-27 16:25:16 +0900503 bool isSameAsUserTypedLength = false;
satokcdbbea72010-12-08 16:04:16 +0900504
satokfd16f1d2011-01-27 16:25:16 +0900505 if (excessivePos == depth && inputIndex < mInputLength - 1) ++inputIndex;
satokcdbbea72010-12-08 16:04:16 +0900506
satok48e432c2010-12-06 17:38:58 +0900507 *nextSiblingPosition = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, pos, &c,
508 &childPosition, &terminal, &freq);
509
510 const bool needsToTraverseChildrenNodes = childPosition != 0;
511
512 // If we are only doing traverseAllNodes, no need to look at the typed characters.
513 if (traverseAllNodes || needsToSkipCurrentNode(c, inputIndex, skipPos, depth)) {
514 mWord[depth] = c;
515 if (traverseAllNodes && terminal) {
satok54fe9e02010-12-13 14:42:35 +0900516 onTerminalWhenUserTypedLengthIsGreaterThanInputLength(mWord, inputIndex, depth,
Jean Chalardf5f834a2011-02-22 15:12:46 +0900517 matchWeight, nextLetters, nextLettersSize, skipPos, excessivePos, transposedPos,
518 freq);
satok48e432c2010-12-06 17:38:58 +0900519 }
520 if (!needsToTraverseChildrenNodes) return false;
521 *newTraverseAllNodes = traverseAllNodes;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900522 *newMatchRate = matchWeight;
satok48e432c2010-12-06 17:38:58 +0900523 *newDiffs = diffs;
524 *newInputIndex = inputIndex;
satok48e432c2010-12-06 17:38:58 +0900525 } else {
satok8fbd5522011-02-22 17:28:55 +0900526 int *currentChars = getInputCharsAt(inputIndex);
satoka3d78f62010-12-09 22:08:33 +0900527
528 if (transposedPos >= 0) {
529 if (inputIndex == transposedPos) currentChars += MAX_PROXIMITY_CHARS;
530 if (inputIndex == (transposedPos + 1)) currentChars -= MAX_PROXIMITY_CHARS;
531 }
532
533 int matchedProximityCharId = getMatchedProximityId(currentChars, c, skipPos, excessivePos,
534 transposedPos);
Jean Chalard8dc754a2011-01-27 14:20:22 +0900535 if (UNRELATED_CHAR == matchedProximityCharId) return false;
satok48e432c2010-12-06 17:38:58 +0900536 mWord[depth] = c;
537 // If inputIndex is greater than mInputLength, that means there is no
538 // proximity chars. So, we don't need to check proximity.
Jean Chalard8dc754a2011-01-27 14:20:22 +0900539 if (SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR == matchedProximityCharId) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900540 matchWeight = matchWeight * TYPED_LETTER_MULTIPLIER;
Jean Chalard8dc754a2011-01-27 14:20:22 +0900541 }
satokfd16f1d2011-01-27 16:25:16 +0900542 bool isSameAsUserTypedLength = mInputLength == inputIndex + 1
543 || (excessivePos == mInputLength - 1 && inputIndex == mInputLength - 2);
satok48e432c2010-12-06 17:38:58 +0900544 if (isSameAsUserTypedLength && terminal) {
Jean Chalardf5f834a2011-02-22 15:12:46 +0900545 onTerminalWhenUserTypedLengthIsSameAsInputLength(mWord, inputIndex, depth, matchWeight,
Jean Chalard8dc754a2011-01-27 14:20:22 +0900546 skipPos, excessivePos, transposedPos, freq);
satok48e432c2010-12-06 17:38:58 +0900547 }
548 if (!needsToTraverseChildrenNodes) return false;
549 // Start traversing all nodes after the index exceeds the user typed length
550 *newTraverseAllNodes = isSameAsUserTypedLength;
Jean Chalardf5f834a2011-02-22 15:12:46 +0900551 *newMatchRate = matchWeight;
Jean Chalard8dc754a2011-01-27 14:20:22 +0900552 *newDiffs = diffs + ((NEAR_PROXIMITY_CHAR == matchedProximityCharId) ? 1 : 0);
satok48e432c2010-12-06 17:38:58 +0900553 *newInputIndex = inputIndex + 1;
satok48e432c2010-12-06 17:38:58 +0900554 }
555 // Optimization: Prune out words that are too long compared to how much was typed.
satokd2997922010-12-07 13:08:39 +0900556 if (depth >= maxDepth || *newDiffs > mMaxEditDistance) {
satok48e432c2010-12-06 17:38:58 +0900557 return false;
558 }
559
560 // If inputIndex is greater than mInputLength, that means there are no proximity chars.
satokfd16f1d2011-01-27 16:25:16 +0900561 // TODO: Check if this can be isSameAsUserTypedLength only.
562 if (isSameAsUserTypedLength || mInputLength <= *newInputIndex) {
satok48e432c2010-12-06 17:38:58 +0900563 *newTraverseAllNodes = true;
564 }
565 // get the count of nodes and increment childAddress.
566 *newCount = Dictionary::getCount(DICT, &childPosition);
567 *newChildPosition = childPosition;
568 if (DEBUG_DICT) assert(needsToTraverseChildrenNodes);
569 return needsToTraverseChildrenNodes;
570}
571
satokaee09dc2010-12-09 19:21:51 +0900572inline int UnigramDictionary::getBestWordFreq(const int startInputIndex, const int inputLength,
573 unsigned short *word) {
satok662fe692010-12-08 17:05:39 +0900574 int pos = ROOT_POS;
575 int count = Dictionary::getCount(DICT, &pos);
satokaee09dc2010-12-09 19:21:51 +0900576 int maxFreq = 0;
577 int depth = 0;
578 unsigned short newWord[MAX_WORD_LENGTH_INTERNAL];
satok662fe692010-12-08 17:05:39 +0900579 bool terminal = false;
580
satokaee09dc2010-12-09 19:21:51 +0900581 mStackChildCount[0] = count;
582 mStackSiblingPos[0] = pos;
583
584 while (depth >= 0) {
585 if (mStackChildCount[depth] > 0) {
586 --mStackChildCount[depth];
587 int firstChildPos;
588 int newFreq;
589 int siblingPos = mStackSiblingPos[depth];
590 const bool needsToTraverseChildrenNodes = processCurrentNodeForExactMatch(siblingPos,
591 startInputIndex, depth, newWord, &firstChildPos, &count, &terminal, &newFreq,
592 &siblingPos);
593 mStackSiblingPos[depth] = siblingPos;
594 if (depth == (inputLength - 1)) {
595 // Traverse sibling node
596 if (terminal) {
597 if (newFreq > maxFreq) {
598 for (int i = 0; i < inputLength; ++i) word[i] = newWord[i];
599 if (DEBUG_DICT && DEBUG_NODE) {
600 char s[inputLength + 1];
601 for (int i = 0; i < inputLength; ++i) s[i] = word[i];
602 s[inputLength] = 0;
603 LOGI("New missing space word found: %d > %d (%s), %d, %d",
604 newFreq, maxFreq, s, inputLength, depth);
605 }
606 maxFreq = newFreq;
607 }
608 }
609 } else if (needsToTraverseChildrenNodes) {
610 // Traverse children nodes
611 ++depth;
612 mStackChildCount[depth] = count;
613 mStackSiblingPos[depth] = firstChildPos;
614 }
615 } else {
616 // Traverse parent node
617 --depth;
satok662fe692010-12-08 17:05:39 +0900618 }
619 }
satokaee09dc2010-12-09 19:21:51 +0900620
621 word[inputLength] = 0;
622 return maxFreq;
satok662fe692010-12-08 17:05:39 +0900623}
624
625inline bool UnigramDictionary::processCurrentNodeForExactMatch(const int firstChildPos,
satokaee09dc2010-12-09 19:21:51 +0900626 const int startInputIndex, const int depth, unsigned short *word, int *newChildPosition,
627 int *newCount, bool *newTerminal, int *newFreq, int *siblingPos) {
628 const int inputIndex = startInputIndex + depth;
satok8fbd5522011-02-22 17:28:55 +0900629 const int *currentChars = getInputCharsAt(inputIndex);
satok662fe692010-12-08 17:05:39 +0900630 unsigned short c;
satokaee09dc2010-12-09 19:21:51 +0900631 *siblingPos = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, firstChildPos, &c,
632 newChildPosition, newTerminal, newFreq);
633 const unsigned int inputC = currentChars[0];
634 if (DEBUG_DICT) assert(inputC <= U_SHORT_MAX);
Jean Chalardf5f834a2011-02-22 15:12:46 +0900635 const unsigned short baseLowerC = toBaseLowerCase(c);
636 const bool matched = (inputC == baseLowerC || inputC == c);
satokaee09dc2010-12-09 19:21:51 +0900637 const bool hasChild = *newChildPosition != 0;
638 if (matched) {
639 word[depth] = c;
640 if (DEBUG_DICT && DEBUG_NODE) {
641 LOGI("Node(%c, %c)<%d>, %d, %d", inputC, c, matched, hasChild, *newFreq);
642 if (*newTerminal) LOGI("Terminal %d", *newFreq);
satok662fe692010-12-08 17:05:39 +0900643 }
satokaee09dc2010-12-09 19:21:51 +0900644 if (hasChild) {
645 *newCount = Dictionary::getCount(DICT, newChildPosition);
646 return true;
647 } else {
648 return false;
649 }
650 } else {
651 // If this node is not user typed character, this method treats this word as unmatched.
652 // Thus newTerminal shouldn't be true.
653 *newTerminal = false;
654 return false;
satok662fe692010-12-08 17:05:39 +0900655 }
satok662fe692010-12-08 17:05:39 +0900656}
satok30088252010-12-01 21:22:15 +0900657} // namespace latinime