1 /* 2 * Copyright (C) 2012 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 #ifndef LATINIME_PROXIMITY_INFO_STATE_H 18 #define LATINIME_PROXIMITY_INFO_STATE_H 19 20 #include <cstring> // for memset() 21 #include <vector> 22 23 #include "defines.h" 24 #include "suggest/core/layout/proximity_info_params.h" 25 #include "suggest/core/layout/proximity_info_state_utils.h" 26 #include "utils/hash_map_compat.h" 27 28 namespace latinime { 29 30 class ProximityInfo; 31 32 class ProximityInfoState { 33 public: 34 ///////////////////////////////////////// 35 // Defined in proximity_info_state.cpp // 36 ///////////////////////////////////////// 37 void initInputParams(const int pointerId, const float maxPointToKeyLength, 38 const ProximityInfo *proximityInfo, const int *const inputCodes, 39 const int inputSize, const int *xCoordinates, const int *yCoordinates, 40 const int *const times, const int *const pointerIds, const bool isGeometric); 41 42 ///////////////////////////////////////// 43 // Defined here // 44 ///////////////////////////////////////// 45 AK_FORCE_INLINE ProximityInfoState() 46 : mProximityInfo(0), mMaxPointToKeyLength(0.0f), mAverageSpeed(0.0f), 47 mHasTouchPositionCorrectionData(false), mMostCommonKeyWidthSquare(0), 48 mKeyCount(0), mCellHeight(0), mCellWidth(0), mGridHeight(0), mGridWidth(0), 49 mIsContinuousSuggestionPossible(false), mHasBeenUpdatedByGeometricInput(false), 50 mSampledInputXs(), mSampledInputYs(), mSampledTimes(), mSampledInputIndice(), 51 mSampledLengthCache(), mBeelineSpeedPercentiles(), 52 mSampledNormalizedSquaredLengthCache(), mSpeedRates(), mDirections(), 53 mCharProbabilities(), mSampledNearKeySets(), mSampledSearchKeySets(), 54 mSampledSearchKeyVectors(), mTouchPositionCorrectionEnabled(false), 55 mSampledInputSize(0), mMostProbableStringProbability(0.0f) { 56 memset(mInputProximities, 0, sizeof(mInputProximities)); 57 memset(mPrimaryInputWord, 0, sizeof(mPrimaryInputWord)); 58 memset(mMostProbableString, 0, sizeof(mMostProbableString)); 59 } 60 61 // Non virtual inline destructor -- never inherit this class 62 AK_FORCE_INLINE ~ProximityInfoState() {} 63 64 inline int getPrimaryCodePointAt(const int index) const { 65 return getProximityCodePointsAt(index)[0]; 66 } 67 68 inline bool sameAsTyped(const int *word, int length) const { 69 if (length != mSampledInputSize) { 70 return false; 71 } 72 const int *inputProximities = mInputProximities; 73 while (length--) { 74 if (*inputProximities != *word) { 75 return false; 76 } 77 inputProximities += MAX_PROXIMITY_CHARS_SIZE; 78 word++; 79 } 80 return true; 81 } 82 83 AK_FORCE_INLINE bool existsCodePointInProximityAt(const int index, const int c) const { 84 const int *codePoints = getProximityCodePointsAt(index); 85 int i = 0; 86 while (codePoints[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE) { 87 if (codePoints[i++] == c) { 88 return true; 89 } 90 } 91 return false; 92 } 93 94 AK_FORCE_INLINE bool existsAdjacentProximityChars(const int index) const { 95 if (index < 0 || index >= mSampledInputSize) return false; 96 const int currentCodePoint = getPrimaryCodePointAt(index); 97 const int leftIndex = index - 1; 98 if (leftIndex >= 0 && existsCodePointInProximityAt(leftIndex, currentCodePoint)) { 99 return true; 100 } 101 const int rightIndex = index + 1; 102 if (rightIndex < mSampledInputSize 103 && existsCodePointInProximityAt(rightIndex, currentCodePoint)) { 104 return true; 105 } 106 return false; 107 } 108 109 inline const int *getPrimaryInputWord() const { 110 return mPrimaryInputWord; 111 } 112 113 inline bool touchPositionCorrectionEnabled() const { 114 return mTouchPositionCorrectionEnabled; 115 } 116 117 bool isUsed() const { 118 return mSampledInputSize > 0; 119 } 120 121 int size() const { 122 return mSampledInputSize; 123 } 124 125 int getInputX(const int index) const { 126 return mSampledInputXs[index]; 127 } 128 129 int getInputY(const int index) const { 130 return mSampledInputYs[index]; 131 } 132 133 int getInputIndexOfSampledPoint(const int sampledIndex) const { 134 return mSampledInputIndice[sampledIndex]; 135 } 136 137 bool hasSpaceProximity(const int index) const; 138 139 int getLengthCache(const int index) const { 140 return mSampledLengthCache[index]; 141 } 142 143 bool isContinuousSuggestionPossible() const { 144 return mIsContinuousSuggestionPossible; 145 } 146 147 // TODO: Rename s/Length/NormalizedSquaredLength/ 148 float getPointToKeyByIdLength(const int inputIndex, const int keyId) const; 149 // TODO: Rename s/Length/NormalizedSquaredLength/ 150 float getPointToKeyLength(const int inputIndex, const int codePoint) const; 151 152 ProximityType getProximityType(const int index, const int codePoint, 153 const bool checkProximityChars, int *proximityIndex = 0) const; 154 155 ProximityType getProximityTypeG(const int index, const int codePoint) const; 156 157 const std::vector<int> *getSearchKeyVector(const int index) const { 158 return &mSampledSearchKeyVectors[index]; 159 } 160 161 float getSpeedRate(const int index) const { 162 return mSpeedRates[index]; 163 } 164 165 AK_FORCE_INLINE int getBeelineSpeedPercentile(const int id) const { 166 return mBeelineSpeedPercentiles[id]; 167 } 168 169 AK_FORCE_INLINE DoubleLetterLevel getDoubleLetterLevel(const int id) const { 170 const int beelineSpeedRate = getBeelineSpeedPercentile(id); 171 if (beelineSpeedRate == 0) { 172 return A_STRONG_DOUBLE_LETTER; 173 } else if (beelineSpeedRate 174 < ProximityInfoParams::MIN_DOUBLE_LETTER_BEELINE_SPEED_PERCENTILE) { 175 return A_DOUBLE_LETTER; 176 } else { 177 return NOT_A_DOUBLE_LETTER; 178 } 179 } 180 181 float getDirection(const int index) const { 182 return mDirections[index]; 183 } 184 // get xy direction 185 float getDirection(const int x, const int y) const; 186 187 float getMostProbableString(int *const codePointBuf) const; 188 189 float getProbability(const int index, const int charCode) const; 190 191 bool isKeyInSerchKeysAfterIndex(const int index, const int keyId) const; 192 193 private: 194 DISALLOW_COPY_AND_ASSIGN(ProximityInfoState); 195 196 inline const int *getProximityCodePointsAt(const int index) const { 197 return ProximityInfoStateUtils::getProximityCodePointsAt(mInputProximities, index); 198 } 199 200 // const 201 const ProximityInfo *mProximityInfo; 202 float mMaxPointToKeyLength; 203 float mAverageSpeed; 204 bool mHasTouchPositionCorrectionData; 205 int mMostCommonKeyWidthSquare; 206 int mKeyCount; 207 int mCellHeight; 208 int mCellWidth; 209 int mGridHeight; 210 int mGridWidth; 211 bool mIsContinuousSuggestionPossible; 212 bool mHasBeenUpdatedByGeometricInput; 213 214 std::vector<int> mSampledInputXs; 215 std::vector<int> mSampledInputYs; 216 std::vector<int> mSampledTimes; 217 std::vector<int> mSampledInputIndice; 218 std::vector<int> mSampledLengthCache; 219 std::vector<int> mBeelineSpeedPercentiles; 220 std::vector<float> mSampledNormalizedSquaredLengthCache; 221 std::vector<float> mSpeedRates; 222 std::vector<float> mDirections; 223 // probabilities of skipping or mapping to a key for each point. 224 std::vector<hash_map_compat<int, float> > mCharProbabilities; 225 // The vector for the key code set which holds nearby keys for each sampled input point 226 // 1. Used to calculate the probability of the key 227 // 2. Used to calculate mSampledSearchKeySets 228 std::vector<ProximityInfoStateUtils::NearKeycodesSet> mSampledNearKeySets; 229 // The vector for the key code set which holds nearby keys of some trailing sampled input points 230 // for each sampled input point. These nearby keys contain the next characters which can be in 231 // the dictionary. Specifically, currently we are looking for keys nearby trailing sampled 232 // inputs including the current input point. 233 std::vector<ProximityInfoStateUtils::NearKeycodesSet> mSampledSearchKeySets; 234 std::vector<std::vector<int> > mSampledSearchKeyVectors; 235 bool mTouchPositionCorrectionEnabled; 236 int mInputProximities[MAX_PROXIMITY_CHARS_SIZE * MAX_WORD_LENGTH]; 237 int mSampledInputSize; 238 int mPrimaryInputWord[MAX_WORD_LENGTH]; 239 float mMostProbableStringProbability; 240 int mMostProbableString[MAX_WORD_LENGTH]; 241 }; 242 } // namespace latinime 243 #endif // LATINIME_PROXIMITY_INFO_STATE_H 244