blob: 361bdacbf9d6c57a27e4f277f0b981c9a4d3b8b8 [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;
satok0cedd2b2011-08-12 01:05:27 +090071 for (int i = 0; i < inputLength; ++i) {
72 mPrimaryInputWord[i] = getPrimaryCharAt(i);
73 }
74 mPrimaryInputWord[inputLength] = 0;
satok1d7eaf82011-07-13 10:32:02 +090075}
76
satokd24df432011-07-14 15:43:42 +090077inline const int* ProximityInfo::getProximityCharsAt(const int index) const {
satok1d7eaf82011-07-13 10:32:02 +090078 return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE);
79}
80
satokd24df432011-07-14 15:43:42 +090081unsigned short ProximityInfo::getPrimaryCharAt(const int index) const {
82 return getProximityCharsAt(index)[0];
83}
84
satok2df30602011-07-15 13:49:00 +090085inline bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const {
satokd24df432011-07-14 15:43:42 +090086 const int *chars = getProximityCharsAt(index);
87 int i = 0;
88 while (chars[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE) {
89 if (chars[i++] == c) {
90 return true;
91 }
92 }
93 return false;
94}
95
96bool ProximityInfo::existsAdjacentProximityChars(const int index) const {
97 if (index < 0 || index >= mInputLength) return false;
98 const int currentChar = getPrimaryCharAt(index);
99 const int leftIndex = index - 1;
100 if (leftIndex >= 0 && existsCharInProximityAt(leftIndex, currentChar)) {
101 return true;
102 }
103 const int rightIndex = index + 1;
104 if (rightIndex < mInputLength && existsCharInProximityAt(rightIndex, currentChar)) {
105 return true;
106 }
107 return false;
108}
109
110// In the following function, c is the current character of the dictionary word
111// currently examined.
112// currentChars is an array containing the keys close to the character the
113// user actually typed at the same position. We want to see if c is in it: if so,
114// then the word contains at that position a character close to what the user
115// typed.
116// What the user typed is actually the first character of the array.
117// Notice : accented characters do not have a proximity list, so they are alone
118// in their list. The non-accented version of the character should be considered
119// "close", but not the other keys close to the non-accented version.
120ProximityInfo::ProximityType ProximityInfo::getMatchedProximityId(
satok985312e2011-08-05 21:21:01 +0900121 const int index, const unsigned short c, const bool checkProximityChars) const {
satokd24df432011-07-14 15:43:42 +0900122 const int *currentChars = getProximityCharsAt(index);
123 const unsigned short baseLowerC = Dictionary::toBaseLowerCase(c);
124
125 // The first char in the array is what user typed. If it matches right away,
126 // that means the user typed that same char for this pos.
127 if (currentChars[0] == baseLowerC || currentChars[0] == c)
128 return SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR;
129
satok985312e2011-08-05 21:21:01 +0900130 if (!checkProximityChars) return UNRELATED_CHAR;
satokd24df432011-07-14 15:43:42 +0900131
132 // If the non-accented, lowercased version of that first character matches c,
133 // then we have a non-accented version of the accented character the user
134 // typed. Treat it as a close char.
135 if (Dictionary::toBaseLowerCase(currentChars[0]) == baseLowerC)
136 return NEAR_PROXIMITY_CHAR;
137
138 // Not an exact nor an accent-alike match: search the list of close keys
139 int j = 1;
140 while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS_SIZE) {
141 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
142 if (matched) return NEAR_PROXIMITY_CHAR;
143 ++j;
144 }
145
146 // Was not included, signal this as an unrelated character.
147 return UNRELATED_CHAR;
148}
149
satok1d7eaf82011-07-13 10:32:02 +0900150bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const {
151 if (length != mInputLength) {
152 return false;
153 }
154 const int *inputCodes = mInputCodes;
155 while (length--) {
156 if ((unsigned int) *inputCodes != (unsigned int) *word) {
157 return false;
158 }
159 inputCodes += MAX_PROXIMITY_CHARS_SIZE;
160 word++;
161 }
162 return true;
163}
164
Ken Wakasace9e52a2011-06-18 13:09:55 +0900165} // namespace latinime