blob: 4c21c2eb31afdc285342e4af7796c545b09b09f0 [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) {
118 mSweetSpotTypes[i] = calculateSweetSpotType(i);
119 }
satok1d7eaf82011-07-13 10:32:02 +0900120}
121
Yusuke Nojima16717152011-10-04 17:04:07 +0900122inline float square(const float x) { return x * x; }
123
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900124ProximityInfo::SweetSpotType ProximityInfo::calculateSweetSpotType(int index) const {
125 if (KEY_COUNT == 0 || !mInputXCoordinates || !mInputYCoordinates) {
126 // We do not have the coordinate data
127 return UNKNOWN;
128 }
129 const int currentChar = getPrimaryCharAt(index);
130 const unsigned short baseLowerC = Dictionary::toBaseLowerCase(currentChar);
131 if (baseLowerC > MAX_CHAR_CODE) {
Yusuke Nojima16717152011-10-04 17:04:07 +0900132 return UNKNOWN;
133 }
134 const int keyIndex = mCodeToKeyIndex[baseLowerC];
135 if (keyIndex < 0) {
136 return UNKNOWN;
137 }
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900138 const float radius = mSweetSpotRadii[keyIndex];
139 if (radius <= 0.0) {
140 // When there are no calibration data for a key,
141 // the radius of the key is assigned to zero.
Yusuke Nojima16717152011-10-04 17:04:07 +0900142 return UNKNOWN;
143 }
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900144 const float squaredRadius = square(radius);
145 const float squaredDistance = calculateSquaredDistanceFromSweetSpotCenter(keyIndex, index);
146 if (squaredDistance <= squaredRadius) {
Yusuke Nojima16717152011-10-04 17:04:07 +0900147 return IN_SWEET_SPOT;
148 }
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900149 if (squaredDistance <= square(NEUTRAL_AREA_RADIUS_RATIO) * squaredRadius) {
Yusuke Nojima16717152011-10-04 17:04:07 +0900150 return IN_NEUTRAL_AREA;
151 }
152 return OUT_OF_NEUTRAL_AREA;
153}
154
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900155float ProximityInfo::calculateSquaredDistanceFromSweetSpotCenter(
156 int keyIndex, int inputIndex) const {
157 const float sweetSpotCenterX = mSweetSpotCenterXs[keyIndex];
158 const float sweetSpotCenterY = mSweetSpotCenterYs[keyIndex];
159 const float inputX = (float)mInputXCoordinates[inputIndex];
160 const float inputY = (float)mInputYCoordinates[inputIndex];
161 return square(inputX - sweetSpotCenterX) + square(inputY - sweetSpotCenterY);
162}
163
satokd24df432011-07-14 15:43:42 +0900164inline const int* ProximityInfo::getProximityCharsAt(const int index) const {
satok1d7eaf82011-07-13 10:32:02 +0900165 return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE);
166}
167
satokd24df432011-07-14 15:43:42 +0900168unsigned short ProximityInfo::getPrimaryCharAt(const int index) const {
169 return getProximityCharsAt(index)[0];
170}
171
satok2df30602011-07-15 13:49:00 +0900172inline bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const {
satokd24df432011-07-14 15:43:42 +0900173 const int *chars = getProximityCharsAt(index);
174 int i = 0;
175 while (chars[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE) {
176 if (chars[i++] == c) {
177 return true;
178 }
179 }
180 return false;
181}
182
183bool ProximityInfo::existsAdjacentProximityChars(const int index) const {
184 if (index < 0 || index >= mInputLength) return false;
185 const int currentChar = getPrimaryCharAt(index);
186 const int leftIndex = index - 1;
187 if (leftIndex >= 0 && existsCharInProximityAt(leftIndex, currentChar)) {
188 return true;
189 }
190 const int rightIndex = index + 1;
191 if (rightIndex < mInputLength && existsCharInProximityAt(rightIndex, currentChar)) {
192 return true;
193 }
194 return false;
195}
196
197// In the following function, c is the current character of the dictionary word
198// currently examined.
199// currentChars is an array containing the keys close to the character the
200// user actually typed at the same position. We want to see if c is in it: if so,
201// then the word contains at that position a character close to what the user
202// typed.
203// What the user typed is actually the first character of the array.
204// Notice : accented characters do not have a proximity list, so they are alone
205// in their list. The non-accented version of the character should be considered
206// "close", but not the other keys close to the non-accented version.
207ProximityInfo::ProximityType ProximityInfo::getMatchedProximityId(
satok985312e2011-08-05 21:21:01 +0900208 const int index, const unsigned short c, const bool checkProximityChars) const {
satokd24df432011-07-14 15:43:42 +0900209 const int *currentChars = getProximityCharsAt(index);
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900210 const int firstChar = currentChars[0];
satokd24df432011-07-14 15:43:42 +0900211 const unsigned short baseLowerC = Dictionary::toBaseLowerCase(c);
212
213 // The first char in the array is what user typed. If it matches right away,
214 // that means the user typed that same char for this pos.
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900215 if (firstChar == baseLowerC || firstChar == c) {
216 if (CALIBRATE_SCORE_BY_TOUCH_COORDINATES) {
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900217 switch (mSweetSpotTypes[index]) {
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900218 case UNKNOWN:
219 return EQUIVALENT_CHAR_NORMAL;
220 case IN_SWEET_SPOT:
221 return EQUIVALENT_CHAR_STRONG;
222 case IN_NEUTRAL_AREA:
223 return EQUIVALENT_CHAR_NORMAL;
224 case OUT_OF_NEUTRAL_AREA:
225 return EQUIVALENT_CHAR_WEAK;
226 default:
227 assert(false);
228 }
229 } else {
230 return EQUIVALENT_CHAR_NORMAL;
231 }
232 }
satokd24df432011-07-14 15:43:42 +0900233
satok985312e2011-08-05 21:21:01 +0900234 if (!checkProximityChars) return UNRELATED_CHAR;
satokd24df432011-07-14 15:43:42 +0900235
236 // If the non-accented, lowercased version of that first character matches c,
237 // then we have a non-accented version of the accented character the user
238 // typed. Treat it as a close char.
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900239 if (Dictionary::toBaseLowerCase(firstChar) == baseLowerC)
satokd24df432011-07-14 15:43:42 +0900240 return NEAR_PROXIMITY_CHAR;
241
242 // Not an exact nor an accent-alike match: search the list of close keys
243 int j = 1;
244 while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS_SIZE) {
245 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
246 if (matched) return NEAR_PROXIMITY_CHAR;
247 ++j;
248 }
249
250 // Was not included, signal this as an unrelated character.
251 return UNRELATED_CHAR;
252}
253
satok1d7eaf82011-07-13 10:32:02 +0900254bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const {
255 if (length != mInputLength) {
256 return false;
257 }
258 const int *inputCodes = mInputCodes;
259 while (length--) {
260 if ((unsigned int) *inputCodes != (unsigned int) *word) {
261 return false;
262 }
263 inputCodes += MAX_PROXIMITY_CHARS_SIZE;
264 word++;
265 }
266 return true;
267}
268
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900269const int ProximityInfo::MAX_KEY_COUNT_IN_A_KEYBOARD;
Yusuke Nojimaad358352011-09-29 16:44:54 +0900270const int ProximityInfo::MAX_CHAR_CODE;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900271
Ken Wakasace9e52a2011-06-18 13:09:55 +0900272} // namespace latinime