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> |
| 19 | #include <string.h> |
| 20 | |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 21 | #define LOG_TAG "LatinIME: proximity_info.cpp" |
| 22 | |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 23 | #include "dictionary.h" |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 24 | #include "proximity_info.h" |
| 25 | |
| 26 | namespace latinime { |
Ken Wakasa | ce9e52a | 2011-06-18 13:09:55 +0900 | [diff] [blame] | 27 | |
Yusuke Nojima | de2f842 | 2011-09-27 11:15:18 +0900 | [diff] [blame] | 28 | inline 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 | |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 36 | ProximityInfo::ProximityInfo(const int maxProximityCharsSize, const int keyboardWidth, |
| 37 | const int keyboardHeight, const int gridWidth, const int gridHeight, |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 38 | 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 Nojima | ad35835 | 2011-09-29 16:44:54 +0900 | [diff] [blame] | 40 | const int32_t *keyCharCodes, const float *sweetSpotCenterXs, const float *sweetSpotCenterYs, |
| 41 | const float *sweetSpotRadii) |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 42 | : 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 Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 45 | CELL_HEIGHT((keyboardHeight + gridHeight - 1) / gridHeight), |
Yusuke Nojima | 258bfe6 | 2011-09-28 12:59:43 +0900 | [diff] [blame] | 46 | KEY_COUNT(min(keyCount, MAX_KEY_COUNT_IN_A_KEYBOARD)), |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 47 | HAS_TOUCH_POSITION_CORRECTION_DATA(keyCount > 0 && keyXCoordinates && keyYCoordinates |
| 48 | && keyWidths && keyHeights && keyCharCodes && sweetSpotCenterXs |
| 49 | && sweetSpotCenterYs && sweetSpotRadii), |
Tadashi G. Takaoka | 0e97148 | 2011-10-28 17:02:09 +0900 | [diff] [blame] | 50 | mInputXCoordinates(0), mInputYCoordinates(0), |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 51 | mTouchPositionCorrectionEnabled(false) { |
Jean Chalard | 8c8ca59 | 2011-11-10 12:07:30 +0900 | [diff] [blame] | 52 | const int proximityGridLength = GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE; |
| 53 | mProximityCharsArray = new uint32_t[proximityGridLength]; |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 54 | if (DEBUG_PROXIMITY_INFO) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 55 | AKLOGI("Create proximity info array %d", proximityGridLength); |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 56 | } |
Jean Chalard | 8c8ca59 | 2011-11-10 12:07:30 +0900 | [diff] [blame] | 57 | memcpy(mProximityCharsArray, proximityCharsArray, |
| 58 | proximityGridLength * sizeof(mProximityCharsArray[0])); |
| 59 | const int normalizedSquaredDistancesLength = |
| 60 | MAX_PROXIMITY_CHARS_SIZE * MAX_WORD_LENGTH_INTERNAL; |
| 61 | mNormalizedSquaredDistances = new int[normalizedSquaredDistancesLength]; |
| 62 | for (int i = 0; i < normalizedSquaredDistancesLength; ++i) { |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 63 | mNormalizedSquaredDistances[i] = NOT_A_DISTANCE; |
| 64 | } |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 65 | |
Yusuke Nojima | de2f842 | 2011-09-27 11:15:18 +0900 | [diff] [blame] | 66 | copyOrFillZero(mKeyXCoordinates, keyXCoordinates, KEY_COUNT * sizeof(mKeyXCoordinates[0])); |
| 67 | copyOrFillZero(mKeyYCoordinates, keyYCoordinates, KEY_COUNT * sizeof(mKeyYCoordinates[0])); |
| 68 | copyOrFillZero(mKeyWidths, keyWidths, KEY_COUNT * sizeof(mKeyWidths[0])); |
| 69 | copyOrFillZero(mKeyHeights, keyHeights, KEY_COUNT * sizeof(mKeyHeights[0])); |
| 70 | copyOrFillZero(mKeyCharCodes, keyCharCodes, KEY_COUNT * sizeof(mKeyCharCodes[0])); |
Yusuke Nojima | ad35835 | 2011-09-29 16:44:54 +0900 | [diff] [blame] | 71 | copyOrFillZero(mSweetSpotCenterXs, sweetSpotCenterXs, |
| 72 | KEY_COUNT * sizeof(mSweetSpotCenterXs[0])); |
| 73 | copyOrFillZero(mSweetSpotCenterYs, sweetSpotCenterYs, |
| 74 | KEY_COUNT * sizeof(mSweetSpotCenterYs[0])); |
| 75 | copyOrFillZero(mSweetSpotRadii, sweetSpotRadii, KEY_COUNT * sizeof(mSweetSpotRadii[0])); |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 76 | |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 77 | initializeCodeToKeyIndex(); |
| 78 | } |
| 79 | |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 80 | // Build the reversed look up table from the char code to the index in mKeyXCoordinates, |
| 81 | // mKeyYCoordinates, mKeyWidths, mKeyHeights, mKeyCharCodes. |
| 82 | void ProximityInfo::initializeCodeToKeyIndex() { |
Yusuke Nojima | ad35835 | 2011-09-29 16:44:54 +0900 | [diff] [blame] | 83 | memset(mCodeToKeyIndex, -1, (MAX_CHAR_CODE + 1) * sizeof(mCodeToKeyIndex[0])); |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 84 | for (int i = 0; i < KEY_COUNT; ++i) { |
| 85 | const int code = mKeyCharCodes[i]; |
Yusuke Nojima | ad35835 | 2011-09-29 16:44:54 +0900 | [diff] [blame] | 86 | if (0 <= code && code <= MAX_CHAR_CODE) { |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 87 | mCodeToKeyIndex[code] = i; |
Yusuke Nojima | ad35835 | 2011-09-29 16:44:54 +0900 | [diff] [blame] | 88 | } |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 89 | } |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | ProximityInfo::~ProximityInfo() { |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 93 | delete[] mNormalizedSquaredDistances; |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 94 | delete[] mProximityCharsArray; |
| 95 | } |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 96 | |
| 97 | inline int ProximityInfo::getStartIndexFromCoordinates(const int x, const int y) const { |
satok | 3c4bb77 | 2011-03-04 22:50:19 -0800 | [diff] [blame] | 98 | return ((y / CELL_HEIGHT) * GRID_WIDTH + (x / CELL_WIDTH)) |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 99 | * MAX_PROXIMITY_CHARS_SIZE; |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 100 | } |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 101 | |
| 102 | bool ProximityInfo::hasSpaceProximity(const int x, const int y) const { |
satok | 744dab6 | 2011-12-15 22:29:05 +0900 | [diff] [blame] | 103 | if (x < 0 || y < 0) { |
| 104 | if (DEBUG_DICT) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 105 | AKLOGI("HasSpaceProximity: Illegal coordinates (%d, %d)", x, y); |
satok | 1a6da63 | 2011-12-16 23:15:06 +0900 | [diff] [blame] | 106 | assert(false); |
satok | 744dab6 | 2011-12-15 22:29:05 +0900 | [diff] [blame] | 107 | } |
| 108 | return false; |
| 109 | } |
| 110 | |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 111 | const int startIndex = getStartIndexFromCoordinates(x, y); |
| 112 | if (DEBUG_PROXIMITY_INFO) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 113 | AKLOGI("hasSpaceProximity: index %d, %d, %d", startIndex, x, y); |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 114 | } |
| 115 | for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) { |
| 116 | if (DEBUG_PROXIMITY_INFO) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 117 | AKLOGI("Index: %d", mProximityCharsArray[startIndex + i]); |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 118 | } |
| 119 | if (mProximityCharsArray[startIndex + i] == KEYCODE_SPACE) { |
| 120 | return true; |
| 121 | } |
| 122 | } |
| 123 | return false; |
| 124 | } |
Ken Wakasa | ce9e52a | 2011-06-18 13:09:55 +0900 | [diff] [blame] | 125 | |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 126 | // TODO: Calculate nearby codes here. |
Yusuke Nojima | 258bfe6 | 2011-09-28 12:59:43 +0900 | [diff] [blame] | 127 | void ProximityInfo::setInputParams(const int* inputCodes, const int inputLength, |
| 128 | const int* xCoordinates, const int* yCoordinates) { |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 129 | mInputCodes = inputCodes; |
Yusuke Nojima | 258bfe6 | 2011-09-28 12:59:43 +0900 | [diff] [blame] | 130 | mInputXCoordinates = xCoordinates; |
| 131 | mInputYCoordinates = yCoordinates; |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 132 | mTouchPositionCorrectionEnabled = |
| 133 | HAS_TOUCH_POSITION_CORRECTION_DATA && xCoordinates && yCoordinates; |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 134 | mInputLength = inputLength; |
satok | 0cedd2b | 2011-08-12 01:05:27 +0900 | [diff] [blame] | 135 | for (int i = 0; i < inputLength; ++i) { |
| 136 | mPrimaryInputWord[i] = getPrimaryCharAt(i); |
| 137 | } |
| 138 | mPrimaryInputWord[inputLength] = 0; |
Yusuke Nojima | c25c7cc | 2011-10-03 16:44:29 +0900 | [diff] [blame] | 139 | for (int i = 0; i < mInputLength; ++i) { |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 140 | const int *proximityChars = getProximityCharsAt(i); |
| 141 | for (int j = 0; j < MAX_PROXIMITY_CHARS_SIZE && proximityChars[j] > 0; ++j) { |
| 142 | const int currentChar = proximityChars[j]; |
| 143 | const int keyIndex = getKeyIndex(currentChar); |
| 144 | const float squaredDistance = calculateNormalizedSquaredDistance(keyIndex, i); |
| 145 | if (squaredDistance >= 0.0f) { |
| 146 | mNormalizedSquaredDistances[i * MAX_PROXIMITY_CHARS_SIZE + j] = |
| 147 | (int)(squaredDistance * NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR); |
| 148 | } else { |
| 149 | mNormalizedSquaredDistances[i * MAX_PROXIMITY_CHARS_SIZE + j] = (j == 0) |
| 150 | ? EQUIVALENT_CHAR_WITHOUT_DISTANCE_INFO |
| 151 | : PROXIMITY_CHAR_WITHOUT_DISTANCE_INFO; |
| 152 | } |
Yusuke Nojima | e4ba822 | 2011-10-05 14:55:07 +0900 | [diff] [blame] | 153 | } |
Yusuke Nojima | c25c7cc | 2011-10-03 16:44:29 +0900 | [diff] [blame] | 154 | } |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 155 | } |
| 156 | |
Yusuke Nojima | 1671715 | 2011-10-04 17:04:07 +0900 | [diff] [blame] | 157 | inline float square(const float x) { return x * x; } |
| 158 | |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 159 | float ProximityInfo::calculateNormalizedSquaredDistance( |
| 160 | const int keyIndex, const int inputIndex) const { |
Yusuke Nojima | e4ba822 | 2011-10-05 14:55:07 +0900 | [diff] [blame] | 161 | static const float NOT_A_DISTANCE_FLOAT = -1.0f; |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 162 | if (keyIndex == NOT_A_INDEX) { |
Yusuke Nojima | e4ba822 | 2011-10-05 14:55:07 +0900 | [diff] [blame] | 163 | return NOT_A_DISTANCE_FLOAT; |
Yusuke Nojima | c25c7cc | 2011-10-03 16:44:29 +0900 | [diff] [blame] | 164 | } |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 165 | if (!hasSweetSpotData(keyIndex)) { |
Yusuke Nojima | e4ba822 | 2011-10-05 14:55:07 +0900 | [diff] [blame] | 166 | return NOT_A_DISTANCE_FLOAT; |
Yusuke Nojima | 1671715 | 2011-10-04 17:04:07 +0900 | [diff] [blame] | 167 | } |
Jean Chalard | 0bfe359 | 2012-01-25 18:11:26 +0900 | [diff] [blame^] | 168 | if (NOT_A_COORDINATE == mInputXCoordinates[inputIndex]) { |
| 169 | return NOT_A_DISTANCE_FLOAT; |
| 170 | } |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 171 | const float squaredDistance = calculateSquaredDistanceFromSweetSpotCenter(keyIndex, inputIndex); |
| 172 | const float squaredRadius = square(mSweetSpotRadii[keyIndex]); |
Yusuke Nojima | e4ba822 | 2011-10-05 14:55:07 +0900 | [diff] [blame] | 173 | return squaredDistance / squaredRadius; |
Yusuke Nojima | 1671715 | 2011-10-04 17:04:07 +0900 | [diff] [blame] | 174 | } |
| 175 | |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 176 | int ProximityInfo::getKeyIndex(const int c) const { |
| 177 | if (KEY_COUNT == 0 || !mInputXCoordinates || !mInputYCoordinates) { |
| 178 | // We do not have the coordinate data |
| 179 | return NOT_A_INDEX; |
| 180 | } |
Tadashi G. Takaoka | 6e3cb27 | 2011-11-11 14:26:13 +0900 | [diff] [blame] | 181 | const unsigned short baseLowerC = toBaseLowerCase(c); |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 182 | if (baseLowerC > MAX_CHAR_CODE) { |
| 183 | return NOT_A_INDEX; |
| 184 | } |
| 185 | return mCodeToKeyIndex[baseLowerC]; |
| 186 | } |
| 187 | |
Yusuke Nojima | c25c7cc | 2011-10-03 16:44:29 +0900 | [diff] [blame] | 188 | float ProximityInfo::calculateSquaredDistanceFromSweetSpotCenter( |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 189 | const int keyIndex, const int inputIndex) const { |
Yusuke Nojima | c25c7cc | 2011-10-03 16:44:29 +0900 | [diff] [blame] | 190 | const float sweetSpotCenterX = mSweetSpotCenterXs[keyIndex]; |
| 191 | const float sweetSpotCenterY = mSweetSpotCenterYs[keyIndex]; |
| 192 | const float inputX = (float)mInputXCoordinates[inputIndex]; |
| 193 | const float inputY = (float)mInputYCoordinates[inputIndex]; |
| 194 | return square(inputX - sweetSpotCenterX) + square(inputY - sweetSpotCenterY); |
| 195 | } |
| 196 | |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 197 | inline const int* ProximityInfo::getProximityCharsAt(const int index) const { |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 198 | return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE); |
| 199 | } |
| 200 | |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 201 | unsigned short ProximityInfo::getPrimaryCharAt(const int index) const { |
| 202 | return getProximityCharsAt(index)[0]; |
| 203 | } |
| 204 | |
satok | 2df3060 | 2011-07-15 13:49:00 +0900 | [diff] [blame] | 205 | inline bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const { |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 206 | const int *chars = getProximityCharsAt(index); |
| 207 | int i = 0; |
| 208 | while (chars[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE) { |
| 209 | if (chars[i++] == c) { |
| 210 | return true; |
| 211 | } |
| 212 | } |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | bool ProximityInfo::existsAdjacentProximityChars(const int index) const { |
| 217 | if (index < 0 || index >= mInputLength) return false; |
| 218 | const int currentChar = getPrimaryCharAt(index); |
| 219 | const int leftIndex = index - 1; |
| 220 | if (leftIndex >= 0 && existsCharInProximityAt(leftIndex, currentChar)) { |
| 221 | return true; |
| 222 | } |
| 223 | const int rightIndex = index + 1; |
| 224 | if (rightIndex < mInputLength && existsCharInProximityAt(rightIndex, currentChar)) { |
| 225 | return true; |
| 226 | } |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | // In the following function, c is the current character of the dictionary word |
| 231 | // currently examined. |
| 232 | // currentChars is an array containing the keys close to the character the |
| 233 | // user actually typed at the same position. We want to see if c is in it: if so, |
| 234 | // then the word contains at that position a character close to what the user |
| 235 | // typed. |
| 236 | // What the user typed is actually the first character of the array. |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 237 | // proximityIndex is a pointer to the variable where getMatchedProximityId returns |
| 238 | // the index of c in the proximity chars of the input index. |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 239 | // Notice : accented characters do not have a proximity list, so they are alone |
| 240 | // in their list. The non-accented version of the character should be considered |
| 241 | // "close", but not the other keys close to the non-accented version. |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 242 | ProximityInfo::ProximityType ProximityInfo::getMatchedProximityId(const int index, |
| 243 | const unsigned short c, const bool checkProximityChars, int *proximityIndex) const { |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 244 | const int *currentChars = getProximityCharsAt(index); |
Yusuke Nojima | 258bfe6 | 2011-09-28 12:59:43 +0900 | [diff] [blame] | 245 | const int firstChar = currentChars[0]; |
Tadashi G. Takaoka | 6e3cb27 | 2011-11-11 14:26:13 +0900 | [diff] [blame] | 246 | const unsigned short baseLowerC = toBaseLowerCase(c); |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 247 | |
| 248 | // The first char in the array is what user typed. If it matches right away, |
| 249 | // that means the user typed that same char for this pos. |
Yusuke Nojima | 258bfe6 | 2011-09-28 12:59:43 +0900 | [diff] [blame] | 250 | if (firstChar == baseLowerC || firstChar == c) { |
Yusuke Nojima | e4ba822 | 2011-10-05 14:55:07 +0900 | [diff] [blame] | 251 | return EQUIVALENT_CHAR; |
Yusuke Nojima | 258bfe6 | 2011-09-28 12:59:43 +0900 | [diff] [blame] | 252 | } |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 253 | |
satok | 985312e | 2011-08-05 21:21:01 +0900 | [diff] [blame] | 254 | if (!checkProximityChars) return UNRELATED_CHAR; |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 255 | |
| 256 | // If the non-accented, lowercased version of that first character matches c, |
| 257 | // then we have a non-accented version of the accented character the user |
| 258 | // typed. Treat it as a close char. |
Tadashi G. Takaoka | 6e3cb27 | 2011-11-11 14:26:13 +0900 | [diff] [blame] | 259 | if (toBaseLowerCase(firstChar) == baseLowerC) |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 260 | return NEAR_PROXIMITY_CHAR; |
| 261 | |
| 262 | // Not an exact nor an accent-alike match: search the list of close keys |
| 263 | int j = 1; |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 264 | while (j < MAX_PROXIMITY_CHARS_SIZE && currentChars[j] > 0) { |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 265 | const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c); |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 266 | if (matched) { |
| 267 | if (proximityIndex) { |
| 268 | *proximityIndex = j; |
| 269 | } |
| 270 | return NEAR_PROXIMITY_CHAR; |
| 271 | } |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 272 | ++j; |
| 273 | } |
| 274 | |
| 275 | // Was not included, signal this as an unrelated character. |
| 276 | return UNRELATED_CHAR; |
| 277 | } |
| 278 | |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 279 | bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const { |
| 280 | if (length != mInputLength) { |
| 281 | return false; |
| 282 | } |
| 283 | const int *inputCodes = mInputCodes; |
| 284 | while (length--) { |
| 285 | if ((unsigned int) *inputCodes != (unsigned int) *word) { |
| 286 | return false; |
| 287 | } |
| 288 | inputCodes += MAX_PROXIMITY_CHARS_SIZE; |
| 289 | word++; |
| 290 | } |
| 291 | return true; |
| 292 | } |
| 293 | |
Yusuke Nojima | e4ba822 | 2011-10-05 14:55:07 +0900 | [diff] [blame] | 294 | const int ProximityInfo::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR_LOG_2; |
| 295 | const int ProximityInfo::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR; |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 296 | const int ProximityInfo::MAX_KEY_COUNT_IN_A_KEYBOARD; |
Yusuke Nojima | ad35835 | 2011-09-29 16:44:54 +0900 | [diff] [blame] | 297 | const int ProximityInfo::MAX_CHAR_CODE; |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 298 | |
Ken Wakasa | ce9e52a | 2011-06-18 13:09:55 +0900 | [diff] [blame] | 299 | } // namespace latinime |