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