blob: de3f421f3b7d64406feae42bc420ba236c297522 [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)),
47 mInputXCoordinates(NULL), mInputYCoordinates(NULL) {
satok817e5172011-03-04 06:06:45 -080048 const int len = GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE;
49 mProximityCharsArray = new uint32_t[len];
50 if (DEBUG_PROXIMITY_INFO) {
51 LOGI("Create proximity info array %d", len);
52 }
53 memcpy(mProximityCharsArray, proximityCharsArray, len * sizeof(mProximityCharsArray[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090054
Yusuke Nojimade2f8422011-09-27 11:15:18 +090055 copyOrFillZero(mKeyXCoordinates, keyXCoordinates, KEY_COUNT * sizeof(mKeyXCoordinates[0]));
56 copyOrFillZero(mKeyYCoordinates, keyYCoordinates, KEY_COUNT * sizeof(mKeyYCoordinates[0]));
57 copyOrFillZero(mKeyWidths, keyWidths, KEY_COUNT * sizeof(mKeyWidths[0]));
58 copyOrFillZero(mKeyHeights, keyHeights, KEY_COUNT * sizeof(mKeyHeights[0]));
59 copyOrFillZero(mKeyCharCodes, keyCharCodes, KEY_COUNT * sizeof(mKeyCharCodes[0]));
Yusuke Nojimaad358352011-09-29 16:44:54 +090060 copyOrFillZero(mSweetSpotCenterXs, sweetSpotCenterXs,
61 KEY_COUNT * sizeof(mSweetSpotCenterXs[0]));
62 copyOrFillZero(mSweetSpotCenterYs, sweetSpotCenterYs,
63 KEY_COUNT * sizeof(mSweetSpotCenterYs[0]));
64 copyOrFillZero(mSweetSpotRadii, sweetSpotRadii, KEY_COUNT * sizeof(mSweetSpotRadii[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090065
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090066 initializeCodeToKeyIndex();
67}
68
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090069// Build the reversed look up table from the char code to the index in mKeyXCoordinates,
70// mKeyYCoordinates, mKeyWidths, mKeyHeights, mKeyCharCodes.
71void ProximityInfo::initializeCodeToKeyIndex() {
Yusuke Nojimaad358352011-09-29 16:44:54 +090072 memset(mCodeToKeyIndex, -1, (MAX_CHAR_CODE + 1) * sizeof(mCodeToKeyIndex[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090073 for (int i = 0; i < KEY_COUNT; ++i) {
74 const int code = mKeyCharCodes[i];
Yusuke Nojimaad358352011-09-29 16:44:54 +090075 if (0 <= code && code <= MAX_CHAR_CODE) {
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090076 mCodeToKeyIndex[code] = i;
Yusuke Nojimaad358352011-09-29 16:44:54 +090077 }
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090078 }
satok8fbd5522011-02-22 17:28:55 +090079}
80
81ProximityInfo::~ProximityInfo() {
82 delete[] mProximityCharsArray;
83}
satok817e5172011-03-04 06:06:45 -080084
85inline int ProximityInfo::getStartIndexFromCoordinates(const int x, const int y) const {
satok3c4bb772011-03-04 22:50:19 -080086 return ((y / CELL_HEIGHT) * GRID_WIDTH + (x / CELL_WIDTH))
satok817e5172011-03-04 06:06:45 -080087 * MAX_PROXIMITY_CHARS_SIZE;
satok8fbd5522011-02-22 17:28:55 +090088}
satok817e5172011-03-04 06:06:45 -080089
90bool ProximityInfo::hasSpaceProximity(const int x, const int y) const {
91 const int startIndex = getStartIndexFromCoordinates(x, y);
92 if (DEBUG_PROXIMITY_INFO) {
93 LOGI("hasSpaceProximity: index %d", startIndex);
94 }
95 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
96 if (DEBUG_PROXIMITY_INFO) {
97 LOGI("Index: %d", mProximityCharsArray[startIndex + i]);
98 }
99 if (mProximityCharsArray[startIndex + i] == KEYCODE_SPACE) {
100 return true;
101 }
102 }
103 return false;
104}
Ken Wakasace9e52a2011-06-18 13:09:55 +0900105
satok1d7eaf82011-07-13 10:32:02 +0900106// TODO: Calculate nearby codes here.
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900107void ProximityInfo::setInputParams(const int* inputCodes, const int inputLength,
108 const int* xCoordinates, const int* yCoordinates) {
satok1d7eaf82011-07-13 10:32:02 +0900109 mInputCodes = inputCodes;
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900110 mInputXCoordinates = xCoordinates;
111 mInputYCoordinates = yCoordinates;
satok1d7eaf82011-07-13 10:32:02 +0900112 mInputLength = inputLength;
satok0cedd2b2011-08-12 01:05:27 +0900113 for (int i = 0; i < inputLength; ++i) {
114 mPrimaryInputWord[i] = getPrimaryCharAt(i);
115 }
116 mPrimaryInputWord[inputLength] = 0;
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900117 for (int i = 0; i < mInputLength; ++i) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900118 float normalizedSquaredDistance = calculateNormalizedSquaredDistance(i);
119 if (normalizedSquaredDistance >= 0.0f) {
120 mNormalizedSquaredDistance[i] =
121 (int)(normalizedSquaredDistance * NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR);
122 } else {
123 mNormalizedSquaredDistance[i] = NOT_A_DISTANCE;
124 }
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900125 }
satok1d7eaf82011-07-13 10:32:02 +0900126}
127
Yusuke Nojima16717152011-10-04 17:04:07 +0900128inline float square(const float x) { return x * x; }
129
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900130float ProximityInfo::calculateNormalizedSquaredDistance(int index) const {
131 static const float NOT_A_DISTANCE_FLOAT = -1.0f;
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900132 if (KEY_COUNT == 0 || !mInputXCoordinates || !mInputYCoordinates) {
133 // We do not have the coordinate data
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900134 return NOT_A_DISTANCE_FLOAT;
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900135 }
136 const int currentChar = getPrimaryCharAt(index);
137 const unsigned short baseLowerC = Dictionary::toBaseLowerCase(currentChar);
138 if (baseLowerC > MAX_CHAR_CODE) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900139 return NOT_A_DISTANCE_FLOAT;
Yusuke Nojima16717152011-10-04 17:04:07 +0900140 }
141 const int keyIndex = mCodeToKeyIndex[baseLowerC];
142 if (keyIndex < 0) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900143 return NOT_A_DISTANCE_FLOAT;
Yusuke Nojima16717152011-10-04 17:04:07 +0900144 }
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900145 const float radius = mSweetSpotRadii[keyIndex];
146 if (radius <= 0.0) {
147 // When there are no calibration data for a key,
148 // the radius of the key is assigned to zero.
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900149 return NOT_A_DISTANCE;
Yusuke Nojima16717152011-10-04 17:04:07 +0900150 }
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900151 const float squaredRadius = square(radius);
152 const float squaredDistance = calculateSquaredDistanceFromSweetSpotCenter(keyIndex, index);
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900153 return squaredDistance / squaredRadius;
Yusuke Nojima16717152011-10-04 17:04:07 +0900154}
155
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900156float ProximityInfo::calculateSquaredDistanceFromSweetSpotCenter(
157 int keyIndex, int inputIndex) const {
158 const float sweetSpotCenterX = mSweetSpotCenterXs[keyIndex];
159 const float sweetSpotCenterY = mSweetSpotCenterYs[keyIndex];
160 const float inputX = (float)mInputXCoordinates[inputIndex];
161 const float inputY = (float)mInputYCoordinates[inputIndex];
162 return square(inputX - sweetSpotCenterX) + square(inputY - sweetSpotCenterY);
163}
164
satokd24df432011-07-14 15:43:42 +0900165inline const int* ProximityInfo::getProximityCharsAt(const int index) const {
satok1d7eaf82011-07-13 10:32:02 +0900166 return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE);
167}
168
satokd24df432011-07-14 15:43:42 +0900169unsigned short ProximityInfo::getPrimaryCharAt(const int index) const {
170 return getProximityCharsAt(index)[0];
171}
172
satok2df30602011-07-15 13:49:00 +0900173inline bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const {
satokd24df432011-07-14 15:43:42 +0900174 const int *chars = getProximityCharsAt(index);
175 int i = 0;
176 while (chars[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE) {
177 if (chars[i++] == c) {
178 return true;
179 }
180 }
181 return false;
182}
183
184bool ProximityInfo::existsAdjacentProximityChars(const int index) const {
185 if (index < 0 || index >= mInputLength) return false;
186 const int currentChar = getPrimaryCharAt(index);
187 const int leftIndex = index - 1;
188 if (leftIndex >= 0 && existsCharInProximityAt(leftIndex, currentChar)) {
189 return true;
190 }
191 const int rightIndex = index + 1;
192 if (rightIndex < mInputLength && existsCharInProximityAt(rightIndex, currentChar)) {
193 return true;
194 }
195 return false;
196}
197
198// In the following function, c is the current character of the dictionary word
199// currently examined.
200// currentChars is an array containing the keys close to the character the
201// user actually typed at the same position. We want to see if c is in it: if so,
202// then the word contains at that position a character close to what the user
203// typed.
204// What the user typed is actually the first character of the array.
205// Notice : accented characters do not have a proximity list, so they are alone
206// in their list. The non-accented version of the character should be considered
207// "close", but not the other keys close to the non-accented version.
208ProximityInfo::ProximityType ProximityInfo::getMatchedProximityId(
satok985312e2011-08-05 21:21:01 +0900209 const int index, const unsigned short c, const bool checkProximityChars) const {
satokd24df432011-07-14 15:43:42 +0900210 const int *currentChars = getProximityCharsAt(index);
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900211 const int firstChar = currentChars[0];
satokd24df432011-07-14 15:43:42 +0900212 const unsigned short baseLowerC = Dictionary::toBaseLowerCase(c);
213
214 // The first char in the array is what user typed. If it matches right away,
215 // that means the user typed that same char for this pos.
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900216 if (firstChar == baseLowerC || firstChar == c) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900217 return EQUIVALENT_CHAR;
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900218 }
satokd24df432011-07-14 15:43:42 +0900219
satok985312e2011-08-05 21:21:01 +0900220 if (!checkProximityChars) return UNRELATED_CHAR;
satokd24df432011-07-14 15:43:42 +0900221
222 // If the non-accented, lowercased version of that first character matches c,
223 // then we have a non-accented version of the accented character the user
224 // typed. Treat it as a close char.
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900225 if (Dictionary::toBaseLowerCase(firstChar) == baseLowerC)
satokd24df432011-07-14 15:43:42 +0900226 return NEAR_PROXIMITY_CHAR;
227
228 // Not an exact nor an accent-alike match: search the list of close keys
229 int j = 1;
230 while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS_SIZE) {
231 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
232 if (matched) return NEAR_PROXIMITY_CHAR;
233 ++j;
234 }
235
236 // Was not included, signal this as an unrelated character.
237 return UNRELATED_CHAR;
238}
239
satok1d7eaf82011-07-13 10:32:02 +0900240bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const {
241 if (length != mInputLength) {
242 return false;
243 }
244 const int *inputCodes = mInputCodes;
245 while (length--) {
246 if ((unsigned int) *inputCodes != (unsigned int) *word) {
247 return false;
248 }
249 inputCodes += MAX_PROXIMITY_CHARS_SIZE;
250 word++;
251 }
252 return true;
253}
254
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900255const int ProximityInfo::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR_LOG_2;
256const int ProximityInfo::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900257const int ProximityInfo::MAX_KEY_COUNT_IN_A_KEYBOARD;
Yusuke Nojimaad358352011-09-29 16:44:54 +0900258const int ProximityInfo::MAX_CHAR_CODE;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900259
Ken Wakasace9e52a2011-06-18 13:09:55 +0900260} // namespace latinime