blob: 15f012d8ab2b1e0a689991fd6acc87995b05f139 [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
Yusuke Nojima16717152011-10-04 17:04:07 +0900119inline float square(const float x) { return x * x; }
120
121ProximityInfo::SweetSpotType ProximityInfo::calculateSweetSpotType(
122 int index, unsigned short baseLowerC) const {
123 if (KEY_COUNT == 0 || !mInputXCoordinates || !mInputYCoordinates
124 || baseLowerC > MAX_CHAR_CODE) {
125 return UNKNOWN;
126 }
127 const int keyIndex = mCodeToKeyIndex[baseLowerC];
128 if (keyIndex < 0) {
129 return UNKNOWN;
130 }
131 const float sweetSpotRadius = mSweetSpotRadii[keyIndex];
132 if (sweetSpotRadius <= 0.0) {
133 return UNKNOWN;
134 }
135 const float sweetSpotCenterX = mSweetSpotCenterXs[keyIndex];
136 const float sweetSpotCenterY = mSweetSpotCenterYs[keyIndex];
137 const float inputX = (float)mInputXCoordinates[index];
138 const float inputY = (float)mInputYCoordinates[index];
139 const float squaredDistance =
140 square(inputX - sweetSpotCenterX) + square(inputY - sweetSpotCenterY);
141 const float squaredSweetSpotRadius = square(sweetSpotRadius);
142 if (squaredDistance <= squaredSweetSpotRadius) {
143 return IN_SWEET_SPOT;
144 }
145 if (squaredDistance <= square(NEUTRAL_AREA_RADIUS_RATIO) * squaredSweetSpotRadius) {
146 return IN_NEUTRAL_AREA;
147 }
148 return OUT_OF_NEUTRAL_AREA;
149}
150
satokd24df432011-07-14 15:43:42 +0900151inline const int* ProximityInfo::getProximityCharsAt(const int index) const {
satok1d7eaf82011-07-13 10:32:02 +0900152 return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE);
153}
154
satokd24df432011-07-14 15:43:42 +0900155unsigned short ProximityInfo::getPrimaryCharAt(const int index) const {
156 return getProximityCharsAt(index)[0];
157}
158
satok2df30602011-07-15 13:49:00 +0900159inline bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const {
satokd24df432011-07-14 15:43:42 +0900160 const int *chars = getProximityCharsAt(index);
161 int i = 0;
162 while (chars[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE) {
163 if (chars[i++] == c) {
164 return true;
165 }
166 }
167 return false;
168}
169
170bool ProximityInfo::existsAdjacentProximityChars(const int index) const {
171 if (index < 0 || index >= mInputLength) return false;
172 const int currentChar = getPrimaryCharAt(index);
173 const int leftIndex = index - 1;
174 if (leftIndex >= 0 && existsCharInProximityAt(leftIndex, currentChar)) {
175 return true;
176 }
177 const int rightIndex = index + 1;
178 if (rightIndex < mInputLength && existsCharInProximityAt(rightIndex, currentChar)) {
179 return true;
180 }
181 return false;
182}
183
184// In the following function, c is the current character of the dictionary word
185// currently examined.
186// currentChars is an array containing the keys close to the character the
187// user actually typed at the same position. We want to see if c is in it: if so,
188// then the word contains at that position a character close to what the user
189// typed.
190// What the user typed is actually the first character of the array.
191// Notice : accented characters do not have a proximity list, so they are alone
192// in their list. The non-accented version of the character should be considered
193// "close", but not the other keys close to the non-accented version.
194ProximityInfo::ProximityType ProximityInfo::getMatchedProximityId(
satok985312e2011-08-05 21:21:01 +0900195 const int index, const unsigned short c, const bool checkProximityChars) const {
satokd24df432011-07-14 15:43:42 +0900196 const int *currentChars = getProximityCharsAt(index);
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900197 const int firstChar = currentChars[0];
satokd24df432011-07-14 15:43:42 +0900198 const unsigned short baseLowerC = Dictionary::toBaseLowerCase(c);
199
200 // The first char in the array is what user typed. If it matches right away,
201 // that means the user typed that same char for this pos.
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900202 if (firstChar == baseLowerC || firstChar == c) {
203 if (CALIBRATE_SCORE_BY_TOUCH_COORDINATES) {
204 const SweetSpotType result = calculateSweetSpotType(index, baseLowerC);
205 switch (result) {
206 case UNKNOWN:
207 return EQUIVALENT_CHAR_NORMAL;
208 case IN_SWEET_SPOT:
209 return EQUIVALENT_CHAR_STRONG;
210 case IN_NEUTRAL_AREA:
211 return EQUIVALENT_CHAR_NORMAL;
212 case OUT_OF_NEUTRAL_AREA:
213 return EQUIVALENT_CHAR_WEAK;
214 default:
215 assert(false);
216 }
217 } else {
218 return EQUIVALENT_CHAR_NORMAL;
219 }
220 }
satokd24df432011-07-14 15:43:42 +0900221
satok985312e2011-08-05 21:21:01 +0900222 if (!checkProximityChars) return UNRELATED_CHAR;
satokd24df432011-07-14 15:43:42 +0900223
224 // If the non-accented, lowercased version of that first character matches c,
225 // then we have a non-accented version of the accented character the user
226 // typed. Treat it as a close char.
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900227 if (Dictionary::toBaseLowerCase(firstChar) == baseLowerC)
satokd24df432011-07-14 15:43:42 +0900228 return NEAR_PROXIMITY_CHAR;
229
230 // Not an exact nor an accent-alike match: search the list of close keys
231 int j = 1;
232 while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS_SIZE) {
233 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
234 if (matched) return NEAR_PROXIMITY_CHAR;
235 ++j;
236 }
237
238 // Was not included, signal this as an unrelated character.
239 return UNRELATED_CHAR;
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