blob: 95e35263b3510b30deb7e86d329bf92a708b6be1 [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),
Tadashi G. Takaoka0e971482011-10-28 17:02:09 +090050 mInputXCoordinates(0), mInputYCoordinates(0),
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +090051 mTouchPositionCorrectionEnabled(false) {
Jean Chalard8c8ca592011-11-10 12:07:30 +090052 const int proximityGridLength = GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE;
53 mProximityCharsArray = new uint32_t[proximityGridLength];
satok817e5172011-03-04 06:06:45 -080054 if (DEBUG_PROXIMITY_INFO) {
Jean Chalard8c8ca592011-11-10 12:07:30 +090055 LOGI("Create proximity info array %d", proximityGridLength);
satok817e5172011-03-04 06:06:45 -080056 }
Jean Chalard8c8ca592011-11-10 12:07:30 +090057 memcpy(mProximityCharsArray, proximityCharsArray,
58 proximityGridLength * sizeof(mProximityCharsArray[0]));
59 const int normalizedSquaredDistancesLength =
60 MAX_PROXIMITY_CHARS_SIZE * MAX_WORD_LENGTH_INTERNAL;
61 mNormalizedSquaredDistances = new int[normalizedSquaredDistancesLength];
62 for (int i = 0; i < normalizedSquaredDistancesLength; ++i) {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +090063 mNormalizedSquaredDistances[i] = NOT_A_DISTANCE;
64 }
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090065
Yusuke Nojimade2f8422011-09-27 11:15:18 +090066 copyOrFillZero(mKeyXCoordinates, keyXCoordinates, KEY_COUNT * sizeof(mKeyXCoordinates[0]));
67 copyOrFillZero(mKeyYCoordinates, keyYCoordinates, KEY_COUNT * sizeof(mKeyYCoordinates[0]));
68 copyOrFillZero(mKeyWidths, keyWidths, KEY_COUNT * sizeof(mKeyWidths[0]));
69 copyOrFillZero(mKeyHeights, keyHeights, KEY_COUNT * sizeof(mKeyHeights[0]));
70 copyOrFillZero(mKeyCharCodes, keyCharCodes, KEY_COUNT * sizeof(mKeyCharCodes[0]));
Yusuke Nojimaad358352011-09-29 16:44:54 +090071 copyOrFillZero(mSweetSpotCenterXs, sweetSpotCenterXs,
72 KEY_COUNT * sizeof(mSweetSpotCenterXs[0]));
73 copyOrFillZero(mSweetSpotCenterYs, sweetSpotCenterYs,
74 KEY_COUNT * sizeof(mSweetSpotCenterYs[0]));
75 copyOrFillZero(mSweetSpotRadii, sweetSpotRadii, KEY_COUNT * sizeof(mSweetSpotRadii[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090076
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090077 initializeCodeToKeyIndex();
78}
79
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090080// Build the reversed look up table from the char code to the index in mKeyXCoordinates,
81// mKeyYCoordinates, mKeyWidths, mKeyHeights, mKeyCharCodes.
82void ProximityInfo::initializeCodeToKeyIndex() {
Yusuke Nojimaad358352011-09-29 16:44:54 +090083 memset(mCodeToKeyIndex, -1, (MAX_CHAR_CODE + 1) * sizeof(mCodeToKeyIndex[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090084 for (int i = 0; i < KEY_COUNT; ++i) {
85 const int code = mKeyCharCodes[i];
Yusuke Nojimaad358352011-09-29 16:44:54 +090086 if (0 <= code && code <= MAX_CHAR_CODE) {
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090087 mCodeToKeyIndex[code] = i;
Yusuke Nojimaad358352011-09-29 16:44:54 +090088 }
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090089 }
satok8fbd5522011-02-22 17:28:55 +090090}
91
92ProximityInfo::~ProximityInfo() {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +090093 delete[] mNormalizedSquaredDistances;
satok8fbd5522011-02-22 17:28:55 +090094 delete[] mProximityCharsArray;
95}
satok817e5172011-03-04 06:06:45 -080096
97inline int ProximityInfo::getStartIndexFromCoordinates(const int x, const int y) const {
satok3c4bb772011-03-04 22:50:19 -080098 return ((y / CELL_HEIGHT) * GRID_WIDTH + (x / CELL_WIDTH))
satok817e5172011-03-04 06:06:45 -080099 * MAX_PROXIMITY_CHARS_SIZE;
satok8fbd5522011-02-22 17:28:55 +0900100}
satok817e5172011-03-04 06:06:45 -0800101
102bool ProximityInfo::hasSpaceProximity(const int x, const int y) const {
satok744dab62011-12-15 22:29:05 +0900103 if (x < 0 || y < 0) {
104 if (DEBUG_DICT) {
105 LOGI("HasSpaceProximity: Illegal coordinates (%d, %d)", x, y);
satok1a6da632011-12-16 23:15:06 +0900106 assert(false);
satok744dab62011-12-15 22:29:05 +0900107 }
108 return false;
109 }
110
satok817e5172011-03-04 06:06:45 -0800111 const int startIndex = getStartIndexFromCoordinates(x, y);
112 if (DEBUG_PROXIMITY_INFO) {
satok744dab62011-12-15 22:29:05 +0900113 LOGI("hasSpaceProximity: index %d, %d, %d", startIndex, x, y);
satok817e5172011-03-04 06:06:45 -0800114 }
115 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
116 if (DEBUG_PROXIMITY_INFO) {
117 LOGI("Index: %d", mProximityCharsArray[startIndex + i]);
118 }
119 if (mProximityCharsArray[startIndex + i] == KEYCODE_SPACE) {
120 return true;
121 }
122 }
123 return false;
124}
Ken Wakasace9e52a2011-06-18 13:09:55 +0900125
satok1d7eaf82011-07-13 10:32:02 +0900126// TODO: Calculate nearby codes here.
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900127void ProximityInfo::setInputParams(const int* inputCodes, const int inputLength,
128 const int* xCoordinates, const int* yCoordinates) {
satok1d7eaf82011-07-13 10:32:02 +0900129 mInputCodes = inputCodes;
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900130 mInputXCoordinates = xCoordinates;
131 mInputYCoordinates = yCoordinates;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900132 mTouchPositionCorrectionEnabled =
133 HAS_TOUCH_POSITION_CORRECTION_DATA && xCoordinates && yCoordinates;
satok1d7eaf82011-07-13 10:32:02 +0900134 mInputLength = inputLength;
satok0cedd2b2011-08-12 01:05:27 +0900135 for (int i = 0; i < inputLength; ++i) {
136 mPrimaryInputWord[i] = getPrimaryCharAt(i);
137 }
138 mPrimaryInputWord[inputLength] = 0;
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900139 for (int i = 0; i < mInputLength; ++i) {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900140 const int *proximityChars = getProximityCharsAt(i);
141 for (int j = 0; j < MAX_PROXIMITY_CHARS_SIZE && proximityChars[j] > 0; ++j) {
142 const int currentChar = proximityChars[j];
143 const int keyIndex = getKeyIndex(currentChar);
144 const float squaredDistance = calculateNormalizedSquaredDistance(keyIndex, i);
145 if (squaredDistance >= 0.0f) {
146 mNormalizedSquaredDistances[i * MAX_PROXIMITY_CHARS_SIZE + j] =
147 (int)(squaredDistance * NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR);
148 } else {
149 mNormalizedSquaredDistances[i * MAX_PROXIMITY_CHARS_SIZE + j] = (j == 0)
150 ? EQUIVALENT_CHAR_WITHOUT_DISTANCE_INFO
151 : PROXIMITY_CHAR_WITHOUT_DISTANCE_INFO;
152 }
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900153 }
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900154 }
satok1d7eaf82011-07-13 10:32:02 +0900155}
156
Yusuke Nojima16717152011-10-04 17:04:07 +0900157inline float square(const float x) { return x * x; }
158
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900159float ProximityInfo::calculateNormalizedSquaredDistance(
160 const int keyIndex, const int inputIndex) const {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900161 static const float NOT_A_DISTANCE_FLOAT = -1.0f;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900162 if (keyIndex == NOT_A_INDEX) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900163 return NOT_A_DISTANCE_FLOAT;
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900164 }
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900165 if (!hasSweetSpotData(keyIndex)) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900166 return NOT_A_DISTANCE_FLOAT;
Yusuke Nojima16717152011-10-04 17:04:07 +0900167 }
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900168 const float squaredDistance = calculateSquaredDistanceFromSweetSpotCenter(keyIndex, inputIndex);
169 const float squaredRadius = square(mSweetSpotRadii[keyIndex]);
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900170 return squaredDistance / squaredRadius;
Yusuke Nojima16717152011-10-04 17:04:07 +0900171}
172
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900173int ProximityInfo::getKeyIndex(const int c) const {
174 if (KEY_COUNT == 0 || !mInputXCoordinates || !mInputYCoordinates) {
175 // We do not have the coordinate data
176 return NOT_A_INDEX;
177 }
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900178 const unsigned short baseLowerC = toBaseLowerCase(c);
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900179 if (baseLowerC > MAX_CHAR_CODE) {
180 return NOT_A_INDEX;
181 }
182 return mCodeToKeyIndex[baseLowerC];
183}
184
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900185float ProximityInfo::calculateSquaredDistanceFromSweetSpotCenter(
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900186 const int keyIndex, const int inputIndex) const {
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900187 const float sweetSpotCenterX = mSweetSpotCenterXs[keyIndex];
188 const float sweetSpotCenterY = mSweetSpotCenterYs[keyIndex];
189 const float inputX = (float)mInputXCoordinates[inputIndex];
190 const float inputY = (float)mInputYCoordinates[inputIndex];
191 return square(inputX - sweetSpotCenterX) + square(inputY - sweetSpotCenterY);
192}
193
satokd24df432011-07-14 15:43:42 +0900194inline const int* ProximityInfo::getProximityCharsAt(const int index) const {
satok1d7eaf82011-07-13 10:32:02 +0900195 return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE);
196}
197
satokd24df432011-07-14 15:43:42 +0900198unsigned short ProximityInfo::getPrimaryCharAt(const int index) const {
199 return getProximityCharsAt(index)[0];
200}
201
satok2df30602011-07-15 13:49:00 +0900202inline bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const {
satokd24df432011-07-14 15:43:42 +0900203 const int *chars = getProximityCharsAt(index);
204 int i = 0;
205 while (chars[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE) {
206 if (chars[i++] == c) {
207 return true;
208 }
209 }
210 return false;
211}
212
213bool ProximityInfo::existsAdjacentProximityChars(const int index) const {
214 if (index < 0 || index >= mInputLength) return false;
215 const int currentChar = getPrimaryCharAt(index);
216 const int leftIndex = index - 1;
217 if (leftIndex >= 0 && existsCharInProximityAt(leftIndex, currentChar)) {
218 return true;
219 }
220 const int rightIndex = index + 1;
221 if (rightIndex < mInputLength && existsCharInProximityAt(rightIndex, currentChar)) {
222 return true;
223 }
224 return false;
225}
226
227// In the following function, c is the current character of the dictionary word
228// currently examined.
229// currentChars is an array containing the keys close to the character the
230// user actually typed at the same position. We want to see if c is in it: if so,
231// then the word contains at that position a character close to what the user
232// typed.
233// What the user typed is actually the first character of the array.
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900234// proximityIndex is a pointer to the variable where getMatchedProximityId returns
235// the index of c in the proximity chars of the input index.
satokd24df432011-07-14 15:43:42 +0900236// Notice : accented characters do not have a proximity list, so they are alone
237// in their list. The non-accented version of the character should be considered
238// "close", but not the other keys close to the non-accented version.
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900239ProximityInfo::ProximityType ProximityInfo::getMatchedProximityId(const int index,
240 const unsigned short c, const bool checkProximityChars, int *proximityIndex) const {
satokd24df432011-07-14 15:43:42 +0900241 const int *currentChars = getProximityCharsAt(index);
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900242 const int firstChar = currentChars[0];
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900243 const unsigned short baseLowerC = toBaseLowerCase(c);
satokd24df432011-07-14 15:43:42 +0900244
245 // The first char in the array is what user typed. If it matches right away,
246 // that means the user typed that same char for this pos.
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900247 if (firstChar == baseLowerC || firstChar == c) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900248 return EQUIVALENT_CHAR;
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900249 }
satokd24df432011-07-14 15:43:42 +0900250
satok985312e2011-08-05 21:21:01 +0900251 if (!checkProximityChars) return UNRELATED_CHAR;
satokd24df432011-07-14 15:43:42 +0900252
253 // If the non-accented, lowercased version of that first character matches c,
254 // then we have a non-accented version of the accented character the user
255 // typed. Treat it as a close char.
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900256 if (toBaseLowerCase(firstChar) == baseLowerC)
satokd24df432011-07-14 15:43:42 +0900257 return NEAR_PROXIMITY_CHAR;
258
259 // Not an exact nor an accent-alike match: search the list of close keys
260 int j = 1;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900261 while (j < MAX_PROXIMITY_CHARS_SIZE && currentChars[j] > 0) {
satokd24df432011-07-14 15:43:42 +0900262 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900263 if (matched) {
264 if (proximityIndex) {
265 *proximityIndex = j;
266 }
267 return NEAR_PROXIMITY_CHAR;
268 }
satokd24df432011-07-14 15:43:42 +0900269 ++j;
270 }
271
272 // Was not included, signal this as an unrelated character.
273 return UNRELATED_CHAR;
274}
275
satok1d7eaf82011-07-13 10:32:02 +0900276bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const {
277 if (length != mInputLength) {
278 return false;
279 }
280 const int *inputCodes = mInputCodes;
281 while (length--) {
282 if ((unsigned int) *inputCodes != (unsigned int) *word) {
283 return false;
284 }
285 inputCodes += MAX_PROXIMITY_CHARS_SIZE;
286 word++;
287 }
288 return true;
289}
290
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900291const int ProximityInfo::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR_LOG_2;
292const int ProximityInfo::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900293const int ProximityInfo::MAX_KEY_COUNT_IN_A_KEYBOARD;
Yusuke Nojimaad358352011-09-29 16:44:54 +0900294const int ProximityInfo::MAX_CHAR_CODE;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900295
Ken Wakasace9e52a2011-06-18 13:09:55 +0900296} // namespace latinime