satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 1 | /* |
| 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 Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 17 | #include <assert.h> |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 18 | #include <stdio.h> |
satok | 552c3c2 | 2012-03-13 16:33:47 +0900 | [diff] [blame] | 19 | #include <string> |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 20 | |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 21 | #define LOG_TAG "LatinIME: proximity_info.cpp" |
| 22 | |
satok | 552c3c2 | 2012-03-13 16:33:47 +0900 | [diff] [blame] | 23 | #include "additional_proximity_chars.h" |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 24 | #include "dictionary.h" |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 25 | #include "proximity_info.h" |
| 26 | |
| 27 | namespace latinime { |
Ken Wakasa | ce9e52a | 2011-06-18 13:09:55 +0900 | [diff] [blame] | 28 | |
Yusuke Nojima | de2f842 | 2011-09-27 11:15:18 +0900 | [diff] [blame] | 29 | inline void copyOrFillZero(void *to, const void *from, size_t size) { |
| 30 | if (from) { |
| 31 | memcpy(to, from, size); |
| 32 | } else { |
| 33 | memset(to, 0, size); |
| 34 | } |
| 35 | } |
| 36 | |
satok | 552c3c2 | 2012-03-13 16:33:47 +0900 | [diff] [blame] | 37 | ProximityInfo::ProximityInfo(const std::string localeStr, const int maxProximityCharsSize, |
| 38 | const int keyboardWidth, const int keyboardHeight, const int gridWidth, |
| 39 | const int gridHeight, const int mostCommonKeyWidth, |
satok | 0cb2097 | 2012-03-13 22:07:56 +0900 | [diff] [blame] | 40 | const int32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates, |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 41 | const int32_t *keyYCoordinates, const int32_t *keyWidths, const int32_t *keyHeights, |
Yusuke Nojima | ad35835 | 2011-09-29 16:44:54 +0900 | [diff] [blame] | 42 | const int32_t *keyCharCodes, const float *sweetSpotCenterXs, const float *sweetSpotCenterYs, |
| 43 | const float *sweetSpotRadii) |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 44 | : MAX_PROXIMITY_CHARS_SIZE(maxProximityCharsSize), KEYBOARD_WIDTH(keyboardWidth), |
| 45 | KEYBOARD_HEIGHT(keyboardHeight), GRID_WIDTH(gridWidth), GRID_HEIGHT(gridHeight), |
satok | a70ee6e | 2012-03-07 15:12:22 +0900 | [diff] [blame] | 46 | MOST_COMMON_KEY_WIDTH_SQUARE(mostCommonKeyWidth * mostCommonKeyWidth), |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 47 | CELL_WIDTH((keyboardWidth + gridWidth - 1) / gridWidth), |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 48 | CELL_HEIGHT((keyboardHeight + gridHeight - 1) / gridHeight), |
Yusuke Nojima | 258bfe6 | 2011-09-28 12:59:43 +0900 | [diff] [blame] | 49 | KEY_COUNT(min(keyCount, MAX_KEY_COUNT_IN_A_KEYBOARD)), |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 50 | HAS_TOUCH_POSITION_CORRECTION_DATA(keyCount > 0 && keyXCoordinates && keyYCoordinates |
| 51 | && keyWidths && keyHeights && keyCharCodes && sweetSpotCenterXs |
| 52 | && sweetSpotCenterYs && sweetSpotRadii), |
satok | 5eec574 | 2012-03-13 18:26:23 +0900 | [diff] [blame] | 53 | mLocaleStr(localeStr), |
Tadashi G. Takaoka | 0e97148 | 2011-10-28 17:02:09 +0900 | [diff] [blame] | 54 | mInputXCoordinates(0), mInputYCoordinates(0), |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 55 | mTouchPositionCorrectionEnabled(false) { |
Jean Chalard | 8c8ca59 | 2011-11-10 12:07:30 +0900 | [diff] [blame] | 56 | const int proximityGridLength = GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE; |
satok | 0cb2097 | 2012-03-13 22:07:56 +0900 | [diff] [blame] | 57 | mProximityCharsArray = new int32_t[proximityGridLength]; |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 58 | if (DEBUG_PROXIMITY_INFO) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 59 | AKLOGI("Create proximity info array %d", proximityGridLength); |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 60 | } |
Jean Chalard | 8c8ca59 | 2011-11-10 12:07:30 +0900 | [diff] [blame] | 61 | memcpy(mProximityCharsArray, proximityCharsArray, |
| 62 | proximityGridLength * sizeof(mProximityCharsArray[0])); |
| 63 | const int normalizedSquaredDistancesLength = |
| 64 | MAX_PROXIMITY_CHARS_SIZE * MAX_WORD_LENGTH_INTERNAL; |
| 65 | mNormalizedSquaredDistances = new int[normalizedSquaredDistancesLength]; |
| 66 | for (int i = 0; i < normalizedSquaredDistancesLength; ++i) { |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 67 | mNormalizedSquaredDistances[i] = NOT_A_DISTANCE; |
| 68 | } |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 69 | |
Yusuke Nojima | de2f842 | 2011-09-27 11:15:18 +0900 | [diff] [blame] | 70 | copyOrFillZero(mKeyXCoordinates, keyXCoordinates, KEY_COUNT * sizeof(mKeyXCoordinates[0])); |
| 71 | copyOrFillZero(mKeyYCoordinates, keyYCoordinates, KEY_COUNT * sizeof(mKeyYCoordinates[0])); |
| 72 | copyOrFillZero(mKeyWidths, keyWidths, KEY_COUNT * sizeof(mKeyWidths[0])); |
| 73 | copyOrFillZero(mKeyHeights, keyHeights, KEY_COUNT * sizeof(mKeyHeights[0])); |
| 74 | copyOrFillZero(mKeyCharCodes, keyCharCodes, KEY_COUNT * sizeof(mKeyCharCodes[0])); |
Yusuke Nojima | ad35835 | 2011-09-29 16:44:54 +0900 | [diff] [blame] | 75 | copyOrFillZero(mSweetSpotCenterXs, sweetSpotCenterXs, |
| 76 | KEY_COUNT * sizeof(mSweetSpotCenterXs[0])); |
| 77 | copyOrFillZero(mSweetSpotCenterYs, sweetSpotCenterYs, |
| 78 | KEY_COUNT * sizeof(mSweetSpotCenterYs[0])); |
| 79 | copyOrFillZero(mSweetSpotRadii, sweetSpotRadii, KEY_COUNT * sizeof(mSweetSpotRadii[0])); |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 80 | |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 81 | initializeCodeToKeyIndex(); |
| 82 | } |
| 83 | |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 84 | // Build the reversed look up table from the char code to the index in mKeyXCoordinates, |
| 85 | // mKeyYCoordinates, mKeyWidths, mKeyHeights, mKeyCharCodes. |
| 86 | void ProximityInfo::initializeCodeToKeyIndex() { |
Yusuke Nojima | ad35835 | 2011-09-29 16:44:54 +0900 | [diff] [blame] | 87 | memset(mCodeToKeyIndex, -1, (MAX_CHAR_CODE + 1) * sizeof(mCodeToKeyIndex[0])); |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 88 | for (int i = 0; i < KEY_COUNT; ++i) { |
| 89 | const int code = mKeyCharCodes[i]; |
Yusuke Nojima | ad35835 | 2011-09-29 16:44:54 +0900 | [diff] [blame] | 90 | if (0 <= code && code <= MAX_CHAR_CODE) { |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 91 | mCodeToKeyIndex[code] = i; |
Yusuke Nojima | ad35835 | 2011-09-29 16:44:54 +0900 | [diff] [blame] | 92 | } |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 93 | } |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | ProximityInfo::~ProximityInfo() { |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 97 | delete[] mNormalizedSquaredDistances; |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 98 | delete[] mProximityCharsArray; |
| 99 | } |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 100 | |
| 101 | inline int ProximityInfo::getStartIndexFromCoordinates(const int x, const int y) const { |
satok | 3c4bb77 | 2011-03-04 22:50:19 -0800 | [diff] [blame] | 102 | return ((y / CELL_HEIGHT) * GRID_WIDTH + (x / CELL_WIDTH)) |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 103 | * MAX_PROXIMITY_CHARS_SIZE; |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 104 | } |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 105 | |
| 106 | bool ProximityInfo::hasSpaceProximity(const int x, const int y) const { |
satok | 744dab6 | 2011-12-15 22:29:05 +0900 | [diff] [blame] | 107 | if (x < 0 || y < 0) { |
| 108 | if (DEBUG_DICT) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 109 | AKLOGI("HasSpaceProximity: Illegal coordinates (%d, %d)", x, y); |
satok | 1a6da63 | 2011-12-16 23:15:06 +0900 | [diff] [blame] | 110 | assert(false); |
satok | 744dab6 | 2011-12-15 22:29:05 +0900 | [diff] [blame] | 111 | } |
| 112 | return false; |
| 113 | } |
| 114 | |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 115 | const int startIndex = getStartIndexFromCoordinates(x, y); |
| 116 | if (DEBUG_PROXIMITY_INFO) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 117 | AKLOGI("hasSpaceProximity: index %d, %d, %d", startIndex, x, y); |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 118 | } |
| 119 | for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) { |
| 120 | if (DEBUG_PROXIMITY_INFO) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 121 | AKLOGI("Index: %d", mProximityCharsArray[startIndex + i]); |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 122 | } |
| 123 | if (mProximityCharsArray[startIndex + i] == KEYCODE_SPACE) { |
| 124 | return true; |
| 125 | } |
| 126 | } |
| 127 | return false; |
| 128 | } |
Ken Wakasa | ce9e52a | 2011-06-18 13:09:55 +0900 | [diff] [blame] | 129 | |
satok | a70ee6e | 2012-03-07 15:12:22 +0900 | [diff] [blame] | 130 | bool ProximityInfo::isOnKey(const int keyId, const int x, const int y) { |
| 131 | const int left = mKeyXCoordinates[keyId]; |
| 132 | const int top = mKeyYCoordinates[keyId]; |
| 133 | const int right = left + mKeyWidths[keyId] + 1; |
| 134 | const int bottom = top + mKeyHeights[keyId]; |
| 135 | return left < right && top < bottom && x >= left && x < right && y >= top && y < bottom; |
| 136 | } |
| 137 | |
| 138 | int ProximityInfo::squaredDistanceToEdge(const int keyId, const int x, const int y) { |
| 139 | const int left = mKeyXCoordinates[keyId]; |
| 140 | const int top = mKeyYCoordinates[keyId]; |
| 141 | const int right = left + mKeyWidths[keyId] + 1; |
| 142 | const int bottom = top + mKeyHeights[keyId]; |
| 143 | const int edgeX = x < left ? left : (x > right ? right : x); |
| 144 | const int edgeY = y < top ? top : (y > bottom ? bottom : y); |
| 145 | const int dx = x - edgeX; |
| 146 | const int dy = y - edgeY; |
| 147 | return dx * dx + dy * dy; |
| 148 | } |
| 149 | |
| 150 | void ProximityInfo::calculateNearbyKeyCodes( |
satok | 0cb2097 | 2012-03-13 22:07:56 +0900 | [diff] [blame] | 151 | const int x, const int y, const int32_t primaryKey, int *inputCodes) { |
satok | a70ee6e | 2012-03-07 15:12:22 +0900 | [diff] [blame] | 152 | int insertPos = 0; |
| 153 | inputCodes[insertPos++] = primaryKey; |
| 154 | const int startIndex = getStartIndexFromCoordinates(x, y); |
| 155 | for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) { |
satok | 0cb2097 | 2012-03-13 22:07:56 +0900 | [diff] [blame] | 156 | const int32_t c = mProximityCharsArray[startIndex + i]; |
satok | a70ee6e | 2012-03-07 15:12:22 +0900 | [diff] [blame] | 157 | if (c < KEYCODE_SPACE || c == primaryKey) { |
| 158 | continue; |
| 159 | } |
satok | 0cb2097 | 2012-03-13 22:07:56 +0900 | [diff] [blame] | 160 | int keyIndex = getKeyIndex(c); |
| 161 | const bool onKey = isOnKey(keyIndex, x, y); |
| 162 | const int distance = squaredDistanceToEdge(keyIndex, x, y); |
| 163 | if (onKey || distance < MOST_COMMON_KEY_WIDTH_SQUARE) { |
| 164 | inputCodes[insertPos++] = c; |
| 165 | if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) { |
| 166 | if (DEBUG_DICT) { |
| 167 | assert(false); |
| 168 | } |
| 169 | return; |
satok | a70ee6e | 2012-03-07 15:12:22 +0900 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | } |
satok | 0cb2097 | 2012-03-13 22:07:56 +0900 | [diff] [blame] | 173 | inputCodes[insertPos++] = ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE; |
| 174 | if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) { |
| 175 | if (DEBUG_DICT) { |
| 176 | assert(false); |
satok | 5eec574 | 2012-03-13 18:26:23 +0900 | [diff] [blame] | 177 | } |
satok | 0cb2097 | 2012-03-13 22:07:56 +0900 | [diff] [blame] | 178 | return; |
| 179 | } |
| 180 | |
| 181 | const int additionalProximitySize = |
| 182 | AdditionalProximityChars::getAdditionalCharsSize(&mLocaleStr, primaryKey); |
| 183 | if (additionalProximitySize > 0) { |
| 184 | const int32_t* additionalProximityChars = |
| 185 | AdditionalProximityChars::getAdditionalChars(&mLocaleStr, primaryKey); |
satok | 5eec574 | 2012-03-13 18:26:23 +0900 | [diff] [blame] | 186 | for (int j = 0; j < additionalProximitySize; ++j) { |
satok | 0cb2097 | 2012-03-13 22:07:56 +0900 | [diff] [blame] | 187 | const int32_t ac = additionalProximityChars[j]; |
satok | 5eec574 | 2012-03-13 18:26:23 +0900 | [diff] [blame] | 188 | int k = 0; |
| 189 | for (; k < insertPos; ++k) { |
| 190 | if ((int)ac == inputCodes[k]) { |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | if (k < insertPos) { |
| 195 | continue; |
| 196 | } |
| 197 | inputCodes[insertPos++] = ac; |
satok | 0cb2097 | 2012-03-13 22:07:56 +0900 | [diff] [blame] | 198 | if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) { |
| 199 | if (DEBUG_DICT) { |
| 200 | assert(false); |
| 201 | } |
| 202 | return; |
| 203 | } |
satok | 5eec574 | 2012-03-13 18:26:23 +0900 | [diff] [blame] | 204 | } |
| 205 | } |
satok | 0cb2097 | 2012-03-13 22:07:56 +0900 | [diff] [blame] | 206 | // Add a delimiter for the proximity characters |
| 207 | inputCodes[insertPos] = 0; |
satok | a70ee6e | 2012-03-07 15:12:22 +0900 | [diff] [blame] | 208 | } |
| 209 | |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 210 | // TODO: Calculate nearby codes here. |
Yusuke Nojima | 258bfe6 | 2011-09-28 12:59:43 +0900 | [diff] [blame] | 211 | void ProximityInfo::setInputParams(const int* inputCodes, const int inputLength, |
| 212 | const int* xCoordinates, const int* yCoordinates) { |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 213 | mInputCodes = inputCodes; |
Yusuke Nojima | 258bfe6 | 2011-09-28 12:59:43 +0900 | [diff] [blame] | 214 | mInputXCoordinates = xCoordinates; |
| 215 | mInputYCoordinates = yCoordinates; |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 216 | mTouchPositionCorrectionEnabled = |
| 217 | HAS_TOUCH_POSITION_CORRECTION_DATA && xCoordinates && yCoordinates; |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 218 | mInputLength = inputLength; |
satok | 0cedd2b | 2011-08-12 01:05:27 +0900 | [diff] [blame] | 219 | for (int i = 0; i < inputLength; ++i) { |
| 220 | mPrimaryInputWord[i] = getPrimaryCharAt(i); |
| 221 | } |
| 222 | mPrimaryInputWord[inputLength] = 0; |
satok | 0cb2097 | 2012-03-13 22:07:56 +0900 | [diff] [blame] | 223 | if (DEBUG_PROXIMITY_CHARS) { |
| 224 | AKLOGI("--- setInputParams"); |
| 225 | } |
Yusuke Nojima | c25c7cc | 2011-10-03 16:44:29 +0900 | [diff] [blame] | 226 | for (int i = 0; i < mInputLength; ++i) { |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 227 | const int *proximityChars = getProximityCharsAt(i); |
satok | 0cb2097 | 2012-03-13 22:07:56 +0900 | [diff] [blame] | 228 | const int primaryKey = proximityChars[0]; |
| 229 | const int x = xCoordinates[i]; |
| 230 | const int y = yCoordinates[i]; |
| 231 | if (DEBUG_PROXIMITY_CHARS) { |
| 232 | int a = x + y + primaryKey; |
| 233 | a += 0; |
| 234 | AKLOGI("--- Primary = %c, x = %d, y = %d", primaryKey, x, y); |
| 235 | // Keep debug code just in case |
| 236 | //int proximities[50]; |
| 237 | //for (int m = 0; m < 50; ++m) { |
| 238 | //proximities[m] = 0; |
| 239 | //} |
| 240 | //calculateNearbyKeyCodes(x, y, primaryKey, proximities); |
| 241 | //for (int l = 0; l < 50 && proximities[l] > 0; ++l) { |
| 242 | //if (DEBUG_PROXIMITY_CHARS) { |
| 243 | //AKLOGI("--- native Proximity (%d) = %c", l, proximities[l]); |
| 244 | //} |
| 245 | //} |
| 246 | } |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 247 | for (int j = 0; j < MAX_PROXIMITY_CHARS_SIZE && proximityChars[j] > 0; ++j) { |
| 248 | const int currentChar = proximityChars[j]; |
| 249 | const int keyIndex = getKeyIndex(currentChar); |
| 250 | const float squaredDistance = calculateNormalizedSquaredDistance(keyIndex, i); |
| 251 | if (squaredDistance >= 0.0f) { |
| 252 | mNormalizedSquaredDistances[i * MAX_PROXIMITY_CHARS_SIZE + j] = |
| 253 | (int)(squaredDistance * NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR); |
| 254 | } else { |
| 255 | mNormalizedSquaredDistances[i * MAX_PROXIMITY_CHARS_SIZE + j] = (j == 0) |
| 256 | ? EQUIVALENT_CHAR_WITHOUT_DISTANCE_INFO |
| 257 | : PROXIMITY_CHAR_WITHOUT_DISTANCE_INFO; |
| 258 | } |
satok | 0cb2097 | 2012-03-13 22:07:56 +0900 | [diff] [blame] | 259 | if (DEBUG_PROXIMITY_CHARS) { |
| 260 | AKLOGI("--- Proximity (%d) = %c", j, currentChar); |
| 261 | } |
Yusuke Nojima | e4ba822 | 2011-10-05 14:55:07 +0900 | [diff] [blame] | 262 | } |
Yusuke Nojima | c25c7cc | 2011-10-03 16:44:29 +0900 | [diff] [blame] | 263 | } |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 264 | } |
| 265 | |
Yusuke Nojima | 1671715 | 2011-10-04 17:04:07 +0900 | [diff] [blame] | 266 | inline float square(const float x) { return x * x; } |
| 267 | |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 268 | float ProximityInfo::calculateNormalizedSquaredDistance( |
| 269 | const int keyIndex, const int inputIndex) const { |
Yusuke Nojima | e4ba822 | 2011-10-05 14:55:07 +0900 | [diff] [blame] | 270 | static const float NOT_A_DISTANCE_FLOAT = -1.0f; |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 271 | if (keyIndex == NOT_A_INDEX) { |
Yusuke Nojima | e4ba822 | 2011-10-05 14:55:07 +0900 | [diff] [blame] | 272 | return NOT_A_DISTANCE_FLOAT; |
Yusuke Nojima | c25c7cc | 2011-10-03 16:44:29 +0900 | [diff] [blame] | 273 | } |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 274 | if (!hasSweetSpotData(keyIndex)) { |
Yusuke Nojima | e4ba822 | 2011-10-05 14:55:07 +0900 | [diff] [blame] | 275 | return NOT_A_DISTANCE_FLOAT; |
Yusuke Nojima | 1671715 | 2011-10-04 17:04:07 +0900 | [diff] [blame] | 276 | } |
Jean Chalard | 0bfe359 | 2012-01-25 18:11:26 +0900 | [diff] [blame] | 277 | if (NOT_A_COORDINATE == mInputXCoordinates[inputIndex]) { |
| 278 | return NOT_A_DISTANCE_FLOAT; |
| 279 | } |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 280 | const float squaredDistance = calculateSquaredDistanceFromSweetSpotCenter(keyIndex, inputIndex); |
| 281 | const float squaredRadius = square(mSweetSpotRadii[keyIndex]); |
Yusuke Nojima | e4ba822 | 2011-10-05 14:55:07 +0900 | [diff] [blame] | 282 | return squaredDistance / squaredRadius; |
Yusuke Nojima | 1671715 | 2011-10-04 17:04:07 +0900 | [diff] [blame] | 283 | } |
| 284 | |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 285 | int ProximityInfo::getKeyIndex(const int c) const { |
| 286 | if (KEY_COUNT == 0 || !mInputXCoordinates || !mInputYCoordinates) { |
| 287 | // We do not have the coordinate data |
| 288 | return NOT_A_INDEX; |
| 289 | } |
Tadashi G. Takaoka | 6e3cb27 | 2011-11-11 14:26:13 +0900 | [diff] [blame] | 290 | const unsigned short baseLowerC = toBaseLowerCase(c); |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 291 | if (baseLowerC > MAX_CHAR_CODE) { |
| 292 | return NOT_A_INDEX; |
| 293 | } |
| 294 | return mCodeToKeyIndex[baseLowerC]; |
| 295 | } |
| 296 | |
Yusuke Nojima | c25c7cc | 2011-10-03 16:44:29 +0900 | [diff] [blame] | 297 | float ProximityInfo::calculateSquaredDistanceFromSweetSpotCenter( |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 298 | const int keyIndex, const int inputIndex) const { |
Yusuke Nojima | c25c7cc | 2011-10-03 16:44:29 +0900 | [diff] [blame] | 299 | const float sweetSpotCenterX = mSweetSpotCenterXs[keyIndex]; |
| 300 | const float sweetSpotCenterY = mSweetSpotCenterYs[keyIndex]; |
| 301 | const float inputX = (float)mInputXCoordinates[inputIndex]; |
| 302 | const float inputY = (float)mInputYCoordinates[inputIndex]; |
| 303 | return square(inputX - sweetSpotCenterX) + square(inputY - sweetSpotCenterY); |
| 304 | } |
| 305 | |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 306 | inline const int* ProximityInfo::getProximityCharsAt(const int index) const { |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 307 | return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE); |
| 308 | } |
| 309 | |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 310 | unsigned short ProximityInfo::getPrimaryCharAt(const int index) const { |
| 311 | return getProximityCharsAt(index)[0]; |
| 312 | } |
| 313 | |
satok | 2df3060 | 2011-07-15 13:49:00 +0900 | [diff] [blame] | 314 | inline bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const { |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 315 | const int *chars = getProximityCharsAt(index); |
| 316 | int i = 0; |
| 317 | while (chars[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE) { |
| 318 | if (chars[i++] == c) { |
| 319 | return true; |
| 320 | } |
| 321 | } |
| 322 | return false; |
| 323 | } |
| 324 | |
| 325 | bool ProximityInfo::existsAdjacentProximityChars(const int index) const { |
| 326 | if (index < 0 || index >= mInputLength) return false; |
| 327 | const int currentChar = getPrimaryCharAt(index); |
| 328 | const int leftIndex = index - 1; |
| 329 | if (leftIndex >= 0 && existsCharInProximityAt(leftIndex, currentChar)) { |
| 330 | return true; |
| 331 | } |
| 332 | const int rightIndex = index + 1; |
| 333 | if (rightIndex < mInputLength && existsCharInProximityAt(rightIndex, currentChar)) { |
| 334 | return true; |
| 335 | } |
| 336 | return false; |
| 337 | } |
| 338 | |
| 339 | // In the following function, c is the current character of the dictionary word |
| 340 | // currently examined. |
| 341 | // currentChars is an array containing the keys close to the character the |
| 342 | // user actually typed at the same position. We want to see if c is in it: if so, |
| 343 | // then the word contains at that position a character close to what the user |
| 344 | // typed. |
| 345 | // What the user typed is actually the first character of the array. |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 346 | // proximityIndex is a pointer to the variable where getMatchedProximityId returns |
| 347 | // the index of c in the proximity chars of the input index. |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 348 | // Notice : accented characters do not have a proximity list, so they are alone |
| 349 | // in their list. The non-accented version of the character should be considered |
| 350 | // "close", but not the other keys close to the non-accented version. |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 351 | ProximityInfo::ProximityType ProximityInfo::getMatchedProximityId(const int index, |
| 352 | const unsigned short c, const bool checkProximityChars, int *proximityIndex) const { |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 353 | const int *currentChars = getProximityCharsAt(index); |
Yusuke Nojima | 258bfe6 | 2011-09-28 12:59:43 +0900 | [diff] [blame] | 354 | const int firstChar = currentChars[0]; |
Tadashi G. Takaoka | 6e3cb27 | 2011-11-11 14:26:13 +0900 | [diff] [blame] | 355 | const unsigned short baseLowerC = toBaseLowerCase(c); |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 356 | |
| 357 | // The first char in the array is what user typed. If it matches right away, |
| 358 | // that means the user typed that same char for this pos. |
Yusuke Nojima | 258bfe6 | 2011-09-28 12:59:43 +0900 | [diff] [blame] | 359 | if (firstChar == baseLowerC || firstChar == c) { |
Yusuke Nojima | e4ba822 | 2011-10-05 14:55:07 +0900 | [diff] [blame] | 360 | return EQUIVALENT_CHAR; |
Yusuke Nojima | 258bfe6 | 2011-09-28 12:59:43 +0900 | [diff] [blame] | 361 | } |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 362 | |
satok | 985312e | 2011-08-05 21:21:01 +0900 | [diff] [blame] | 363 | if (!checkProximityChars) return UNRELATED_CHAR; |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 364 | |
| 365 | // If the non-accented, lowercased version of that first character matches c, |
| 366 | // then we have a non-accented version of the accented character the user |
| 367 | // typed. Treat it as a close char. |
Tadashi G. Takaoka | 6e3cb27 | 2011-11-11 14:26:13 +0900 | [diff] [blame] | 368 | if (toBaseLowerCase(firstChar) == baseLowerC) |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 369 | return NEAR_PROXIMITY_CHAR; |
| 370 | |
| 371 | // Not an exact nor an accent-alike match: search the list of close keys |
| 372 | int j = 1; |
satok | e05b3f4 | 2012-01-31 17:15:43 +0900 | [diff] [blame] | 373 | while (j < MAX_PROXIMITY_CHARS_SIZE |
| 374 | && currentChars[j] > ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) { |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 375 | const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c); |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 376 | if (matched) { |
| 377 | if (proximityIndex) { |
| 378 | *proximityIndex = j; |
| 379 | } |
| 380 | return NEAR_PROXIMITY_CHAR; |
| 381 | } |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 382 | ++j; |
| 383 | } |
satok | e05b3f4 | 2012-01-31 17:15:43 +0900 | [diff] [blame] | 384 | if (j < MAX_PROXIMITY_CHARS_SIZE |
| 385 | && currentChars[j] == ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) { |
| 386 | ++j; |
| 387 | while (j < MAX_PROXIMITY_CHARS_SIZE |
| 388 | && currentChars[j] > ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) { |
| 389 | const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c); |
| 390 | if (matched) { |
| 391 | if (proximityIndex) { |
| 392 | *proximityIndex = j; |
| 393 | } |
| 394 | return ADDITIONAL_PROXIMITY_CHAR; |
| 395 | } |
| 396 | ++j; |
| 397 | } |
| 398 | } |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 399 | |
| 400 | // Was not included, signal this as an unrelated character. |
| 401 | return UNRELATED_CHAR; |
| 402 | } |
| 403 | |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 404 | bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const { |
| 405 | if (length != mInputLength) { |
| 406 | return false; |
| 407 | } |
| 408 | const int *inputCodes = mInputCodes; |
| 409 | while (length--) { |
| 410 | if ((unsigned int) *inputCodes != (unsigned int) *word) { |
| 411 | return false; |
| 412 | } |
| 413 | inputCodes += MAX_PROXIMITY_CHARS_SIZE; |
| 414 | word++; |
| 415 | } |
| 416 | return true; |
| 417 | } |
| 418 | |
Yusuke Nojima | e4ba822 | 2011-10-05 14:55:07 +0900 | [diff] [blame] | 419 | const int ProximityInfo::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR_LOG_2; |
| 420 | const int ProximityInfo::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR; |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 421 | const int ProximityInfo::MAX_KEY_COUNT_IN_A_KEYBOARD; |
Yusuke Nojima | ad35835 | 2011-09-29 16:44:54 +0900 | [diff] [blame] | 422 | const int ProximityInfo::MAX_CHAR_CODE; |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 423 | |
Ken Wakasa | ce9e52a | 2011-06-18 13:09:55 +0900 | [diff] [blame] | 424 | } // namespace latinime |