blob: 20fa18a44c8add2117d31186ad9121d8c2c9717a [file] [log] [blame]
satok8fbd5522011-02-22 17:28:55 +09001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090017#include <assert.h>
satok8fbd5522011-02-22 17:28:55 +090018#include <stdio.h>
19#include <string.h>
20
satok817e5172011-03-04 06:06:45 -080021#define LOG_TAG "LatinIME: proximity_info.cpp"
22
satokd24df432011-07-14 15:43:42 +090023#include "dictionary.h"
satok8fbd5522011-02-22 17:28:55 +090024#include "proximity_info.h"
25
26namespace latinime {
Ken Wakasace9e52a2011-06-18 13:09:55 +090027
Yusuke Nojimade2f8422011-09-27 11:15:18 +090028inline void copyOrFillZero(void *to, const void *from, size_t size) {
29 if (from) {
30 memcpy(to, from, size);
31 } else {
32 memset(to, 0, size);
33 }
34}
35
satok817e5172011-03-04 06:06:45 -080036ProximityInfo::ProximityInfo(const int maxProximityCharsSize, const int keyboardWidth,
37 const int keyboardHeight, const int gridWidth, const int gridHeight,
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090038 const uint32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates,
39 const int32_t *keyYCoordinates, const int32_t *keyWidths, const int32_t *keyHeights,
Yusuke Nojimaad358352011-09-29 16:44:54 +090040 const int32_t *keyCharCodes, const float *sweetSpotCenterXs, const float *sweetSpotCenterYs,
41 const float *sweetSpotRadii)
satok817e5172011-03-04 06:06:45 -080042 : MAX_PROXIMITY_CHARS_SIZE(maxProximityCharsSize), KEYBOARD_WIDTH(keyboardWidth),
43 KEYBOARD_HEIGHT(keyboardHeight), GRID_WIDTH(gridWidth), GRID_HEIGHT(gridHeight),
44 CELL_WIDTH((keyboardWidth + gridWidth - 1) / gridWidth),
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090045 CELL_HEIGHT((keyboardHeight + gridHeight - 1) / gridHeight),
Yusuke Nojima258bfe62011-09-28 12:59:43 +090046 KEY_COUNT(min(keyCount, MAX_KEY_COUNT_IN_A_KEYBOARD)),
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +090047 HAS_TOUCH_POSITION_CORRECTION_DATA(keyCount > 0 && keyXCoordinates && keyYCoordinates
48 && keyWidths && keyHeights && keyCharCodes && sweetSpotCenterXs
49 && sweetSpotCenterYs && sweetSpotRadii),
50 mInputXCoordinates(NULL), mInputYCoordinates(NULL),
51 mTouchPositionCorrectionEnabled(false) {
satok817e5172011-03-04 06:06:45 -080052 const int len = GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE;
53 mProximityCharsArray = new uint32_t[len];
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +090054 mNormalizedSquaredDistances = new int[len];
satok817e5172011-03-04 06:06:45 -080055 if (DEBUG_PROXIMITY_INFO) {
56 LOGI("Create proximity info array %d", len);
57 }
58 memcpy(mProximityCharsArray, proximityCharsArray, len * sizeof(mProximityCharsArray[0]));
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +090059 for (int i = 0; i < len; ++i) {
60 mNormalizedSquaredDistances[i] = NOT_A_DISTANCE;
61 }
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090062
Yusuke Nojimade2f8422011-09-27 11:15:18 +090063 copyOrFillZero(mKeyXCoordinates, keyXCoordinates, KEY_COUNT * sizeof(mKeyXCoordinates[0]));
64 copyOrFillZero(mKeyYCoordinates, keyYCoordinates, KEY_COUNT * sizeof(mKeyYCoordinates[0]));
65 copyOrFillZero(mKeyWidths, keyWidths, KEY_COUNT * sizeof(mKeyWidths[0]));
66 copyOrFillZero(mKeyHeights, keyHeights, KEY_COUNT * sizeof(mKeyHeights[0]));
67 copyOrFillZero(mKeyCharCodes, keyCharCodes, KEY_COUNT * sizeof(mKeyCharCodes[0]));
Yusuke Nojimaad358352011-09-29 16:44:54 +090068 copyOrFillZero(mSweetSpotCenterXs, sweetSpotCenterXs,
69 KEY_COUNT * sizeof(mSweetSpotCenterXs[0]));
70 copyOrFillZero(mSweetSpotCenterYs, sweetSpotCenterYs,
71 KEY_COUNT * sizeof(mSweetSpotCenterYs[0]));
72 copyOrFillZero(mSweetSpotRadii, sweetSpotRadii, KEY_COUNT * sizeof(mSweetSpotRadii[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090073
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090074 initializeCodeToKeyIndex();
75}
76
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090077// Build the reversed look up table from the char code to the index in mKeyXCoordinates,
78// mKeyYCoordinates, mKeyWidths, mKeyHeights, mKeyCharCodes.
79void ProximityInfo::initializeCodeToKeyIndex() {
Yusuke Nojimaad358352011-09-29 16:44:54 +090080 memset(mCodeToKeyIndex, -1, (MAX_CHAR_CODE + 1) * sizeof(mCodeToKeyIndex[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090081 for (int i = 0; i < KEY_COUNT; ++i) {
82 const int code = mKeyCharCodes[i];
Yusuke Nojimaad358352011-09-29 16:44:54 +090083 if (0 <= code && code <= MAX_CHAR_CODE) {
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090084 mCodeToKeyIndex[code] = i;
Yusuke Nojimaad358352011-09-29 16:44:54 +090085 }
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090086 }
satok8fbd5522011-02-22 17:28:55 +090087}
88
89ProximityInfo::~ProximityInfo() {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +090090 delete[] mNormalizedSquaredDistances;
satok8fbd5522011-02-22 17:28:55 +090091 delete[] mProximityCharsArray;
92}
satok817e5172011-03-04 06:06:45 -080093
94inline int ProximityInfo::getStartIndexFromCoordinates(const int x, const int y) const {
satok3c4bb772011-03-04 22:50:19 -080095 return ((y / CELL_HEIGHT) * GRID_WIDTH + (x / CELL_WIDTH))
satok817e5172011-03-04 06:06:45 -080096 * MAX_PROXIMITY_CHARS_SIZE;
satok8fbd5522011-02-22 17:28:55 +090097}
satok817e5172011-03-04 06:06:45 -080098
99bool ProximityInfo::hasSpaceProximity(const int x, const int y) const {
100 const int startIndex = getStartIndexFromCoordinates(x, y);
101 if (DEBUG_PROXIMITY_INFO) {
102 LOGI("hasSpaceProximity: index %d", startIndex);
103 }
104 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
105 if (DEBUG_PROXIMITY_INFO) {
106 LOGI("Index: %d", mProximityCharsArray[startIndex + i]);
107 }
108 if (mProximityCharsArray[startIndex + i] == KEYCODE_SPACE) {
109 return true;
110 }
111 }
112 return false;
113}
Ken Wakasace9e52a2011-06-18 13:09:55 +0900114
satok1d7eaf82011-07-13 10:32:02 +0900115// TODO: Calculate nearby codes here.
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900116void ProximityInfo::setInputParams(const int* inputCodes, const int inputLength,
117 const int* xCoordinates, const int* yCoordinates) {
satok1d7eaf82011-07-13 10:32:02 +0900118 mInputCodes = inputCodes;
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900119 mInputXCoordinates = xCoordinates;
120 mInputYCoordinates = yCoordinates;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900121 mTouchPositionCorrectionEnabled =
122 HAS_TOUCH_POSITION_CORRECTION_DATA && xCoordinates && yCoordinates;
satok1d7eaf82011-07-13 10:32:02 +0900123 mInputLength = inputLength;
satok0cedd2b2011-08-12 01:05:27 +0900124 for (int i = 0; i < inputLength; ++i) {
125 mPrimaryInputWord[i] = getPrimaryCharAt(i);
126 }
127 mPrimaryInputWord[inputLength] = 0;
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900128 for (int i = 0; i < mInputLength; ++i) {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900129 const int *proximityChars = getProximityCharsAt(i);
130 for (int j = 0; j < MAX_PROXIMITY_CHARS_SIZE && proximityChars[j] > 0; ++j) {
131 const int currentChar = proximityChars[j];
132 const int keyIndex = getKeyIndex(currentChar);
133 const float squaredDistance = calculateNormalizedSquaredDistance(keyIndex, i);
134 if (squaredDistance >= 0.0f) {
135 mNormalizedSquaredDistances[i * MAX_PROXIMITY_CHARS_SIZE + j] =
136 (int)(squaredDistance * NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR);
137 } else {
138 mNormalizedSquaredDistances[i * MAX_PROXIMITY_CHARS_SIZE + j] = (j == 0)
139 ? EQUIVALENT_CHAR_WITHOUT_DISTANCE_INFO
140 : PROXIMITY_CHAR_WITHOUT_DISTANCE_INFO;
141 }
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900142 }
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900143 }
satok1d7eaf82011-07-13 10:32:02 +0900144}
145
Yusuke Nojima16717152011-10-04 17:04:07 +0900146inline float square(const float x) { return x * x; }
147
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900148float ProximityInfo::calculateNormalizedSquaredDistance(
149 const int keyIndex, const int inputIndex) const {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900150 static const float NOT_A_DISTANCE_FLOAT = -1.0f;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900151 if (keyIndex == NOT_A_INDEX) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900152 return NOT_A_DISTANCE_FLOAT;
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900153 }
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900154 if (!hasSweetSpotData(keyIndex)) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900155 return NOT_A_DISTANCE_FLOAT;
Yusuke Nojima16717152011-10-04 17:04:07 +0900156 }
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900157 const float squaredDistance = calculateSquaredDistanceFromSweetSpotCenter(keyIndex, inputIndex);
158 const float squaredRadius = square(mSweetSpotRadii[keyIndex]);
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900159 return squaredDistance / squaredRadius;
Yusuke Nojima16717152011-10-04 17:04:07 +0900160}
161
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900162int ProximityInfo::getKeyIndex(const int c) const {
163 if (KEY_COUNT == 0 || !mInputXCoordinates || !mInputYCoordinates) {
164 // We do not have the coordinate data
165 return NOT_A_INDEX;
166 }
167 const unsigned short baseLowerC = Dictionary::toBaseLowerCase(c);
168 if (baseLowerC > MAX_CHAR_CODE) {
169 return NOT_A_INDEX;
170 }
171 return mCodeToKeyIndex[baseLowerC];
172}
173
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900174float ProximityInfo::calculateSquaredDistanceFromSweetSpotCenter(
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900175 const int keyIndex, const int inputIndex) const {
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900176 const float sweetSpotCenterX = mSweetSpotCenterXs[keyIndex];
177 const float sweetSpotCenterY = mSweetSpotCenterYs[keyIndex];
178 const float inputX = (float)mInputXCoordinates[inputIndex];
179 const float inputY = (float)mInputYCoordinates[inputIndex];
180 return square(inputX - sweetSpotCenterX) + square(inputY - sweetSpotCenterY);
181}
182
satokd24df432011-07-14 15:43:42 +0900183inline const int* ProximityInfo::getProximityCharsAt(const int index) const {
satok1d7eaf82011-07-13 10:32:02 +0900184 return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE);
185}
186
satokd24df432011-07-14 15:43:42 +0900187unsigned short ProximityInfo::getPrimaryCharAt(const int index) const {
188 return getProximityCharsAt(index)[0];
189}
190
satok2df30602011-07-15 13:49:00 +0900191inline bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const {
satokd24df432011-07-14 15:43:42 +0900192 const int *chars = getProximityCharsAt(index);
193 int i = 0;
194 while (chars[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE) {
195 if (chars[i++] == c) {
196 return true;
197 }
198 }
199 return false;
200}
201
202bool ProximityInfo::existsAdjacentProximityChars(const int index) const {
203 if (index < 0 || index >= mInputLength) return false;
204 const int currentChar = getPrimaryCharAt(index);
205 const int leftIndex = index - 1;
206 if (leftIndex >= 0 && existsCharInProximityAt(leftIndex, currentChar)) {
207 return true;
208 }
209 const int rightIndex = index + 1;
210 if (rightIndex < mInputLength && existsCharInProximityAt(rightIndex, currentChar)) {
211 return true;
212 }
213 return false;
214}
215
216// In the following function, c is the current character of the dictionary word
217// currently examined.
218// currentChars is an array containing the keys close to the character the
219// user actually typed at the same position. We want to see if c is in it: if so,
220// then the word contains at that position a character close to what the user
221// typed.
222// What the user typed is actually the first character of the array.
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900223// proximityIndex is a pointer to the variable where getMatchedProximityId returns
224// the index of c in the proximity chars of the input index.
satokd24df432011-07-14 15:43:42 +0900225// Notice : accented characters do not have a proximity list, so they are alone
226// in their list. The non-accented version of the character should be considered
227// "close", but not the other keys close to the non-accented version.
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900228ProximityInfo::ProximityType ProximityInfo::getMatchedProximityId(const int index,
229 const unsigned short c, const bool checkProximityChars, int *proximityIndex) const {
satokd24df432011-07-14 15:43:42 +0900230 const int *currentChars = getProximityCharsAt(index);
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900231 const int firstChar = currentChars[0];
satokd24df432011-07-14 15:43:42 +0900232 const unsigned short baseLowerC = Dictionary::toBaseLowerCase(c);
233
234 // The first char in the array is what user typed. If it matches right away,
235 // that means the user typed that same char for this pos.
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900236 if (firstChar == baseLowerC || firstChar == c) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900237 return EQUIVALENT_CHAR;
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900238 }
satokd24df432011-07-14 15:43:42 +0900239
satok985312e2011-08-05 21:21:01 +0900240 if (!checkProximityChars) return UNRELATED_CHAR;
satokd24df432011-07-14 15:43:42 +0900241
242 // If the non-accented, lowercased version of that first character matches c,
243 // then we have a non-accented version of the accented character the user
244 // typed. Treat it as a close char.
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900245 if (Dictionary::toBaseLowerCase(firstChar) == baseLowerC)
satokd24df432011-07-14 15:43:42 +0900246 return NEAR_PROXIMITY_CHAR;
247
248 // Not an exact nor an accent-alike match: search the list of close keys
249 int j = 1;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900250 while (j < MAX_PROXIMITY_CHARS_SIZE && currentChars[j] > 0) {
satokd24df432011-07-14 15:43:42 +0900251 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900252 if (matched) {
253 if (proximityIndex) {
254 *proximityIndex = j;
255 }
256 return NEAR_PROXIMITY_CHAR;
257 }
satokd24df432011-07-14 15:43:42 +0900258 ++j;
259 }
260
261 // Was not included, signal this as an unrelated character.
262 return UNRELATED_CHAR;
263}
264
satok1d7eaf82011-07-13 10:32:02 +0900265bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const {
266 if (length != mInputLength) {
267 return false;
268 }
269 const int *inputCodes = mInputCodes;
270 while (length--) {
271 if ((unsigned int) *inputCodes != (unsigned int) *word) {
272 return false;
273 }
274 inputCodes += MAX_PROXIMITY_CHARS_SIZE;
275 word++;
276 }
277 return true;
278}
279
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900280const int ProximityInfo::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR_LOG_2;
281const int ProximityInfo::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900282const int ProximityInfo::MAX_KEY_COUNT_IN_A_KEYBOARD;
Yusuke Nojimaad358352011-09-29 16:44:54 +0900283const int ProximityInfo::MAX_CHAR_CODE;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900284
Ken Wakasace9e52a2011-06-18 13:09:55 +0900285} // namespace latinime