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