2012-06-05 08:55:52 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef LATINIME_PROXIMITY_INFO_STATE_H
|
|
|
|
#define LATINIME_PROXIMITY_INFO_STATE_H
|
|
|
|
|
2012-09-04 08:00:24 +00:00
|
|
|
#include <bitset>
|
2012-08-24 05:51:15 +00:00
|
|
|
#include <cstring> // for memset()
|
2012-06-05 08:55:52 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string>
|
2012-08-23 06:46:43 +00:00
|
|
|
#include <vector>
|
2012-06-05 08:55:52 +00:00
|
|
|
|
2012-06-08 06:29:44 +00:00
|
|
|
#include "char_utils.h"
|
2012-06-05 08:55:52 +00:00
|
|
|
#include "defines.h"
|
2012-08-31 12:22:14 +00:00
|
|
|
#include "hash_map_compat.h"
|
2012-06-05 08:55:52 +00:00
|
|
|
|
|
|
|
namespace latinime {
|
|
|
|
|
|
|
|
class ProximityInfo;
|
|
|
|
|
|
|
|
class ProximityInfoState {
|
|
|
|
public:
|
2012-09-04 08:00:24 +00:00
|
|
|
typedef std::bitset<MAX_KEY_COUNT_IN_A_KEYBOARD> NearKeycodesSet;
|
2012-09-15 16:23:56 +00:00
|
|
|
static const int NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR_LOG_2;
|
|
|
|
static const int NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR;
|
|
|
|
static const float NOT_A_DISTANCE_FLOAT;
|
|
|
|
static const int NOT_A_CODE;
|
2012-06-05 08:55:52 +00:00
|
|
|
|
|
|
|
/////////////////////////////////////////
|
|
|
|
// Defined in proximity_info_state.cpp //
|
|
|
|
/////////////////////////////////////////
|
2012-08-24 05:45:54 +00:00
|
|
|
void initInputParams(const int pointerId, const float maxPointToKeyLength,
|
2012-08-24 06:19:56 +00:00
|
|
|
const ProximityInfo *proximityInfo, const int32_t *const inputCodes,
|
|
|
|
const int inputSize, const int *xCoordinates, const int *yCoordinates,
|
|
|
|
const int *const times, const int *const pointerIds, const bool isGeometric);
|
2012-06-05 08:55:52 +00:00
|
|
|
|
|
|
|
/////////////////////////////////////////
|
|
|
|
// Defined here //
|
|
|
|
/////////////////////////////////////////
|
2012-08-24 05:51:15 +00:00
|
|
|
ProximityInfoState()
|
|
|
|
: mProximityInfo(0), mMaxPointToKeyLength(0),
|
|
|
|
mHasTouchPositionCorrectionData(false), mMostCommonKeyWidthSquare(0), mLocaleStr(),
|
|
|
|
mKeyCount(0), mCellHeight(0), mCellWidth(0), mGridHeight(0), mGridWidth(0),
|
2012-09-07 12:04:12 +00:00
|
|
|
mIsContinuationPossible(false), mInputXs(), mInputYs(), mTimes(), mInputIndice(),
|
2012-09-24 11:02:57 +00:00
|
|
|
mDistanceCache(), mLengthCache(), mRelativeSpeeds(), mNearKeysVector(),
|
2012-09-07 12:04:12 +00:00
|
|
|
mTouchPositionCorrectionEnabled(false), mInputSize(0) {
|
2012-08-24 05:51:15 +00:00
|
|
|
memset(mInputCodes, 0, sizeof(mInputCodes));
|
|
|
|
memset(mNormalizedSquaredDistances, 0, sizeof(mNormalizedSquaredDistances));
|
|
|
|
memset(mPrimaryInputWord, 0, sizeof(mPrimaryInputWord));
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~ProximityInfoState() {}
|
2012-06-05 08:55:52 +00:00
|
|
|
|
|
|
|
inline unsigned short getPrimaryCharAt(const int index) const {
|
|
|
|
return getProximityCharsAt(index)[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool existsCharInProximityAt(const int index, const int c) const {
|
|
|
|
const int *chars = getProximityCharsAt(index);
|
|
|
|
int i = 0;
|
2012-06-08 06:29:44 +00:00
|
|
|
while (chars[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE_INTERNAL) {
|
2012-06-05 08:55:52 +00:00
|
|
|
if (chars[i++] == c) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool existsAdjacentProximityChars(const int index) const {
|
2012-08-23 06:46:43 +00:00
|
|
|
if (index < 0 || index >= mInputSize) return false;
|
2012-06-05 08:55:52 +00:00
|
|
|
const int currentChar = getPrimaryCharAt(index);
|
|
|
|
const int leftIndex = index - 1;
|
|
|
|
if (leftIndex >= 0 && existsCharInProximityAt(leftIndex, currentChar)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const int rightIndex = index + 1;
|
2012-08-23 06:46:43 +00:00
|
|
|
if (rightIndex < mInputSize && existsCharInProximityAt(rightIndex, currentChar)) {
|
2012-06-05 08:55:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In the following function, c is the current character of the dictionary word
|
|
|
|
// currently examined.
|
|
|
|
// currentChars is an array containing the keys close to the character the
|
|
|
|
// user actually typed at the same position. We want to see if c is in it: if so,
|
|
|
|
// then the word contains at that position a character close to what the user
|
|
|
|
// typed.
|
|
|
|
// What the user typed is actually the first character of the array.
|
|
|
|
// proximityIndex is a pointer to the variable where getMatchedProximityId returns
|
|
|
|
// the index of c in the proximity chars of the input index.
|
|
|
|
// Notice : accented characters do not have a proximity list, so they are alone
|
|
|
|
// in their list. The non-accented version of the character should be considered
|
|
|
|
// "close", but not the other keys close to the non-accented version.
|
|
|
|
inline ProximityType getMatchedProximityId(const int index,
|
2012-06-08 06:29:44 +00:00
|
|
|
const unsigned short c, const bool checkProximityChars, int *proximityIndex = 0) const {
|
2012-06-05 08:55:52 +00:00
|
|
|
const int *currentChars = getProximityCharsAt(index);
|
|
|
|
const int firstChar = currentChars[0];
|
|
|
|
const unsigned short baseLowerC = toBaseLowerCase(c);
|
|
|
|
|
|
|
|
// The first char in the array is what user typed. If it matches right away,
|
|
|
|
// that means the user typed that same char for this pos.
|
|
|
|
if (firstChar == baseLowerC || firstChar == c) {
|
|
|
|
return EQUIVALENT_CHAR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!checkProximityChars) return UNRELATED_CHAR;
|
|
|
|
|
|
|
|
// If the non-accented, lowercased version of that first character matches c,
|
|
|
|
// then we have a non-accented version of the accented character the user
|
|
|
|
// typed. Treat it as a close char.
|
|
|
|
if (toBaseLowerCase(firstChar) == baseLowerC)
|
|
|
|
return NEAR_PROXIMITY_CHAR;
|
|
|
|
|
|
|
|
// Not an exact nor an accent-alike match: search the list of close keys
|
|
|
|
int j = 1;
|
2012-06-08 06:29:44 +00:00
|
|
|
while (j < MAX_PROXIMITY_CHARS_SIZE_INTERNAL
|
2012-06-05 08:55:52 +00:00
|
|
|
&& currentChars[j] > ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) {
|
|
|
|
const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
|
|
|
|
if (matched) {
|
|
|
|
if (proximityIndex) {
|
|
|
|
*proximityIndex = j;
|
|
|
|
}
|
|
|
|
return NEAR_PROXIMITY_CHAR;
|
|
|
|
}
|
|
|
|
++j;
|
|
|
|
}
|
2012-06-08 06:29:44 +00:00
|
|
|
if (j < MAX_PROXIMITY_CHARS_SIZE_INTERNAL
|
2012-06-05 08:55:52 +00:00
|
|
|
&& currentChars[j] == ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) {
|
|
|
|
++j;
|
2012-06-08 06:29:44 +00:00
|
|
|
while (j < MAX_PROXIMITY_CHARS_SIZE_INTERNAL
|
2012-06-05 08:55:52 +00:00
|
|
|
&& currentChars[j] > ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) {
|
|
|
|
const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
|
|
|
|
if (matched) {
|
|
|
|
if (proximityIndex) {
|
|
|
|
*proximityIndex = j;
|
|
|
|
}
|
|
|
|
return ADDITIONAL_PROXIMITY_CHAR;
|
|
|
|
}
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Was not included, signal this as an unrelated character.
|
|
|
|
return UNRELATED_CHAR;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int getNormalizedSquaredDistance(
|
|
|
|
const int inputIndex, const int proximityIndex) const {
|
2012-06-08 06:29:44 +00:00
|
|
|
return mNormalizedSquaredDistances[
|
|
|
|
inputIndex * MAX_PROXIMITY_CHARS_SIZE_INTERNAL + proximityIndex];
|
2012-06-05 08:55:52 +00:00
|
|
|
}
|
|
|
|
|
2012-07-25 08:51:43 +00:00
|
|
|
inline const unsigned short *getPrimaryInputWord() const {
|
2012-06-05 08:55:52 +00:00
|
|
|
return mPrimaryInputWord;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool touchPositionCorrectionEnabled() const {
|
|
|
|
return mTouchPositionCorrectionEnabled;
|
|
|
|
}
|
|
|
|
|
2012-08-10 07:52:27 +00:00
|
|
|
inline bool sameAsTyped(const unsigned short *word, int length) const {
|
2012-08-23 06:46:43 +00:00
|
|
|
if (length != mInputSize) {
|
2012-08-10 07:52:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const int *inputCodes = mInputCodes;
|
|
|
|
while (length--) {
|
|
|
|
if (static_cast<unsigned int>(*inputCodes) != static_cast<unsigned int>(*word)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
inputCodes += MAX_PROXIMITY_CHARS_SIZE_INTERNAL;
|
|
|
|
word++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-23 06:46:43 +00:00
|
|
|
int getDuration(const int index) const;
|
|
|
|
|
|
|
|
bool isUsed() const {
|
|
|
|
return mInputSize > 0;
|
|
|
|
}
|
|
|
|
|
2012-08-27 05:39:17 +00:00
|
|
|
uint32_t size() const {
|
|
|
|
return mInputSize;
|
|
|
|
}
|
|
|
|
|
2012-09-06 11:55:45 +00:00
|
|
|
int getInputX(const int index) const {
|
2012-08-27 05:39:17 +00:00
|
|
|
return mInputXs[index];
|
|
|
|
}
|
|
|
|
|
2012-09-06 11:55:45 +00:00
|
|
|
int getInputY(const int index) const {
|
2012-08-27 05:39:17 +00:00
|
|
|
return mInputYs[index];
|
|
|
|
}
|
|
|
|
|
2012-09-06 11:55:45 +00:00
|
|
|
int getLengthCache(const int index) const {
|
2012-08-27 05:39:17 +00:00
|
|
|
return mLengthCache[index];
|
|
|
|
}
|
|
|
|
|
2012-09-07 12:04:12 +00:00
|
|
|
bool isContinuationPossible() const {
|
|
|
|
return mIsContinuationPossible;
|
|
|
|
}
|
|
|
|
|
2012-09-06 11:55:45 +00:00
|
|
|
float getPointToKeyLength(const int inputIndex, const int charCode, const float scale) const;
|
2012-08-23 06:46:43 +00:00
|
|
|
|
2012-09-06 11:55:45 +00:00
|
|
|
int getSpaceY() const;
|
2012-08-23 06:46:43 +00:00
|
|
|
|
2012-09-04 08:00:24 +00:00
|
|
|
int32_t getAllPossibleChars(
|
2012-09-06 11:55:45 +00:00
|
|
|
const size_t startIndex, int32_t *const filter, const int32_t filterSize) const;
|
2012-09-04 08:00:24 +00:00
|
|
|
|
2012-09-24 11:02:57 +00:00
|
|
|
float getRelativeSpeed(const int index) const {
|
|
|
|
return mRelativeSpeeds[index];
|
|
|
|
}
|
2012-06-05 08:55:52 +00:00
|
|
|
private:
|
2012-06-14 18:25:50 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(ProximityInfoState);
|
2012-08-31 12:22:14 +00:00
|
|
|
typedef hash_map_compat<int, float> NearKeysDistanceMap;
|
2012-06-08 06:29:44 +00:00
|
|
|
/////////////////////////////////////////
|
|
|
|
// Defined in proximity_info_state.cpp //
|
|
|
|
/////////////////////////////////////////
|
|
|
|
float calculateNormalizedSquaredDistance(const int keyIndex, const int inputIndex) const;
|
2012-06-05 08:55:52 +00:00
|
|
|
|
2012-06-08 06:29:44 +00:00
|
|
|
float calculateSquaredDistanceFromSweetSpotCenter(
|
|
|
|
const int keyIndex, const int inputIndex) const;
|
|
|
|
|
2012-09-07 12:04:12 +00:00
|
|
|
bool pushTouchPoint(const int inputIndex, const int nodeChar, int x, int y, const int time,
|
2012-08-31 12:22:14 +00:00
|
|
|
const bool sample, const bool isLastPoint,
|
|
|
|
NearKeysDistanceMap *const currentNearKeysDistances,
|
|
|
|
const NearKeysDistanceMap *const prevNearKeysDistances,
|
|
|
|
const NearKeysDistanceMap *const prevPrevNearKeysDistances);
|
2012-06-08 06:29:44 +00:00
|
|
|
/////////////////////////////////////////
|
|
|
|
// Defined here //
|
|
|
|
/////////////////////////////////////////
|
|
|
|
inline float square(const float x) const { return x * x; }
|
2012-06-05 08:55:52 +00:00
|
|
|
|
|
|
|
bool hasInputCoordinates() const {
|
2012-08-23 06:46:43 +00:00
|
|
|
return mInputXs.size() > 0 && mInputYs.size() > 0;
|
2012-06-05 08:55:52 +00:00
|
|
|
}
|
|
|
|
|
2012-08-16 10:34:02 +00:00
|
|
|
inline const int *getProximityCharsAt(const int index) const {
|
|
|
|
return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE_INTERNAL);
|
|
|
|
}
|
|
|
|
|
2012-08-31 12:22:14 +00:00
|
|
|
float updateNearKeysDistances(const int x, const int y,
|
|
|
|
NearKeysDistanceMap *const currentNearKeysDistances);
|
|
|
|
bool isPrevLocalMin(const NearKeysDistanceMap *const currentNearKeysDistances,
|
|
|
|
const NearKeysDistanceMap *const prevNearKeysDistances,
|
|
|
|
const NearKeysDistanceMap *const prevPrevNearKeysDistances) const;
|
|
|
|
float getPointScore(
|
|
|
|
const int x, const int y, const int time, const bool last, const float nearest,
|
|
|
|
const NearKeysDistanceMap *const currentNearKeysDistances,
|
|
|
|
const NearKeysDistanceMap *const prevNearKeysDistances,
|
|
|
|
const NearKeysDistanceMap *const prevPrevNearKeysDistances) const;
|
2012-09-07 12:04:12 +00:00
|
|
|
bool checkAndReturnIsContinuationPossible(const int inputSize, const int *const xCoordinates,
|
|
|
|
const int *const yCoordinates, const int *const times);
|
|
|
|
void popInputData();
|
2012-08-31 12:22:14 +00:00
|
|
|
|
2012-06-08 06:29:44 +00:00
|
|
|
// const
|
|
|
|
const ProximityInfo *mProximityInfo;
|
2012-08-23 06:46:43 +00:00
|
|
|
float mMaxPointToKeyLength;
|
2012-06-08 06:29:44 +00:00
|
|
|
bool mHasTouchPositionCorrectionData;
|
|
|
|
int mMostCommonKeyWidthSquare;
|
|
|
|
std::string mLocaleStr;
|
|
|
|
int mKeyCount;
|
|
|
|
int mCellHeight;
|
|
|
|
int mCellWidth;
|
|
|
|
int mGridHeight;
|
|
|
|
int mGridWidth;
|
2012-09-07 12:04:12 +00:00
|
|
|
bool mIsContinuationPossible;
|
2012-06-05 08:55:52 +00:00
|
|
|
|
2012-08-23 06:46:43 +00:00
|
|
|
std::vector<int> mInputXs;
|
|
|
|
std::vector<int> mInputYs;
|
|
|
|
std::vector<int> mTimes;
|
2012-09-07 12:04:12 +00:00
|
|
|
std::vector<int> mInputIndice;
|
2012-08-23 06:46:43 +00:00
|
|
|
std::vector<float> mDistanceCache;
|
|
|
|
std::vector<int> mLengthCache;
|
2012-09-24 11:02:57 +00:00
|
|
|
std::vector<float> mRelativeSpeeds;
|
2012-09-04 08:00:24 +00:00
|
|
|
std::vector<NearKeycodesSet> mNearKeysVector;
|
2012-06-05 08:55:52 +00:00
|
|
|
bool mTouchPositionCorrectionEnabled;
|
|
|
|
int32_t mInputCodes[MAX_PROXIMITY_CHARS_SIZE_INTERNAL * MAX_WORD_LENGTH_INTERNAL];
|
|
|
|
int mNormalizedSquaredDistances[MAX_PROXIMITY_CHARS_SIZE_INTERNAL * MAX_WORD_LENGTH_INTERNAL];
|
2012-08-23 06:46:43 +00:00
|
|
|
int mInputSize;
|
2012-06-05 08:55:52 +00:00
|
|
|
unsigned short mPrimaryInputWord[MAX_WORD_LENGTH_INTERNAL];
|
|
|
|
};
|
|
|
|
} // namespace latinime
|
|
|
|
#endif // LATINIME_PROXIMITY_INFO_STATE_H
|