blob: e4edc5ab61f7a6d9a6992c02d12cc2edd5289048 [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
18#include <stdio.h>
19#include <fcntl.h>
20#include <sys/mman.h>
21#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,
33 int fullWordMultiplier, int maxWordLength, int maxWords, int maxAlternatives,
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),
36 MAX_ALTERNATIVES(maxAlternatives), IS_LATEST_DICT_VERSION(isLatestDictVersion),
satok18c28f42010-12-02 18:11:54 +090037 TYPED_LETTER_MULTIPLIER(typedLetterMultiplier), FULL_WORD_MULTIPLIER(fullWordMultiplier) {
satok30088252010-12-01 21:22:15 +090038 LOGI("UnigramDictionary - constructor");
satok30088252010-12-01 21:22:15 +090039}
40
satok18c28f42010-12-02 18:11:54 +090041UnigramDictionary::~UnigramDictionary() {}
satok30088252010-12-01 21:22:15 +090042
satok18c28f42010-12-02 18:11:54 +090043int UnigramDictionary::getSuggestions(int *codes, int codesSize, unsigned short *outWords,
44 int *frequencies, int *nextLetters, int nextLettersSize)
satok30088252010-12-01 21:22:15 +090045{
46
47 initSuggestions(codes, codesSize, outWords, frequencies);
48
49 int suggestedWordsCount = getSuggestionCandidates(codesSize, -1, nextLetters,
50 nextLettersSize);
51
52 // If there aren't sufficient suggestions, search for words by allowing wild cards at
53 // the different character positions. This feature is not ready for prime-time as we need
54 // to figure out the best ranking for such words compared to proximity corrections and
55 // completions.
56 if (SUGGEST_MISSING_CHARACTERS && suggestedWordsCount < SUGGEST_MISSING_CHARACTERS_THRESHOLD) {
57 for (int i = 0; i < codesSize; ++i) {
58 int tempCount = getSuggestionCandidates(codesSize, i, NULL, 0);
59 if (tempCount > suggestedWordsCount) {
60 suggestedWordsCount = tempCount;
61 break;
62 }
63 }
64 }
65
66 if (DEBUG_DICT) {
67 LOGI("Returning %d words", suggestedWordsCount);
68 LOGI("Next letters: ");
69 for (int k = 0; k < nextLettersSize; k++) {
70 if (nextLetters[k] > 0) {
71 LOGI("%c = %d,", k, nextLetters[k]);
72 }
73 }
74 LOGI("\n");
75 }
76 return suggestedWordsCount;
77}
78
79void UnigramDictionary::initSuggestions(int *codes, int codesSize, unsigned short *outWords,
80 int *frequencies) {
81 mFrequencies = frequencies;
82 mOutputChars = outWords;
83 mInputCodes = codes;
84 mInputLength = codesSize;
85 mMaxEditDistance = mInputLength < 5 ? 2 : mInputLength / 2;
86}
87
88int UnigramDictionary::getSuggestionCandidates(int inputLength, int skipPos,
89 int *nextLetters, int nextLettersSize) {
satoke808e432010-12-02 14:53:24 +090090 if (IS_LATEST_DICT_VERSION) {
satok30088252010-12-01 21:22:15 +090091 getWordsRec(DICTIONARY_HEADER_SIZE, 0, inputLength * 3, false, 1, 0, 0, skipPos,
92 nextLetters, nextLettersSize);
93 } else {
94 getWordsRec(0, 0, inputLength * 3, false, 1, 0, 0, skipPos, nextLetters, nextLettersSize);
95 }
96
97 // Get the word count
98 int suggestedWordsCount = 0;
99 while (suggestedWordsCount < MAX_WORDS && mFrequencies[suggestedWordsCount] > 0) {
100 suggestedWordsCount++;
101 }
102 return suggestedWordsCount;
103}
104
satok715514d2010-12-02 20:19:59 +0900105void UnigramDictionary::registerNextLetter(
106 unsigned short c, int *nextLetters, int nextLettersSize) {
satok30088252010-12-01 21:22:15 +0900107 if (c < nextLettersSize) {
108 nextLetters[c]++;
109 }
110}
111
satok30088252010-12-01 21:22:15 +0900112bool
113UnigramDictionary::addWord(unsigned short *word, int length, int frequency)
114{
115 word[length] = 0;
116 if (DEBUG_DICT) {
117 char s[length + 1];
118 for (int i = 0; i <= length; i++) s[i] = word[i];
119 LOGI("Found word = %s, freq = %d : \n", s, frequency);
120 }
121
122 // Find the right insertion point
123 int insertAt = 0;
124 while (insertAt < MAX_WORDS) {
satok715514d2010-12-02 20:19:59 +0900125 if (frequency > mFrequencies[insertAt] || (mFrequencies[insertAt] == frequency
126 && length < Dictionary::wideStrLen(mOutputChars + insertAt * MAX_WORD_LENGTH))) {
satok30088252010-12-01 21:22:15 +0900127 break;
128 }
129 insertAt++;
130 }
131 if (insertAt < MAX_WORDS) {
132 memmove((char*) mFrequencies + (insertAt + 1) * sizeof(mFrequencies[0]),
133 (char*) mFrequencies + insertAt * sizeof(mFrequencies[0]),
134 (MAX_WORDS - insertAt - 1) * sizeof(mFrequencies[0]));
135 mFrequencies[insertAt] = frequency;
136 memmove((char*) mOutputChars + (insertAt + 1) * MAX_WORD_LENGTH * sizeof(short),
satok715514d2010-12-02 20:19:59 +0900137 (char*) mOutputChars + insertAt * MAX_WORD_LENGTH * sizeof(short),
satok30088252010-12-01 21:22:15 +0900138 (MAX_WORDS - insertAt - 1) * sizeof(short) * MAX_WORD_LENGTH);
satok715514d2010-12-02 20:19:59 +0900139 unsigned short *dest = mOutputChars + insertAt * MAX_WORD_LENGTH;
satok30088252010-12-01 21:22:15 +0900140 while (length--) {
141 *dest++ = *word++;
142 }
143 *dest = 0; // NULL terminate
144 if (DEBUG_DICT) LOGI("Added word at %d\n", insertAt);
145 return true;
146 }
147 return false;
148}
149
satok30088252010-12-01 21:22:15 +0900150unsigned short
151UnigramDictionary::toLowerCase(unsigned short c) {
152 if (c < sizeof(BASE_CHARS) / sizeof(BASE_CHARS[0])) {
153 c = BASE_CHARS[c];
154 }
155 if (c >='A' && c <= 'Z') {
156 c |= 32;
157 } else if (c > 127) {
158 c = latin_tolower(c);
159 }
160 return c;
161}
162
163bool
164UnigramDictionary::sameAsTyped(unsigned short *word, int length)
165{
166 if (length != mInputLength) {
167 return false;
168 }
169 int *inputCodes = mInputCodes;
170 while (length--) {
171 if ((unsigned int) *inputCodes != (unsigned int) *word) {
172 return false;
173 }
174 inputCodes += MAX_ALTERNATIVES;
175 word++;
176 }
177 return true;
178}
179
satok715514d2010-12-02 20:19:59 +0900180static const char QUOTE = '\'';
satok30088252010-12-01 21:22:15 +0900181
satok715514d2010-12-02 20:19:59 +0900182// snr : frequency?
satok30088252010-12-01 21:22:15 +0900183void
satok18c28f42010-12-02 18:11:54 +0900184UnigramDictionary::getWordsRec(int pos, int depth, int maxDepth, bool completion, int snr,
185 int inputIndex, int diffs, int skipPos, int *nextLetters, int nextLettersSize)
satok30088252010-12-01 21:22:15 +0900186{
187 // Optimization: Prune out words that are too long compared to how much was typed.
188 if (depth > maxDepth) {
189 return;
190 }
191 if (diffs > mMaxEditDistance) {
192 return;
193 }
satok715514d2010-12-02 20:19:59 +0900194 // get the count of nodes and increment pos.
satoke808e432010-12-02 14:53:24 +0900195 int count = Dictionary::getCount(DICT, &pos);
satok30088252010-12-01 21:22:15 +0900196 int *currentChars = NULL;
satok715514d2010-12-02 20:19:59 +0900197 // If inputIndex is greater than mInputLength, that means there are no proximity chars.
satok30088252010-12-01 21:22:15 +0900198 if (mInputLength <= inputIndex) {
199 completion = true;
200 } else {
201 currentChars = mInputCodes + (inputIndex * MAX_ALTERNATIVES);
202 }
203
204 for (int i = 0; i < count; i++) {
205 // -- at char
satoke808e432010-12-02 14:53:24 +0900206 unsigned short c = Dictionary::getChar(DICT, &pos);
satok30088252010-12-01 21:22:15 +0900207 // -- at flag/add
208 unsigned short lowerC = toLowerCase(c);
satoke808e432010-12-02 14:53:24 +0900209 bool terminal = Dictionary::getTerminal(DICT, &pos);
210 int childrenAddress = Dictionary::getAddress(DICT, &pos);
satok715514d2010-12-02 20:19:59 +0900211 const bool needsToContinue = childrenAddress != 0;
satok30088252010-12-01 21:22:15 +0900212 // -- after address or flag
213 int freq = 1;
satok715514d2010-12-02 20:19:59 +0900214 // If terminal, increment pos
satoke808e432010-12-02 14:53:24 +0900215 if (terminal) freq = Dictionary::getFreq(DICT, IS_LATEST_DICT_VERSION, &pos);
satok30088252010-12-01 21:22:15 +0900216 // -- after add or freq
217
218 // If we are only doing completions, no need to look at the typed characters.
219 if (completion) {
220 mWord[depth] = c;
221 if (terminal) {
satok715514d2010-12-02 20:19:59 +0900222 onTerminalWhenUserTypedLengthIsGreaterThanInputLength(mWord, mInputLength, depth,
223 snr, nextLetters, nextLettersSize, skipPos, freq);
satok30088252010-12-01 21:22:15 +0900224 }
satok715514d2010-12-02 20:19:59 +0900225 if (needsToContinue) {
226 // No need to do proximity suggest any more.
227 getWordsRec(childrenAddress, depth + 1, maxDepth, true, snr, inputIndex,
satok30088252010-12-01 21:22:15 +0900228 diffs, skipPos, nextLetters, nextLettersSize);
229 }
230 } else if ((c == QUOTE && currentChars[0] != QUOTE) || skipPos == depth) {
231 // Skip the ' or other letter and continue deeper
232 mWord[depth] = c;
satok715514d2010-12-02 20:19:59 +0900233 if (needsToContinue) {
234 getWordsRec(childrenAddress, depth + 1, maxDepth, false, snr, inputIndex,
235 diffs, skipPos, nextLetters, nextLettersSize);
satok30088252010-12-01 21:22:15 +0900236 }
237 } else {
238 int j = 0;
239 while (currentChars[j] > 0) {
satok715514d2010-12-02 20:19:59 +0900240 // Move to child node
satok30088252010-12-01 21:22:15 +0900241 if (currentChars[j] == lowerC || currentChars[j] == c) {
satok30088252010-12-01 21:22:15 +0900242 mWord[depth] = c;
satok715514d2010-12-02 20:19:59 +0900243 const int addedWeight = j == 0 ? TYPED_LETTER_MULTIPLIER : 1;
244 const bool isSameAsUserTypedLength = mInputLength == inputIndex + 1;
245 // If inputIndex is greater than mInputLength, that means there is no
246 // proximity chars. So, we don't need to check proximity.
247 if (isSameAsUserTypedLength) {
satok30088252010-12-01 21:22:15 +0900248 if (terminal) {
satok715514d2010-12-02 20:19:59 +0900249 onTerminalWhenUserTypedLengthIsSameAsInputLength(mWord, depth, snr,
250 skipPos, freq, addedWeight);
satok30088252010-12-01 21:22:15 +0900251 }
satok715514d2010-12-02 20:19:59 +0900252 }
253 if (needsToContinue) {
satok30088252010-12-01 21:22:15 +0900254 getWordsRec(childrenAddress, depth + 1, maxDepth,
satok715514d2010-12-02 20:19:59 +0900255 isSameAsUserTypedLength, snr * addedWeight, inputIndex + 1,
256 diffs + (j > 0), skipPos, nextLetters, nextLettersSize);
satok30088252010-12-01 21:22:15 +0900257 }
258 }
satok715514d2010-12-02 20:19:59 +0900259 ++j;
260 // If skipPos is defined, not to search proximity collections.
261 // First char is what user typed.
satok30088252010-12-01 21:22:15 +0900262 if (skipPos >= 0) break;
263 }
264 }
265 }
266}
267
satok715514d2010-12-02 20:19:59 +0900268inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsGreaterThanInputLength(
269 unsigned short *word, const int inputLength, const int depth, const int snr,
270 int *nextLetters, const int nextLettersSize, const int skipPos, const int freq) {
271 addWord(word, depth + 1, freq * snr);
272 if (depth >= inputLength && skipPos < 0) {
273 registerNextLetter(mWord[mInputLength], nextLetters, nextLettersSize);
274 }
275}
276
277inline void UnigramDictionary::onTerminalWhenUserTypedLengthIsSameAsInputLength(
278 unsigned short *word, const int depth, const int snr, const int skipPos, const int freq,
279 const int addedWeight) {
280 if (!sameAsTyped(word, depth + 1)) {
281 int finalFreq = freq * snr * addedWeight;
282 // Proximity collection will promote a word of the same length as
283 // what user typed.
284 if (skipPos < 0) finalFreq *= FULL_WORD_MULTIPLIER;
285 addWord(word, depth + 1, finalFreq);
286 }
287}
satok30088252010-12-01 21:22:15 +0900288} // namespace latinime