blob: f679001cfc7ef20f69cbd76d99c7f89fe2a87286 [file] [log] [blame]
satok30088252010-12-01 21:22:15 +09001/*
2**
3** Copyright 2010, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
satok48e432c2010-12-06 17:38:58 +090018#include <assert.h>
satok30088252010-12-01 21:22:15 +090019#include <fcntl.h>
satokf5cded12010-12-06 21:28:24 +090020#include <stdio.h>
satok30088252010-12-01 21:22:15 +090021#include <string.h>
22
satoke808e432010-12-02 14:53:24 +090023#define LOG_TAG "LatinIME: unigram_dictionary.cpp"
satok30088252010-12-01 21:22:15 +090024
satok30088252010-12-01 21:22:15 +090025#include "basechars.h"
26#include "char_utils.h"
satoke808e432010-12-02 14:53:24 +090027#include "dictionary.h"
28#include "unigram_dictionary.h"
satok30088252010-12-01 21:22:15 +090029
30namespace latinime {
31
satoke808e432010-12-02 14:53:24 +090032UnigramDictionary::UnigramDictionary(const unsigned char *dict, int typedLetterMultiplier,
satok662fe692010-12-08 17:05:39 +090033 int fullWordMultiplier, int maxWordLength, int maxWords, int maxProximityChars,
satok18c28f42010-12-02 18:11:54 +090034 const bool isLatestDictVersion)
satoke808e432010-12-02 14:53:24 +090035 : DICT(dict), MAX_WORD_LENGTH(maxWordLength),MAX_WORDS(maxWords),
satok662fe692010-12-08 17:05:39 +090036 MAX_PROXIMITY_CHARS(maxProximityChars), IS_LATEST_DICT_VERSION(isLatestDictVersion),
37 TYPED_LETTER_MULTIPLIER(typedLetterMultiplier), FULL_WORD_MULTIPLIER(fullWordMultiplier),
38 ROOT_POS(isLatestDictVersion ? DICTIONARY_HEADER_SIZE : 0) {
satoka3d78f62010-12-09 22:08:33 +090039 if (DEBUG_DICT) LOGI("UnigramDictionary - constructor");
satok30088252010-12-01 21:22:15 +090040}
41
satok18c28f42010-12-02 18:11:54 +090042UnigramDictionary::~UnigramDictionary() {}
satok30088252010-12-01 21:22:15 +090043
satok18c28f42010-12-02 18:11:54 +090044int UnigramDictionary::getSuggestions(int *codes, int codesSize, unsigned short *outWords,
45 int *frequencies, int *nextLetters, int nextLettersSize)
satok30088252010-12-01 21:22:15 +090046{
satok30088252010-12-01 21:22:15 +090047 initSuggestions(codes, codesSize, outWords, frequencies);
satok54fe9e02010-12-13 14:42:35 +090048 if (DEBUG_DICT) assert(codesSize == mInputLength);
49
satoka3d78f62010-12-09 22:08:33 +090050 const int MAX_DEPTH = min(mInputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH);
satok54fe9e02010-12-13 14:42:35 +090051 getSuggestionCandidates(-1, -1, -1, nextLetters, nextLettersSize, MAX_DEPTH);
satok30088252010-12-01 21:22:15 +090052
satok662fe692010-12-08 17:05:39 +090053 // Suggestion with missing character
54 if (SUGGEST_WORDS_WITH_MISSING_CHARACTER) {
satok30088252010-12-01 21:22:15 +090055 for (int i = 0; i < codesSize; ++i) {
satokcdbbea72010-12-08 16:04:16 +090056 if (DEBUG_DICT) LOGI("--- Suggest missing characters %d", i);
satok54fe9e02010-12-13 14:42:35 +090057 getSuggestionCandidates(i, -1, -1, NULL, 0, MAX_DEPTH);
satokcdbbea72010-12-08 16:04:16 +090058 }
59 }
60
satok662fe692010-12-08 17:05:39 +090061 // Suggestion with excessive character
satok54fe9e02010-12-13 14:42:35 +090062 if (SUGGEST_WORDS_WITH_EXCESSIVE_CHARACTER
63 && mInputLength >= MIN_USER_TYPED_LENGTH_FOR_EXCESSIVE_CHARACTER_SUGGESTION) {
satokcdbbea72010-12-08 16:04:16 +090064 for (int i = 0; i < codesSize; ++i) {
satok54fe9e02010-12-13 14:42:35 +090065 if (DEBUG_DICT) LOGI("--- Suggest excessive characters %d", i);
66 getSuggestionCandidates(-1, i, -1, NULL, 0, MAX_DEPTH);
satok30088252010-12-01 21:22:15 +090067 }
68 }
69
satoka3d78f62010-12-09 22:08:33 +090070 // Suggestion with transposed characters
71 // Only suggest words that length is mInputLength
72 if (SUGGEST_WORDS_WITH_TRANSPOSED_CHARACTERS) {
73 for (int i = 0; i < codesSize; ++i) {
74 if (DEBUG_DICT) LOGI("--- Suggest transposed characters %d", i);
satok54fe9e02010-12-13 14:42:35 +090075 getSuggestionCandidates(-1, -1, i, NULL, 0, mInputLength - 1);
satoka3d78f62010-12-09 22:08:33 +090076 }
77 }
78
satok662fe692010-12-08 17:05:39 +090079 // Suggestions with missing space
satok54fe9e02010-12-13 14:42:35 +090080 if (SUGGEST_WORDS_WITH_MISSING_SPACE_CHARACTER
81 && mInputLength >= MIN_USER_TYPED_LENGTH_FOR_MISSING_SPACE_SUGGESTION) {
satok662fe692010-12-08 17:05:39 +090082 for (int i = 1; i < codesSize; ++i) {
83 if (DEBUG_DICT) LOGI("--- Suggest missing space characters %d", i);
84 getMissingSpaceWords(mInputLength, i);
85 }
86 }
87
88 // Get the word count
89 int suggestedWordsCount = 0;
90 while (suggestedWordsCount < MAX_WORDS && mFrequencies[suggestedWordsCount] > 0) {
91 suggestedWordsCount++;
92 }
93
satok30088252010-12-01 21:22:15 +090094 if (DEBUG_DICT) {
95 LOGI("Returning %d words", suggestedWordsCount);
96 LOGI("Next letters: ");
97 for (int k = 0; k < nextLettersSize; k++) {
98 if (nextLetters[k] > 0) {
99 LOGI("%c = %d,", k, nextLetters[k]);
100 }
101 }
102 LOGI("\n");
103 }
satok662fe692010-12-08 17:05:39 +0900104
satok30088252010-12-01 21:22:15 +0900105 return suggestedWordsCount;
106}
107
108void UnigramDictionary::initSuggestions(int *codes, int codesSize, unsigned short *outWords,
109 int *frequencies) {
satokf5cded12010-12-06 21:28:24 +0900110 if (DEBUG_DICT) LOGI("initSuggest");
satok30088252010-12-01 21:22:15 +0900111 mFrequencies = frequencies;
112 mOutputChars = outWords;
113 mInputCodes = codes;
114 mInputLength = codesSize;
115 mMaxEditDistance = mInputLength < 5 ? 2 : mInputLength / 2;
116}
117
satok715514d2010-12-02 20:19:59 +0900118void UnigramDictionary::registerNextLetter(
119 unsigned short c, int *nextLetters, int nextLettersSize) {
satok30088252010-12-01 21:22:15 +0900120 if (c < nextLettersSize) {
121 nextLetters[c]++;
122 }
123}
124
satok662fe692010-12-08 17:05:39 +0900125// TODO: We need to optimize addWord by using STL or something
satok28bd03b2010-12-03 16:39:16 +0900126bool UnigramDictionary::addWord(unsigned short *word, int length, int frequency) {
satok30088252010-12-01 21:22:15 +0900127 word[length] = 0;
satok662fe692010-12-08 17:05:39 +0900128 if (DEBUG_DICT && DEBUG_SHOW_FOUND_WORD) {
satok30088252010-12-01 21:22:15 +0900129 char s[length + 1];
130 for (int i = 0; i <= length; i++) s[i] = word[i];
satok662fe692010-12-08 17:05:39 +0900131 LOGI("Found word = %s, freq = %d", s, frequency);
satok30088252010-12-01 21:22:15 +0900132 }
satokf5cded12010-12-06 21:28:24 +0900133 if (length > MAX_WORD_LENGTH) {
134 if (DEBUG_DICT) LOGI("Exceeded max word length.");
135 return false;
136 }
satok30088252010-12-01 21:22:15 +0900137
138 // Find the right insertion point
139 int insertAt = 0;
140 while (insertAt < MAX_WORDS) {
satok715514d2010-12-02 20:19:59 +0900141 if (frequency > mFrequencies[insertAt] || (mFrequencies[insertAt] == frequency
142 && length < Dictionary::wideStrLen(mOutputChars + insertAt * MAX_WORD_LENGTH))) {
satok30088252010-12-01 21:22:15 +0900143 break;
144 }
145 insertAt++;
146 }
147 if (insertAt < MAX_WORDS) {
satokcdbbea72010-12-08 16:04:16 +0900148 if (DEBUG_DICT) {
149 char s[length + 1];
150 for (int i = 0; i <= length; i++) s[i] = word[i];
satok662fe692010-12-08 17:05:39 +0900151 LOGI("Added word = %s, freq = %d", s, frequency);
satokcdbbea72010-12-08 16:04:16 +0900152 }
satok30088252010-12-01 21:22:15 +0900153 memmove((char*) mFrequencies + (insertAt + 1) * sizeof(mFrequencies[0]),
154 (char*) mFrequencies + insertAt * sizeof(mFrequencies[0]),
155 (MAX_WORDS - insertAt - 1) * sizeof(mFrequencies[0]));
156 mFrequencies[insertAt] = frequency;
157 memmove((char*) mOutputChars + (insertAt + 1) * MAX_WORD_LENGTH * sizeof(short),
satok715514d2010-12-02 20:19:59 +0900158 (char*) mOutputChars + insertAt * MAX_WORD_LENGTH * sizeof(short),
satok30088252010-12-01 21:22:15 +0900159 (MAX_WORDS - insertAt - 1) * sizeof(short) * MAX_WORD_LENGTH);
satok715514d2010-12-02 20:19:59 +0900160 unsigned short *dest = mOutputChars + insertAt * MAX_WORD_LENGTH;
satok30088252010-12-01 21:22:15 +0900161 while (length--) {
162 *dest++ = *word++;
163 }
164 *dest = 0; // NULL terminate
satok662fe692010-12-08 17:05:39 +0900165 if (DEBUG_DICT) LOGI("Added word at %d", insertAt);
satok30088252010-12-01 21:22:15 +0900166 return true;
167 }
168 return false;
169}
170
satok28bd03b2010-12-03 16:39:16 +0900171unsigned short UnigramDictionary::toLowerCase(unsigned short c) {
satok30088252010-12-01 21:22:15 +0900172 if (c < sizeof(BASE_CHARS) / sizeof(BASE_CHARS[0])) {
173 c = BASE_CHARS[c];
174 }
175 if (c >='A' && c <= 'Z') {
176 c |= 32;
177 } else if (c > 127) {
178 c = latin_tolower(c);
179 }
180 return c;
181}
182
satok28bd03b2010-12-03 16:39:16 +0900183bool UnigramDictionary::sameAsTyped(unsigned short *word, int length) {
satok30088252010-12-01 21:22:15 +0900184 if (length != mInputLength) {
185 return false;
186 }
187 int *inputCodes = mInputCodes;
188 while (length--) {
189 if ((unsigned int) *inputCodes != (unsigned int) *word) {
190 return false;
191 }
satok662fe692010-12-08 17:05:39 +0900192 inputCodes += MAX_PROXIMITY_CHARS;
satok30088252010-12-01 21:22:15 +0900193 word++;
194 }
195 return true;
196}
197
satok715514d2010-12-02 20:19:59 +0900198static const char QUOTE = '\'';
satok662fe692010-12-08 17:05:39 +0900199static const char SPACE = ' ';
satok30088252010-12-01 21:22:15 +0900200
satok54fe9e02010-12-13 14:42:35 +0900201void UnigramDictionary::getSuggestionCandidates(const int skipPos,
satoka3d78f62010-12-09 22:08:33 +0900202 const int excessivePos, const int transposedPos, int *nextLetters,
203 const int nextLettersSize, const int maxDepth) {
satok54fe9e02010-12-13 14:42:35 +0900204 if (DEBUG_DICT) {
205 LOGI("getSuggestionCandidates %d", maxDepth);
206 assert(transposedPos + 1 < mInputLength);
207 assert(excessivePos < mInputLength);
208 assert(missingPos < mInputLength);
209 }
satok662fe692010-12-08 17:05:39 +0900210 int rootPosition = ROOT_POS;
satokd2997922010-12-07 13:08:39 +0900211 // Get the number of child of root, then increment the position
212 int childCount = Dictionary::getCount(DICT, &rootPosition);
213 int depth = 0;
214
215 mStackChildCount[0] = childCount;
216 mStackTraverseAll[0] = (mInputLength <= 0);
217 mStackNodeFreq[0] = 1;
218 mStackInputIndex[0] = 0;
219 mStackDiffs[0] = 0;
220 mStackSiblingPos[0] = rootPosition;
221
satok662fe692010-12-08 17:05:39 +0900222 // Depth first search
satokd2997922010-12-07 13:08:39 +0900223 while (depth >= 0) {
224 if (mStackChildCount[depth] > 0) {
225 --mStackChildCount[depth];
226 bool traverseAllNodes = mStackTraverseAll[depth];
227 int snr = mStackNodeFreq[depth];
228 int inputIndex = mStackInputIndex[depth];
229 int diffs = mStackDiffs[depth];
230 int siblingPos = mStackSiblingPos[depth];
231 int firstChildPos;
satoka3d78f62010-12-09 22:08:33 +0900232 // depth will never be greater than maxDepth because in that case,
satokd2997922010-12-07 13:08:39 +0900233 // needsToTraverseChildrenNodes should be false
234 const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth,
satoka3d78f62010-12-09 22:08:33 +0900235 maxDepth, traverseAllNodes, snr, inputIndex, diffs, skipPos, excessivePos,
236 transposedPos, nextLetters, nextLettersSize, &childCount, &firstChildPos,
237 &traverseAllNodes, &snr, &inputIndex, &diffs, &siblingPos);
satok662fe692010-12-08 17:05:39 +0900238 // Update next sibling pos
satokd2997922010-12-07 13:08:39 +0900239 mStackSiblingPos[depth] = siblingPos;
240 if (needsToTraverseChildrenNodes) {
241 // Goes to child node
242 ++depth;
243 mStackChildCount[depth] = childCount;
244 mStackTraverseAll[depth] = traverseAllNodes;
245 mStackNodeFreq[depth] = snr;
246 mStackInputIndex[depth] = inputIndex;
247 mStackDiffs[depth] = diffs;
248 mStackSiblingPos[depth] = firstChildPos;
249 }
250 } else {
satokcdbbea72010-12-08 16:04:16 +0900251 // Goes to parent sibling node
satokd2997922010-12-07 13:08:39 +0900252 --depth;
253 }
254 }
255}
256
satok662fe692010-12-08 17:05:39 +0900257bool UnigramDictionary::getMissingSpaceWords(const int inputLength, const int missingSpacePos) {
satokaee09dc2010-12-09 19:21:51 +0900258 if (missingSpacePos <= 0 || missingSpacePos >= inputLength
259 || inputLength >= MAX_WORD_LENGTH) return false;
satok662fe692010-12-08 17:05:39 +0900260 const int newWordLength = inputLength + 1;
261 // Allocating variable length array on stack
262 unsigned short word[newWordLength];
satokaee09dc2010-12-09 19:21:51 +0900263 const int firstFreq = getBestWordFreq(0, missingSpacePos, mWord);
264 if (DEBUG_DICT) LOGI("First freq: %d", firstFreq);
265 if (firstFreq <= 0) return false;
266
satok662fe692010-12-08 17:05:39 +0900267 for (int i = 0; i < missingSpacePos; ++i) {
satokaee09dc2010-12-09 19:21:51 +0900268 word[i] = mWord[i];
satok662fe692010-12-08 17:05:39 +0900269 }
satokaee09dc2010-12-09 19:21:51 +0900270
271 const int secondFreq = getBestWordFreq(missingSpacePos, inputLength - missingSpacePos, mWord);
satoka3d78f62010-12-09 22:08:33 +0900272 if (DEBUG_DICT) LOGI("Second freq: %d", secondFreq);
satokaee09dc2010-12-09 19:21:51 +0900273 if (secondFreq <= 0) return false;
274
satok662fe692010-12-08 17:05:39 +0900275 word[missingSpacePos] = SPACE;
276 for (int i = (missingSpacePos + 1); i < newWordLength; ++i) {
satokaee09dc2010-12-09 19:21:51 +0900277 word[i] = mWord[i - missingSpacePos - 1];
satok662fe692010-12-08 17:05:39 +0900278 }
satokaee09dc2010-12-09 19:21:51 +0900279
280 int pairFreq = ((firstFreq + secondFreq) / 2);
281 for (int i = 0; i < inputLength; ++i) pairFreq *= TYPED_LETTER_MULTIPLIER;
satoka3d78f62010-12-09 22:08:33 +0900282 pairFreq = pairFreq * WORDS_WITH_MISSING_SPACE_CHARACTER_DEMOTION_RATE / 100;
satok662fe692010-12-08 17:05:39 +0900283 addWord(word, newWordLength, pairFreq);
284 return true;
285}
286
287// Keep this for comparing spec to new getWords
288void UnigramDictionary::getWordsOld(const int initialPos, const int inputLength, const int skipPos,
satoka3d78f62010-12-09 22:08:33 +0900289 const int excessivePos, const int transposedPos,int *nextLetters,
290 const int nextLettersSize) {
satok662fe692010-12-08 17:05:39 +0900291 int initialPosition = initialPos;
292 const int count = Dictionary::getCount(DICT, &initialPosition);
293 getWordsRec(count, initialPosition, 0,
294 min(inputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH),
satoka3d78f62010-12-09 22:08:33 +0900295 mInputLength <= 0, 1, 0, 0, skipPos, excessivePos, transposedPos, nextLetters,
296 nextLettersSize);
satok662fe692010-12-08 17:05:39 +0900297}
298
satok68319262010-12-03 19:38:08 +0900299void UnigramDictionary::getWordsRec(const int childrenCount, const int pos, const int depth,
300 const int maxDepth, const bool traverseAllNodes, const int snr, const int inputIndex,
satoka3d78f62010-12-09 22:08:33 +0900301 const int diffs, const int skipPos, const int excessivePos, const int transposedPos,
302 int *nextLetters, const int nextLettersSize) {
satok48e432c2010-12-06 17:38:58 +0900303 int siblingPos = pos;
satok68319262010-12-03 19:38:08 +0900304 for (int i = 0; i < childrenCount; ++i) {
satok48e432c2010-12-06 17:38:58 +0900305 int newCount;
306 int newChildPosition;
satokd2997922010-12-07 13:08:39 +0900307 const int newDepth = depth + 1;
satok48e432c2010-12-06 17:38:58 +0900308 bool newTraverseAllNodes;
309 int newSnr;
310 int newInputIndex;
311 int newDiffs;
312 int newSiblingPos;
313 const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, depth, maxDepth,
satoka3d78f62010-12-09 22:08:33 +0900314 traverseAllNodes, snr, inputIndex, diffs, skipPos, excessivePos, transposedPos,
315 nextLetters, nextLettersSize,
satokd2997922010-12-07 13:08:39 +0900316 &newCount, &newChildPosition, &newTraverseAllNodes, &newSnr,
satok48e432c2010-12-06 17:38:58 +0900317 &newInputIndex, &newDiffs, &newSiblingPos);
318 siblingPos = newSiblingPos;
satok30088252010-12-01 21:22:15 +0900319
satok48e432c2010-12-06 17:38:58 +0900320 if (needsToTraverseChildrenNodes) {
321 getWordsRec(newCount, newChildPosition, newDepth, maxDepth, newTraverseAllNodes,
satoka3d78f62010-12-09 22:08:33 +0900322 newSnr, newInputIndex, newDiffs, skipPos, excessivePos, transposedPos,
323 nextLetters, nextLettersSize);
satok30088252010-12-01 21:22:15 +0900324 }
325 }
326}
327
satok54fe9e02010-12-13 14:42:35 +0900328inline int UnigramDictionary::calculateFinalFreq(const int inputIndex, const int snr,
329 const int skipPos, const int excessivePos, const int transposedPos, const int freq,
330 const bool sameLength) {
satoka3d78f62010-12-09 22:08:33 +0900331 // TODO: Demote by edit distance
satok54fe9e02010-12-13 14:42:35 +0900332 int finalFreq = freq * snr;
satoka3d78f62010-12-09 22:08:33 +0900333 if (skipPos >= 0) finalFreq = finalFreq * WORDS_WITH_MISSING_CHARACTER_DEMOTION_RATE / 100;
satoka3d78f62010-12-09 22:08:33 +0900334 if (transposedPos >= 0) finalFreq = finalFreq
335 * WORDS_WITH_TRANSPOSED_CHARACTERS_DEMOTION_RATE / 100;
satok54fe9e02010-12-13 14:42:35 +0900336 if (excessivePos >= 0) {
337 finalFreq = finalFreq * WORDS_WITH_EXCESSIVE_CHARACTER_DEMOTION_RATE / 100;
338 if (!existsAdjacentProximityChars(inputIndex, mInputLength)) {
339 finalFreq = finalFreq
340 * WORDS_WITH_EXCESSIVE_CHARACTER_OUT_OF_PROXIMITY_DEMOTION_RATE / 100;
341 }
342 }
343 if (sameLength && skipPos < 0) finalFreq *= FULL_WORD_MULTIPLIER;
344 return finalFreq;
345}
satoka3d78f62010-12-09 22:08:33 +0900346
satok54fe9e02010-12-13 14:42:35 +0900347inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsGreaterThanInputLength(
348 unsigned short *word, const int inputIndex, const int depth, const int snr,
349 int *nextLetters, const int nextLettersSize, const int skipPos, const int excessivePos,
350 const int transposedPos, const int freq) {
351 const int finalFreq = calculateFinalFreq(inputIndex, snr, skipPos, excessivePos, transposedPos,
352 freq, false);
satoka3d78f62010-12-09 22:08:33 +0900353 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq);
satok54fe9e02010-12-13 14:42:35 +0900354 if (depth >= mInputLength && skipPos < 0) {
satok715514d2010-12-02 20:19:59 +0900355 registerNextLetter(mWord[mInputLength], nextLetters, nextLettersSize);
356 }
357}
358
359inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsSameAsInputLength(
satok54fe9e02010-12-13 14:42:35 +0900360 unsigned short *word, const int inputIndex, const int depth, const int snr,
361 const int skipPos, const int excessivePos, const int transposedPos, const int freq,
362 const int addedWeight) {
363 if (sameAsTyped(word, depth + 1)) return;
364 const int finalFreq = calculateFinalFreq(inputIndex, snr * addedWeight, skipPos,
365 excessivePos, transposedPos, freq, true);
366 // Proximity collection will promote a word of the same length as what user typed.
367 if (depth >= MIN_SUGGEST_DEPTH) addWord(word, depth + 1, finalFreq);
satok715514d2010-12-02 20:19:59 +0900368}
satok28bd03b2010-12-03 16:39:16 +0900369
370inline bool UnigramDictionary::needsToSkipCurrentNode(const unsigned short c,
satok68319262010-12-03 19:38:08 +0900371 const int inputIndex, const int skipPos, const int depth) {
satok662fe692010-12-08 17:05:39 +0900372 const unsigned short userTypedChar = (mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS))[0];
satok28bd03b2010-12-03 16:39:16 +0900373 // Skip the ' or other letter and continue deeper
374 return (c == QUOTE && userTypedChar != QUOTE) || skipPos == depth;
375}
376
satoke07baa62010-12-09 21:55:40 +0900377inline bool UnigramDictionary::existsAdjacentProximityChars(const int inputIndex,
378 const int inputLength) {
379 if (inputIndex < 0 || inputIndex >= inputLength) return false;
380 const int currentChar = *getInputCharsAt(inputIndex);
381 const int leftIndex = inputIndex - 1;
382 if (leftIndex >= 0) {
383 int *leftChars = getInputCharsAt(leftIndex);
384 int i = 0;
385 while (leftChars[i] > 0 && i < MAX_PROXIMITY_CHARS) {
386 if (leftChars[i++] == currentChar) return true;
387 }
388 }
389 const int rightIndex = inputIndex + 1;
390 if (rightIndex < inputLength) {
391 int *rightChars = getInputCharsAt(rightIndex);
392 int i = 0;
393 while (rightChars[i] > 0 && i < MAX_PROXIMITY_CHARS) {
394 if (rightChars[i++] == currentChar) return true;
395 }
396 }
397 return false;
398}
399
satok28bd03b2010-12-03 16:39:16 +0900400inline int UnigramDictionary::getMatchedProximityId(const int *currentChars,
satoka3d78f62010-12-09 22:08:33 +0900401 const unsigned short c, const int skipPos, const int excessivePos,
402 const int transposedPos) {
satok48e432c2010-12-06 17:38:58 +0900403 const unsigned short lowerC = toLowerCase(c);
satok28bd03b2010-12-03 16:39:16 +0900404 int j = 0;
satoke07baa62010-12-09 21:55:40 +0900405 while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS) {
satok68319262010-12-03 19:38:08 +0900406 const bool matched = (currentChars[j] == lowerC || currentChars[j] == c);
satok28bd03b2010-12-03 16:39:16 +0900407 // If skipPos is defined, not to search proximity collections.
satoka3d78f62010-12-09 22:08:33 +0900408 // First char is what user typed.
satok28bd03b2010-12-03 16:39:16 +0900409 if (matched) {
410 return j;
satoka3d78f62010-12-09 22:08:33 +0900411 } else if (skipPos >= 0 || excessivePos >= 0 || transposedPos >= 0) {
412 // Not to check proximity characters
satok28bd03b2010-12-03 16:39:16 +0900413 return -1;
414 }
415 ++j;
416 }
417 return -1;
418}
419
satok48e432c2010-12-06 17:38:58 +0900420inline bool UnigramDictionary::processCurrentNode(const int pos, const int depth,
satokcdbbea72010-12-08 16:04:16 +0900421 const int maxDepth, const bool traverseAllNodes, const int snr, int inputIndex,
satoka3d78f62010-12-09 22:08:33 +0900422 const int diffs, const int skipPos, const int excessivePos, const int transposedPos,
423 int *nextLetters, const int nextLettersSize, int *newCount, int *newChildPosition,
424 bool *newTraverseAllNodes, int *newSnr, int*newInputIndex, int *newDiffs,
425 int *nextSiblingPosition) {
426 if (DEBUG_DICT) {
427 int inputCount = 0;
428 if (skipPos >= 0) ++inputCount;
429 if (excessivePos >= 0) ++inputCount;
430 if (transposedPos >= 0) ++inputCount;
431 assert(inputCount <= 1);
432 }
satok48e432c2010-12-06 17:38:58 +0900433 unsigned short c;
434 int childPosition;
435 bool terminal;
436 int freq;
satokcdbbea72010-12-08 16:04:16 +0900437
438 if (excessivePos == depth) ++inputIndex;
439
satok48e432c2010-12-06 17:38:58 +0900440 *nextSiblingPosition = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, pos, &c,
441 &childPosition, &terminal, &freq);
442
443 const bool needsToTraverseChildrenNodes = childPosition != 0;
444
445 // If we are only doing traverseAllNodes, no need to look at the typed characters.
446 if (traverseAllNodes || needsToSkipCurrentNode(c, inputIndex, skipPos, depth)) {
447 mWord[depth] = c;
448 if (traverseAllNodes && terminal) {
satok54fe9e02010-12-13 14:42:35 +0900449 onTerminalWhenUserTypedLengthIsGreaterThanInputLength(mWord, inputIndex, depth,
satoka3d78f62010-12-09 22:08:33 +0900450 snr, nextLetters, nextLettersSize, skipPos, excessivePos, transposedPos, freq);
satok48e432c2010-12-06 17:38:58 +0900451 }
452 if (!needsToTraverseChildrenNodes) return false;
453 *newTraverseAllNodes = traverseAllNodes;
454 *newSnr = snr;
455 *newDiffs = diffs;
456 *newInputIndex = inputIndex;
satok48e432c2010-12-06 17:38:58 +0900457 } else {
satok662fe692010-12-08 17:05:39 +0900458 int *currentChars = mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS);
satoka3d78f62010-12-09 22:08:33 +0900459
460 if (transposedPos >= 0) {
461 if (inputIndex == transposedPos) currentChars += MAX_PROXIMITY_CHARS;
462 if (inputIndex == (transposedPos + 1)) currentChars -= MAX_PROXIMITY_CHARS;
463 }
464
465 int matchedProximityCharId = getMatchedProximityId(currentChars, c, skipPos, excessivePos,
466 transposedPos);
satok48e432c2010-12-06 17:38:58 +0900467 if (matchedProximityCharId < 0) return false;
468 mWord[depth] = c;
469 // If inputIndex is greater than mInputLength, that means there is no
470 // proximity chars. So, we don't need to check proximity.
471 const int addedWeight = matchedProximityCharId == 0 ? TYPED_LETTER_MULTIPLIER : 1;
472 const bool isSameAsUserTypedLength = mInputLength == inputIndex + 1;
473 if (isSameAsUserTypedLength && terminal) {
satok54fe9e02010-12-13 14:42:35 +0900474 onTerminalWhenUserTypedLengthIsSameAsInputLength(mWord, inputIndex, depth, snr,
satoka3d78f62010-12-09 22:08:33 +0900475 skipPos, excessivePos, transposedPos, freq, addedWeight);
satok48e432c2010-12-06 17:38:58 +0900476 }
477 if (!needsToTraverseChildrenNodes) return false;
478 // Start traversing all nodes after the index exceeds the user typed length
479 *newTraverseAllNodes = isSameAsUserTypedLength;
480 *newSnr = snr * addedWeight;
satoka3d78f62010-12-09 22:08:33 +0900481 *newDiffs = diffs + ((matchedProximityCharId > 0) ? 1 : 0);
satok48e432c2010-12-06 17:38:58 +0900482 *newInputIndex = inputIndex + 1;
satok48e432c2010-12-06 17:38:58 +0900483 }
484 // Optimization: Prune out words that are too long compared to how much was typed.
satokd2997922010-12-07 13:08:39 +0900485 if (depth >= maxDepth || *newDiffs > mMaxEditDistance) {
satok48e432c2010-12-06 17:38:58 +0900486 return false;
487 }
488
489 // If inputIndex is greater than mInputLength, that means there are no proximity chars.
490 if (mInputLength <= *newInputIndex) {
491 *newTraverseAllNodes = true;
492 }
493 // get the count of nodes and increment childAddress.
494 *newCount = Dictionary::getCount(DICT, &childPosition);
495 *newChildPosition = childPosition;
496 if (DEBUG_DICT) assert(needsToTraverseChildrenNodes);
497 return needsToTraverseChildrenNodes;
498}
499
satokaee09dc2010-12-09 19:21:51 +0900500inline int UnigramDictionary::getBestWordFreq(const int startInputIndex, const int inputLength,
501 unsigned short *word) {
satok662fe692010-12-08 17:05:39 +0900502 int pos = ROOT_POS;
503 int count = Dictionary::getCount(DICT, &pos);
satokaee09dc2010-12-09 19:21:51 +0900504 int maxFreq = 0;
505 int depth = 0;
506 unsigned short newWord[MAX_WORD_LENGTH_INTERNAL];
satok662fe692010-12-08 17:05:39 +0900507 bool terminal = false;
508
satokaee09dc2010-12-09 19:21:51 +0900509 mStackChildCount[0] = count;
510 mStackSiblingPos[0] = pos;
511
512 while (depth >= 0) {
513 if (mStackChildCount[depth] > 0) {
514 --mStackChildCount[depth];
515 int firstChildPos;
516 int newFreq;
517 int siblingPos = mStackSiblingPos[depth];
518 const bool needsToTraverseChildrenNodes = processCurrentNodeForExactMatch(siblingPos,
519 startInputIndex, depth, newWord, &firstChildPos, &count, &terminal, &newFreq,
520 &siblingPos);
521 mStackSiblingPos[depth] = siblingPos;
522 if (depth == (inputLength - 1)) {
523 // Traverse sibling node
524 if (terminal) {
525 if (newFreq > maxFreq) {
526 for (int i = 0; i < inputLength; ++i) word[i] = newWord[i];
527 if (DEBUG_DICT && DEBUG_NODE) {
528 char s[inputLength + 1];
529 for (int i = 0; i < inputLength; ++i) s[i] = word[i];
530 s[inputLength] = 0;
531 LOGI("New missing space word found: %d > %d (%s), %d, %d",
532 newFreq, maxFreq, s, inputLength, depth);
533 }
534 maxFreq = newFreq;
535 }
536 }
537 } else if (needsToTraverseChildrenNodes) {
538 // Traverse children nodes
539 ++depth;
540 mStackChildCount[depth] = count;
541 mStackSiblingPos[depth] = firstChildPos;
542 }
543 } else {
544 // Traverse parent node
545 --depth;
satok662fe692010-12-08 17:05:39 +0900546 }
547 }
satokaee09dc2010-12-09 19:21:51 +0900548
549 word[inputLength] = 0;
550 return maxFreq;
satok662fe692010-12-08 17:05:39 +0900551}
552
553inline bool UnigramDictionary::processCurrentNodeForExactMatch(const int firstChildPos,
satokaee09dc2010-12-09 19:21:51 +0900554 const int startInputIndex, const int depth, unsigned short *word, int *newChildPosition,
555 int *newCount, bool *newTerminal, int *newFreq, int *siblingPos) {
556 const int inputIndex = startInputIndex + depth;
satok662fe692010-12-08 17:05:39 +0900557 const int *currentChars = mInputCodes + (inputIndex * MAX_PROXIMITY_CHARS);
satok662fe692010-12-08 17:05:39 +0900558 unsigned short c;
satokaee09dc2010-12-09 19:21:51 +0900559 *siblingPos = Dictionary::setDictionaryValues(DICT, IS_LATEST_DICT_VERSION, firstChildPos, &c,
560 newChildPosition, newTerminal, newFreq);
561 const unsigned int inputC = currentChars[0];
562 if (DEBUG_DICT) assert(inputC <= U_SHORT_MAX);
563 const unsigned short lowerC = toLowerCase(c);
564 const bool matched = (inputC == lowerC || inputC == c);
565 const bool hasChild = *newChildPosition != 0;
566 if (matched) {
567 word[depth] = c;
568 if (DEBUG_DICT && DEBUG_NODE) {
569 LOGI("Node(%c, %c)<%d>, %d, %d", inputC, c, matched, hasChild, *newFreq);
570 if (*newTerminal) LOGI("Terminal %d", *newFreq);
satok662fe692010-12-08 17:05:39 +0900571 }
satokaee09dc2010-12-09 19:21:51 +0900572 if (hasChild) {
573 *newCount = Dictionary::getCount(DICT, newChildPosition);
574 return true;
575 } else {
576 return false;
577 }
578 } else {
579 // If this node is not user typed character, this method treats this word as unmatched.
580 // Thus newTerminal shouldn't be true.
581 *newTerminal = false;
582 return false;
satok662fe692010-12-08 17:05:39 +0900583 }
satok662fe692010-12-08 17:05:39 +0900584}
satok30088252010-12-01 21:22:15 +0900585} // namespace latinime