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 | ad35835 | 2011-09-29 16:44:54 +0900 | [diff] [blame^] | 46 | KEY_COUNT(min(keyCount, MAX_KEY_COUNT_IN_A_KEYBOARD)) { |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 47 | const int len = GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE; |
| 48 | mProximityCharsArray = new uint32_t[len]; |
| 49 | if (DEBUG_PROXIMITY_INFO) { |
| 50 | LOGI("Create proximity info array %d", len); |
| 51 | } |
| 52 | memcpy(mProximityCharsArray, proximityCharsArray, len * sizeof(mProximityCharsArray[0])); |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 53 | |
Yusuke Nojima | de2f842 | 2011-09-27 11:15:18 +0900 | [diff] [blame] | 54 | copyOrFillZero(mKeyXCoordinates, keyXCoordinates, KEY_COUNT * sizeof(mKeyXCoordinates[0])); |
| 55 | copyOrFillZero(mKeyYCoordinates, keyYCoordinates, KEY_COUNT * sizeof(mKeyYCoordinates[0])); |
| 56 | copyOrFillZero(mKeyWidths, keyWidths, KEY_COUNT * sizeof(mKeyWidths[0])); |
| 57 | copyOrFillZero(mKeyHeights, keyHeights, KEY_COUNT * sizeof(mKeyHeights[0])); |
| 58 | copyOrFillZero(mKeyCharCodes, keyCharCodes, KEY_COUNT * sizeof(mKeyCharCodes[0])); |
Yusuke Nojima | ad35835 | 2011-09-29 16:44:54 +0900 | [diff] [blame^] | 59 | copyOrFillZero(mSweetSpotCenterXs, sweetSpotCenterXs, |
| 60 | KEY_COUNT * sizeof(mSweetSpotCenterXs[0])); |
| 61 | copyOrFillZero(mSweetSpotCenterYs, sweetSpotCenterYs, |
| 62 | KEY_COUNT * sizeof(mSweetSpotCenterYs[0])); |
| 63 | copyOrFillZero(mSweetSpotRadii, sweetSpotRadii, KEY_COUNT * sizeof(mSweetSpotRadii[0])); |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 64 | |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 65 | initializeCodeToKeyIndex(); |
| 66 | } |
| 67 | |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 68 | // Build the reversed look up table from the char code to the index in mKeyXCoordinates, |
| 69 | // mKeyYCoordinates, mKeyWidths, mKeyHeights, mKeyCharCodes. |
| 70 | void ProximityInfo::initializeCodeToKeyIndex() { |
Yusuke Nojima | ad35835 | 2011-09-29 16:44:54 +0900 | [diff] [blame^] | 71 | memset(mCodeToKeyIndex, -1, (MAX_CHAR_CODE + 1) * sizeof(mCodeToKeyIndex[0])); |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 72 | for (int i = 0; i < KEY_COUNT; ++i) { |
| 73 | const int code = mKeyCharCodes[i]; |
Yusuke Nojima | ad35835 | 2011-09-29 16:44:54 +0900 | [diff] [blame^] | 74 | if (0 <= code && code <= MAX_CHAR_CODE) { |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 75 | mCodeToKeyIndex[code] = i; |
Yusuke Nojima | ad35835 | 2011-09-29 16:44:54 +0900 | [diff] [blame^] | 76 | } |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 77 | } |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | ProximityInfo::~ProximityInfo() { |
| 81 | delete[] mProximityCharsArray; |
| 82 | } |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 83 | |
| 84 | inline int ProximityInfo::getStartIndexFromCoordinates(const int x, const int y) const { |
satok | 3c4bb77 | 2011-03-04 22:50:19 -0800 | [diff] [blame] | 85 | return ((y / CELL_HEIGHT) * GRID_WIDTH + (x / CELL_WIDTH)) |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 86 | * MAX_PROXIMITY_CHARS_SIZE; |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 87 | } |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 88 | |
| 89 | bool ProximityInfo::hasSpaceProximity(const int x, const int y) const { |
| 90 | const int startIndex = getStartIndexFromCoordinates(x, y); |
| 91 | if (DEBUG_PROXIMITY_INFO) { |
| 92 | LOGI("hasSpaceProximity: index %d", startIndex); |
| 93 | } |
| 94 | for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) { |
| 95 | if (DEBUG_PROXIMITY_INFO) { |
| 96 | LOGI("Index: %d", mProximityCharsArray[startIndex + i]); |
| 97 | } |
| 98 | if (mProximityCharsArray[startIndex + i] == KEYCODE_SPACE) { |
| 99 | return true; |
| 100 | } |
| 101 | } |
| 102 | return false; |
| 103 | } |
Ken Wakasa | ce9e52a | 2011-06-18 13:09:55 +0900 | [diff] [blame] | 104 | |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 105 | // TODO: Calculate nearby codes here. |
| 106 | void ProximityInfo::setInputParams(const int* inputCodes, const int inputLength) { |
| 107 | mInputCodes = inputCodes; |
| 108 | mInputLength = inputLength; |
satok | 0cedd2b | 2011-08-12 01:05:27 +0900 | [diff] [blame] | 109 | for (int i = 0; i < inputLength; ++i) { |
| 110 | mPrimaryInputWord[i] = getPrimaryCharAt(i); |
| 111 | } |
| 112 | mPrimaryInputWord[inputLength] = 0; |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 113 | } |
| 114 | |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 115 | inline const int* ProximityInfo::getProximityCharsAt(const int index) const { |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 116 | return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE); |
| 117 | } |
| 118 | |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 119 | unsigned short ProximityInfo::getPrimaryCharAt(const int index) const { |
| 120 | return getProximityCharsAt(index)[0]; |
| 121 | } |
| 122 | |
satok | 2df3060 | 2011-07-15 13:49:00 +0900 | [diff] [blame] | 123 | inline bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const { |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 124 | const int *chars = getProximityCharsAt(index); |
| 125 | int i = 0; |
| 126 | while (chars[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE) { |
| 127 | if (chars[i++] == c) { |
| 128 | return true; |
| 129 | } |
| 130 | } |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | bool ProximityInfo::existsAdjacentProximityChars(const int index) const { |
| 135 | if (index < 0 || index >= mInputLength) return false; |
| 136 | const int currentChar = getPrimaryCharAt(index); |
| 137 | const int leftIndex = index - 1; |
| 138 | if (leftIndex >= 0 && existsCharInProximityAt(leftIndex, currentChar)) { |
| 139 | return true; |
| 140 | } |
| 141 | const int rightIndex = index + 1; |
| 142 | if (rightIndex < mInputLength && existsCharInProximityAt(rightIndex, currentChar)) { |
| 143 | return true; |
| 144 | } |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | // In the following function, c is the current character of the dictionary word |
| 149 | // currently examined. |
| 150 | // currentChars is an array containing the keys close to the character the |
| 151 | // user actually typed at the same position. We want to see if c is in it: if so, |
| 152 | // then the word contains at that position a character close to what the user |
| 153 | // typed. |
| 154 | // What the user typed is actually the first character of the array. |
| 155 | // Notice : accented characters do not have a proximity list, so they are alone |
| 156 | // in their list. The non-accented version of the character should be considered |
| 157 | // "close", but not the other keys close to the non-accented version. |
| 158 | ProximityInfo::ProximityType ProximityInfo::getMatchedProximityId( |
satok | 985312e | 2011-08-05 21:21:01 +0900 | [diff] [blame] | 159 | const int index, const unsigned short c, const bool checkProximityChars) const { |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 160 | const int *currentChars = getProximityCharsAt(index); |
| 161 | const unsigned short baseLowerC = Dictionary::toBaseLowerCase(c); |
| 162 | |
| 163 | // The first char in the array is what user typed. If it matches right away, |
| 164 | // that means the user typed that same char for this pos. |
| 165 | if (currentChars[0] == baseLowerC || currentChars[0] == c) |
| 166 | return SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR; |
| 167 | |
satok | 985312e | 2011-08-05 21:21:01 +0900 | [diff] [blame] | 168 | if (!checkProximityChars) return UNRELATED_CHAR; |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 169 | |
| 170 | // If the non-accented, lowercased version of that first character matches c, |
| 171 | // then we have a non-accented version of the accented character the user |
| 172 | // typed. Treat it as a close char. |
| 173 | if (Dictionary::toBaseLowerCase(currentChars[0]) == baseLowerC) |
| 174 | return NEAR_PROXIMITY_CHAR; |
| 175 | |
| 176 | // Not an exact nor an accent-alike match: search the list of close keys |
| 177 | int j = 1; |
| 178 | while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS_SIZE) { |
| 179 | const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c); |
| 180 | if (matched) return NEAR_PROXIMITY_CHAR; |
| 181 | ++j; |
| 182 | } |
| 183 | |
| 184 | // Was not included, signal this as an unrelated character. |
| 185 | return UNRELATED_CHAR; |
| 186 | } |
| 187 | |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 188 | bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const { |
| 189 | if (length != mInputLength) { |
| 190 | return false; |
| 191 | } |
| 192 | const int *inputCodes = mInputCodes; |
| 193 | while (length--) { |
| 194 | if ((unsigned int) *inputCodes != (unsigned int) *word) { |
| 195 | return false; |
| 196 | } |
| 197 | inputCodes += MAX_PROXIMITY_CHARS_SIZE; |
| 198 | word++; |
| 199 | } |
| 200 | return true; |
| 201 | } |
| 202 | |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 203 | const int ProximityInfo::MAX_KEY_COUNT_IN_A_KEYBOARD; |
Yusuke Nojima | ad35835 | 2011-09-29 16:44:54 +0900 | [diff] [blame^] | 204 | const int ProximityInfo::MAX_CHAR_CODE; |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 205 | |
Ken Wakasa | ce9e52a | 2011-06-18 13:09:55 +0900 | [diff] [blame] | 206 | } // namespace latinime |