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 | |
| 17 | #include <stdio.h> |
| 18 | #include <string.h> |
| 19 | |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 20 | #define LOG_TAG "LatinIME: proximity_info.cpp" |
| 21 | |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 22 | #include "dictionary.h" |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 23 | #include "proximity_info.h" |
| 24 | |
| 25 | namespace latinime { |
Ken Wakasa | ce9e52a | 2011-06-18 13:09:55 +0900 | [diff] [blame] | 26 | |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 27 | ProximityInfo::ProximityInfo(const int maxProximityCharsSize, const int keyboardWidth, |
| 28 | const int keyboardHeight, const int gridWidth, const int gridHeight, |
| 29 | const uint32_t *proximityCharsArray) |
| 30 | : MAX_PROXIMITY_CHARS_SIZE(maxProximityCharsSize), KEYBOARD_WIDTH(keyboardWidth), |
| 31 | KEYBOARD_HEIGHT(keyboardHeight), GRID_WIDTH(gridWidth), GRID_HEIGHT(gridHeight), |
| 32 | CELL_WIDTH((keyboardWidth + gridWidth - 1) / gridWidth), |
| 33 | CELL_HEIGHT((keyboardHeight + gridHeight - 1) / gridHeight) { |
| 34 | const int len = GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE; |
| 35 | mProximityCharsArray = new uint32_t[len]; |
| 36 | if (DEBUG_PROXIMITY_INFO) { |
| 37 | LOGI("Create proximity info array %d", len); |
| 38 | } |
| 39 | memcpy(mProximityCharsArray, proximityCharsArray, len * sizeof(mProximityCharsArray[0])); |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | ProximityInfo::~ProximityInfo() { |
| 43 | delete[] mProximityCharsArray; |
| 44 | } |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 45 | |
| 46 | inline int ProximityInfo::getStartIndexFromCoordinates(const int x, const int y) const { |
satok | 3c4bb77 | 2011-03-04 22:50:19 -0800 | [diff] [blame] | 47 | return ((y / CELL_HEIGHT) * GRID_WIDTH + (x / CELL_WIDTH)) |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 48 | * MAX_PROXIMITY_CHARS_SIZE; |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 49 | } |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 50 | |
| 51 | bool ProximityInfo::hasSpaceProximity(const int x, const int y) const { |
| 52 | const int startIndex = getStartIndexFromCoordinates(x, y); |
| 53 | if (DEBUG_PROXIMITY_INFO) { |
| 54 | LOGI("hasSpaceProximity: index %d", startIndex); |
| 55 | } |
| 56 | for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) { |
| 57 | if (DEBUG_PROXIMITY_INFO) { |
| 58 | LOGI("Index: %d", mProximityCharsArray[startIndex + i]); |
| 59 | } |
| 60 | if (mProximityCharsArray[startIndex + i] == KEYCODE_SPACE) { |
| 61 | return true; |
| 62 | } |
| 63 | } |
| 64 | return false; |
| 65 | } |
Ken Wakasa | ce9e52a | 2011-06-18 13:09:55 +0900 | [diff] [blame] | 66 | |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 67 | // TODO: Calculate nearby codes here. |
| 68 | void ProximityInfo::setInputParams(const int* inputCodes, const int inputLength) { |
| 69 | mInputCodes = inputCodes; |
| 70 | mInputLength = inputLength; |
| 71 | } |
| 72 | |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 73 | inline const int* ProximityInfo::getProximityCharsAt(const int index) const { |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 74 | return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE); |
| 75 | } |
| 76 | |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 77 | unsigned short ProximityInfo::getPrimaryCharAt(const int index) const { |
| 78 | return getProximityCharsAt(index)[0]; |
| 79 | } |
| 80 | |
satok | 2df3060 | 2011-07-15 13:49:00 +0900 | [diff] [blame] | 81 | inline bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const { |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 82 | const int *chars = getProximityCharsAt(index); |
| 83 | int i = 0; |
| 84 | while (chars[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE) { |
| 85 | if (chars[i++] == c) { |
| 86 | return true; |
| 87 | } |
| 88 | } |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | bool ProximityInfo::existsAdjacentProximityChars(const int index) const { |
| 93 | if (index < 0 || index >= mInputLength) return false; |
| 94 | const int currentChar = getPrimaryCharAt(index); |
| 95 | const int leftIndex = index - 1; |
| 96 | if (leftIndex >= 0 && existsCharInProximityAt(leftIndex, currentChar)) { |
| 97 | return true; |
| 98 | } |
| 99 | const int rightIndex = index + 1; |
| 100 | if (rightIndex < mInputLength && existsCharInProximityAt(rightIndex, currentChar)) { |
| 101 | return true; |
| 102 | } |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | // In the following function, c is the current character of the dictionary word |
| 107 | // currently examined. |
| 108 | // currentChars is an array containing the keys close to the character the |
| 109 | // user actually typed at the same position. We want to see if c is in it: if so, |
| 110 | // then the word contains at that position a character close to what the user |
| 111 | // typed. |
| 112 | // What the user typed is actually the first character of the array. |
| 113 | // Notice : accented characters do not have a proximity list, so they are alone |
| 114 | // in their list. The non-accented version of the character should be considered |
| 115 | // "close", but not the other keys close to the non-accented version. |
| 116 | ProximityInfo::ProximityType ProximityInfo::getMatchedProximityId( |
satok | 985312e | 2011-08-05 21:21:01 +0900 | [diff] [blame^] | 117 | const int index, const unsigned short c, const bool checkProximityChars) const { |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 118 | const int *currentChars = getProximityCharsAt(index); |
| 119 | const unsigned short baseLowerC = Dictionary::toBaseLowerCase(c); |
| 120 | |
| 121 | // The first char in the array is what user typed. If it matches right away, |
| 122 | // that means the user typed that same char for this pos. |
| 123 | if (currentChars[0] == baseLowerC || currentChars[0] == c) |
| 124 | return SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR; |
| 125 | |
satok | 985312e | 2011-08-05 21:21:01 +0900 | [diff] [blame^] | 126 | if (!checkProximityChars) return UNRELATED_CHAR; |
satok | d24df43 | 2011-07-14 15:43:42 +0900 | [diff] [blame] | 127 | |
| 128 | // If the non-accented, lowercased version of that first character matches c, |
| 129 | // then we have a non-accented version of the accented character the user |
| 130 | // typed. Treat it as a close char. |
| 131 | if (Dictionary::toBaseLowerCase(currentChars[0]) == baseLowerC) |
| 132 | return NEAR_PROXIMITY_CHAR; |
| 133 | |
| 134 | // Not an exact nor an accent-alike match: search the list of close keys |
| 135 | int j = 1; |
| 136 | while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS_SIZE) { |
| 137 | const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c); |
| 138 | if (matched) return NEAR_PROXIMITY_CHAR; |
| 139 | ++j; |
| 140 | } |
| 141 | |
| 142 | // Was not included, signal this as an unrelated character. |
| 143 | return UNRELATED_CHAR; |
| 144 | } |
| 145 | |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 146 | bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const { |
| 147 | if (length != mInputLength) { |
| 148 | return false; |
| 149 | } |
| 150 | const int *inputCodes = mInputCodes; |
| 151 | while (length--) { |
| 152 | if ((unsigned int) *inputCodes != (unsigned int) *word) { |
| 153 | return false; |
| 154 | } |
| 155 | inputCodes += MAX_PROXIMITY_CHARS_SIZE; |
| 156 | word++; |
| 157 | } |
| 158 | return true; |
| 159 | } |
| 160 | |
Ken Wakasa | ce9e52a | 2011-06-18 13:09:55 +0900 | [diff] [blame] | 161 | } // namespace latinime |