blob: 6857caf00a745a08b040ce4239d05b8a17722c02 [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 {
103 const int startIndex = getStartIndexFromCoordinates(x, y);
104 if (DEBUG_PROXIMITY_INFO) {
105 LOGI("hasSpaceProximity: index %d", startIndex);
106 }
107 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
108 if (DEBUG_PROXIMITY_INFO) {
109 LOGI("Index: %d", mProximityCharsArray[startIndex + i]);
110 }
111 if (mProximityCharsArray[startIndex + i] == KEYCODE_SPACE) {
112 return true;
113 }
114 }
115 return false;
116}
Ken Wakasace9e52a2011-06-18 13:09:55 +0900117
satok1d7eaf82011-07-13 10:32:02 +0900118// TODO: Calculate nearby codes here.
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900119void ProximityInfo::setInputParams(const int* inputCodes, const int inputLength,
120 const int* xCoordinates, const int* yCoordinates) {
satok1d7eaf82011-07-13 10:32:02 +0900121 mInputCodes = inputCodes;
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900122 mInputXCoordinates = xCoordinates;
123 mInputYCoordinates = yCoordinates;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900124 mTouchPositionCorrectionEnabled =
125 HAS_TOUCH_POSITION_CORRECTION_DATA && xCoordinates && yCoordinates;
satok1d7eaf82011-07-13 10:32:02 +0900126 mInputLength = inputLength;
satok0cedd2b2011-08-12 01:05:27 +0900127 for (int i = 0; i < inputLength; ++i) {
128 mPrimaryInputWord[i] = getPrimaryCharAt(i);
129 }
130 mPrimaryInputWord[inputLength] = 0;
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900131 for (int i = 0; i < mInputLength; ++i) {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900132 const int *proximityChars = getProximityCharsAt(i);
133 for (int j = 0; j < MAX_PROXIMITY_CHARS_SIZE && proximityChars[j] > 0; ++j) {
134 const int currentChar = proximityChars[j];
135 const int keyIndex = getKeyIndex(currentChar);
136 const float squaredDistance = calculateNormalizedSquaredDistance(keyIndex, i);
137 if (squaredDistance >= 0.0f) {
138 mNormalizedSquaredDistances[i * MAX_PROXIMITY_CHARS_SIZE + j] =
139 (int)(squaredDistance * NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR);
140 } else {
141 mNormalizedSquaredDistances[i * MAX_PROXIMITY_CHARS_SIZE + j] = (j == 0)
142 ? EQUIVALENT_CHAR_WITHOUT_DISTANCE_INFO
143 : PROXIMITY_CHAR_WITHOUT_DISTANCE_INFO;
144 }
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900145 }
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900146 }
satok1d7eaf82011-07-13 10:32:02 +0900147}
148
Yusuke Nojima16717152011-10-04 17:04:07 +0900149inline float square(const float x) { return x * x; }
150
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900151float ProximityInfo::calculateNormalizedSquaredDistance(
152 const int keyIndex, const int inputIndex) const {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900153 static const float NOT_A_DISTANCE_FLOAT = -1.0f;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900154 if (keyIndex == NOT_A_INDEX) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900155 return NOT_A_DISTANCE_FLOAT;
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900156 }
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900157 if (!hasSweetSpotData(keyIndex)) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900158 return NOT_A_DISTANCE_FLOAT;
Yusuke Nojima16717152011-10-04 17:04:07 +0900159 }
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900160 const float squaredDistance = calculateSquaredDistanceFromSweetSpotCenter(keyIndex, inputIndex);
161 const float squaredRadius = square(mSweetSpotRadii[keyIndex]);
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900162 return squaredDistance / squaredRadius;
Yusuke Nojima16717152011-10-04 17:04:07 +0900163}
164
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900165int ProximityInfo::getKeyIndex(const int c) const {
166 if (KEY_COUNT == 0 || !mInputXCoordinates || !mInputYCoordinates) {
167 // We do not have the coordinate data
168 return NOT_A_INDEX;
169 }
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900170 const unsigned short baseLowerC = toBaseLowerCase(c);
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900171 if (baseLowerC > MAX_CHAR_CODE) {
172 return NOT_A_INDEX;
173 }
174 return mCodeToKeyIndex[baseLowerC];
175}
176
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900177float ProximityInfo::calculateSquaredDistanceFromSweetSpotCenter(
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900178 const int keyIndex, const int inputIndex) const {
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900179 const float sweetSpotCenterX = mSweetSpotCenterXs[keyIndex];
180 const float sweetSpotCenterY = mSweetSpotCenterYs[keyIndex];
181 const float inputX = (float)mInputXCoordinates[inputIndex];
182 const float inputY = (float)mInputYCoordinates[inputIndex];
183 return square(inputX - sweetSpotCenterX) + square(inputY - sweetSpotCenterY);
184}
185
satokd24df432011-07-14 15:43:42 +0900186inline const int* ProximityInfo::getProximityCharsAt(const int index) const {
satok1d7eaf82011-07-13 10:32:02 +0900187 return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE);
188}
189
satokd24df432011-07-14 15:43:42 +0900190unsigned short ProximityInfo::getPrimaryCharAt(const int index) const {
191 return getProximityCharsAt(index)[0];
192}
193
satok2df30602011-07-15 13:49:00 +0900194inline bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const {
satokd24df432011-07-14 15:43:42 +0900195 const int *chars = getProximityCharsAt(index);
196 int i = 0;
197 while (chars[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE) {
198 if (chars[i++] == c) {
199 return true;
200 }
201 }
202 return false;
203}
204
205bool ProximityInfo::existsAdjacentProximityChars(const int index) const {
206 if (index < 0 || index >= mInputLength) return false;
207 const int currentChar = getPrimaryCharAt(index);
208 const int leftIndex = index - 1;
209 if (leftIndex >= 0 && existsCharInProximityAt(leftIndex, currentChar)) {
210 return true;
211 }
212 const int rightIndex = index + 1;
213 if (rightIndex < mInputLength && existsCharInProximityAt(rightIndex, currentChar)) {
214 return true;
215 }
216 return false;
217}
218
219// In the following function, c is the current character of the dictionary word
220// currently examined.
221// currentChars is an array containing the keys close to the character the
222// user actually typed at the same position. We want to see if c is in it: if so,
223// then the word contains at that position a character close to what the user
224// typed.
225// What the user typed is actually the first character of the array.
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900226// proximityIndex is a pointer to the variable where getMatchedProximityId returns
227// the index of c in the proximity chars of the input index.
satokd24df432011-07-14 15:43:42 +0900228// Notice : accented characters do not have a proximity list, so they are alone
229// in their list. The non-accented version of the character should be considered
230// "close", but not the other keys close to the non-accented version.
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900231ProximityInfo::ProximityType ProximityInfo::getMatchedProximityId(const int index,
232 const unsigned short c, const bool checkProximityChars, int *proximityIndex) const {
satokd24df432011-07-14 15:43:42 +0900233 const int *currentChars = getProximityCharsAt(index);
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900234 const int firstChar = currentChars[0];
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900235 const unsigned short baseLowerC = toBaseLowerCase(c);
satokd24df432011-07-14 15:43:42 +0900236
237 // The first char in the array is what user typed. If it matches right away,
238 // that means the user typed that same char for this pos.
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900239 if (firstChar == baseLowerC || firstChar == c) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900240 return EQUIVALENT_CHAR;
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900241 }
satokd24df432011-07-14 15:43:42 +0900242
satok985312e2011-08-05 21:21:01 +0900243 if (!checkProximityChars) return UNRELATED_CHAR;
satokd24df432011-07-14 15:43:42 +0900244
245 // If the non-accented, lowercased version of that first character matches c,
246 // then we have a non-accented version of the accented character the user
247 // typed. Treat it as a close char.
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900248 if (toBaseLowerCase(firstChar) == baseLowerC)
satokd24df432011-07-14 15:43:42 +0900249 return NEAR_PROXIMITY_CHAR;
250
251 // Not an exact nor an accent-alike match: search the list of close keys
252 int j = 1;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900253 while (j < MAX_PROXIMITY_CHARS_SIZE && currentChars[j] > 0) {
satokd24df432011-07-14 15:43:42 +0900254 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900255 if (matched) {
256 if (proximityIndex) {
257 *proximityIndex = j;
258 }
259 return NEAR_PROXIMITY_CHAR;
260 }
satokd24df432011-07-14 15:43:42 +0900261 ++j;
262 }
263
264 // Was not included, signal this as an unrelated character.
265 return UNRELATED_CHAR;
266}
267
satok1d7eaf82011-07-13 10:32:02 +0900268bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const {
269 if (length != mInputLength) {
270 return false;
271 }
272 const int *inputCodes = mInputCodes;
273 while (length--) {
274 if ((unsigned int) *inputCodes != (unsigned int) *word) {
275 return false;
276 }
277 inputCodes += MAX_PROXIMITY_CHARS_SIZE;
278 word++;
279 }
280 return true;
281}
282
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900283const int ProximityInfo::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR_LOG_2;
284const int ProximityInfo::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900285const int ProximityInfo::MAX_KEY_COUNT_IN_A_KEYBOARD;
Yusuke Nojimaad358352011-09-29 16:44:54 +0900286const int ProximityInfo::MAX_CHAR_CODE;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900287
Ken Wakasace9e52a2011-06-18 13:09:55 +0900288} // namespace latinime