blob: bed92cf9eae83f558a6e273b6988e21839578963 [file] [log] [blame]
satok8fbd5522011-02-22 17:28:55 +09001/*
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
satok817e5172011-03-04 06:06:45 -080020#define LOG_TAG "LatinIME: proximity_info.cpp"
21
satokd24df432011-07-14 15:43:42 +090022#include "dictionary.h"
satok8fbd5522011-02-22 17:28:55 +090023#include "proximity_info.h"
24
25namespace latinime {
Ken Wakasace9e52a2011-06-18 13:09:55 +090026
satok817e5172011-03-04 06:06:45 -080027ProximityInfo::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]));
satok8fbd5522011-02-22 17:28:55 +090040}
41
42ProximityInfo::~ProximityInfo() {
43 delete[] mProximityCharsArray;
44}
satok817e5172011-03-04 06:06:45 -080045
46inline int ProximityInfo::getStartIndexFromCoordinates(const int x, const int y) const {
satok3c4bb772011-03-04 22:50:19 -080047 return ((y / CELL_HEIGHT) * GRID_WIDTH + (x / CELL_WIDTH))
satok817e5172011-03-04 06:06:45 -080048 * MAX_PROXIMITY_CHARS_SIZE;
satok8fbd5522011-02-22 17:28:55 +090049}
satok817e5172011-03-04 06:06:45 -080050
51bool 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 Wakasace9e52a2011-06-18 13:09:55 +090066
satok1d7eaf82011-07-13 10:32:02 +090067// TODO: Calculate nearby codes here.
68void ProximityInfo::setInputParams(const int* inputCodes, const int inputLength) {
69 mInputCodes = inputCodes;
70 mInputLength = inputLength;
71}
72
satokd24df432011-07-14 15:43:42 +090073inline const int* ProximityInfo::getProximityCharsAt(const int index) const {
satok1d7eaf82011-07-13 10:32:02 +090074 return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE);
75}
76
satokd24df432011-07-14 15:43:42 +090077unsigned short ProximityInfo::getPrimaryCharAt(const int index) const {
78 return getProximityCharsAt(index)[0];
79}
80
satok2df30602011-07-15 13:49:00 +090081inline bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const {
satokd24df432011-07-14 15:43:42 +090082 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
92bool 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.
116ProximityInfo::ProximityType ProximityInfo::getMatchedProximityId(
satok2df30602011-07-15 13:49:00 +0900117 const int index, const unsigned short c, CorrectionState *correctionState) const {
118 const int skipPos = correctionState->getSkipPos();
119 const int excessivePos = correctionState->getExcessivePos();
120 const int transposedPos = correctionState->getTransposedPos();
satokd24df432011-07-14 15:43:42 +0900121 const int *currentChars = getProximityCharsAt(index);
122 const unsigned short baseLowerC = Dictionary::toBaseLowerCase(c);
123
124 // The first char in the array is what user typed. If it matches right away,
125 // that means the user typed that same char for this pos.
126 if (currentChars[0] == baseLowerC || currentChars[0] == c)
127 return SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR;
128
129 // If one of those is true, we should not check for close characters at all.
130 if (skipPos >= 0 || excessivePos >= 0 || transposedPos >= 0)
131 return UNRELATED_CHAR;
132
133 // If the non-accented, lowercased version of that first character matches c,
134 // then we have a non-accented version of the accented character the user
135 // typed. Treat it as a close char.
136 if (Dictionary::toBaseLowerCase(currentChars[0]) == baseLowerC)
137 return NEAR_PROXIMITY_CHAR;
138
139 // Not an exact nor an accent-alike match: search the list of close keys
140 int j = 1;
141 while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS_SIZE) {
142 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
143 if (matched) return NEAR_PROXIMITY_CHAR;
144 ++j;
145 }
146
147 // Was not included, signal this as an unrelated character.
148 return UNRELATED_CHAR;
149}
150
satok1d7eaf82011-07-13 10:32:02 +0900151bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const {
152 if (length != mInputLength) {
153 return false;
154 }
155 const int *inputCodes = mInputCodes;
156 while (length--) {
157 if ((unsigned int) *inputCodes != (unsigned int) *word) {
158 return false;
159 }
160 inputCodes += MAX_PROXIMITY_CHARS_SIZE;
161 word++;
162 }
163 return true;
164}
165
Ken Wakasace9e52a2011-06-18 13:09:55 +0900166} // namespace latinime