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 <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-11-22 11:15:40 +00:00
|
|
|
static const int LOOKUP_RADIUS_PERCENTILE;
|
|
|
|
static const int FIRST_POINT_TIME_OFFSET_MILLIS;
|
|
|
|
static const int STRONG_DOUBLE_LETTER_TIME_MILLIS;
|
|
|
|
static const int MIN_DOUBLE_LETTER_BEELINE_SPEED_PERCENTILE;
|
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-10-29 09:06:22 +00:00
|
|
|
const ProximityInfo *proximityInfo, const int *const inputCodes,
|
2012-08-24 06:19:56 +00:00
|
|
|
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-11-02 17:50:47 +00:00
|
|
|
AK_FORCE_INLINE ProximityInfoState()
|
2012-11-16 14:06:41 +00:00
|
|
|
: mProximityInfo(0), mMaxPointToKeyLength(0.0f), mAverageSpeed(0.0f),
|
2012-08-24 05:51:15 +00:00
|
|
|
mHasTouchPositionCorrectionData(false), mMostCommonKeyWidthSquare(0), mLocaleStr(),
|
|
|
|
mKeyCount(0), mCellHeight(0), mCellWidth(0), mGridHeight(0), mGridWidth(0),
|
2012-11-16 10:03:36 +00:00
|
|
|
mIsContinuationPossible(false), mSampledInputXs(), mSampledInputYs(), mTimes(),
|
2013-01-15 10:16:38 +00:00
|
|
|
mInputIndice(), mLengthCache(), mBeelineSpeedPercentiles(), mDistanceCache_G(),
|
2012-11-22 11:15:40 +00:00
|
|
|
mSpeedRates(), mDirections(), mCharProbabilities(), mNearKeysVector(),
|
2012-11-16 14:06:41 +00:00
|
|
|
mSearchKeysVector(), mTouchPositionCorrectionEnabled(false), mSampledInputSize(0) {
|
2013-01-17 10:46:12 +00:00
|
|
|
memset(mInputProximities, 0, sizeof(mInputProximities));
|
2012-08-24 05:51:15 +00:00
|
|
|
memset(mNormalizedSquaredDistances, 0, sizeof(mNormalizedSquaredDistances));
|
|
|
|
memset(mPrimaryInputWord, 0, sizeof(mPrimaryInputWord));
|
|
|
|
}
|
|
|
|
|
2012-11-02 17:50:47 +00:00
|
|
|
// Non virtual inline destructor -- never inherit this class
|
|
|
|
AK_FORCE_INLINE ~ProximityInfoState() {}
|
2012-06-05 08:55:52 +00:00
|
|
|
|
2012-10-29 09:06:22 +00:00
|
|
|
inline int getPrimaryCodePointAt(const int index) const {
|
|
|
|
return getProximityCodePointsAt(index)[0];
|
2012-06-05 08:55:52 +00:00
|
|
|
}
|
|
|
|
|
2012-11-02 17:50:47 +00:00
|
|
|
AK_FORCE_INLINE bool existsCodePointInProximityAt(const int index, const int c) const {
|
2012-10-29 09:06:22 +00:00
|
|
|
const int *codePoints = getProximityCodePointsAt(index);
|
2012-06-05 08:55:52 +00:00
|
|
|
int i = 0;
|
2012-10-29 09:06:22 +00:00
|
|
|
while (codePoints[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE_INTERNAL) {
|
|
|
|
if (codePoints[i++] == c) {
|
2012-06-05 08:55:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool existsAdjacentProximityChars(const int index) const {
|
2012-11-16 10:03:36 +00:00
|
|
|
if (index < 0 || index >= mSampledInputSize) return false;
|
2012-10-29 09:06:22 +00:00
|
|
|
const int currentCodePoint = getPrimaryCodePointAt(index);
|
2012-06-05 08:55:52 +00:00
|
|
|
const int leftIndex = index - 1;
|
2012-10-29 09:06:22 +00:00
|
|
|
if (leftIndex >= 0 && existsCodePointInProximityAt(leftIndex, currentCodePoint)) {
|
2012-06-05 08:55:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const int rightIndex = index + 1;
|
2012-11-16 10:03:36 +00:00
|
|
|
if (rightIndex < mSampledInputSize
|
|
|
|
&& existsCodePointInProximityAt(rightIndex, currentCodePoint)) {
|
2012-06-05 08:55:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10-29 09:06:22 +00:00
|
|
|
inline const int *getPrimaryInputWord() const {
|
2012-06-05 08:55:52 +00:00
|
|
|
return mPrimaryInputWord;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool touchPositionCorrectionEnabled() const {
|
|
|
|
return mTouchPositionCorrectionEnabled;
|
|
|
|
}
|
|
|
|
|
2012-10-29 09:06:22 +00:00
|
|
|
inline bool sameAsTyped(const int *word, int length) const {
|
2012-11-16 10:03:36 +00:00
|
|
|
if (length != mSampledInputSize) {
|
2012-08-10 07:52:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-01-17 10:46:12 +00:00
|
|
|
const int *inputProximities = mInputProximities;
|
2012-08-10 07:52:27 +00:00
|
|
|
while (length--) {
|
2013-01-17 10:46:12 +00:00
|
|
|
if (*inputProximities != *word) {
|
2012-08-10 07:52:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-01-17 10:46:12 +00:00
|
|
|
inputProximities += MAX_PROXIMITY_CHARS_SIZE_INTERNAL;
|
2012-08-10 07:52:27 +00:00
|
|
|
word++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-23 06:46:43 +00:00
|
|
|
int getDuration(const int index) const;
|
|
|
|
|
|
|
|
bool isUsed() const {
|
2012-11-16 10:03:36 +00:00
|
|
|
return mSampledInputSize > 0;
|
2012-08-23 06:46:43 +00:00
|
|
|
}
|
|
|
|
|
2012-12-10 10:12:46 +00:00
|
|
|
int size() const {
|
2012-11-16 10:03:36 +00:00
|
|
|
return mSampledInputSize;
|
2012-08-27 05:39:17 +00:00
|
|
|
}
|
|
|
|
|
2012-09-06 11:55:45 +00:00
|
|
|
int getInputX(const int index) const {
|
2012-11-16 10:03:36 +00:00
|
|
|
return mSampledInputXs[index];
|
2012-08-27 05:39:17 +00:00
|
|
|
}
|
|
|
|
|
2012-09-06 11:55:45 +00:00
|
|
|
int getInputY(const int index) const {
|
2012-11-16 10:03:36 +00:00
|
|
|
return mSampledInputYs[index];
|
2012-08-27 05:39:17 +00:00
|
|
|
}
|
|
|
|
|
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-12-12 11:19:18 +00:00
|
|
|
float getPointToKeyByIdLength(const int inputIndex, const int keyId, const float scale) const;
|
2012-10-09 10:57:08 +00:00
|
|
|
float getPointToKeyByIdLength(const int inputIndex, const int keyId) const;
|
2012-12-12 11:19:18 +00:00
|
|
|
float getPointToKeyLength(const int inputIndex, const int codePoint, const float scale) const;
|
2013-01-15 10:16:38 +00:00
|
|
|
float getPointToKeyLength_G(const int inputIndex, const int codePoint) const;
|
2012-08-23 06:46:43 +00:00
|
|
|
|
2012-11-02 09:29:03 +00:00
|
|
|
ProximityType getMatchedProximityId(const int index, const int c,
|
|
|
|
const bool checkProximityChars, int *proximityIndex = 0) const;
|
|
|
|
|
2012-09-06 11:55:45 +00:00
|
|
|
int getSpaceY() const;
|
2012-08-23 06:46:43 +00:00
|
|
|
|
2012-12-10 14:36:22 +00:00
|
|
|
int getAllPossibleChars(const size_t startIndex, int *const filter, const int filterSize) const;
|
2012-09-04 08:00:24 +00:00
|
|
|
|
2012-11-16 14:06:41 +00:00
|
|
|
float getSpeedRate(const int index) const {
|
|
|
|
return mSpeedRates[index];
|
|
|
|
}
|
|
|
|
|
2012-11-22 11:15:40 +00:00
|
|
|
AK_FORCE_INLINE int getBeelineSpeedPercentile(const int id) const {
|
|
|
|
return mBeelineSpeedPercentiles[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
AK_FORCE_INLINE DoubleLetterLevel getDoubleLetterLevel(const int id) const {
|
|
|
|
const int beelineSpeedRate = getBeelineSpeedPercentile(id);
|
|
|
|
if (beelineSpeedRate == 0) {
|
|
|
|
return A_STRONG_DOUBLE_LETTER;
|
|
|
|
} else if (beelineSpeedRate < MIN_DOUBLE_LETTER_BEELINE_SPEED_PERCENTILE) {
|
|
|
|
return A_DOUBLE_LETTER;
|
|
|
|
} else {
|
|
|
|
return NOT_A_DOUBLE_LETTER;
|
|
|
|
}
|
2012-09-24 11:02:57 +00:00
|
|
|
}
|
2012-10-09 10:57:08 +00:00
|
|
|
|
2012-10-11 11:24:41 +00:00
|
|
|
float getDirection(const int index) const {
|
|
|
|
return mDirections[index];
|
|
|
|
}
|
2012-10-11 04:08:06 +00:00
|
|
|
// get xy direction
|
|
|
|
float getDirection(const int x, const int y) const;
|
|
|
|
|
2012-10-09 10:57:08 +00:00
|
|
|
float getPointAngle(const int index) const;
|
|
|
|
// Returns angle of three points. x, y, and z are indices.
|
|
|
|
float getPointsAngle(const int index0, const int index1, const int index2) const;
|
|
|
|
|
2012-12-17 06:20:58 +00:00
|
|
|
float getMostProbableString(int *const codePointBuf) const;
|
2012-10-11 04:08:06 +00:00
|
|
|
|
|
|
|
float getProbability(const int index, const int charCode) const;
|
|
|
|
|
|
|
|
float getLineToKeyDistance(
|
|
|
|
const int from, const int to, const int keyId, const bool extend) const;
|
|
|
|
|
|
|
|
bool isKeyInSerchKeysAfterIndex(const int index, const int keyId) const;
|
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-10-29 09:06:22 +00:00
|
|
|
bool pushTouchPoint(const int inputIndex, const int nodeCodePoint, int x, int y, const int time,
|
2012-10-11 04:08:06 +00:00
|
|
|
const bool sample, const bool isLastPoint, const float sumAngle,
|
2012-08-31 12:22:14 +00:00
|
|
|
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-11-16 10:03:36 +00:00
|
|
|
return mSampledInputXs.size() > 0 && mSampledInputYs.size() > 0;
|
2012-06-05 08:55:52 +00:00
|
|
|
}
|
|
|
|
|
2012-10-29 09:06:22 +00:00
|
|
|
inline const int *getProximityCodePointsAt(const int index) const {
|
2013-01-17 10:46:12 +00:00
|
|
|
return mInputProximities + (index * MAX_PROXIMITY_CHARS_SIZE_INTERNAL);
|
2012-08-16 10:34:02 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2012-10-11 04:08:06 +00:00
|
|
|
const float sumAngle, const NearKeysDistanceMap *const currentNearKeysDistances,
|
2012-08-31 12:22:14 +00:00
|
|
|
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,
|
2012-12-10 08:17:52 +00:00
|
|
|
const int *const yCoordinates, const int *const times, const bool isGeometric) const;
|
2012-09-07 12:04:12 +00:00
|
|
|
void popInputData();
|
2012-10-11 04:08:06 +00:00
|
|
|
void updateAlignPointProbabilities(const int start);
|
2012-10-09 10:57:08 +00:00
|
|
|
bool suppressCharProbabilities(const int index1, const int index2);
|
2012-11-16 14:06:41 +00:00
|
|
|
void refreshSpeedRates(const int inputSize, const int *const xCoordinates,
|
2012-11-16 10:03:36 +00:00
|
|
|
const int *const yCoordinates, const int *const times, const int lastSavedInputSize);
|
2012-11-16 14:06:41 +00:00
|
|
|
void refreshBeelineSpeedRates(const int inputSize,
|
|
|
|
const int *const xCoordinates, const int *const yCoordinates, const int * times);
|
|
|
|
float calculateBeelineSpeedRate(const int id, const int inputSize,
|
|
|
|
const int *const xCoordinates, const int *const yCoordinates, const int * times) const;
|
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-11-16 14:06:41 +00:00
|
|
|
float mAverageSpeed;
|
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-11-16 10:03:36 +00:00
|
|
|
std::vector<int> mSampledInputXs;
|
|
|
|
std::vector<int> mSampledInputYs;
|
2012-08-23 06:46:43 +00:00
|
|
|
std::vector<int> mTimes;
|
2012-09-07 12:04:12 +00:00
|
|
|
std::vector<int> mInputIndice;
|
2012-11-16 14:06:41 +00:00
|
|
|
std::vector<int> mLengthCache;
|
2012-11-22 11:15:40 +00:00
|
|
|
std::vector<int> mBeelineSpeedPercentiles;
|
2013-01-15 10:16:38 +00:00
|
|
|
std::vector<float> mDistanceCache_G;
|
2012-11-16 14:06:41 +00:00
|
|
|
std::vector<float> mSpeedRates;
|
2012-10-11 11:24:41 +00:00
|
|
|
std::vector<float> mDirections;
|
2012-10-09 10:57:08 +00:00
|
|
|
// probabilities of skipping or mapping to a key for each point.
|
|
|
|
std::vector<hash_map_compat<int, float> > mCharProbabilities;
|
2012-10-11 04:08:06 +00:00
|
|
|
// The vector for the key code set which holds nearby keys for each sampled input point
|
|
|
|
// 1. Used to calculate the probability of the key
|
|
|
|
// 2. Used to calculate mSearchKeysVector
|
2012-09-04 08:00:24 +00:00
|
|
|
std::vector<NearKeycodesSet> mNearKeysVector;
|
2012-10-11 04:08:06 +00:00
|
|
|
// The vector for the key code set which holds nearby keys of some trailing sampled input points
|
|
|
|
// for each sampled input point. These nearby keys contain the next characters which can be in
|
|
|
|
// the dictionary. Specifically, currently we are looking for keys nearby trailing sampled
|
|
|
|
// inputs including the current input point.
|
|
|
|
std::vector<NearKeycodesSet> mSearchKeysVector;
|
2012-06-05 08:55:52 +00:00
|
|
|
bool mTouchPositionCorrectionEnabled;
|
2013-01-17 10:46:12 +00:00
|
|
|
int mInputProximities[MAX_PROXIMITY_CHARS_SIZE_INTERNAL * MAX_WORD_LENGTH];
|
2013-01-11 16:18:00 +00:00
|
|
|
int mNormalizedSquaredDistances[MAX_PROXIMITY_CHARS_SIZE_INTERNAL * MAX_WORD_LENGTH];
|
2012-11-16 10:03:36 +00:00
|
|
|
int mSampledInputSize;
|
2013-01-11 16:18:00 +00:00
|
|
|
int mPrimaryInputWord[MAX_WORD_LENGTH];
|
2012-06-05 08:55:52 +00:00
|
|
|
};
|
|
|
|
} // namespace latinime
|
|
|
|
#endif // LATINIME_PROXIMITY_INFO_STATE_H
|