blob: 081cb61c39e715e754424e655284a694f2d851be [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;
satok1d7eaf82011-07-13 10:32:02 +0900117}
118
satokd24df432011-07-14 15:43:42 +0900119inline const int* ProximityInfo::getProximityCharsAt(const int index) const {
satok1d7eaf82011-07-13 10:32:02 +0900120 return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE);
121}
122
satokd24df432011-07-14 15:43:42 +0900123unsigned short ProximityInfo::getPrimaryCharAt(const int index) const {
124 return getProximityCharsAt(index)[0];
125}
126
satok2df30602011-07-15 13:49:00 +0900127inline bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const {
satokd24df432011-07-14 15:43:42 +0900128 const int *chars = getProximityCharsAt(index);
129 int i = 0;
130 while (chars[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE) {
131 if (chars[i++] == c) {
132 return true;
133 }
134 }
135 return false;
136}
137
138bool ProximityInfo::existsAdjacentProximityChars(const int index) const {
139 if (index < 0 || index >= mInputLength) return false;
140 const int currentChar = getPrimaryCharAt(index);
141 const int leftIndex = index - 1;
142 if (leftIndex >= 0 && existsCharInProximityAt(leftIndex, currentChar)) {
143 return true;
144 }
145 const int rightIndex = index + 1;
146 if (rightIndex < mInputLength && existsCharInProximityAt(rightIndex, currentChar)) {
147 return true;
148 }
149 return false;
150}
151
152// In the following function, c is the current character of the dictionary word
153// currently examined.
154// currentChars is an array containing the keys close to the character the
155// user actually typed at the same position. We want to see if c is in it: if so,
156// then the word contains at that position a character close to what the user
157// typed.
158// What the user typed is actually the first character of the array.
159// Notice : accented characters do not have a proximity list, so they are alone
160// in their list. The non-accented version of the character should be considered
161// "close", but not the other keys close to the non-accented version.
162ProximityInfo::ProximityType ProximityInfo::getMatchedProximityId(
satok985312e2011-08-05 21:21:01 +0900163 const int index, const unsigned short c, const bool checkProximityChars) const {
satokd24df432011-07-14 15:43:42 +0900164 const int *currentChars = getProximityCharsAt(index);
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900165 const int firstChar = currentChars[0];
satokd24df432011-07-14 15:43:42 +0900166 const unsigned short baseLowerC = Dictionary::toBaseLowerCase(c);
167
168 // The first char in the array is what user typed. If it matches right away,
169 // that means the user typed that same char for this pos.
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900170 if (firstChar == baseLowerC || firstChar == c) {
171 if (CALIBRATE_SCORE_BY_TOUCH_COORDINATES) {
172 const SweetSpotType result = calculateSweetSpotType(index, baseLowerC);
173 switch (result) {
174 case UNKNOWN:
175 return EQUIVALENT_CHAR_NORMAL;
176 case IN_SWEET_SPOT:
177 return EQUIVALENT_CHAR_STRONG;
178 case IN_NEUTRAL_AREA:
179 return EQUIVALENT_CHAR_NORMAL;
180 case OUT_OF_NEUTRAL_AREA:
181 return EQUIVALENT_CHAR_WEAK;
182 default:
183 assert(false);
184 }
185 } else {
186 return EQUIVALENT_CHAR_NORMAL;
187 }
188 }
satokd24df432011-07-14 15:43:42 +0900189
satok985312e2011-08-05 21:21:01 +0900190 if (!checkProximityChars) return UNRELATED_CHAR;
satokd24df432011-07-14 15:43:42 +0900191
192 // If the non-accented, lowercased version of that first character matches c,
193 // then we have a non-accented version of the accented character the user
194 // typed. Treat it as a close char.
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900195 if (Dictionary::toBaseLowerCase(firstChar) == baseLowerC)
satokd24df432011-07-14 15:43:42 +0900196 return NEAR_PROXIMITY_CHAR;
197
198 // Not an exact nor an accent-alike match: search the list of close keys
199 int j = 1;
200 while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS_SIZE) {
201 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
202 if (matched) return NEAR_PROXIMITY_CHAR;
203 ++j;
204 }
205
206 // Was not included, signal this as an unrelated character.
207 return UNRELATED_CHAR;
208}
209
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900210inline float square(const float x) { return x * x; }
211
212ProximityInfo::SweetSpotType ProximityInfo::calculateSweetSpotType(
213 int index, unsigned short baseLowerC) const {
214 if (KEY_COUNT == 0 || !mInputXCoordinates || !mInputYCoordinates
215 || baseLowerC > MAX_CHAR_CODE) {
216 return UNKNOWN;
217 }
218 const int keyIndex = mCodeToKeyIndex[baseLowerC];
219 if (keyIndex < 0) {
220 return UNKNOWN;
221 }
222 const float sweetSpotRadius = mSweetSpotRadii[keyIndex];
223 if (sweetSpotRadius <= 0.0) {
224 return UNKNOWN;
225 }
226 const float sweetSpotCenterX = mSweetSpotCenterXs[keyIndex];
227 const float sweetSpotCenterY = mSweetSpotCenterXs[keyIndex];
228 const float inputX = (float)mInputXCoordinates[index];
229 const float inputY = (float)mInputYCoordinates[index];
230 const float squaredDistance =
231 square(inputX - sweetSpotCenterX) + square(inputY - sweetSpotCenterY);
232 const float squaredSweetSpotRadius = square(sweetSpotRadius);
233 if (squaredDistance <= squaredSweetSpotRadius) {
234 return IN_SWEET_SPOT;
235 }
236 if (squaredDistance <= square(NEUTRAL_AREA_RADIUS_RATIO) * squaredSweetSpotRadius) {
237 return IN_NEUTRAL_AREA;
238 }
239 return OUT_OF_NEUTRAL_AREA;
240}
241
satok1d7eaf82011-07-13 10:32:02 +0900242bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const {
243 if (length != mInputLength) {
244 return false;
245 }
246 const int *inputCodes = mInputCodes;
247 while (length--) {
248 if ((unsigned int) *inputCodes != (unsigned int) *word) {
249 return false;
250 }
251 inputCodes += MAX_PROXIMITY_CHARS_SIZE;
252 word++;
253 }
254 return true;
255}
256
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